15 #include "spdlog/spdlog.h"
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
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");
46 throw std::runtime_error(
"SPDLOG_ACTIVE_LEVEL is not defined");
57 spdlog::set_pattern(
"[%s:%#][%^%l%$] %v");
60 spdlog::set_pattern(
"[%s][%^%l%$] %v");
89 template <
typename Func,
typename LogFunc,
typename... Args>
90 void LoggerPrint(
const std::string& LibName, LogFunc logFunction, Func&& func, Args&&... args)
93 std::stringstream sss_cout;
94 std::stringstream sss_cerr;
97 std::streambuf* coutBuf =
nullptr;
98 std::streambuf* cerrBuf =
nullptr;
101 if (std::cout.rdbuf() && std::cerr.rdbuf()) {
102 coutBuf = std::cout.rdbuf();
103 cerrBuf = std::cerr.rdbuf();
106 std::cout.rdbuf(sss_cout.rdbuf());
107 std::cerr.rdbuf(sss_cerr.rdbuf());
112 func(std::forward<Args>(args)...);
115 if (coutBuf) std::cout.rdbuf(coutBuf);
116 if (cerrBuf) std::cerr.rdbuf(cerrBuf);
119 while (std::getline(sss_cout, line))
121 auto formatted_message = fmt::format(
"[{}] {}", LibName, line);
122 logFunction(formatted_message);
124 while (std::getline(sss_cerr, line))
126 auto formatted_message = fmt::format(
"[{}] {}", LibName, line);
127 logFunction(formatted_message);
129 }
catch (std::runtime_error &err) {
131 std::cout.rdbuf(coutBuf);
132 std::cerr.rdbuf(cerrBuf);
134 std::cout <<
"\nConsole cout output:" << std::endl;
135 std::cout << sss_cout.rdbuf()->str() << std::endl;
137 std::cout <<
"\nConsole cerr output:" << std::endl;
138 std::cout << sss_cerr.rdbuf()->str() << std::endl;
#define _MaCh3_Safe_Include_Start_
KS: Avoiding warning checking for headers.
#define _MaCh3_Safe_Include_End_
KS: Restore warning checking after including external headers.
spdlog::level::level_enum get_default_log_level()
KS: Map string macro to spdlog::level enum.
void SetMaCh3LoggerFormat()
Set messaging format of the logger.
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...