DynExp
Highly flexible laboratory automation for dynamically changing experiments.
Util::EventLogger Class Reference

Logs events like errors and writes them immediately to a HTML file in a human-readable format. The logger also stores the events in an internal event log to be displayed within DynExp. The class is designed such that instances can be shared between different threads. Member function calls are synchronized. More...

+ Inheritance diagram for Util::EventLogger:

Public Member Functions

 EventLogger ()
 Constructs the event logger without opening a log file on disk. Events are only stored in the internal log until OpenLogFile() is called to open a log file on disk. More...
 
 EventLogger (std::string Filename)
 Constructs the event logger with opening a log file on disk. More...
 
 ~EventLogger ()
 Destructor closes the log file on disk. More...
 
void OpenLogFile (std::string Filename)
 Opens the HTML log file on disk. If it already exists, the file gets overwritten. More...
 
void CloseLogFile ()
 Closes the log file on disk and writes terminating HTML tags. More...
 
bool IsOpen () const
 Determines whether the log file has been openend on disk. More...
 
std::string GetLogFilename () const
 Determines the full file path to the currently openend log file. More...
 
void ClearLog ()
 Clears the internal event log. More...
 
std::vector< LogEntryGetLog (size_t FirstElement=0) const
 Returns the internal event log starting from the n-th stored element. More...
 
auto GetLogSize () const
 Determines the number of entries in the internal event log. More...
 
Noexcept logging

These functions do not throw exceptions. Instead, they do not perform logging if an exception occurs. This ensures that catching and logging exceptions does not possibly cause itself an exception which propagates through functions meant to be noexcept by catching and logging exceptions occurring within.

void Log (const std::string &Message, const ErrorType Type=ErrorType::Info, const size_t Line=0, const std::string &Function="", const std::string &File="", const int ErrorCode=0, const std::stacktrace &Trace={}) noexcept
 Logs an event from information specified manually. More...
 
void Log (const Exception &E) noexcept
 Logs a DynExp exception. More...
 
void Log (const Warning &W) noexcept
 Logs a warning. More...
 
- Public Member Functions inherited from Util::INonCopyable
 INonCopyable (const INonCopyable &)=delete
 
INonCopyableoperator= (const INonCopyable &)=delete
 

Static Public Member Functions

static std::string FormatLog (const std::string &Message, const size_t Line=0, const std::string &Function="", const std::string &Filename="", const int ErrorCode=0, const bool PrefixMessage=true)
 Formats a log entry as plain text to be displayed within DynExp. More...
 
static std::string FormatLogHTML (const std::string &Message, const ErrorType Type=ErrorType::Info, const size_t Line=0, const std::string &Function="", const std::string &Filename="", const int ErrorCode=0, const std::stacktrace &Trace={})
 Formats a log entry as HTML code to be displayed in a web browser. More...
 

Private Member Functions

Not thread-safe

These functions are not thread-safe. To be called from thread-safe functions only. Thread-safe functions must only call unsafe (private) functions.

bool IsOpenUnsafe () const
 
void CloseLogFileUnsafe ()
 
void ClearLogUnsafe ()
 

Private Attributes

std::ofstream LogFile
 Stream object to write to the log file on disk. More...
 
std::string Filename
 Filename and path to the log file on disk. More...
 
std::vector< LogEntryLogEntries
 Internally stored log entries. More...
 

Static Private Attributes

static constexpr auto LogOperationTimeout = std::chrono::milliseconds(100)
 Internal timeout for locking the mutex which synchronizes the calls to member function calls. If the timeout is exceeded while locking the mutex to log an event, the event is not logged. More...
 

Additional Inherited Members

- Static Public Attributes inherited from Util::ILockable
static constexpr std::chrono::milliseconds DefaultTimeout = std::chrono::milliseconds(10)
 Duration which is used as a default timeout within all methods of this class if no different duration is passed to them. More...
 
- Protected Types inherited from Util::ILockable
using MutexType = std::timed_mutex
 
using LockType = std::unique_lock< MutexType >
 
- Protected Member Functions inherited from Util::ILockable
 ILockable ()=default
 
 ~ILockable ()=default
 
LockType AcquireLock (const std::chrono::milliseconds Timeout=DefaultTimeout) const
 Locks the internal mutex. Blocks until the mutex is locked or until the timeout duration is exceeded. More...
 
- Protected Member Functions inherited from Util::INonCopyable
constexpr INonCopyable ()=default
 
 ~INonCopyable ()=default
 

Detailed Description

Logs events like errors and writes them immediately to a HTML file in a human-readable format. The logger also stores the events in an internal event log to be displayed within DynExp. The class is designed such that instances can be shared between different threads. Member function calls are synchronized.

Definition at line 1060 of file Util.h.

Constructor & Destructor Documentation

◆ EventLogger() [1/2]

Util::EventLogger::EventLogger ( )

Constructs the event logger without opening a log file on disk. Events are only stored in the internal log until OpenLogFile() is called to open a log file on disk.

Definition at line 304 of file Util.cpp.

◆ EventLogger() [2/2]

Util::EventLogger::EventLogger ( std::string  Filename)
inline

Constructs the event logger with opening a log file on disk.

Parameters
FilenameName and path of the HTML log file to write the events to

Definition at line 1073 of file Util.h.

◆ ~EventLogger()

Util::EventLogger::~EventLogger ( )
inline

Destructor closes the log file on disk.

Definition at line 1078 of file Util.h.

Member Function Documentation

◆ ClearLog()

void Util::EventLogger::ClearLog ( )
inline

Clears the internal event log.

Definition at line 1174 of file Util.h.

◆ ClearLogUnsafe()

void Util::EventLogger::ClearLogUnsafe ( )
inlineprivate

Definition at line 1198 of file Util.h.

◆ CloseLogFile()

void Util::EventLogger::CloseLogFile ( )
inline

Closes the log file on disk and writes terminating HTML tags.

Definition at line 1157 of file Util.h.

◆ CloseLogFileUnsafe()

void Util::EventLogger::CloseLogFileUnsafe ( )
private

Definition at line 496 of file Util.cpp.

◆ FormatLog()

std::string Util::EventLogger::FormatLog ( const std::string &  Message,
const size_t  Line = 0,
const std::string &  Function = "",
const std::string &  Filename = "",
const int  ErrorCode = 0,
const bool  PrefixMessage = true 
)
static

Formats a log entry as plain text to be displayed within DynExp.

Parameters
MessageMessage string describing the reason and consequences of the message
LineLine in source code where the message occurred
FunctionFunction in source code where the message occurred
FilenameSource code file where the message occurred
ErrorCodeDynExp error code from DynExpErrorCodes::DynExpErrorCodes
PrefixMessageIf true or Filename or Function are not empty, Message is prepended by a colon.
Returns
Returns the formatted log entry.

Definition at line 365 of file Util.cpp.

◆ FormatLogHTML()

std::string Util::EventLogger::FormatLogHTML ( const std::string &  Message,
const ErrorType  Type = ErrorType::Info,
const size_t  Line = 0,
const std::string &  Function = "",
const std::string &  Filename = "",
const int  ErrorCode = 0,
const std::stacktrace &  Trace = {} 
)
static

Formats a log entry as HTML code to be displayed in a web browser.

Parameters
MessageMessage string describing the reason and consequences of the message
TypeDynExp error type from Util::ErrorType
LineLine in source code where the message occurred
FunctionFunction in source code where the message occurred
FilenameSource code file where the message occurred
ErrorCodeDynExp error code from DynExpErrorCodes::DynExpErrorCodes
TraceStack trace object created where the message occurred. If it contains entries, they are displayed in an accordion-like style.
Returns
Returns the formatted log entry.

Definition at line 390 of file Util.cpp.

◆ GetLog()

std::vector< LogEntry > Util::EventLogger::GetLog ( size_t  FirstElement = 0) const

Returns the internal event log starting from the n-th stored element.

Parameters
FirstElementn-th element from which the returned event log starts
Returns
Event log as a vector of log entries of type Util::LogEntry
Exceptions
OutOfRangeExceptionis thrown if FirstElement exceeds the log size.

Definition at line 486 of file Util.cpp.

◆ GetLogFilename()

std::string Util::EventLogger::GetLogFilename ( ) const
inline

Determines the full file path to the currently openend log file.

Returns
Path to the openend log file. Empty string if the log file is not opened.

Definition at line 1169 of file Util.h.

◆ GetLogSize()

auto Util::EventLogger::GetLogSize ( ) const
inline

Determines the number of entries in the internal event log.

Returns
Number of events stored in the event log

Definition at line 1188 of file Util.h.

◆ IsOpen()

bool Util::EventLogger::IsOpen ( ) const
inline

Determines whether the log file has been openend on disk.

Returns
True if the log file is opened, false otherwise.

Definition at line 1163 of file Util.h.

◆ IsOpenUnsafe()

bool Util::EventLogger::IsOpenUnsafe ( ) const
inlineprivate

Definition at line 1196 of file Util.h.

◆ Log() [1/3]

void Util::EventLogger::Log ( const Exception E)
noexcept

Logs a DynExp exception.

Parameters
EException to log

Definition at line 342 of file Util.cpp.

◆ Log() [2/3]

void Util::EventLogger::Log ( const std::string &  Message,
const ErrorType  Type = ErrorType::Info,
const size_t  Line = 0,
const std::string &  Function = "",
const std::string &  File = "",
const int  ErrorCode = 0,
const std::stacktrace &  Trace = {} 
)
noexcept

Logs an event from information specified manually.

Parameters
MessageMessage string describing the reason and consequences of the message
TypeDynExp error type from Util::ErrorType
LineLine in source code where the message occurred
FunctionFunction in source code where the message occurred
FileSource code file where the message occurred
ErrorCodeDynExp error code from DynExpErrorCodes::DynExpErrorCodes
TraceStack trace object created where the message occurred.

Definition at line 309 of file Util.cpp.

◆ Log() [3/3]

void Util::EventLogger::Log ( const Warning W)
noexcept

Logs a warning.

Parameters
WWarning to log

Definition at line 351 of file Util.cpp.

◆ OpenLogFile()

void Util::EventLogger::OpenLogFile ( std::string  Filename)

Opens the HTML log file on disk. If it already exists, the file gets overwritten.

Parameters
FilenameName and path of the HTML log file to write the events to.

Definition at line 446 of file Util.cpp.

Member Data Documentation

◆ Filename

std::string Util::EventLogger::Filename
private

Filename and path to the log file on disk.

Definition at line 1208 of file Util.h.

◆ LogEntries

std::vector<LogEntry> Util::EventLogger::LogEntries
private

Internally stored log entries.

Definition at line 1210 of file Util.h.

◆ LogFile

std::ofstream Util::EventLogger::LogFile
private

Stream object to write to the log file on disk.

Definition at line 1207 of file Util.h.

◆ LogOperationTimeout

constexpr auto Util::EventLogger::LogOperationTimeout = std::chrono::milliseconds(100)
staticconstexprprivate

Internal timeout for locking the mutex which synchronizes the calls to member function calls. If the timeout is exceeded while locking the mutex to log an event, the event is not logged.

Definition at line 1205 of file Util.h.


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