|
ndmspc
v1.2.0-0.1.rc3
|
Thread-safe singleton logger with per-thread file output. More...
#include <NLogger.h>
Public Member Functions | |
| NLogger () | |
| Constructs a new NLogger instance. | |
| virtual | ~NLogger () |
| Destroys the NLogger instance. | |
| NLogger (const NLogger &)=delete | |
| NLogger & | operator= (const NLogger &)=delete |
Static Public Member Functions | |
| static NLogger * | Instance () |
| Returns the singleton instance of NLogger. More... | |
| static std::string | SeverityToString (logs::Severity level) |
| Converts a logs::Severity enum value to its string representation. More... | |
| static logs::Severity | GetSeverityFromString (const std::string &severity_str) |
| Parses a string to obtain the corresponding logs::Severity enum value. More... | |
| static void | Log (const char *file, int line, logs::Severity level, const char *format,...) |
| Logs a formatted message with the specified severity level. More... | |
| static void | SetMinSeverity (logs::Severity level) |
| Sets the minimum severity level for logging. More... | |
| static logs::Severity | GetMinSeverity () |
| Gets the current minimum severity level for logging. More... | |
| static void | SetLogDirectory (const std::string &dir) |
| Sets the directory where log files will be stored. More... | |
| static void | SetConsoleOutput (bool enable) |
| Enables or disables logging output to the console. More... | |
| static void | SetFileOutput (bool enable) |
| Enables or disables logging output to a file. More... | |
| static void | SetProcessName (const std::string &name) |
| Sets the name of the current process. More... | |
| static std::string | GetProcessName () |
| Retrieves the name of the current process. More... | |
| static void | SetThreadName (const std::string &name, std::thread::id thread_id=std::this_thread::get_id()) |
| Sets the name of a thread. More... | |
| static std::string | GetThreadName () |
| Retrieves the name of the current thread. More... | |
| static bool | GetConsoleOutput () |
| Get console output flag. | |
| static bool | GetFileOutput () |
| Get file output flag. | |
| static std::string | GetLogDirectory () |
| Get log directory path. | |
| static std::mutex & | GetLoggerMutex () |
| Get logger mutex reference. | |
Private Member Functions | |
| void | Init () |
| Initializes the logger. | |
| void | Cleanup () |
| Cleans up logger resources. | |
| std::ofstream & | GetThreadStream () |
| Retrieves the thread-local output file stream for logging. More... | |
| void | CloseThreadStream (std::thread::id thread_id) |
| Closes and removes the output file stream associated with a specific thread. More... | |
| std::string | GetThreadIdentifier () |
| Get thread name or ID as string. | |
Private Attributes | |
| std::mutex | fStreamMapMutex |
| std::unordered_map< std::thread::id, std::unique_ptr< std::ofstream > > | fThreadStreams |
| Map storing unique output file streams for each thread. More... | |
| std::unordered_map< std::thread::id, std::string > | fThreadNames |
| Map of thread IDs to custom thread names. | |
Static Private Attributes | |
| static std::mutex | fgLoggerMutex |
| Mutex for thread-safe singleton access. | |
| static logs::Severity | fgMinSeverity = logs::Severity::kInfo |
| Minimum severity level for logging. | |
| static std::string | fgLogDirectory = "/tmp/.ndmspc/logs" |
| Directory for log files. | |
| static bool | fgConsoleOutput = true |
| Flag for console output. | |
| static bool | fgFileOutput = false |
| Flag for file output. | |
| static std::string | fgProcessName = "" |
| Process name prefix for log files. | |
| static std::unique_ptr< NLogger > | fgLogger |
| Singleton instance. | |
Thread-safe singleton logger with per-thread file output.
NLogger provides a thread-safe logging system where each thread can write to its own log file. Supports multiple severity levels, console and file output, and custom naming for processes and threads.
NLogger is a thread-safe logging system for C++ applications with per-thread log files, configurable severity levels, and flexible output options.
| Macro | Level | Description |
|---|---|---|
| NLogTrace(format, ...) | TRACE | Detailed trace information |
| NLogDebug(format, ...) | DEBUG | Debug info (includes file:line) |
| NLogInfo(format, ...) | INFO | Informational messages |
| NLogWarning(format, ...) | WARN | Warning messages |
| NLogError(format, ...) | ERROR | Error messages |
| NLogFatal(format, ...) | FATAL | Fatal error messages |
Each level has 4 variants (e.g., TRACE, TRACE2, TRACE3, TRACE4):
TRACE (most verbose) → DEBUG → INFO (default) → WARN → ERROR → FATAL (least verbose)
| Variable | Default | Description |
|---|---|---|
| NDMSPC_LOG_LEVEL | INFO | Minimum severity (TRACE/DEBUG/INFO/WARN/ERROR/FATAL) |
| NDMSPC_LOG_FILE | false | Enable file output (1/true/TRUE) |
| NDMSPC_LOG_CONSOLE | true | Enable console output (0/1/false/true) |
| NDMSPC_LOG_DIR | ./logs | Log directory path |
| NDMSPC_PROCESS_NAME | pid_<PID> | Process name prefix |
| NDMSPC_MAIN_THREAD_NAME | MainThread | Main thread name |
Pattern: <LogDirectory>/<ProcessName>_<ThreadName>.log
Examples:
Standard format:
Debug format (includes file:line):
Each log file starts with:
Creates:
Or:
Or:
Or:
|
private |
Closes and removes the output file stream associated with a specific thread.
| thread_id | The ID of the thread whose stream should be closed. |
Definition at line 223 of file NLogger.cxx.
References fStreamMapMutex, fThreadNames, and fThreadStreams.
|
inlinestatic |
Gets the current minimum severity level for logging.
Definition at line 503 of file NLogger.h.
References fgMinSeverity.
Referenced by Log().
|
inlinestatic |
Retrieves the name of the current process.
Definition at line 533 of file NLogger.h.
References fgProcessName.
|
static |
Parses a string to obtain the corresponding logs::Severity enum value.
| severity_str | The string representation of the severity. |
Definition at line 243 of file NLogger.cxx.
Referenced by Init().
|
static |
Retrieves the name of the current thread.
Definition at line 138 of file NLogger.cxx.
References fStreamMapMutex, fThreadNames, and Instance().
|
private |
Retrieves the thread-local output file stream for logging.
Ensures that each thread has its own output file stream for writing log messages, creating a new stream if one does not already exist for the current thread.
Definition at line 170 of file NLogger.cxx.
References fgLogDirectory, fgProcessName, fStreamMapMutex, fThreadStreams, and GetThreadIdentifier().
|
static |
Returns the singleton instance of NLogger.
Definition at line 36 of file NLogger.cxx.
Referenced by GetThreadName(), Log(), and SetThreadName().
|
static |
Logs a formatted message with the specified severity level.
| file | The name of the source file where the log is generated. |
| line | The line number in the source file where the log is generated. |
| level | The severity level of the log message. |
| format | The printf-style format string for the log message. |
| ... | Additional arguments for the format string. |
Definition at line 253 of file NLogger.cxx.
References GetMinSeverity(), Instance(), and SeverityToString().
|
inlinestatic |
Enables or disables logging output to the console.
| enable | True to enable console output, false to disable. |
Definition at line 515 of file NLogger.h.
References fgConsoleOutput.
|
inlinestatic |
Enables or disables logging output to a file.
| enable | True to enable file output, false to disable. |
Definition at line 521 of file NLogger.h.
References fgFileOutput.
|
static |
Sets the directory where log files will be stored.
| dir | The path to the log directory. |
Definition at line 113 of file NLogger.cxx.
References fgFileOutput, and fgLogDirectory.
|
inlinestatic |
Sets the minimum severity level for logging.
| level | The minimum severity level to be set. |
Definition at line 497 of file NLogger.h.
References fgMinSeverity.
|
static |
Sets the name of the current process.
| name | The name to assign to the process. |
Definition at line 126 of file NLogger.cxx.
References fgLoggerMutex, and fgProcessName.
|
static |
Sets the name of a thread.
| name | The name to assign to the thread. |
| thread_id | The ID of the thread to name (defaults to current thread). |
Definition at line 132 of file NLogger.cxx.
References fStreamMapMutex, fThreadNames, and Instance().
Referenced by Ndmspc::NDimensionalExecutor::ExecuteParallel().
|
static |
Converts a logs::Severity enum value to its string representation.
| level | The severity level to convert. |
Definition at line 230 of file NLogger.cxx.
Referenced by Log().
|
private |
Mutex for synchronizing access to the thread stream map.
Definition at line 589 of file NLogger.h.
Referenced by Cleanup(), CloseThreadStream(), GetThreadName(), GetThreadStream(), and SetThreadName().
|
private |
Map storing unique output file streams for each thread.
Maps thread IDs to their corresponding std::ofstream instances.
Definition at line 596 of file NLogger.h.
Referenced by Cleanup(), CloseThreadStream(), and GetThreadStream().