|
ONPOSIX
2.0
|
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 Logger & | getInstance () |
| 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 Logger * | m_ = 0 |
| Pointer to the unique Logger (i.e., Singleton) | |
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: *
Example of usage of the Logger:
Definition at line 210 of file Logger.hpp.
|
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.

|
private |
Destructor.
It only closes the file, if open, and cleans memory.
Definition at line 116 of file Logger.cpp.

|
static |
Method to get a reference to the object (i.e., Singleton)
This is a static method.
Definition at line 132 of file Logger.cpp.

|
inline |
Method to know if the latest message has been printed on file.
Definition at line 238 of file Logger.hpp.
|
inline |
Method to know if the latest message has been printed on file.
Definition at line 229 of file Logger.hpp.
|
inlinestaticprivate |
Method to lock in case of multithreading.
Definition at line 46 of file Logger.cpp.

| 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.
| severitylevel | Severity of the debug message |
| file | Source file where the method has been called (set equal to FILE by the DEBUG macro) |
| line | Number of line in the source code where the method has been called (automatically set equal to LINE by the DEBUG macro) |
| message | Message to be logged |
Definition at line 154 of file Logger.cpp.

| 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.
| severitylevel | Severity of the debug message |
| file | Source file where the method has been called (set equal to FILE by the DEBUG macro) |
| line | Number of line in the source code where the method has been called (automatically set equal to LINE by the DEBUG macro) |
| message | Message to be logged |
Definition at line 186 of file Logger.cpp.

| void setFile | ( | const std::string & | outputFile | ) |
Method to configure the logger.
This method is called by the LOG_FILE() macro.
| outputFile | Name of the file used for logging |
Definition at line 77 of file Logger.cpp.

|
inlinestaticprivate |
Method to unlock in case of multithreading.
Definition at line 47 of file Logger.cpp.

|
private |
Debug: to know if the latest message has been printed on console.
Definition at line 285 of file Logger.hpp.
|
private |
Debug: to know if the latest message has been printed on file.
Definition at line 279 of file Logger.hpp.
|
staticprivate |
Lock for mutual exclusion between different threads.
Definition at line 250 of file Logger.hpp.
|
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.
|
staticprivate |
Pointer to the unique Logger (i.e., Singleton)
Definition at line 256 of file Logger.hpp.
|
private |
Stream used when logging on a file.
Definition at line 268 of file Logger.hpp.