ONPOSIX  2.0
 All Classes Namespaces Files Functions Variables Enumerator Friends Macros Pages
Logger Class Reference

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

#include <Logger.hpp>

Public Member Functions

void printOnFile (const std::string &sourceFile, const int codeLine, const std::string &message)
 Method used to print messages on file.
void printOnConsole (const std::string &sourceFile, const int codeLine, const std::string &message)
 Method used to print messages on console.
void setFile (const std::string &outputFile)
 Method to configure the logger.
bool latestMsgPrintedOnFile () const
 Method to know if the latest message has been printed on file.
bool latestMsgPrintedOnConsole () const
 Method to know if the latest message has been printed on file.

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.
bool latestMsgPrintedOnFile_
 Debug: to know if the latest message has been printed on file.
bool latestMsgPrintedOnConsole_
 Debug: to know if the latest message has been printed on console.

Static Private Attributes

static PosixMutex 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 console.

Example of configuration of the Logger: *

LOG_FILE("/tmp/myproject);

Example of usage of the Logger:

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

Definition at line 210 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 63 of file Logger.cpp.

:
logFile_(""),
{
gettimeofday(&initialTime_, NULL);
}

Here is the caller graph for this function:

~Logger ( )
private

Destructor.

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

Definition at line 116 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 132 of file Logger.cpp.

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

Here is the call graph for this function:

bool latestMsgPrintedOnConsole ( ) const
inline

Method to know if the latest message has been printed on file.

Returns
true if it has been printed; false otherwise

Definition at line 238 of file Logger.hpp.

bool latestMsgPrintedOnFile ( ) const
inline

Method to know if the latest message has been printed on file.

Returns
true if it has been printed; false otherwise

Definition at line 229 of file Logger.hpp.

void lock ( )
inlinestaticprivate

Method to lock in case of multithreading.

Definition at line 46 of file Logger.cpp.

{}

Here is the caller graph for this function:

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

Method used to print messages on console.

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 154 of file Logger.cpp.

{
struct timeval currentTime;
gettimeofday(&currentTime, NULL);
std::cout <<
(currentTime.tv_sec - initialTime_.tv_sec) <<
":" << message << "\t\t[" << file << ":" << line << "]" <<
std::endl;
}

Here is the call graph for this function:

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

Method used to print messages on file.

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 186 of file Logger.cpp.

{
struct timeval currentTime;
gettimeofday(&currentTime, NULL);
if (logFile_ != "") {
out_ <<
(currentTime.tv_sec - initialTime_.tv_sec) <<
":" << message << "\t\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 77 of file Logger.cpp.

{
// Compute a new file name, if needed
if (outputFile != logFile_){
if (logFile_ != "")
out_.close();
std::ostringstream oss;
time_t currTime;
time(&currTime);
struct tm *currTm = localtime(&currTime);
oss << outputFile << "_" <<
currTm->tm_mday << "_" <<
currTm->tm_mon << "_" <<
(1900 + currTm->tm_year) << "_" <<
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 47 of file Logger.cpp.

{}

Here is the caller graph for this function:

Member Data Documentation

bool latestMsgPrintedOnConsole_
private

Debug: to know if the latest message has been printed on console.

Definition at line 285 of file Logger.hpp.

bool latestMsgPrintedOnFile_
private

Debug: to know if the latest message has been printed on file.

Definition at line 279 of file Logger.hpp.

PosixMutex lock_
staticprivate

Lock for mutual exclusion between different threads.

Definition at line 250 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 263 of file Logger.hpp.

Logger * m_ = 0
staticprivate

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

Definition at line 256 of file Logger.hpp.

std::ofstream out_
private

Stream used when logging on a file.

Definition at line 268 of file Logger.hpp.


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