MaCh3  2.2.3
Reference Guide
Macros | Functions
MaCh3Logger.h File Reference

KS: Based on this https://github.com/gabime/spdlog/blob/a2b4262090fd3f005c2315dcb5be2f0f1774a005/include/spdlog/spdlog.h#L284. More...

#include <iostream>
#include <sstream>
#include <functional>
#include <string>
#include <exception>
#include "Manager/Core.h"
#include "spdlog/spdlog.h"
Include dependency graph for MaCh3Logger.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define MACH3LOG_TRACE   SPDLOG_TRACE
 
#define MACH3LOG_DEBUG   SPDLOG_DEBUG
 
#define MACH3LOG_INFO   SPDLOG_INFO
 
#define MACH3LOG_WARN   SPDLOG_WARN
 
#define MACH3LOG_ERROR   SPDLOG_ERROR
 
#define MACH3LOG_CRITICAL   SPDLOG_CRITICAL
 
#define MACH3LOG_OFF   SPDLOG_OFF
 

Functions

spdlog::level::level_enum get_default_log_level ()
 KS: Map string macro to spdlog::level enum. More...
 
void SetMaCh3LoggerFormat ()
 Set messaging format of the logger. More...
 
template<typename Func , typename LogFunc , typename... Args>
void LoggerPrint (const std::string &LibName, LogFunc logFunction, Func &&func, Args &&... args)
 KS: This is bit convoluted but this is to allow redirecting cout and errors from external library into MaCh3 logger format. More...
 

Detailed Description

KS: Based on this https://github.com/gabime/spdlog/blob/a2b4262090fd3f005c2315dcb5be2f0f1774a005/include/spdlog/spdlog.h#L284.

Note
can read more about spdlog here
Author
Kamil Skwarczynski

Definition in file MaCh3Logger.h.

Macro Definition Documentation

◆ MACH3LOG_CRITICAL

#define MACH3LOG_CRITICAL   SPDLOG_CRITICAL

Definition at line 28 of file MaCh3Logger.h.

◆ MACH3LOG_DEBUG

#define MACH3LOG_DEBUG   SPDLOG_DEBUG

Definition at line 24 of file MaCh3Logger.h.

◆ MACH3LOG_ERROR

#define MACH3LOG_ERROR   SPDLOG_ERROR

Definition at line 27 of file MaCh3Logger.h.

◆ MACH3LOG_INFO

#define MACH3LOG_INFO   SPDLOG_INFO

Definition at line 25 of file MaCh3Logger.h.

◆ MACH3LOG_OFF

#define MACH3LOG_OFF   SPDLOG_OFF

Definition at line 29 of file MaCh3Logger.h.

◆ MACH3LOG_TRACE

#define MACH3LOG_TRACE   SPDLOG_TRACE

Definition at line 23 of file MaCh3Logger.h.

◆ MACH3LOG_WARN

#define MACH3LOG_WARN   SPDLOG_WARN

Definition at line 26 of file MaCh3Logger.h.

Function Documentation

◆ get_default_log_level()

spdlog::level::level_enum get_default_log_level ( )
inline

KS: Map string macro to spdlog::level enum.

Todo:
make constexpr with c++17

Definition at line 33 of file MaCh3Logger.h.

33  {
34  #ifdef SPDLOG_ACTIVE_LEVEL
35  switch (SPDLOG_ACTIVE_LEVEL) {
36  case SPDLOG_LEVEL_TRACE: return spdlog::level::trace;
37  case SPDLOG_LEVEL_DEBUG: return spdlog::level::debug;
38  case SPDLOG_LEVEL_INFO: return spdlog::level::info;
39  case SPDLOG_LEVEL_WARN: return spdlog::level::warn;
40  case SPDLOG_LEVEL_ERROR: return spdlog::level::err;
41  case SPDLOG_LEVEL_CRITICAL: return spdlog::level::critical;
42  case SPDLOG_LEVEL_OFF: return spdlog::level::off;
43  default: throw std::runtime_error("Unknown SPDLOG_ACTIVE_LEVEL");
44  }
45  #else
46  throw std::runtime_error("SPDLOG_ACTIVE_LEVEL is not defined");
47  #endif
48 }

◆ LoggerPrint()

template<typename Func , typename LogFunc , typename... Args>
void LoggerPrint ( const std::string &  LibName,
LogFunc  logFunction,
Func &&  func,
Args &&...  args 
)

KS: This is bit convoluted but this is to allow redirecting cout and errors from external library into MaCh3 logger format.

