logger.cpp 4.54 KB
Newer Older
gauthier's avatar
gauthier committed
1
/*
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 * Copyright (c) 2017 Sprint
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
gauthier's avatar
gauthier committed
16
17

#include "logger.hpp"
18
#include "spdlog/sinks/syslog_sink.h"
gauthier's avatar
gauthier committed
19
20
21
22
23
24

#include <iostream>
#include <sstream>
#include <string>
#include <memory>

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
25
Logger* Logger::m_singleton = NULL;
gauthier's avatar
gauthier committed
26

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
27
28
void Logger::_init(
    const char* app, const bool log_stdout, bool const log_rot_file) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
29
30
  int num_sinks = 0;
  spdlog::set_async_mode(2048);
gauthier's avatar
gauthier committed
31
#if TRACE_IS_ON
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
32
  spdlog::level::level_enum llevel = spdlog::level::trace;
gauthier's avatar
gauthier committed
33
#elif DEBUG_IS_ON
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
34
  spdlog::level::level_enum llevel = spdlog::level::debug;
gauthier's avatar
gauthier committed
35
#elif INFO_IS_ON
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
36
  spdlog::level::level_enum llevel = spdlog::level::info;
gauthier's avatar
gauthier committed
37
#else
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
38
  spdlog::level::level_enum llevel = spdlog::level::warn;
gauthier's avatar
gauthier committed
39
#endif
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
40
41
  if (log_stdout) {
    std::string filename = fmt::format("./{}.log", app);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
42
43
    m_sinks.push_back(
        std::make_shared<spdlog::sinks::ansicolor_stdout_sink_mt>());
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
44
45
46
47
    m_sinks[num_sinks++].get()->set_level(llevel);
  }
  if (log_rot_file) {
    std::string filename = fmt::format("./{}.log", app);
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
48
49
    m_sinks.push_back(std::make_shared<spdlog::sinks::rotating_file_sink_mt>(
        filename, 5 * 1024 * 1024, 3));
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
50
51
52
53
54
55
    m_sinks[num_sinks++].get()->set_level(llevel);
  }

  std::stringstream ss;
  ss << "[%Y-%m-%dT%H:%M:%S.%f] [" << app << "] [%n] [%l] %v";

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
56
57
58
59
60
61
62
63
64
65
  m_async_cmd      = new _Logger("async_c", m_sinks, ss.str().c_str());
  m_itti           = new _Logger("itti   ", m_sinks, ss.str().c_str());
  m_smf_app        = new _Logger("smf_app", m_sinks, ss.str().c_str());
  m_system         = new _Logger("system ", m_sinks, ss.str().c_str());
  m_udp            = new _Logger("udp    ", m_sinks, ss.str().c_str());
  m_pfcp           = new _Logger("pfcp   ", m_sinks, ss.str().c_str());
  m_pfcp_switch    = new _Logger("pfcp_sw ", m_sinks, ss.str().c_str());
  m_smf_n1         = new _Logger("smf_n1 ", m_sinks, ss.str().c_str());
  m_smf_n2         = new _Logger("smf_n2 ", m_sinks, ss.str().c_str());
  m_smf_n4         = new _Logger("smf_n4 ", m_sinks, ss.str().c_str());
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
66
  m_smf_sbi        = new _Logger("smf_sbi", m_sinks, ss.str().c_str());
67
  m_smf_api_server = new _Logger("sbi_srv", m_sinks, ss.str().c_str());
gauthier's avatar
gauthier committed
68
69
70
71
72
}

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
73
74
75
76
_Logger::_Logger(
    const char* category, std::vector<spdlog::sink_ptr>& sinks,
    const char* pattern)
    : m_log(category, sinks.begin(), sinks.end()) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
77
  m_log.set_pattern(pattern);
gauthier's avatar
gauthier committed
78
#if TRACE_IS_ON
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
79
  m_log.set_level(spdlog::level::trace);
gauthier's avatar
gauthier committed
80
#elif DEBUG_IS_ON
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
81
  m_log.set_level(spdlog::level::debug);
gauthier's avatar
gauthier committed
82
#elif INFO_IS_ON
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
83
  m_log.set_level(spdlog::level::info);
gauthier's avatar
gauthier committed
84
#else
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
85
  m_log.set_level(spdlog::level::warn);
gauthier's avatar
gauthier committed
86
87
88
#endif
}

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
89
void _Logger::trace(const char* format, ...) {
gauthier's avatar
gauthier committed
90
#if TRACE_IS_ON
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
91
92
93
94
  va_list args;
  va_start(args, format);
  log(_ltTrace, format, args);
  va_end(args);
gauthier's avatar
gauthier committed
95
96
97
#endif
}

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
98
void _Logger::debug(const char* format, ...) {
gauthier's avatar
gauthier committed
99
#if DEBUG_IS_ON
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
100
101
102
103
  va_list args;
  va_start(args, format);
  log(_ltDebug, format, args);
  va_end(args);
gauthier's avatar
gauthier committed
104
105
106
#endif
}

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
107
void _Logger::info(const char* format, ...) {
gauthier's avatar
gauthier committed
108
#if INFO_IS_ON
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
109
110
111
112
  va_list args;
  va_start(args, format);
  log(_ltInfo, format, args);
  va_end(args);
gauthier's avatar
gauthier committed
113
114
115
#endif
}

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
116
void _Logger::startup(const char* format, ...) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
117
118
119
120
  va_list args;
  va_start(args, format);
  log(_ltStartup, format, args);
  va_end(args);
gauthier's avatar
gauthier committed
121
122
}

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
123
void _Logger::warn(const char* format, ...) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
124
125
126
127
  va_list args;
  va_start(args, format);
  log(_ltWarn, format, args);
  va_end(args);
gauthier's avatar
gauthier committed
128
129
}

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
130
void _Logger::error(const char* format, ...) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
131
132
133
134
  va_list args;
  va_start(args, format);
  log(_ltError, format, args);
  va_end(args);
gauthier's avatar
gauthier committed
135
136
}

Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
137
void _Logger::log(_LogType lt, const char* format, va_list& args) {
Tien-Thinh Nguyen's avatar
Tien-Thinh Nguyen committed
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
  char buffer[2048];

  vsnprintf(buffer, sizeof(buffer), format, args);

  switch (lt) {
    case _ltTrace:
      m_log.trace(buffer);
      break;
    case _ltDebug:
      m_log.debug(buffer);
      break;
    case _ltInfo:
      m_log.info(buffer);
      break;
    case _ltStartup:
      m_log.warn(buffer);
      break;
    case _ltWarn:
      m_log.error(buffer);
      break;
    case _ltError:
      m_log.critical(buffer);
      break;
  }
gauthier's avatar
gauthier committed
162
}