MaCh3 2.2.1
Reference Guide
Loading...
Searching...
No Matches
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
20
21#define MACH3LOG_TRACE SPDLOG_TRACE
22#define MACH3LOG_DEBUG SPDLOG_DEBUG
23#define MACH3LOG_INFO SPDLOG_INFO
24#define MACH3LOG_WARN SPDLOG_WARN
25#define MACH3LOG_ERROR SPDLOG_ERROR
26#define MACH3LOG_CRITICAL SPDLOG_CRITICAL
27#define MACH3LOG_OFF SPDLOG_OFF
28
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}
42
66template <typename Func, typename LogFunc, typename... Args>
67void LoggerPrint(const std::string& LibName, LogFunc logFunction, Func&& func, Args&&... args)
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}
#define _MaCh3_Safe_Include_Start_
KS: Avoiding warning checking for headers.
Definition: Core.h:106
#define _MaCh3_Safe_Include_End_
KS: Restore warning checking after including external headers.
Definition: Core.h:117
void SetMaCh3LoggerFormat()
Set messaging format of the logger.
Definition: MaCh3Logger.h:30
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