[ <- Previous Page ] [ ^ Table Of Contents ^ ] [ Next Page -> ]
zLog Class

Chapter 5.3
zLog Class

Like many programs operating in an Unix or GNU/Linux environment, zWebit uses log files for monitoring ongoing activity. The zWebit logs are configurable and roll. For example, a program could be set to have four log files, each 200K in size. When the first log file fills, it rolls to the second log file, then third, and finally fourth. When the fourth log file fills, it rolls back around to the first log file. zWebit appends a number 1 thru n to the logfile name.

All log entries are prefixed with a current time stamp. The default number of logfiles is two and can be changed with zLog::zlSetCount ( ). The default logfile size is 50,000 bytes, but it can be changed using zLog::zlSetLogSize ( ). There is an internal buffer used by the zLog::zlWriteFmt ( ) method that is 1024 bytes as default. If the program is using this class will be logging data longer than 1024 bytes with the zLog::zlWriteFmt ( ) method, the buffer size can be adjusted with zLog::zlGetBufSize ( ) and zLog::zlSetBufSize ( ). If the buffer is not big enough for the data in zLog::writeFmtLog ( ), it will result in a buffer overflow, which can crash the program.

zLog API

Method/FunctionDescriptionReturn Code
zLog (int Bufsize ) This is the constructor for the zLog class. Optional buffer size Bufsize parameter.
~zLog ( ) This is the class destructor.
int openLog (const zString & fn) Open log file with name of /fn. -1 Error
0 Success
void closeLog ( ) Close log file.
int writeFmtLog (const char * fmt, ...) Write data to log file using fmt, similar to standard C printf command. -1 Error
number of bytes logged
void setLogSize (long size) {LogSize = size} Set the size of the log file to size bytes.
void setCount (int count) Set the number of log files to count.
void flush ( ) Flush any buffered data to log file.
void operator << (const zString &data) Write data to the current open logfile.
int getBufSize ( ) Return the size of the work buffer used by zlWriteFmt ( ). BufSize
void setBufSize (int size) Update the size of the work buffer to size.
int getLogSize ( ) Return the size of the logfile for roll over purposes. Logfile rollover size
int getLogCount ( ) Return the count of logfiles for roll over purposes. Logfile rollover count
int writeLog (const zString& data) Write data to the current open logfile. -1 Error
number logged

Example zLog Program

/* zlog test program

   Copyright (c) 2000 HSC  GNU/GPL


#include "../../base/zstring.cpp"
#include "../../base/zlog.cpp"

using namespace std;

int main()
  int rc;
  zLog log;

/* set the log file size to 512 bytes for testing purposes 
   a more reasonable production type value would be 200000 for 200K 
  log.setLogSize( 512 );

  /* set the log count to 3 */
  log.setLogCount( 3 );

  rc = log.openLog( "mylogfile" );
  if( rc != 0 ){
    cout << "Error opening log file\n";
    return -1;

  log << "A log entry";

  log.writeLog( "Another way to do a log entry" );

  log.writeFmtLog( "Formatted log entry => %d %s", 
       1, "test of zlWriteFmt.." );
  log.writeFmtLog( "current log bufsize = %d bytes", log.getBufSize());
  log.setBufSize( 1024 );
  log.writeFmtLog( "New log bufsize = %d", log.getBufSize());

  log.writeFmtLog( "current log rollover size = %d", log.getLogSize());
  log.writeFmtLog( "current log rollover count = %d", log.getLogCount());
  for( int i = 0; i < 5; i++ )
    log.writeFmtLog( "Some looping log data i= %d", i );

  return 0;

[ <- Previous Page ] [ ^ Table Of Contents ^ ] [ ^ Top Of Page ^ ] [ Next Page -> ]

Visit the GNU home page.

FSF & GNU inquiries & questions to gnu@gnu.org.

Comments on these web pages to info@zhsac.com.

Copyright (C) 2003 HealthCare Systems and Consulting

Verbatim copying and distribution of this entire article is permitted in any medium, provided this notice is preserved.

Last updated: 07/21/2003