ndmspc  v1.1.1-1
NLogger.h
1 #pragma once
2 #include <memory>
3 #include <mutex>
4 #include <string>
5 #include <unordered_map>
6 #include <memory>
7 #include <mutex>
8 #include <string>
9 #include <unordered_map>
10 #include <fstream>
11 #include <thread>
12 
22 #define NLog(level, format, ...) Ndmspc::NLogger::Log(__FILE__, __LINE__, level, format, ##__VA_ARGS__)
23 
30 #define NLogTrace(format, ...) \
31  Ndmspc::NLogger::Log(__FILE__, __LINE__, Ndmspc::logs::Severity::kTrace, format, ##__VA_ARGS__)
32 
39 #define NLogDebug(format, ...) \
40  Ndmspc::NLogger::Log(__FILE__, __LINE__, Ndmspc::logs::Severity::kDebug, format, ##__VA_ARGS__)
41 
48 #define NLogInfo(format, ...) \
49  Ndmspc::NLogger::Log(__FILE__, __LINE__, Ndmspc::logs::Severity::kInfo, format, ##__VA_ARGS__)
50 
57 #define NLogWarning(format, ...) \
58  Ndmspc::NLogger::Log(__FILE__, __LINE__, Ndmspc::logs::Severity::kWarn, format, ##__VA_ARGS__)
59 
66 #define NLogError(format, ...) \
67  Ndmspc::NLogger::Log(__FILE__, __LINE__, Ndmspc::logs::Severity::kError, format, ##__VA_ARGS__)
68 
75 #define NLogFatal(format, ...) \
76  Ndmspc::NLogger::Log(__FILE__, __LINE__, Ndmspc::logs::Severity::kFatal, format, ##__VA_ARGS__)
77 
78 namespace Ndmspc {
79 
80 namespace logs {
81 
89 enum class Severity {
90  kTrace = 0,
91  kTrace2,
92  kTrace3,
93  kTrace4,
94  kDebug,
95  kDebug2,
96  kDebug3,
97  kDebug4,
98  kInfo,
99  kInfo2,
100  kInfo3,
101  kInfo4,
102  kWarn,
103  kWarn2,
104  kWarn3,
105  kWarn4,
106  kError,
107  kError2,
108  kError3,
109  kError4,
110  kFatal,
111  kFatal2,
112  kFatal3,
113  kFatal4
114 };
115 
117 static const std::unordered_map<std::string, Severity> fgSeverityMap = {
118  {"TRACE", Severity::kTrace}, {"TRACE2", Severity::kTrace2}, {"TRACE3", Severity::kTrace3},
119  {"TRACE4", Severity::kTrace4}, {"DEBUG", Severity::kDebug}, {"DEBUG2", Severity::kDebug2},
120  {"DEBUG3", Severity::kDebug3}, {"DEBUG4", Severity::kDebug4}, {"INFO", Severity::kInfo},
121  {"INFO2", Severity::kInfo2}, {"INFO3", Severity::kInfo3}, {"INFO4", Severity::kInfo4},
122  {"WARN", Severity::kWarn}, {"WARN2", Severity::kWarn2}, {"WARN3", Severity::kWarn3},
123  {"WARN4", Severity::kWarn4}, {"ERROR", Severity::kError}, {"ERROR2", Severity::kError2},
124  {"ERROR3", Severity::kError3}, {"ERROR4", Severity::kError4}, {"FATAL", Severity::kFatal},
125  {"FATAL2", Severity::kFatal2}, {"FATAL3", Severity::kFatal3}, {"FATAL4", Severity::kFatal4}};
126 } // namespace logs
127 
431 class NLogger {
432  public:
436  NLogger();
437 
441  virtual ~NLogger();
442 
443  // Delete copy constructor and assignment operator
444  NLogger(const NLogger &) = delete;
445  NLogger & operator=(const NLogger &) = delete;
446 
451  static NLogger * Instance();
452 
458  static std::string SeverityToString(logs::Severity level);
459 
465  static logs::Severity GetSeverityFromString(const std::string & severity_str);
466 
476  static void Log(const char * file, int line, logs::Severity level, const char * format, ...);
477 
482  static void SetMinSeverity(logs::Severity level) { fgMinSeverity = level; }
483 
488  static logs::Severity GetMinSeverity() { return fgMinSeverity; }
489 
494  static void SetLogDirectory(const std::string & dir);
495 
500  static void SetConsoleOutput(bool enable) { fgConsoleOutput = enable; }
501 
506  static void SetFileOutput(bool enable) { fgFileOutput = enable; } // New: Set process name (prefix for all log files)
507 
512  static void SetProcessName(const std::string & name);
513 
518  static std::string GetProcessName() { return fgProcessName; }
519 
525  static void SetThreadName(const std::string & name, std::thread::id thread_id = std::this_thread::get_id());
526 
531  static std::string GetThreadName();
532 
533  static bool GetConsoleOutput() { return fgConsoleOutput; }
534  static bool GetFileOutput() { return fgFileOutput; }
535  static std::string GetLogDirectory() { return fgLogDirectory; }
536  static std::mutex & GetLoggerMutex() { return fgLoggerMutex; }
537 
538  private:
539  static std::mutex fgLoggerMutex;
540  static logs::Severity fgMinSeverity;
541  static std::string fgLogDirectory;
542  static bool fgConsoleOutput;
543  static bool fgFileOutput;
544  static std::string fgProcessName;
545  static std::unique_ptr<NLogger> fgLogger;
546 
550  void Init();
551 
555  void Cleanup();
564  std::ofstream & GetThreadStream();
565 
571  void CloseThreadStream(std::thread::id thread_id);
572 
574  std::mutex fStreamMapMutex;
575 
581  std::unordered_map<std::thread::id, std::unique_ptr<std::ofstream>> fThreadStreams;
582  std::unordered_map<std::thread::id, std::string> fThreadNames;
583  std::string GetThreadIdentifier();
584 
586  // ClassDefOverride(NLogger, 1);
588 };
589 } // namespace Ndmspc
Thread-safe singleton logger with per-thread file output.
Definition: NLogger.h:431
static std::string fgLogDirectory
Directory for log files.
Definition: NLogger.h:541
std::mutex fStreamMapMutex
Definition: NLogger.h:574
static void SetMinSeverity(logs::Severity level)
Sets the minimum severity level for logging.
Definition: NLogger.h:482
std::unordered_map< std::thread::id, std::string > fThreadNames
Map of thread IDs to custom thread names.
Definition: NLogger.h:582
void CloseThreadStream(std::thread::id thread_id)
Closes and removes the output file stream associated with a specific thread.
Definition: NLogger.cxx:219
static bool fgFileOutput
Flag for file output.
Definition: NLogger.h:543
static void SetProcessName(const std::string &name)
Sets the name of the current process.
Definition: NLogger.cxx:122
static void SetConsoleOutput(bool enable)
Enables or disables logging output to the console.
Definition: NLogger.h:500
static std::mutex fgLoggerMutex
Mutex for thread-safe singleton access.
Definition: NLogger.h:539
static std::string GetThreadName()
Retrieves the name of the current thread.
Definition: NLogger.cxx:134
static logs::Severity fgMinSeverity
Minimum severity level for logging.
Definition: NLogger.h:540
void Init()
Initializes the logger.
Definition: NLogger.cxx:40
static std::mutex & GetLoggerMutex()
Get logger mutex reference.
Definition: NLogger.h:536
static std::string GetLogDirectory()
Get log directory path.
Definition: NLogger.h:535
static bool GetConsoleOutput()
Get console output flag.
Definition: NLogger.h:533
static std::string GetProcessName()
Retrieves the name of the current process.
Definition: NLogger.h:518
static void SetFileOutput(bool enable)
Enables or disables logging output to a file.
Definition: NLogger.h:506
NLogger()
Constructs a new NLogger instance.
Definition: NLogger.cxx:22
static void SetThreadName(const std::string &name, std::thread::id thread_id=std::this_thread::get_id())
Sets the name of a thread.
Definition: NLogger.cxx:128
static bool fgConsoleOutput
Flag for console output.
Definition: NLogger.h:542
static logs::Severity GetMinSeverity()
Gets the current minimum severity level for logging.
Definition: NLogger.h:488
std::string GetThreadIdentifier()
Get thread name or ID as string.
Definition: NLogger.cxx:150
std::ofstream & GetThreadStream()
Retrieves the thread-local output file stream for logging.
Definition: NLogger.cxx:166
static void Log(const char *file, int line, logs::Severity level, const char *format,...)
Logs a formatted message with the specified severity level.
Definition: NLogger.cxx:249
static std::string fgProcessName
Process name prefix for log files.
Definition: NLogger.h:544
std::unordered_map< std::thread::id, std::unique_ptr< std::ofstream > > fThreadStreams
Map storing unique output file streams for each thread.
Definition: NLogger.h:581
static logs::Severity GetSeverityFromString(const std::string &severity_str)
Parses a string to obtain the corresponding logs::Severity enum value.
Definition: NLogger.cxx:239
virtual ~NLogger()
Destroys the NLogger instance.
Definition: NLogger.cxx:28
static std::string SeverityToString(logs::Severity level)
Converts a logs::Severity enum value to its string representation.
Definition: NLogger.cxx:226
static NLogger * Instance()
Returns the singleton instance of NLogger.
Definition: NLogger.cxx:33
static bool GetFileOutput()
Get file output flag.
Definition: NLogger.h:534
static void SetLogDirectory(const std::string &dir)
Sets the directory where log files will be stored.
Definition: NLogger.cxx:109
void Cleanup()
Cleans up logger resources.
Definition: NLogger.cxx:101
static std::unique_ptr< NLogger > fgLogger
Singleton instance.
Definition: NLogger.h:545