Skip to content

Logging and Tracing

galkahana edited this page Aug 15, 2011 · 4 revisions

The library provides error information through a Log file. If logging is enabled, the library will write error information with a time signature to the file.The location of the log file, as well as whether logging actually takes place, can be controlled by the library user.
This can be done through the LogConfiguration parameter of the StartPDF method of the PDFWriter class.
For example:

PDFWriter pdfWriter;
pdfWriter.StartPDF("c:\\test.pdf",ePDFVersion13,LogConfiguration(true,"c:\\log.txt"));

Calling StartPDF this way will turn on logging (through the true value), and any log messages would be written in c:\log.txt.
The LogConfiguration structure has 4 fields:

  • ShouldLog – boolean value determining whether logging will take place or not.
  • LogFileLocation – string value determining the path of the log file. Note that as with all file paths for the library, it should be UTF8 encoded.
  • StartWithBom – for file input, should a UTF8 BOM be placed at the beginning (yeah, the file is written in UTF8 encoding).
  • LogStream – you can choose to use custom stream output, instead of file output. In this case set LogStream instead of LogFileLocation. Note that in this case the library still encodes output as UTF8, but does not place a BOM, regardless of the choice of StartWithBom

Important!
Note that all library implementation is thread safe, but the logging mechanism, which is a unprotected singleton. i’m afraid that till that time this will be mended, turning off logging (which is this way by default) is the right policy in a multi thread environment. It is also possible to implement your own custom streaming solution which supports multi-thread environment.

Implementation

The logging capabilities of the library are implemented through the Trace and Log classes.

The Log class writes a string to a file, with a preceding time stamp.
The Trace class holds a Log object as a member. It controls writing to a log and has two public methods for Tracing formatted strings.

The library uses a singleton of the Trace class to trace errors. the Trace header file defines macros. you can also use the Trace methods when using the library to log.
For example:

#include "trace.h"
// later...
TRACE_LOG1("My page index is %d",1);

The above code will write the string “My page index is 1” to the log.

Clone this wiki locally