Rogue Wave banner
Previous fileTop of DocumentContentsIndex pageNext file
Objective Toolkit User's Guide
Rogue Wave web site:  Home Page  |  Main Documentation Page

16.4 Safe Multi-Threaded Trace Output

Objective Toolkit now includes support for safe, synchronized logging of trace-type output from within multi-threaded code. Using objects derived and instantiated from the base template class MultiTrace, developers of multithreaded applications can implement synchronized output from multiple threads to a common text file. Features of this class include:

16.4.1 Use of the Multi-Threaded Logging Class

Using the logging class is straightforward.

  1. Include these two files:

  2. Declare an instance of the MultiTrace class:

      multithreadedlogger mt;
      

    This should have file scope. Note that this declaration uses one of the typedefs from MultiTrace.h, which is synonymous with:

      MultiTrace<MultiThreadLogTraits> mt;
      


    As mentioned above, a non-synchronizing, single-threaded version is provided also, typedefed as singlethreadedlogger.

  3. Set the desired properties of the logger, and call initialize():

      mt.fname=_T("c:\logfile.txt");
      mt.initialize();
      
  4. In your thread procedure where you desire to log output, invoke an output method:

      mt.LogWrite(_T("At this point, the value of x is %i"),x);
      

    By default, this output will be bracketed in the output file by HTML-like tags, which incorporate the thread ID and the time (to the millisecond):

      <25c 15:24:10.0024> At this point, the value of x is 4 </25C>
      

The MultiTrace object will perform the file I/O operations in a critical section so that I/O from multiple threads in your application will be properly sequenced in the output stream.

Binary data can be logged as well (converted to text via base64 conversion) through method LogBinary(). Arguments for this method are a pointer to the item and its length. For example, assume oStructure is a data structure in memory. The following code will convert the contents of the structure via base64 conversion and output them to the log file:

This will produce output resembling the following:

When your MultiTrace object falls out of scope, its destructor will be called, which in turn will close the output file and free up any buffers allocated for the purpose of base64 conversion of binary data.

See the MultiThreadLog sample console application (sample\toolkit\MFC\utility) included with the product for an example of the setup and use of the multithreaded logging class.


This class has no dependencies on MFC.



Previous fileTop of DocumentContentsNo linkNext file

Copyright © Rogue Wave Software, Inc. All Rights Reserved.

The Rogue Wave name and logo, and Stingray, are registered trademarks of Rogue Wave Software. All other trademarks are the property of their respective owners.
Provide feedback to Rogue Wave about its documentation.