Template Parameters
FuncThe type of the function to be called, which outputs to stdout and stderr.
LogFuncThe type of the logging function, typically a lambda that formats and logs messages.
ArgsThe types of the arguments to be passed to func.
Parameters
LibNameThe name of the library or component from which the log message originates.
logFunctionThe lambda function responsible for formatting and logging messages. It should accept a single const std::string& parameter.
funcThe function to be called, whose output needs to be captured and logged.
argsThe arguments to be passed to func.
void ExampleFunc(int x, const std::string& str) {
std::cout << "Output from exampleFunc: " << x << ", " << str << "\n";
std::cerr << "Error from exampleFunc: " << x << ", " << str << "\n";
}
int main() {
LoggerPrint("ExampleLib", [](const std::string& message) { MACH3LOG_INFO("{}", message); },
static_cast<void(*)(int, const std::string&)>(ExampleFunc), 666, "Number of the BEAST");
return 0;
}
int main(int argc, char *argv[])
#define MACH3LOG_INFO
Definition: MaCh3Logger.h:25
void LoggerPrint(const std::string &LibName, LogFunc logFunction, Func &&func, Args &&... args)
KS: This is bit convoluted but this is to allow redirecting cout and errors from external library int...
Definition: MaCh3Logger.h:90
Note
second argument is lambda fucniton whih convers mach3 so template works, it is bit faff... This approach allows seamless integration of func into an existing logging mechanism without modifying func itself

Definition at line 90 of file MaCh3Logger.h.

91 {
92  // Create a stringstream to capture the output
93  std::stringstream sss_cout;
94  std::stringstream sss_cerr;
95 
96  // Save the original stream buffers
97  std::streambuf* coutBuf = nullptr;
98  std::streambuf* cerrBuf = nullptr;
99 
100  // This should be rare but in case buffers no longer exist ignore
101  if (std::cout.rdbuf() && std::cerr.rdbuf()) {
102  coutBuf = std::cout.rdbuf(); // Save original cout buffer
103  cerrBuf = std::cerr.rdbuf(); // Save original cerr buffer
104 
105  // Redirect std::cout and std::cerr to the stringstream buffer
106  std::cout.rdbuf(sss_cout.rdbuf());
107  std::cerr.rdbuf(sss_cerr.rdbuf());
108  }
109 
110  try {
111  // Call the provided function
112  func(std::forward<Args>(args)...);
113 
114  // Restore the original stream buffers
115  if (coutBuf) std::cout.rdbuf(coutBuf);
116  if (cerrBuf) std::cerr.rdbuf(cerrBuf);
117 
118  std::string line;
119  while (std::getline(sss_cout, line))
120  {
121  auto formatted_message = fmt::format("[{}] {}", LibName, line);
122  logFunction(formatted_message);
123  }
124  while (std::getline(sss_cerr, line))
125  {
126  auto formatted_message = fmt::format("[{}] {}", LibName, line);
127  logFunction(formatted_message);
128  }
129  } catch (std::runtime_error &err) {
130  // Restore the original buffers in case of an exception
131  std::cout.rdbuf(coutBuf);
132  std::cerr.rdbuf(cerrBuf);
133 
134  std::cout << "\nConsole cout output:" << std::endl;
135  std::cout << sss_cout.rdbuf()->str() << std::endl;
136 
137  std::cout << "\nConsole cerr output:" << std::endl;
138  std::cout << sss_cerr.rdbuf()->str() << std::endl;
139  throw;
140  }
141 }

◆ SetMaCh3LoggerFormat()

void SetMaCh3LoggerFormat ( )
inline

Set messaging format of the logger.

Definition at line 51 of file MaCh3Logger.h.

52 {
53  //KS: %H for hour, %M for minute, %S for second, [%s:%#] for class and line
54  //For documentation see https://github.com/gabime/spdlog/wiki/3.-Custom-formatting
55  #ifdef DEBUG
56  //spdlog::set_pattern("[%H:%M:%S][%s:%#][%^%l%$] %v");
57  spdlog::set_pattern("[%s:%#][%^%l%$] %v");
58  #else
59  //spdlog::set_pattern("[%H:%M:%S][%s][%^%l%$] %v");
60  spdlog::set_pattern("[%s][%^%l%$] %v");
61  #endif
62 
63  spdlog::set_level(get_default_log_level());
64 }
spdlog::level::level_enum get_default_log_level()
KS: Map string macro to spdlog::level enum.
Definition: MaCh3Logger.h:33