C++ Trivial Logger Tutorial |
||||
IntroductionThe library is designed to be included into the projects contents without external compilation.
This kind of design was chosen because library contains two header files and one code file.
And also library interacts with compilation flags, so there is a need to make desired
compilation with library included as a part of the program.
Present release contains files:
UsageThe most important part of usage the library is to decide which available stream
has to be chosen at the beginning of the program - when the program launch.
Once more there is very important to highlight that usual programs starting before
main() function. So the CxxTL have to be initialized before it will be used.
The flexibility of the logger lies because it uses overloaded operator<<.
Therefore all user stream operations will be reused in logger. However simplicity of the library
allow in few minutes to define own kind of logging functionality
(see User-defined extensions).
Configuration of the code belongs to the user. Important files are:
Next do the following steps:
C++ Trivial Logger uses three definitions that have to be passed as compilation flags to the compiled code.
Below are shown all function like macros which are defined in library in simple source code.
Demonstration program can be downloaded from SourceForge web site.
#include "tri_logger.hpp"
void finalize() { TRI_LOG_STR("atexit()->finalize()->EXIT_SUCCESS"); } template < typename T, typename U > std::ostream & operator<< ( std::ostream & os, std::pair < T, U > const & p ) { os << "( " << p.first << ", " << p.second << " )"; return os; } int main () { std::atexit(finalize); double const a = 1.0; std::string const str = "test"; std::pair < std::string, double > p ( "PI", 3.1415 ); std::pair < unsigned long, std::string > q ( 10, "EUR" ); TRI_LOG_STR ("Trivial logger in main()."); TRI_LOG (a); TRI_LOG (str); TRI_LOG (p); TRI_LOG_OFF(); TRI_LOG_STR ("Trivial logger in main()."); TRI_LOG_ON(); TRI_LOG_STR("Test of the user-defined TRI_LOG_FN macro."); TRI_LOG_FN (str); } The results are like below.
foo.cpp [7] : Creation of the foo object.
main.cpp [23] : Trivial logger in main(). main.cpp [24] : a = 1 main.cpp [25] : str = test main.cpp [26] : p = ( PI, 3.1415 ) main.cpp [27] : q = ( 10, EUR ) main.cpp [33] : Test of the user-defined TRI_LOG_FN macro. main.cpp (34) : str = test main.cpp [12] : atexit()->finalize()->EXIT_SUCCES foo.cpp [12] : Destruction of the foo object.
With that simple example we can learn that Trivial Logger:
Remarks:
User-defined extensionsAs example of extension can be considered simple logging function:
template < typename T >
void put_log_info ( tri_logger_t & log, T const & t ) { if ( log.is_activated() ) { *(log.ostream_ptr()) << t << std::endl; } } The central point of the code is o use *(log.ostream_ptr())
as a raw pointer to the stream. Then any data can be sent there.
Good is to define macro similar to shown below, but it is not necessary if
Logger will work all the time. And the second reason for defining
macros is that in macros are available __LINE__, __FILE__ directives.
#define TRI_LOG_INFO(name) \
trivial_logger::put_log_info ( trivial_logger::tri_logger(), (name) ) Library internalsVisual Studio configuration and tricks
To use CxxTL in Visual Studio there is need to copy nullstream.hpp,
tri_logger.hpp and
tri_logger.cpp files in to the proper project directory. Then this files have to be added
to the project, by clicking on them and choosing "Import ...".
To launch the Trivial Logger there is a need to add its flag to preprocessor invocation.
On the screenshots below are shown examples what and where should be added. The "main" is a logger file name.
Also is possible to add Trivial Logger to Managed C++ projects. However it needs to be added manually one line
to the tri_logger.cpp file. Instead of:
#include "tri_logger.hpp"
Should be:
#include "stdafx.h"
#include "tri_logger.hpp" |