1 #ifndef NdmspcCoreNLogger_H
2 #define NdmspcCoreNLogger_H
7 #include <unordered_map>
11 #include <unordered_map>
14 #include <nlohmann/json.hpp>
15 using json = nlohmann::json;
27 #define NLog(level, format, ...) Ndmspc::NLogger::Log(__FILE__, __LINE__, level, format, ##__VA_ARGS__)
35 #define NLogTrace(format, ...) \
36 Ndmspc::NLogger::Log(__FILE__, __LINE__, Ndmspc::logs::Severity::kTrace, format, ##__VA_ARGS__)
44 #define NLogDebug(format, ...) \
45 Ndmspc::NLogger::Log(__FILE__, __LINE__, Ndmspc::logs::Severity::kDebug, format, ##__VA_ARGS__)
53 #define NLogInfo(format, ...) \
54 Ndmspc::NLogger::Log(__FILE__, __LINE__, Ndmspc::logs::Severity::kInfo, format, ##__VA_ARGS__)
62 #define NLogWarning(format, ...) \
63 Ndmspc::NLogger::Log(__FILE__, __LINE__, Ndmspc::logs::Severity::kWarn, format, ##__VA_ARGS__)
71 #define NLogError(format, ...) \
72 Ndmspc::NLogger::Log(__FILE__, __LINE__, Ndmspc::logs::Severity::kError, format, ##__VA_ARGS__)
80 #define NLogFatal(format, ...) \
81 Ndmspc::NLogger::Log(__FILE__, __LINE__, Ndmspc::logs::Severity::kFatal, format, ##__VA_ARGS__)
91 #define NLogPrint(format, ...) fprintf(stdout, format "\n", ##__VA_ARGS__)
104 enum class Severity {
132 static const std::unordered_map<std::string, Severity> fgSeverityMap = {
133 {
"TRACE", Severity::kTrace}, {
"TRACE2", Severity::kTrace2}, {
"TRACE3", Severity::kTrace3},
134 {
"TRACE4", Severity::kTrace4}, {
"DEBUG", Severity::kDebug}, {
"DEBUG2", Severity::kDebug2},
135 {
"DEBUG3", Severity::kDebug3}, {
"DEBUG4", Severity::kDebug4}, {
"INFO", Severity::kInfo},
136 {
"INFO2", Severity::kInfo2}, {
"INFO3", Severity::kInfo3}, {
"INFO4", Severity::kInfo4},
137 {
"WARN", Severity::kWarn}, {
"WARN2", Severity::kWarn2}, {
"WARN3", Severity::kWarn3},
138 {
"WARN4", Severity::kWarn4}, {
"ERROR", Severity::kError}, {
"ERROR2", Severity::kError2},
139 {
"ERROR3", Severity::kError3}, {
"ERROR4", Severity::kError4}, {
"FATAL", Severity::kFatal},
140 {
"FATAL2", Severity::kFatal2}, {
"FATAL3", Severity::kFatal3}, {
"FATAL4", Severity::kFatal4}};
491 static void Log(
const char * file,
int line, logs::Severity level,
const char * format, ...);
540 static void SetThreadName(
const std::string & name, std::thread::id thread_id = std::this_thread::get_id());
596 std::unordered_map<std::thread::id, std::unique_ptr<std::ofstream>>
fThreadStreams;
Thread-safe singleton logger with per-thread file output.
static std::string fgLogDirectory
Directory for log files.
std::mutex fStreamMapMutex
static void SetMinSeverity(logs::Severity level)
Sets the minimum severity level for logging.
std::unordered_map< std::thread::id, std::string > fThreadNames
Map of thread IDs to custom thread names.
void CloseThreadStream(std::thread::id thread_id)
Closes and removes the output file stream associated with a specific thread.
static bool fgFileOutput
Flag for file output.
static void SetProcessName(const std::string &name)
Sets the name of the current process.
static void SetConsoleOutput(bool enable)
Enables or disables logging output to the console.
static std::mutex fgLoggerMutex
Mutex for thread-safe singleton access.
static std::string GetThreadName()
Retrieves the name of the current thread.
static logs::Severity fgMinSeverity
Minimum severity level for logging.
void Init()
Initializes the logger.
static std::mutex & GetLoggerMutex()
Get logger mutex reference.
static std::string GetLogDirectory()
Get log directory path.
static bool GetConsoleOutput()
Get console output flag.
static std::string GetProcessName()
Retrieves the name of the current process.
static void SetFileOutput(bool enable)
Enables or disables logging output to a file.
NLogger()
Constructs a new NLogger instance.
static void SetThreadName(const std::string &name, std::thread::id thread_id=std::this_thread::get_id())
Sets the name of a thread.
static bool fgConsoleOutput
Flag for console output.
static logs::Severity GetMinSeverity()
Gets the current minimum severity level for logging.
std::string GetThreadIdentifier()
Get thread name or ID as string.
std::ofstream & GetThreadStream()
Retrieves the thread-local output file stream for logging.
static void Log(const char *file, int line, logs::Severity level, const char *format,...)
Logs a formatted message with the specified severity level.
static std::string fgProcessName
Process name prefix for log files.
std::unordered_map< std::thread::id, std::unique_ptr< std::ofstream > > fThreadStreams
Map storing unique output file streams for each thread.
static logs::Severity GetSeverityFromString(const std::string &severity_str)
Parses a string to obtain the corresponding logs::Severity enum value.
virtual ~NLogger()
Destroys the NLogger instance.
static std::string SeverityToString(logs::Severity level)
Converts a logs::Severity enum value to its string representation.
static NLogger * Instance()
Returns the singleton instance of NLogger.
static bool GetFileOutput()
Get file output flag.
static void SetLogDirectory(const std::string &dir)
Sets the directory where log files will be stored.
void Cleanup()
Cleans up logger resources.
static std::unique_ptr< NLogger > fgLogger
Singleton instance.