MaCh3 2.2.1
Reference Guide
Loading...
Searching...
No Matches
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

void SetMaCh3LoggerFormat ()
 Set messaging format of the logger.
 
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.
 

Detailed Description

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

Definition in file MaCh3Logger.h.

Macro Definition Documentation

◆ MACH3LOG_CRITICAL

#define MACH3LOG_CRITICAL   SPDLOG_CRITICAL

Definition at line 26 of file MaCh3Logger.h.

◆ MACH3LOG_DEBUG

#define MACH3LOG_DEBUG   SPDLOG_DEBUG

Definition at line 22 of file MaCh3Logger.h.

◆ MACH3LOG_ERROR

#define MACH3LOG_ERROR   SPDLOG_ERROR

Definition at line 25 of file MaCh3Logger.h.

◆ MACH3LOG_INFO

#define MACH3LOG_INFO   SPDLOG_INFO

Definition at line 23 of file MaCh3Logger.h.

◆ MACH3LOG_OFF

#define MACH3LOG_OFF   SPDLOG_OFF

Definition at line 27 of file MaCh3Logger.h.

◆ MACH3LOG_TRACE

#define MACH3LOG_TRACE   SPDLOG_TRACE

Definition at line 21 of file MaCh3Logger.h.

◆ MACH3LOG_WARN

#define MACH3LOG_WARN   SPDLOG_WARN

Definition at line 24 of file MaCh3Logger.h.

Function Documentation

◆ 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:23
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:67
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 67 of file MaCh3Logger.h.

68{
69 // Create a stringstream to capture the output
70 std::stringstream sss_cout;
71 std::stringstream sss_cerr;
72
73 // Save the original stream buffers
74 std::streambuf* coutBuf = nullptr;
75 std::streambuf* cerrBuf = nullptr;
76
77 // This should be rare but in case buffers no longer exist ignore
78 if (std::cout.rdbuf() && std::cerr.rdbuf()) {
79 coutBuf = std::cout.rdbuf(); // Save original cout buffer
80 cerrBuf = std::cerr.rdbuf(); // Save original cerr buffer
81
82 // Redirect std::cout and std::cerr to the stringstream buffer
83 std::cout.rdbuf(sss_cout.rdbuf());
84 std::cerr.rdbuf(sss_cerr.rdbuf());
85 }
86
87 try {
88 // Call the provided function
89 func(std::forward<Args>(args)...);
90
91 // Restore the original stream buffers
92 if (coutBuf) std::cout.rdbuf(coutBuf);
93 if (cerrBuf) std::cerr.rdbuf(cerrBuf);
94
95 std::string line;
96 while (std::getline(sss_cout, line))
97 {
98 auto formatted_message = fmt::format("[{}] {}", LibName, line);
99 logFunction(formatted_message);
100 }
101 while (std::getline(sss_cerr, line))
102 {
103 auto formatted_message = fmt::format("[{}] {}", LibName, line);
104 logFunction(formatted_message);
105 }
106 } catch (std::runtime_error &err) {
107 // Restore the original buffers in case of an exception
108 std::cout.rdbuf(coutBuf);
109 std::cerr.rdbuf(cerrBuf);
110
111 std::cout << "\nConsole cout output:" << std::endl;
112 std::cout << sss_cout.rdbuf()->str() << std::endl;
113
114 std::cout << "\nConsole cerr output:" << std::endl;
115 std::cout << sss_cerr.rdbuf()->str() << std::endl;
116 throw;
117 }
118}

◆ SetMaCh3LoggerFormat()

void SetMaCh3LoggerFormat ( )
inline

Set messaging format of the logger.

Definition at line 30 of file MaCh3Logger.h.

31{
32 //KS: %H for hour, %M for minute, %S for second, [%s:%#] for class and line
33 //For documentation see https://github.com/gabime/spdlog/wiki/3.-Custom-formatting
34 #ifdef DEBUG
35 //spdlog::set_pattern("[%H:%M:%S][%s:%#][%^%l%$] %v");
36 spdlog::set_pattern("[%s:%#][%^%l%$] %v");
37 #else
38 //spdlog::set_pattern("[%H:%M:%S][%s][%^%l%$] %v");
39 spdlog::set_pattern("[%s][%^%l%$] %v");
40 #endif
41}