E-MailRelay
|
Controls and implements low-level logging output, as used by G::Log. More...
#include <glogoutput.h>
Classes | |
struct | Config |
A configuration structure for G::LogOutput. More... | |
struct | Instance |
A set of convenience functions for calling LogOutput methods on LogOutput::instance(). More... | |
Public Member Functions | |
LogOutput (const std::string &exename, const Config &config, const Path &logfile={}) | |
Constructor. More... | |
LogOutput (const std::string &exename, const Config &config, int fd) | |
Constructor overload taking a file descriptor. More... | |
LogOutput (bool enabled, bool verbose=true, const G::Path &logfile={}) | |
Constructor overload for test programs. More... | |
~LogOutput () | |
Destructor. More... | |
Config | config () const noexcept |
Returns the current configuration. More... | |
int | fd () const noexcept |
Returns the output file descriptor. More... | |
void | configure (const Config &) |
Updates the current configuration. More... | |
bool | at (Severity) const noexcept |
Returns true if logging should occur for the given severity level. More... | |
void | context (std::string_view(*fn)(void *)=nullptr, void *fn_arg=nullptr) noexcept |
Sets a functor that is used to provide a context string for every log line, if configured. More... | |
void * | contextarg () noexcept |
Returns the functor argument as set by the last call to context(). More... | |
LogStream | start (Severity, const char *file, int line) noexcept |
Returns an ostream for a new log line. More... | |
void | output (LogStream &) noexcept |
Emits the current log line (see start()). More... | |
LogOutput (const LogOutput &)=delete | |
LogOutput (LogOutput &&)=delete | |
LogOutput & | operator= (const LogOutput &)=delete |
LogOutput & | operator= (LogOutput &&)=delete |
Static Public Member Functions | |
static LogOutput * | instance () noexcept |
Returns a pointer to the controlling LogOutput object. More... | |
static void | assertion (LogOutput *, const char *file, int line, bool test, const char *test_string) |
Performs an assertion check. More... | |
static void | assertion (LogOutput *, const char *file, int line, void *test, const char *test_string) |
Performs an assertion check. More... | |
static void | assertionFailure (LogOutput *, const char *file, int line, const char *test_string) noexcept |
Reports an assertion failure. More... | |
static GDEF_NORETURN_LHS void | assertionAbort () GDEF_NORETURN_RHS |
Aborts the program when an assertion has failed. More... | |
static void | register_ (const Path &exe) |
Registers the given executable as a source of logging. More... | |
Friends | |
struct | Instance |
Controls and implements low-level logging output, as used by G::Log.
Applications should instantiate a LogOutput object in main() to enable and configure log output.
The implementation uses a file descriptor for osoutput() rather than a stream because windows file-sharing options are not accessible when building with mingw streams, and to avoid double buffering and mixed output from multiple threads.
Definition at line 61 of file glogoutput.h.
|
strong |
Definition at line 67 of file glogoutput.h.
|
strong |
Definition at line 78 of file glogoutput.h.
G::LogOutput::LogOutput | ( | const std::string & | exename, |
const Config & | config, | ||
const Path & | logfile = {} |
||
) |
Constructor.
If there is no LogOutput object, or if 'config.output_enabled' is false, then there is no output at all except for assertions to stderr.
The 'exename' value is emitted as an additional prefix on each log line if it is non-empty. See also G::Arg::prefix().
The log-file path can contain substitution variables "%d" or "%h" for the current day and hour respectively. If no path is given then logging is sent to the standard error stream by default; stderr should be re-opened onto /dev/null if only syslog logging is required.
More than one LogOutput object may be created but only the first one controls output (see instance()).
Definition at line 86 of file glogoutput.cpp.
G::LogOutput::LogOutput | ( | const std::string & | exename, |
const Config & | config, | ||
int | fd | ||
) |
Constructor overload taking a file descriptor.
Typically used for non-main threads when instance() is thread-local.
Definition at line 95 of file glogoutput.cpp.
|
explicit |
Constructor overload for test programs.
Definition at line 104 of file glogoutput.cpp.
G::LogOutput::~LogOutput | ( | ) |
Destructor.
Definition at line 138 of file glogoutput.cpp.
|
inlinestatic |
Performs an assertion check.
Definition at line 289 of file glogoutput.h.
|
inlinestatic |
Performs an assertion check.
This overload, using a test on a pointer, is motivated by MSVC warnings.
Definition at line 298 of file glogoutput.h.
|
static |
Aborts the program when an assertion has failed.
Definition at line 363 of file glogoutput.cpp.
|
staticnoexcept |
Reports an assertion failure.
Definition at line 341 of file glogoutput.cpp.
|
noexcept |
Returns true if logging should occur for the given severity level.
Definition at line 180 of file glogoutput.cpp.
|
noexcept |
Returns the current configuration.
Definition at line 121 of file glogoutput.cpp.
void G::LogOutput::configure | ( | const Config & | config | ) |
Updates the current configuration.
Definition at line 133 of file glogoutput.cpp.
|
noexcept |
Sets a functor that is used to provide a context string for every log line, if configured.
The functor should return the context string with trailing punctuation, typically colon and space.
Definition at line 162 of file glogoutput.cpp.
|
noexcept |
Returns the functor argument as set by the last call to context().
Definition at line 173 of file glogoutput.cpp.
|
noexcept |
Returns the output file descriptor.
Definition at line 127 of file glogoutput.cpp.
|
staticnoexcept |
Returns a pointer to the controlling LogOutput object.
Returns nullptr if none.
Non-main threads can do logging safely if the (private) instance pointer is declared 'thread_local' at build-time. Thread functions can then instantiate their own LogOutput object on the stack, typically with the Config object and file descriptor passed in from the main thread.
Definition at line 157 of file glogoutput.cpp.
|
noexcept |
Emits the current log line (see start()).
Does nothing if there is no LogOutput instance.
Definition at line 209 of file glogoutput.cpp.
|
static |
Registers the given executable as a source of logging.
This is called from osinit(), but it might also need to be done as a program installation step with the necessary process permissions.
Definition at line 92 of file glogoutput_unix.cpp.
|
noexcept |
Returns an ostream for a new log line.
The caller should stream out the rest of the log line into the ostream and then call output(). Calls to start() and output() should be in pairs.
Definition at line 194 of file glogoutput.cpp.
|
friend |
Definition at line 264 of file glogoutput.h.