|
FlatImage
A configurable Linux containerization system
|
A library for file logging. More...
#include <pthread.h>#include <filesystem>#include <iostream>#include <fstream>#include <print>#include <ranges>#include <unistd.h>#include "../std/concept.hpp"#include "../std/string.hpp"Go to the source code of this file.
Classes | |
| class | ns_log::anonymous_namespace{log.hpp}::Logger |
| struct | ns_log::Location |
| Source code location information for log messages. More... | |
| class | ns_log::Writer |
| Base class for level-specific log writers (debug, info, warn, error, critical) More... | |
| class | ns_log::debug |
| Debug-level logger (lowest priority) More... | |
| class | ns_log::info |
| Info-level logger (informational messages) More... | |
| class | ns_log::warn |
| Warning-level logger (potential issues) More... | |
| class | ns_log::error |
| Error-level logger (recoverable errors) More... | |
| class | ns_log::critical |
| Critical-level logger (highest priority, always shown) More... | |
Namespaces | |
| namespace | ns_log |
| Multi-level logging system with file and stdout sinks. | |
Macros | |
| #define | logger(fmt, ...) |
| Compile-time log level dispatch macro with automatic location capture. | |
| #define | logger_loc(loc, fmt, ...) |
| Compile-time log level dispatch macro with manual location specification. | |
Enumerations | |
| enum class | Level : int { CRITICAL , ERROR , WARN , INFO , DEBUG } |
Functions | |
| void | ns_log::anonymous_namespace{log.hpp}::fork_handler_child () |
| Fork handler that resets the logger in child processes. | |
| void | ns_log::set_level (Level level) |
| Sets the logging verbosity (CRITICAL,ERROR,INFO,DEBUG) | |
| Level | ns_log::get_level () |
| Get current verbosity level of the logger. | |
| void | ns_log::set_sink_file (fs::path const &path_file_sink) |
| Sets the sink file of the logger. | |
| void | ns_log::set_as_fork () |
| Marks the logger as being in a forked child process. | |
| template<typename... Ts> | |
| std::string | ns_log::vformat (std::string_view fmt, Ts &&... ts) |
| Workaround make_format_args only taking references. | |
| template<ns_string::static_string str> | |
| constexpr decltype(auto) | ns_log::impl_log (Location loc) |
| Implementation function for compile-time log level dispatch. | |
Variables | |
| thread_local Logger | ns_log::anonymous_namespace{log.hpp}::logger |
| Thread-local logger instance. | |
A library for file logging.
This logger uses thread_local storage combined with pthread_atfork() to handle both multi-threading and process forking correctly.
thread_local)The thread_local Logger logger ensures that each thread in a process gets its own independent Logger instance:
Benefits:
When fork() is called, the child process inherits a copy of the parent's memory:
Without pthread_atfork (problematic):
With pthread_atfork (this implementation):
In the Logger constructor:
fork_handler_child runs in child process after fork()File descriptors are NOT shared memory - they're kernel objects with reference counts:
Therefore, closing the file in the child does NOT affect the parent's ability to log.
The Logger class is completely non-copyable and non-movable:
fork_handler_child() calls set_sink_file("/dev/null")| Scenario | Behavior | Thread-Safe? |
|---|---|---|
| Single thread | One logger instance | ✅ Yes |
| Multiple threads | Separate logger per thread | ✅ Yes (isolated) |
| Fork (parent) | Keeps existing logger | ✅ Yes |
| Fork (child) | Fresh logger (/dev/null) | ✅ Yes (auto-reset) |
| Fork then thread | Child's threads get new loggers | ✅ Yes |
Definition in file log.hpp.
| #define logger | ( | fmt, | |
| ... ) |
Compile-time log level dispatch macro with automatic location capture.
This macro provides a convenient interface for logging with compile-time level selection based on message prefix. The source file and line number are automatically captured.
Log Level Prefixes:
D:: - Debug (lowest priority, only shown when level = DEBUG)I:: - Info (informational messages)W:: - Warning (potential issues, uses stderr)E:: - Error (recoverable errors, uses stderr)C:: - Critical (highest priority, always shown, uses stderr)Q:: - Quiet (message discarded, useful for conditional compilation)Format String: Uses std::format syntax with positional arguments: {0}, {1}, {}
Examples:
Output Format:
Example output:
| fmt | Format string with log level prefix (e.g., "I::Message: {}") |
| ... | Variadic arguments for format string placeholders |
| #define logger_loc | ( | loc, | |
| fmt, | |||
| ... ) |
Compile-time log level dispatch macro with manual location specification.
Similar to logger() but allows specifying a custom source location. Useful for logging from helper functions while preserving the original call site location.
Use Cases:
Examples:
| loc | ns_log::Location instance with file/line information |
| fmt | Format string with log level prefix (e.g., "I::Message: {}") |
| ... | Variadic arguments for format string placeholders |