NRP Core  1.4.1
nrp_logger.h
Go to the documentation of this file.
1 /* * NRP Core - Backend infrastructure to synchronize simulations
2  *
3  * Copyright 2020-2023 NRP Team
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * This project has received funding from the European Union’s Horizon 2020
18  * Framework Programme for Research and Innovation under the Specific Grant
19  * Agreement No. 945539 (Human Brain Project SGA3).
20  */
21 
22 #ifndef NRP_LOGGER_H
23 #define NRP_LOGGER_H
24 
25 #ifdef PRODUCTION_RELEASE
26 #define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_DEBUG
27 #else
28 #define SPDLOG_TRACE_ON
29 #define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_TRACE
30 #endif
31 
32 #include <spdlog/spdlog.h>
33 
34 #include <semaphore.h>
35 
39 #define NRP_LOGGER_TRACE(...) SPDLOG_TRACE(__VA_ARGS__)
40 
41 
45 class NRPLogger
46 {
47 public:
48 
52  typedef spdlog::level::level_enum level_t;
53 
58  NRPLogger(
59  std::string loggerName = _defaultLoggerName.data());
60 
74  NRPLogger(
75  std::string loggerName,
76  NRPLogger::level_t fileLogLevel,
77  NRPLogger::level_t consoleLogLevel,
78  std::string logDir,
79  bool doSavePars = false);
80 
81  ~NRPLogger();
82 
87  static std::string level_to_string(const NRPLogger::level_t &level)
88  {
89  return spdlog::level::to_string_view(level).data();
90  }
91 
96  static NRPLogger::level_t level_from_string(const std::string &level)
97  {
98  return spdlog::level::from_str(level);
99  }
100 
104  static void shutdownDefault();
105 
109  void flush();
110 
114  static spdlog::logger &nrpLogger();
115 
119  using spdlog_out_fcn_t = void (&)(const std::string &);
120 
126  template <typename FormatString, typename... Args>
127  static void debug(const FormatString &fmt, const Args &...args)
128  {
129  spdlog::default_logger()->log(spdlog::level::debug, fmt, args...);
130  }
131 
137  template <typename FormatString, typename... Args>
138  static void info(const FormatString &fmt, const Args &...args)
139  {
140  spdlog::default_logger()->log(spdlog::level::info, fmt, args...);
141  }
142 
148  template <typename FormatString, typename... Args>
149  static void warn(const FormatString &fmt, const Args &...args)
150  {
151  spdlog::default_logger()->log(spdlog::level::warn, fmt, args...);
152  }
153 
159  template <typename FormatString, typename... Args>
160  static void error(const FormatString &fmt, const Args &...args)
161  {
162  spdlog::default_logger()->log(spdlog::level::err, fmt, args...);
163  }
164 
170  template <typename FormatString, typename... Args>
171  static void critical(const FormatString &fmt, const Args &...args)
172  {
173  spdlog::default_logger()->log(spdlog::level::critical, fmt, args...);
174  }
175 
180  template <typename Message>
181  static void debug(const Message &msg)
182  {
183  spdlog::default_logger()->log(spdlog::level::debug, msg);
184  }
185 
190  template <typename Message>
191  static void info(const Message &msg)
192  {
193  spdlog::default_logger()->log(spdlog::level::info, msg);
194  }
195 
200  template <typename Message>
201  static void warn(const Message &msg)
202  {
203  spdlog::default_logger()->log(spdlog::level::warn, msg);
204  }
205 
210  template <typename Message>
211  static void error(const Message &msg)
212  {
213  spdlog::default_logger()->log(spdlog::level::err, msg);
214  }
215 
220  template <typename Message>
221  static void critical(const Message &msg)
222  {
223  spdlog::default_logger()->log(spdlog::level::critical, msg);
224  }
225 
226 private:
230  static constexpr std::string_view _defaultLoggerName = "nrp_core";
231 
235  std::string _baseFilename;
236 
240  std::string _loggerName;
241 
245  NRPLogger::level_t _fileLogLevel;
246 
250  NRPLogger::level_t _consoleLogLevel;
251 
255  std::string _logDir;
256 
260  FILE *_consoleOut;
261 
265  bool _doSavePars;
266 
270  static constexpr std::string_view _defaultLogDir = "log";
271 
275  static constexpr std::string_view _sharedMemCfgName = "/NRPLogger";
276 
289  void registerDefaultLogger();
290 
295  bool setSharedMemoryForLauncher();
296 
301  bool getSharedMemoryFromLauncher();
302 
308  bool shutdownSharedMemory(int& fd);
309 
313  struct LoggerConfig {
314  sem_t sem1;
315  uint fileLogLevel;
316  uint consoleLogLevel;
317  size_t logDirLen;
318  char logDir[1024];
319  };
320 };
321 
322 #endif // NRP_LOGGER_H
NRPLogger::flush
void flush()
Flush default logger.
Definition: nrp_logger.cpp:321
NRPLogger::warn
static void warn(const FormatString &fmt, const Args &...args)
NRP logging function with message formatting for warning level.
Definition: nrp_logger.h:149
NRPLogger::level_to_string
static std::string level_to_string(const NRPLogger::level_t &level)
Wrapper function for converting enumed log level into string.
Definition: nrp_logger.h:87
NRPLogger::~NRPLogger
~NRPLogger()
Definition: nrp_logger.cpp:307
NRPLogger::debug
static void debug(const Message &msg)
NRP logging function for debug level.
Definition: nrp_logger.h:181
python_json_engine.args
Namespace args
Definition: python_json_engine.py:196
NRPLogger::info
static void info(const Message &msg)
NRP logging function for info level.
Definition: nrp_logger.h:191
NRPLogger::spdlog_out_fcn_t
void(&)(const std::string &) spdlog_out_fcn_t
Logging function type, is used by Exception.
Definition: nrp_logger.h:119
NRPLogger::info
static void info(const FormatString &fmt, const Args &...args)
NRP logging function with message formatting for info level.
Definition: nrp_logger.h:138
NRPLogger::critical
static void critical(const Message &msg)
NRP logging function for critical error level.
Definition: nrp_logger.h:221
NRPLogger
NRP Logging functions.
Definition: nrp_logger.h:45
NRPLogger::level_from_string
static NRPLogger::level_t level_from_string(const std::string &level)
Wrapper function for getting enumed log level from string. Non-valid string is converted to enum::off...
Definition: nrp_logger.h:96
NRPLogger::NRPLogger
NRPLogger(std::string loggerName=_defaultLoggerName.data())
The creation of the configurable instance of spdlog, that is set to default logger,...
Definition: nrp_logger.cpp:149
NRPLogger::shutdownDefault
static void shutdownDefault()
Shutdown default logger.
Definition: nrp_logger.cpp:325
NRPLogger::nrpLogger
static spdlog::logger & nrpLogger()
Get default NRPLogger.
NRPLogger::critical
static void critical(const FormatString &fmt, const Args &...args)
NRP logging function with message formatting for critical error level.
Definition: nrp_logger.h:171
NRPLogger::error
static void error(const FormatString &fmt, const Args &...args)
NRP logging function with message formatting for error level.
Definition: nrp_logger.h:160
NRPLogger::level_t
spdlog::level::level_enum level_t
The wrapper type for log levels.
Definition: nrp_logger.h:52
NRPLogger::error
static void error(const Message &msg)
NRP logging function for error level.
Definition: nrp_logger.h:211
NRPLogger::debug
static void debug(const FormatString &fmt, const Args &...args)
NRP logging function with message formatting for debug level.
Definition: nrp_logger.h:127
NRPLogger::warn
static void warn(const Message &msg)
NRP logging function for warning level.
Definition: nrp_logger.h:201