MaCh3  2.2.3
Reference Guide
MaCh3Logger.h
Go to the documentation of this file.
1 #pragma once
2 
3 // C++ Includes
4 #include <iostream>
5 #include <sstream>
6 #include <functional>
7 #include <string>
8 #include <exception>
9 
10 // MaCh3 Includes
11 #include "Manager/Core.h"
12 
14 // spdlog Includes
15 #include "spdlog/spdlog.h"
17 
22 
23 #define MACH3LOG_TRACE SPDLOG_TRACE
24 #define MACH3LOG_DEBUG SPDLOG_DEBUG
25 #define MACH3LOG_INFO SPDLOG_INFO
26 #define MACH3LOG_WARN SPDLOG_WARN
27 #define MACH3LOG_ERROR SPDLOG_ERROR
28 #define MACH3LOG_CRITICAL SPDLOG_CRITICAL
29 #define MACH3LOG_OFF SPDLOG_OFF
30 
33 inline spdlog::level::level_enum get_default_log_level() {
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 }
49 
51 inline void SetMaCh3LoggerFormat()
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 }
65 
89 template <typename Func, typename LogFunc, typename... Args>
90 void LoggerPrint(const std::string& LibName, LogFunc logFunction, Func&& func, Args&&... args)
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 }
#define _MaCh3_Safe_Include_Start_
KS: Avoiding warning checking for headers.
Definition: Core.h:109
#define _MaCh3_Safe_Include_End_
KS: Restore warning checking after including external headers.
Definition: Core.h:120
spdlog::level::level_enum get_default_log_level()
KS: Map string macro to spdlog::level enum.
Definition: MaCh3Logger.h:33
void SetMaCh3LoggerFormat()
Set messaging format of the logger.
Definition: MaCh3Logger.h:51
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