MaCh3  2.6.0
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 
32 
33 #define MACH3LOG_TRACE SPDLOG_TRACE
34 #define MACH3LOG_DEBUG SPDLOG_DEBUG
35 #define MACH3LOG_INFO SPDLOG_INFO
36 #define MACH3LOG_WARN SPDLOG_WARN
37 #define MACH3LOG_ERROR SPDLOG_ERROR
38 #define MACH3LOG_CRITICAL SPDLOG_CRITICAL
39 #define MACH3LOG_OFF SPDLOG_OFF
40 
42 inline constexpr spdlog::level::level_enum get_default_log_level() {
43  #ifdef SPDLOG_ACTIVE_LEVEL
44  switch (SPDLOG_ACTIVE_LEVEL) {
45  case SPDLOG_LEVEL_TRACE: return spdlog::level::trace;
46  case SPDLOG_LEVEL_DEBUG: return spdlog::level::debug;
47  case SPDLOG_LEVEL_INFO: return spdlog::level::info;
48  case SPDLOG_LEVEL_WARN: return spdlog::level::warn;
49  case SPDLOG_LEVEL_ERROR: return spdlog::level::err;
50  case SPDLOG_LEVEL_CRITICAL: return spdlog::level::critical;
51  case SPDLOG_LEVEL_OFF: return spdlog::level::off;
52  default: throw std::runtime_error("Unknown SPDLOG_ACTIVE_LEVEL");
53  }
54  #else
55  throw std::runtime_error("SPDLOG_ACTIVE_LEVEL is not defined");
56  #endif
57 }
58 
60 inline void SetMaCh3LoggerFormat()
61 {
62  //KS: %H for hour, %M for minute, %S for second, [%s:%#] for class and line
63  //For documentation see https://github.com/gabime/spdlog/wiki/3.-Custom-formatting
64  #ifdef MACH3_DEBUG
65  //spdlog::set_pattern("[%H:%M:%S][%s:%#][%^%l%$] %v");
66  spdlog::set_pattern("[%s:%#][%^%l%$] %v");
67  #else
68  //spdlog::set_pattern("[%H:%M:%S][%s][%^%l%$] %v");
69  spdlog::set_pattern("[%s][%^%l%$] %v");
70  #endif
71 
72  spdlog::set_level(get_default_log_level());
73 }
74 
98 template <typename Func, typename LogFunc, typename... Args>
99 void LoggerPrint(const std::string& LibName, LogFunc logFunction, Func&& func, Args&&... args)
100 {
101  // Create a stringstream to capture the output
102  std::stringstream sss_cout;
103  std::stringstream sss_cerr;
104 
105  // Save the original stream buffers
106  std::streambuf* coutBuf = nullptr;
107  std::streambuf* cerrBuf = nullptr;
108 
109  // This should be rare but in case buffers no longer exist ignore
110  if (std::cout.rdbuf() && std::cerr.rdbuf()) {
111  coutBuf = std::cout.rdbuf(); // Save original cout buffer
112  cerrBuf = std::cerr.rdbuf(); // Save original cerr buffer
113 
114  // Redirect std::cout and std::cerr to the stringstream buffer
115  std::cout.rdbuf(sss_cout.rdbuf());
116  std::cerr.rdbuf(sss_cerr.rdbuf());
117  }
118 
119  try {
120  // Call the provided function
121  func(std::forward<Args>(args)...);
122 
123  // Restore the original stream buffers
124  if (coutBuf) std::cout.rdbuf(coutBuf);
125  if (cerrBuf) std::cerr.rdbuf(cerrBuf);
126 
127  std::string line;
128  while (std::getline(sss_cout, line))
129  {
130  auto formatted_message = fmt::format("[{}] {}", LibName, line);
131  logFunction(formatted_message);
132  }
133  while (std::getline(sss_cerr, line))
134  {
135  auto formatted_message = fmt::format("[{}] {}", LibName, line);
136  logFunction(formatted_message);
137  }
138  } catch (std::runtime_error &err) {
139  // Restore the original buffers in case of an exception
140  std::cout.rdbuf(coutBuf);
141  std::cerr.rdbuf(cerrBuf);
142 
143  std::cout << "\nConsole cout output:" << std::endl;
144  std::cout << sss_cout.rdbuf()->str() << std::endl;
145 
146  std::cout << "\nConsole cerr output:" << std::endl;
147  std::cout << sss_cerr.rdbuf()->str() << std::endl;
148  throw;
149  }
150 }
KS: Core MaCh3 definitions and compile-time configuration utilities.
#define _MaCh3_Safe_Include_Start_
KS: Avoiding warning checking for headers.
Definition: Core.h:126
#define _MaCh3_Safe_Include_End_
void SetMaCh3LoggerFormat()
Set messaging format of the logger.
Definition: MaCh3Logger.h:60
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:99
constexpr spdlog::level::level_enum get_default_log_level()
KS: Map string macro to spdlog::level enum.
Definition: MaCh3Logger.h:42