Boost logging - destinations and formatters - c++

I am having a problem with using Boost Logging library, that if I add a formatter or a destination to a logger, using my own Log class, I cannot change that destination or formatter.
Does anybody know how to change the destination or formatter on a boost log object?
The scenario I have is that I want a different destination (file name) for each request my server component handles, so I need to have flexible way to change them. Also the fact that I will be logging from different thread simultanuously, and each Log should really have it's own destination's, easily added - removed.
The fact that with the macro's the logging objects are really app global, does not really aid this.
Can anybody give me some guidance on how I can create a flexible way to add/remove destinations to a Logger from boost::logging?

Ok, here's what I would try. It might work for you. It looks as if the logging library is tailored for global loggers, while you are wanting to use thread-local loggers. I'd look up how to create a logger on demand (i.e. directly), for example by analysing BOOST_DECLARE_LOG. Then you can declare a std::map<int, Logger> that you use to map thread-id to specific logger. Probably you can create your own wrapper class that handles this transparently for client code. Then you just log using your own logging layer and create thread-specific loggers when needed. If you need to remove them after your request is finished you can add a method to do this.
Hope this helps!

Related

Eiffel: EWF_APP_EXECUTION (EWF application) exit function

As there is an initialize function, is there an exit/on_exit function such as I can close my database connection into it?
I would suggest to handle this at the request level to be fully portable among various EiffelWeb connectors.
Now, could you tell us which solution you are using ? EiffelWeb standalone connector, or rather libfcgi with apache for instance? or else?
For standalone, you can redefine the "launch" procedure, in order to perform cleanup task when you exit the application (which is also the server).
For libfcgi, the C API may provide such facility, but so far, the Eiffel libfcgi library does not wrap it. If needed this may be possible to implement it.
Called in each request which is probably not the best solution but I have chosen following way for the moment:
Redefine the clean procedure of WSF_FILTERED_ROUTED_EXECUTION inherited into the classical EWF_APP_EXECUTION to close the connection
Connect into the redefined initialize

Is it OK to log from a Flask Extension?

I was looking at source from the Flask Extension Registry. I probably looked at about 6 or 7 projects and couldn't find any of them logging output.
My question is: is it OK to do so? If so, should I use app.logger or my own logger?
Yes, you should log - no, you should not use the application logger since the user will not the be able to configure your extension's logging verbosity separately from their own.
Instead, create your own logger using Python's built-in logging module. Add a NullHandler (Python 3 has one, create your own NullHandler for Python2) so that by default your library will not log anything. Add a documentation section explaining how the user can access your logger to setup handlers (and explicitly configure levels, should they so desire).
As is often the case, there is some very good advice on this subject in Python's documentation:
Configuring Logging for a Library
When developing a library which uses logging, you should take care to document how the library uses logging - for example, the names of loggers used. Some consideration also needs to be given to its logging configuration. If the using application does not use logging, and library code makes logging calls, then (as described in the previous section) events of severity WARNING and greater will be printed to sys.stderr. This is regarded as the best default behaviour.
If for some reason you don’t want these messages printed in the absence of any logging configuration, you can attach a do-nothing handler to the top-level logger for your library. This avoids the message being printed, since a handler will be always be found for the library’s events: it just doesn’t produce any output. If the library user configures logging for application use, presumably that configuration will add some handlers, and if levels are suitably configured then logging calls made in library code will send output to those handlers, as normal.

Logging from within a C++ Windows DLL file

What is the best practice of logging DLL activity and internal parameter values.
I wrote a DLL that is used by a few applications I wrote. Lately, some users were complaining about problems that I suspect are related to this DLL but I cannot reproduce on my dev machine.
I'd like send them an equivalent DLL file that will log its activity, such as function calls, function return values and some internal parameters.
I don't think there should be a problem for a DLL to create a log file and write to it. Is there a common practice related to this issue. Is there a standard place to write the data to?
I am currently facing a similar issue of how to provide logging from a DLL. The way I went about it is to provide callback function setters and a callback type header file as part of the API. That way, the caller of the dll can create their own type of logging (boost, log4cpp, etc) and the dll will call the callback function in the exe that the user can do whatever they want with the data. In my case, I forward the log msgs to a boost logger that is instantiated in the exe. It's a little hacky looking but seems to work pretty well since the exe controls when and how to log everything and the user can format the data any way they choose.
You could provide an interface on your DLL which allows the user to provide a log file path/handle as the logging sink.
If you auto-generate the log file you'll probably want the file name to contain the process name and the process id (to disambiguate multiple simultaneous runs).
on unix machines the usual place is /var/log
on windows the standard way is to log to the event log (a separate subsystem of the OS).
I would suggest using the active user's TEMP directory if you want to log to file...
...or you could be clever and get the DLL to log to your own machines via TCP.

C++ Output: GUI or Console?

I am making an application in C++ that runs a simulation for a health club. The user simply enters the simulation data at the beginning (3 integer values) and presses run. After that there is no user input - so very simple.
After starting the simulation a lot of the logic is deep down in lower classes but a lot of them need to print simple output messages to the UI. Returning a message is not possible as objects need to print to the UI but keep working.
I was going to pass a reference to the UI object to all classes that need it but I end up passing it around quite a lot - there must be a better way.
What I really need is something that can make calling the UI's printOutput(string) function as easy (or not much more difficult) than cout << string;
The UI also has a displayConnectionStatus(bool[] connections) method.
Bear in mind the UI inherits an abstract 'UserInterface' class so simple console UIs and GUIs can be changed in and out easily.
How do you suggest I implement this link to the UI?
If I were to use a global function, how can I redirect it to call methods of the UserInterface implementation that I selected to use?
Don't be afraid of globals.
Global objects hurt encapsulation, but for a targeted solution with no concern for immediate reusability, globals are just fine.
Expose a global object that processes events from your simulation. You can then choose to print the events, send them by e-mail, render them with OpenGL or whatever you fancy. Make a uniform interface that catches what happens inside the simulation via report callbacks, and then you can subclass this global object to suit your needs.
If the object wasn't global, you'd be passing a pointer around all the codebase.
I would suggest to go for logging framework i.e. your own class LogMessages, which got functions which get data and log the data, it can be to a UI, file, over network or anything.
And each class which needs logging, can use your logging class.
This way you can avoid globals and a generic solution , also have a look at http://www.pantheios.org/ which is open source C/C++ Diagnostic Logging API library, you may use that also...

Logging for multi=threaded server component

I have a multi-threaded sever application that I'm writing in C++ and I need to implement a good and fairly efficient logging system. By efficient I mean that whatever amount of logging is configured, the application shouldn't ever come to a grinding halt. So preferably there is some thread that is dedicated to writing it's log files.
I want to log each request that the server component handles in it's own file, having a rotation system that removes files older then some threshold. A request is handled by 2 threads, one that does some conversion work and the a worker-thread that is part of thread pool (BOOST threadpool) that does all the other actions (database gets, calculations, etc). So the logging need be threadsafe and I have to be able to configure it for levels and let each Logger class instance (my own logger that implements some library) accept a new file name. So that each new Logger instance is created for a specific request.
My ultimate question is: Which logging library allows me to have a new Log file for each request and allows me to configure log levels? (IE: error, warning, critical, etc)
Or should I implement everything myself? (no logging is not an option)
I have looked at Boost::Logging v2 and since the main logger object, that holds all state (levels, files) is global, I cannot change the files for each request.
I have looked at templog.org and this I can't even get to compile. No matter what I include or which references I set, it can never find the templog namespace or any of its classes.
Have a look at Apache log4cxx. It a great logging library !