NAWA 0.9
Web Application Framework for C++
nawa::Log Class Reference

#include <nawa/logging/Log.h>

Public Types

enum class  Level {
  OFF , ERROR , WARNING , INFORMATIONAL ,
  DEBUG
}
 

Public Member Functions

 Log () noexcept
 
 Log (std::string appname, Level level=Level::INFORMATIONAL) noexcept
 
 Log (Level level) noexcept
 
 Log (Log const &other) noexcept
 
Logoperator= (Log const &other) noexcept
 
virtual ~Log ()
 
void setAppname (std::string appname) noexcept
 
void setDefaultLogLevel (Level level) noexcept
 
void write (std::string const &msg)
 
void write (std::string const &msg, Level logLevel)
 
void operator() (std::string const &msg)
 
void operator() (std::string const &msg, Level logLevel)
 

Static Public Member Functions

static void setStream (std::ostream *os) noexcept
 
static void setOutfile (std::string const &filename)
 
static void setOutputLevel (Level level)
 
static void setExtendedFormat (bool useExtendedFormat)
 
static void lockStream () noexcept
 
static bool isLocked () noexcept
 

Detailed Description

Simple class for thread-safe logging to stderr or to any other output stream. This class uses the same ostream for logging in all instances and synchronizes the output. Every instance can have its own app name. By default, the logger will write to std::cout.

Definition at line 38 of file Log.h.

Member Enumeration Documentation

◆ Level

enum class nawa::Log::Level
strong

Available log levels. OFF disables logging, ERROR is for errors only, WARNING for warnings, INFORMATIONAL for useful messages such as startup actions, DEBUG for detailed log messages useful for debugging only.

Enumerator
OFF 
ERROR 
WARNING 
INFORMATIONAL 
DEBUG 

Definition at line 47 of file Log.h.

Constructor & Destructor Documentation

◆ Log() [1/4]

Log::Log ( )
noexcept

Construct a logger object with the default app name nawa and default log level STANDARD (can be changed later).

Definition at line 59 of file Log.cpp.

◆ Log() [2/4]

Log::Log ( std::string  appname,
Level  level = Level::INFORMATIONAL 
)
explicitnoexcept

Construct a logger object with a custom app name (can be changed later), and a default log level.

Parameters
appnameThe app name, appears in brackets in the log.
levelDefault log level. Defaults to STANDARD. Messages logged as OFF will be silently ignored.

Definition at line 78 of file Log.cpp.

◆ Log() [3/4]

Log::Log ( Level  level)
explicitnoexcept

Construct a logger object with a custom default log level.

Parameters
levelDefault log level. Messages logged as OFF will be silently ignored.

Definition at line 83 of file Log.cpp.

◆ Log() [4/4]

Log::Log ( Log const &  other)
noexcept

Definition at line 87 of file Log.cpp.

◆ ~Log()

Log::~Log ( )
virtual

Definition at line 100 of file Log.cpp.

Member Function Documentation

◆ operator=()

Log & Log::operator= ( Log const &  other)
noexcept

Definition at line 92 of file Log.cpp.

◆ setStream()

void Log::setStream ( std::ostream *  os)
staticnoexcept

Change the output stream to the specified one. This change will be permanent until every active Log object has been destructed. If the output stream is locked, this function will have no effect, and throw no exception (make sure to check isLocked() first).

This function is not thread-safe as long as the stream is unlocked. Please lock the stream before using the logger in a multi-threaded environment.

Parameters
osPointer to the output stream. Make sure that this stream will be available until NAWA terminates.

Definition at line 116 of file Log.cpp.

◆ setOutfile()

void Log::setOutfile ( std::string const &  filename)
static

Change the output to append to the specified log file. Will throw a nawa::Exception with error code 1 if the requested file cannot be opened for writing. This change will be permanent until every active Log object has been destructed. If the output stream is locked, this function will have no effect, and throw no exception (make sure to check isLocked() first).

This function is not thread-safe as long as the stream is unlocked. Please lock the stream before using the logger in a multi-threaded environment.

Parameters
filenamePath to the log file.

Definition at line 122 of file Log.cpp.

◆ setOutputLevel()

void Log::setOutputLevel ( Level  level)
static

Set the desired output log level. Only messages with a log level lower or equal the given level will be written to the output stream. Setting the output level to OFF will completely disable logging. The default output level is STANDARD. Modifying the output log level does not work while the output stream is locked, this function will have no effect then, and throw no exception (make sure to check isLocked() first).

Parameters
levelDesired output log level.

Definition at line 136 of file Log.cpp.

◆ setExtendedFormat()

void Log::setExtendedFormat ( bool  useExtendedFormat)
static

Use systemd-style extended log messages in the format {date} {time} {hostname} {process}[{PID}]: [{appname}] {message}. If off, the format is just [{appname}] {message}. Does not work while the output stream is locked, this function will have no effect then, and throw no exception (make sure to check isLocked() first).

Parameters
useExtendedFormatWhether to use the extended format. Off by default.

Definition at line 142 of file Log.cpp.

◆ lockStream()

void Log::lockStream ( )
staticnoexcept

Lock the output stream. It will not be possible to change the output stream of the logger anymore as long as there is at least one active Log object. If the output stream is already locked, this will have no effect. This operation is not reversible. Please run this function before multiple threads might construct Log objects, as setting the output stream/file is not thread-safe.

Definition at line 148 of file Log.cpp.

◆ isLocked()

bool Log::isLocked ( )
staticnoexcept

Check whether the output stream is locked.

Returns
True if locked, false if not.

Definition at line 152 of file Log.cpp.

◆ setAppname()

void Log::setAppname ( std::string  appname)
noexcept

Set the app name to use in debugging output. Defaults to nawa, but can be changed in order to use this logging class inside of NAWA apps.

Parameters
appnameThe app name, which will be include in the log in brackets.

Definition at line 156 of file Log.cpp.

◆ setDefaultLogLevel()

void Log::setDefaultLogLevel ( Level  level)
noexcept

Set the default log level for this logger.

Parameters
levelDefault log level. Messages logged as OFF will be silently ignored.

Definition at line 160 of file Log.cpp.

◆ write() [1/2]

void Log::write ( std::string const &  msg)

Write a message to the log using the default log level

Parameters
msgThe message.

Definition at line 164 of file Log.cpp.

◆ write() [2/2]

void Log::write ( std::string const &  msg,
Level  logLevel 
)

Write a message to the log.

Parameters
msgThe message.
logLevelLog level of this message. Messages logged as OFF will be silently ignored.

Definition at line 168 of file Log.cpp.

◆ operator()() [1/2]

void Log::operator() ( std::string const &  msg)

Write a message to the log using the default log level.

Parameters
msgThe message.

Definition at line 181 of file Log.cpp.

◆ operator()() [2/2]

void Log::operator() ( std::string const &  msg,
Level  logLevel 
)

Write a message to the log.

Parameters
msgThe message.
logLevelLog level of this message. Messages logged as OFF will be silently ignored.

Definition at line 185 of file Log.cpp.


The documentation for this class was generated from the following files: