NetCpp  v0.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Macros Pages
Logger Class Reference

Simple logger to log messages on file and console. More...

#include <logger.hpp>

Public Member Functions

void print (const std::string &sourceFile, const int codeLine, const std::string &message)
 Method used to print messages.
void setFile (const std::string &outputFile)
 Method to configure the logger.

Static Public Member Functions

static LoggergetInstance ()
 Method to get a reference to the object (i.e., Singleton)

Private Member Functions

 Logger ()
 Constructor.
 ~Logger ()
 Destructor.

Static Private Member Functions

static void lock ()
 Method to lock in case of multithreading.
static void unlock ()
 Method to unlock in case of multithreading.

Private Attributes

std::string logFile_
 Initial part of the name of the file used for Logging.
std::ofstream out_
 Stream used when logging on a file.
std::chrono::time_point
< std::chrono::system_clock > 
initialTime_
 Initial time (used to print relative times)

Static Private Attributes

static std::mutex lock_
 Lock for mutual exclusion between different threads.
static Loggerm_ = 0
 Pointer to the unique Logger (i.e., Singleton)

Detailed Description

Simple logger to log messages on file and console.

This is the implementation of a simple logger in C++. It is implemented as a Singleton, so it can be easily called through the DEBUG, WARNING and ERROR macros. It is Pthread-safe. It allows to log on both file and screen.

Example of configuration of the Logger: *

LOG_FILE("/tmp/myproject);

Example of usage of the Logger:

DEBUG("hello " << "world");

Definition at line 158 of file logger.hpp.

Constructor & Destructor Documentation

Logger ( )
private

Constructor.

It is a private constructor, called only by getInstance() and only the first time. It is called inside a lock, so lock inside this method is not required. It only initializes the initial time. All configuration is done inside the configure() method.

Definition at line 67 of file logger.cpp.

:
{
initialTime_ = std::chrono::system_clock::now();
}

Here is the caller graph for this function:

~Logger ( )
private

Destructor.

It only closes the file, if open, and cleans memory.

Definition at line 109 of file logger.cpp.

{
if (logFile_ != "")
out_.close();
delete m_;
}

Here is the call graph for this function:

Member Function Documentation

Logger & getInstance ( )
static

Method to get a reference to the object (i.e., Singleton)

This is a static method.

Returns
Reference to the object.

Definition at line 125 of file logger.cpp.

{
if (m_ == 0){
if (m_ == 0)
m_ = new Logger;
}
return *m_;
}

Here is the call graph for this function:

void lock ( )
inlinestaticprivate

Method to lock in case of multithreading.

Definition at line 53 of file logger.cpp.

{}

Here is the caller graph for this function:

void print ( const std::string &  file,
const int  line,
const std::string &  message 
)

Method used to print messages.

This method is called by the DEBUG(), WARNING() and ERROR() macros.

Parameters
severitylevelSeverity of the debug message
fileSource file where the method has been called (set equal to FILE by the DEBUG macro)
lineNumber of line in the source code where the method has been called (automatically set equal to LINE by the DEBUG macro)
messageMessage to be logged

Definition at line 148 of file logger.cpp.

{
std::chrono::time_point<std::chrono::system_clock> currentTime =
std::chrono::system_clock::now();
int elapsed_seconds = std::chrono::duration_cast<std::chrono::seconds>
(currentTime - initialTime_).count();
if (logFile_ != "") {
out_ << "[ " << elapsed_seconds << " ] " << message <<
"\t[ " << file << ":" << line << "]" << std::endl;
}
std::cerr << "[ " << elapsed_seconds << " ] " << message <<
"\t[ " << file << ":" << line << "]" << std::endl;
}

Here is the call graph for this function:

void setFile ( const std::string &  outputFile)

Method to configure the logger.

This method is called by the LOG_FILE() macro.

Parameters
outputFileName of the file used for logging

Definition at line 79 of file logger.cpp.

{
// Compute the whole file name:
std::ostringstream oss;
auto now = std::chrono::system_clock::now();
std::time_t currTime = std::chrono::system_clock::to_time_t(now);
struct tm *currTm = std::localtime(&currTime);
oss << outputFile << "_" <<
(1900 + currTm->tm_year) << "-" <<
currTm->tm_mon << "-" <<
currTm->tm_mday << "_" <<
currTm->tm_hour << "-" <<
currTm->tm_min << "-" <<
currTm->tm_sec << ".log";
logFile_ = oss.str().c_str();
// Open a new stream:
out_.open(logFile_.c_str(), std::ios::app);
}

Here is the call graph for this function:

void unlock ( )
inlinestaticprivate

Method to unlock in case of multithreading.

Definition at line 54 of file logger.cpp.

{}

Here is the caller graph for this function:

Member Data Documentation

std::chrono::time_point<std::chrono::system_clock> initialTime_
private

Initial time (used to print relative times)

Definition at line 200 of file logger.hpp.

std::mutex lock_
staticprivate

Lock for mutual exclusion between different threads.

Definition at line 177 of file logger.hpp.

std::string logFile_
private

Initial part of the name of the file used for Logging.

Date and time are automatically appended.

Definition at line 190 of file logger.hpp.

Logger * m_ = 0
staticprivate

Pointer to the unique Logger (i.e., Singleton)

Definition at line 183 of file logger.hpp.

std::ofstream out_
private

Stream used when logging on a file.

Definition at line 195 of file logger.hpp.


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