📜 ⬆️ ⬇️

Introduction to the Pantheios Logging Library

Everyone somehow had to build a logging system into their program. Many write their own, because believe that smarter than others of its most optimal. Others use ready-made libraries that can greatly simplify life.

Today I would like to tell you about the Pantheios logging library, which, according to the creators, has high speed , multi-streaming, unicode support, and simultaneous output to different recipients of logs.


General remarks


I will consider logger with tz use on Windows, with a compiler that comes with Visual Studio 2010 SP1, given the fact that I would like to have logs in UTF-16LE (in the file) and at the same time display them in colors on the console. It is also important for me to be able to output logs to a cosol in Unicode mode in order to support output in national alphabets.
')
The article is written for beginners and for quick familiarization with the library.

Getting the library




Build a library


So, for the assembly we need the actual source files pantheios and stlsoft.
The first can be downloaded from here , and STLSoft STL from here .
For definiteness, I took the latest available stable source versions of pantheios-1.0.1-beta211 and stlsoft-1.9.108.

Unpack the sources in C: \ temp and proceed to build.
Launch the VS2010 console, where we specify the STLSOFT environment variable and run the assembly:
>SET STLSOFT=c:\temp\stlsoft-1.9.108\
>cd C:\temp\pantheios-1.0.1-beta211\build\vc10
>nmake 2>&1 > out.log

and smoke until everything comes together.

The result will be located in
c:\temp\pantheios-1.0.1-beta211\bin\ --
c:\temp\pantheios-1.0.1-beta211\lib\ --
c:\temp\pantheios-1.0.1-beta211\include\ -- include


If you glance at the lib, you can see a large number of libraries collected in different modes of assembly.

But there are good people who have written a utility for selecting the modes of operation of the logger, which gives a good hint how to turn this soup collection of libraries and headers into a working logger.

Briefly about architecture


The library is actually divided into layer cheats:


The kernel is the connecting part of all other parts of the library.
The API for applications is just that set of functions that we can use directly for output to the log in our application.
Front-end is a subsystem responsible for filtering attempts to display messages by logging level.
Back-end is a subsystem that is responsible for outputting messages to a specific recipient (file, console, syslog, etc.), as well as for setting up this recipient (output format for example).

More information about the architecture can be found on the page: the project itself .

Testing work


Finish the environment variables as follows and run VS2010:
>set PANTHEIOS_ROOT=c:\temp\pantheios-1.0.1-beta211\
>set STLSOFT=c:\temp\stlsoft-1.9.108\
>devnev.exe


Create a standard C ++ Console Project and configure the project so that it can see our library through environment variables:




It's time to do the actual writing of the test.
As we remember, the task was previously set: output simultaneously to the log and to the colorful console in UTF-16.

For this purpose, you and I should use two Back-End files: cosnol and file. Both the back and front ends can be connected to the program using pre-prepared files. This greatly simplifies logging setup.

To further simplify the settings, a utility was written, where you can choose a combination of the necessary headers. You can take it here . Its external interface is quite simple:


Actually on the screenshot just the options we need are set.
Briefly explain:

The rest, I think, is intuitive.

Ok, now we can copy and correct what the utility wrote to us (yes, she was mistaken in choosing be.ltsplit, but probably the file where it was kept was renamed, which was not considered in the utility). We must also supplement the include-s directly specifying nuclear.

As a result, we obtain the following files:

 #include <pantheios/pantheios.hpp> #include <pantheios/inserters/args.hpp> #include <pantheios\backends\bec.file.h> #include <pantheios/implicit_link/core.h> #include <pantheios/implicit_link/util.h> #include <pantheios/implicit_link/fe.simple.h> #include <pantheios/implicit_link/be.lrsplit.h> #include <pantheios/implicit_link/bel.file.h> #include <pantheios/implicit_link/ber.WindowsConsole.h> 


Now it is necessary to take into account that fe.simple requires the definition of the application name (for indication in the log file).
To do this, let's variable:
 extern "C" const TCHAR PANTHEIOS_FE_PROCESS_IDENTITY[] = L"MyMegaLogger"; 


Well, actually now we implement our test:
 void testfoo(void*) { for(int i=0;i<10;++i) { try { pantheios::log_DEBUG(TEXT(",    UTF16?")); pantheios::log_DEBUG(TEXT("debug")); pantheios::log_INFORMATIONAL(TEXT("informational")); pantheios::log_NOTICE(TEXT("notice")); pantheios::log_WARNING(TEXT("warning")); pantheios::log_ERROR(TEXT("error")); pantheios::log_CRITICAL(TEXT("critical")); pantheios::log_ALERT(TEXT("alert")); pantheios::log_EMERGENCY(TEXT("EMERGENCY")); } catch(std::bad_alloc&) { pantheios::log_ALERT(TEXT("out of memory")); } catch(std::exception& x) { pantheios::log_CRITICAL(TEXT("Exception: "), x); } catch(...) { pantheios::logputs(pantheios::emergency, TEXT("Unexpected unknown error")); } } } int _tmain(int argc, _TCHAR* argv[]) { pantheios::log(pantheios::debug, TEXT("Entering main("), pantheios::args(argc, argv, pantheios::args::arg0FileOnly), TEXT(")")); pantheios_be_file_setFilePath(TEXT("local.log"), PANTHEIOS_BE_FILE_F_TRUNCATE, PANTHEIOS_BE_FILE_F_TRUNCATE, PANTHEIOS_BEID_LOCAL); for(int i=0;i<5;++i){ _beginthread(testfoo, 0, NULL); } std::cin.get(); return EXIT_SUCCESS; } 


and get the result:


and similar (but without color) in the local.log file.

Conclusion


We looked at a very basic part of the pantheios logger, but there are still a lot of interesting things about it.
From the pros, we can distinguish the speed of work, unicode support “out of the box”, cross-platform, type-safe-ing.
Of the minuses, I would single out a rather complicated procedure of using and not supporting configuration from a file. However, often, these disadvantages can not pay attention.

Overboard there is a comparison with other loggers, advanced configuration (output format), perhaps it will appear in another note.

Thanks to read to the end and have a great weekend!

Source: https://habr.com/ru/post/117973/


All Articles