logfile.cc


      //  $Id: logfile.cc,v 1.2 2002/03/28 19:50:00 vickery Exp $
      
      /*    Log file routines for the qserver module.
       *
       *    Functions
       *
       *      getTimestamp()  Returns string representing current time.
       *      openLog()       Open log file, write signon message.
       *      writeLog()      Append a message to log file.
       *      closeLog()      Close log file.
       *
       *    C. Vickery
       *    CS-701, Spring 2002
       *
       *    $Log: logfile.cc,v $
       *    Revision 1.2  2002/03/28 19:50:00  vickery
       *    Completed implementing all functions.
       *
       *    Revision 1.1  2002/03/23 04:56:33  vickery
       *    Initial revision
       *
       */
      
      #include "qserver.h"
      #include <time.h>
      
      static const char*  VERSION       = "$Revision: 1.2 $";
      static       char   timestamp[]   = "YYYY-MM-DD HH:MM:SS";
      static const char*  ts_format     = "%Y-%m-%d %H:%M:%S";
      static       FILE*  logfile       = stderr;
      
      //  updateTimestamp()
      //  ------------------------------------------------------------------
      /*
       *    Upates the static global timestamp string.  The format of the
       *    string is YYYY-MM-DD HH:MM:SS representing the current time of
       *    day.
       *
       */
      void
      updateTimestamp()
      {
        time_t  now = time( 0 );
        strftime( timestamp, sizeof(timestamp), 
                  ts_format, localtime( &now ) );
      }
      
      
      //  openLog()
      //  ------------------------------------------------------------------
      /*
       *    Initializes logfile, and writes an initial message to it.
       *    Returns -1 if logfile cannot be opened.
       */
      int
      openLog( const char *pathname, const char *msg, bool overwrite )
      {
        if ( overwrite )
          logfile = fopen( pathname, "w" );
        else
          logfile = fopen( pathname, "a" );
        if ( 0 == logfile )
        {
          perror( pathname );
          exit( 1 );
        }
        updateTimestamp();
        fprintf( logfile, "%sn", VERSION );
        fprintf( logfile, "%s  STARTUPn    %sn", timestamp, 
            ( 0 == msg ) ? "" : msg );
        fflush( logfile );
        return 0;
      }
      
      
      //  writeLog()
      //  ------------------------------------------------------------------
      /*
       *    Write a timestamped event name and message to the log file.
       *    Empty strings will be substituted for null pointers.
       *
       *    Returns -1 if logfile is not initialized.
       */
      int
      writeLog( const char *event_name, const char *msg )
      {
        if ( 0 == logfile )
          return -1;
        updateTimestamp();
        fprintf( logfile, "%s  %sn    %sn", timestamp,
            ( 0 == event_name ) ? "" : event_name,
            ( 0 == msg )        ? "" : msg );
        fflush( logfile );
        return 0;
      }
      
      
      //  closeLog()
      //  ------------------------------------------------------------------
      /*
       *    Writes a shutdown event and a message to logfile, and closes it.
       *    Omits the message if it is null.
       */
      void
      closeLog( const char * msg )
      {
        updateTimestamp();
        fprintf( logfile, "%s  SHUTDOWNn", timestamp );
        if ( msg )
          fprintf( logfile, "    %sn", msg );
        fflush( logfile );
        fclose( logfile );
      }