ndmspc v1.2.0-0.1.rc7
Loading...
Searching...
No Matches
NLogger.h
1#ifndef NdmspcCoreNLogger_H
2#define NdmspcCoreNLogger_H
3
4#include <memory>
5#include <mutex>
6#include <string>
7#include <unordered_map>
8#include <memory>
9#include <mutex>
10#include <string>
11#include <unordered_map>
12#include <fstream>
13#include <thread>
14#include <nlohmann/json.hpp>
15using json = nlohmann::json;
16// using json = nlohmann::ordered_json;
17
27#define NLog(level, format, ...) Ndmspc::NLogger::Log(__FILE__, __LINE__, level, format, ##__VA_ARGS__)
28
35#define NLogTrace(format, ...) \
36 Ndmspc::NLogger::Log(__FILE__, __LINE__, Ndmspc::logs::Severity::kTrace, format, ##__VA_ARGS__)
37
44#define NLogDebug(format, ...) \
45 Ndmspc::NLogger::Log(__FILE__, __LINE__, Ndmspc::logs::Severity::kDebug, format, ##__VA_ARGS__)
46
53#define NLogInfo(format, ...) \
54 Ndmspc::NLogger::Log(__FILE__, __LINE__, Ndmspc::logs::Severity::kInfo, format, ##__VA_ARGS__)
55
62#define NLogWarning(format, ...) \
63 Ndmspc::NLogger::Log(__FILE__, __LINE__, Ndmspc::logs::Severity::kWarn, format, ##__VA_ARGS__)
64
71#define NLogError(format, ...) \
72 Ndmspc::NLogger::Log(__FILE__, __LINE__, Ndmspc::logs::Severity::kError, format, ##__VA_ARGS__)
73
80#define NLogFatal(format, ...) \
81 Ndmspc::NLogger::Log(__FILE__, __LINE__, Ndmspc::logs::Severity::kFatal, format, ##__VA_ARGS__)
82
91#define NLogPrint(format, ...) fprintf(stdout, format "\n", ##__VA_ARGS__)
92
93namespace Ndmspc {
94
95namespace logs {
96
104enum class Severity {
105 kTrace = 0,
106 kTrace2,
107 kTrace3,
108 kTrace4,
109 kDebug,
110 kDebug2,
111 kDebug3,
112 kDebug4,
113 kInfo,
114 kInfo2,
115 kInfo3,
116 kInfo4,
117 kWarn,
118 kWarn2,
119 kWarn3,
120 kWarn4,
121 kError,
122 kError2,
123 kError3,
124 kError4,
125 kFatal,
126 kFatal2,
127 kFatal3,
128 kFatal4
129};
130
132static 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}};
141} // namespace logs
142
446class NLogger {
447 public:
451 NLogger();
452
456 virtual ~NLogger();
457
458 // Delete copy constructor and assignment operator
459 NLogger(const NLogger &) = delete;
460 NLogger & operator=(const NLogger &) = delete;
461
466 static NLogger * Instance();
467
473 static std::string SeverityToString(logs::Severity level);
474
480 static logs::Severity GetSeverityFromString(const std::string & severity_str);
481
491 static void Log(const char * file, int line, logs::Severity level, const char * format, ...);
492
497 static void SetMinSeverity(logs::Severity level) { fgMinSeverity = level; }
498
503 static logs::Severity GetMinSeverity() { return fgMinSeverity; }
504
509 static void SetLogDirectory(const std::string & dir);
510
515 static void SetConsoleOutput(bool enable) { fgConsoleOutput = enable; }
516
521 static void SetFileOutput(bool enable) { fgFileOutput = enable; } // New: Set process name (prefix for all log files)
522
527 static void SetProcessName(const std::string & name);
528
533 static std::string GetProcessName() { return fgProcessName; }
534
540 static void SetThreadName(const std::string & name, std::thread::id thread_id = std::this_thread::get_id());
541
546 static std::string GetThreadName();
547
548 static bool GetConsoleOutput() { return fgConsoleOutput; }
549 static bool GetFileOutput() { return fgFileOutput; }
550 static std::string GetLogDirectory() { return fgLogDirectory; }
551 static std::mutex & GetLoggerMutex() { return fgLoggerMutex; }
552
553 private:
554 static std::mutex fgLoggerMutex;
555 static logs::Severity fgMinSeverity;
556 static std::string fgLogDirectory;
557 static bool fgConsoleOutput;
558 static bool fgFileOutput;
559 static std::string fgProcessName;
560 static std::unique_ptr<NLogger> fgLogger;
561
565 void Init();
566
570 void Cleanup();
579 std::ofstream & GetThreadStream();
580
586 void CloseThreadStream(std::thread::id thread_id);
587
589 std::mutex fStreamMapMutex;
590
596 std::unordered_map<std::thread::id, std::unique_ptr<std::ofstream>> fThreadStreams;
597 std::unordered_map<std::thread::id, std::string> fThreadNames;
598 std::string GetThreadIdentifier();
599
601 // ClassDefOverride(NLogger, 1);
603};
604} // namespace Ndmspc
605
606#endif // NdmspcCoreNLogger_H
static std::string fgLogDirectory
Directory for log files.
Definition NLogger.h:556
std::mutex fStreamMapMutex
Definition NLogger.h:589
static void SetMinSeverity(logs::Severity level)
Sets the minimum severity level for logging.
Definition NLogger.h:497
std::unordered_map< std::thread::id, std::string > fThreadNames
Map of thread IDs to custom thread names.
Definition NLogger.h:597
void CloseThreadStream(std::thread::id thread_id)
Closes and removes the output file stream associated with a specific thread.
Definition NLogger.cxx:223
static bool fgFileOutput
Flag for file output.
Definition NLogger.h:558
static void SetProcessName(const std::string &name)
Sets the name of the current process.
Definition NLogger.cxx:126
static void SetConsoleOutput(bool enable)
Enables or disables logging output to the console.
Definition NLogger.h:515
static std::mutex fgLoggerMutex
Mutex for thread-safe singleton access.
Definition NLogger.h:554
static std::string GetThreadName()
Retrieves the name of the current thread.
Definition NLogger.cxx:138
static logs::Severity fgMinSeverity
Minimum severity level for logging.
Definition NLogger.h:555
void Init()
Initializes the logger.
Definition NLogger.cxx:43
static std::string GetLogDirectory()
Get log directory path.
Definition NLogger.h:550
static bool GetConsoleOutput()
Get console output flag.
Definition NLogger.h:548
static std::string GetProcessName()
Retrieves the name of the current process.
Definition NLogger.h:533
static void SetFileOutput(bool enable)
Enables or disables logging output to a file.
Definition NLogger.h:521
NLogger()
Constructs a new NLogger instance.
Definition NLogger.cxx:25
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:132
static bool fgConsoleOutput
Flag for console output.
Definition NLogger.h:557
static logs::Severity GetMinSeverity()
Gets the current minimum severity level for logging.
Definition NLogger.h:503
std::string GetThreadIdentifier()
Get thread name or ID as string.
Definition NLogger.cxx:154
std::ofstream & GetThreadStream()
Retrieves the thread-local output file stream for logging.
Definition NLogger.cxx:170
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:253
static std::string fgProcessName
Process name prefix for log files.
Definition NLogger.h:559
std::unordered_map< std::thread::id, std::unique_ptr< std::ofstream > > fThreadStreams
Map storing unique output file streams for each thread.
Definition NLogger.h:596
static logs::Severity GetSeverityFromString(const std::string &severity_str)
Parses a string to obtain the corresponding logs::Severity enum value.
Definition NLogger.cxx:243
virtual ~NLogger()
Destroys the NLogger instance.
Definition NLogger.cxx:31
static std::string SeverityToString(logs::Severity level)
Converts a logs::Severity enum value to its string representation.
Definition NLogger.cxx:230
static NLogger * Instance()
Returns the singleton instance of NLogger.
Definition NLogger.cxx:36
static std::mutex & GetLoggerMutex()
Get logger mutex reference.
Definition NLogger.h:551
static bool GetFileOutput()
Get file output flag.
Definition NLogger.h:549
static void SetLogDirectory(const std::string &dir)
Sets the directory where log files will be stored.
Definition NLogger.cxx:113
void Cleanup()
Cleans up logger resources.
Definition NLogger.cxx:105
static std::unique_ptr< NLogger > fgLogger
Singleton instance.
Definition NLogger.h:560
Global callback function for libwebsockets client events.