Next: , Previous: , Up: Using the Dezyne locator to distribute runtime objects   [Contents]


3.7.1.3 Alternative logging methods

Trace logging by all Dezyne components is hardwired to use the std::ostream object in the dzn::locator. If you wish to use your own logging rules, for instance if you would like to log to a file instead of logging straight to std::clog, you can do so by overwriting the std::clog object in the dzn::locator.

To do this, you need to create an std::ostream object that uses a file as output buffer; say we want to log to “log.txt”. std::ostream can not directly write to files, it can only write to buffers; so, you need to create a buffer that writes to “log.txt”. This can be done using std::ofstream as follows:

std::ofstream logfile("log.txt");
std::ostream outstream(nullptr);
outstream.rdbuf(logfile.rdbuf());

With std::ofstream, a filestream to “log.txt” is created. The buffer for this filestream can be accessed by calling the rdbuf() member function of logfile. The buffer that the std::ostream writes to can be set using the same rdbuf() function, on the std::ostream object this time. In the end, you will have an std::ostream object that uses a filestream as its output buffer.

If you then use set() with the custom std::ostream object, your Dezyne application will send all of the trace logging to “log.txt” instead of displaying it in the console output. This could be useful for logging remote applications where you don’t have access to console output, for example.