home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR13 / TI_BC1.ZIP / TI1566.ZIP / TI1566.ASC
Text File  |  1993-10-06  |  6KB  |  265 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.   PRODUCT  :  Borland C++                           NUMBER  :  1566
  9.   VERSION  :  All
  10.        OS  :  All
  11.      DATE  :  October 6, 1993                          PAGE  :  1/4
  12.  
  13.     TITLE  :  Creating an error log stream
  14.  
  15.  
  16.  
  17.  
  18.   //----------------------------------------------------------
  19.   //
  20.   //    errlog.cpp
  21.   //
  22.   //  This file demonstrates redirecting clog to a text file
  23.   // for logging  error messages.
  24.   //
  25.   //----------------------------------------------------------
  26.  
  27.   #include <io.h>         //  open, close
  28.   #include <fcntl.h>      //  O_CREAT, O_RDWR
  29.   #include <sys\stat.h>   //  S_IREAD, S_IWRITE
  30.   #include <stdio.h>      //  sprintf
  31.   #include <iostream.h>   //  cout, clog, cerr
  32.   #include <fstream.h>    //  filebuf, ofstream
  33.   #include <string.h>     //  strcpy
  34.  
  35.   //----------------------------------------------------------
  36.   //
  37.   //  User-defined types
  38.   //
  39.   //----------------------------------------------------------
  40.  
  41.   //  none
  42.  
  43.   //----------------------------------------------------------
  44.   //
  45.   //  Function prototypes
  46.   //
  47.   //----------------------------------------------------------
  48.  
  49.   void CLOG( char * file, unsigned line, char * msg );
  50.   void CERR( char * file, unsigned line, char * msg );
  51.  
  52.   //----------------------------------------------------------
  53.   //
  54.   //  Macros to simplify logging and error reporting while
  55.   // retaining  proper filename and line number information.
  56.   // There should be no  need for the programmer to call CLOG
  57.   // and CERR directly.
  58.   //----------------------------------------------------------
  59.  
  60.   #define LOG(p) CLOG( __FILE__, __LINE__, p );
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.   PRODUCT  :  Borland C++                           NUMBER  :  1566
  75.   VERSION  :  All
  76.        OS  :  All
  77.      DATE  :  October 6, 1993                          PAGE  :  2/4
  78.  
  79.     TITLE  :  Creating an error log stream
  80.  
  81.  
  82.  
  83.  
  84.   #define ERR(p) CERR( __FILE__, __LINE__, p );
  85.  
  86.   //----------------------------------------------------------
  87.   //
  88.   //  Globals and constants
  89.   //
  90.   //----------------------------------------------------------
  91.  
  92.   const char logFileName[] = "logfile.dat";
  93.  
  94.   //-----------------------------------------------------------
  95.   //
  96.   //  The example program redirects clog to an output file, and
  97.   // logs all  events. If a command-line parameter specifies a
  98.   // file, then cerr is  redirected to that fle.
  99.   //
  100.   //-----------------------------------------------------------
  101.  
  102.   int main( int argc, char ** argv )
  103.   {
  104.       char msgBfr[100];
  105.       int logFile=0, errFile=0;
  106.       filebuf * oldCerrBuf, * oldClogBuf;
  107.       filebuf * newCerrBuf, * newClogBuf;
  108.  
  109.       //  Redirect clog to a file.
  110.  
  111.       oldClogBuf = (filebuf*) clog.rdbuf();
  112.       logFile = open( logFileName, O_CREAT | O_RDWR,
  113.                     S_IREAD | S_IWRITE );
  114.       newClogBuf = new filebuf( logFile );
  115.       clog.flush();
  116.       clog = newClogBuf;
  117.  
  118.       //  Say hello.
  119.  
  120.       sprintf( msgBfr, "Testing %s {%s %s}", argv[0], __DATE__,
  121.              __TIME__ );
  122.       cout << msgBfr << endl;
  123.       LOG( msgBfr );
  124.  
  125.       //  If a filename is passed, we will redirect cerr to
  126.       // that file.  Note that since we initialized errFile to
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.   PRODUCT  :  Borland C++                           NUMBER  :  1566
  141.   VERSION  :  All
  142.        OS  :  All
  143.      DATE  :  October 6, 1993                          PAGE  :  3/4
  144.  
  145.     TITLE  :  Creating an error log stream
  146.  
  147.  
  148.  
  149.  
  150.       // 0, we will be able to  count on it's value to indicate
  151.       // what has happened:    -1  failure to open file
  152.       //     0  not redirected
  153.       //    >0  error file in use
  154.  
  155.       if( argc > 1 )
  156.       {
  157.           oldCerrBuf = (filebuf*) cerr.rdbuf();
  158.           errFile = open( argv[1], O_CREAT | O_RDWR,
  159.                         S_IREAD | S_IWRITE );
  160.           newCerrBuf = new filebuf( errFile );
  161.           cerr.flush();
  162.           cerr = newCerrBuf;
  163.           sprintf( msgBfr, "cerr redirected to %s", argv[1] );
  164.           LOG( msgBfr );
  165.       }
  166.  
  167.       //  Pretend something happened.
  168.  
  169.       strcpy( msgBfr, "Oh, my. There's an error!" );
  170.       cout << "\ncout: " << msgBfr << endl;
  171.       LOG( msgBfr );
  172.       ERR( msgBfr );
  173.  
  174.       //  Clean up cerr if necessary.
  175.  
  176.       if( errFile > 0 )
  177.       {
  178.           cerr.flush();
  179.           cerr = oldCerrBuf;
  180.           delete newCerrBuf;
  181.           close( errFile );
  182.           LOG( "cerr restored to default" );
  183.       }
  184.  
  185.       //  Demonstrate things are back to normal.
  186.  
  187.       strcpy( msgBfr, "Program termination." );
  188.       cout << "\ncout: " << msgBfr << endl;
  189.       LOG( msgBfr );
  190.       ERR( msgBfr );
  191.  
  192.       //  Reset clog on way out of program.
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.   PRODUCT  :  Borland C++                           NUMBER  :  1566
  207.   VERSION  :  All
  208.        OS  :  All
  209.      DATE  :  October 6, 1993                          PAGE  :  4/4
  210.  
  211.     TITLE  :  Creating an error log stream
  212.  
  213.  
  214.  
  215.  
  216.       clog.flush();
  217.       clog = oldClogBuf;
  218.       delete newClogBuf;
  219.       close( logFile );
  220.  
  221.       return argc;
  222.   }
  223.  
  224.   //-----------------------------------------------------------
  225.   //
  226.   //  Log a message with file and line number info.
  227.   //
  228.   //-----------------------------------------------------------
  229.  
  230.   void CLOG( char * file, unsigned line, char * msg )
  231.   {
  232.       clog << "Log entry: " << file << ",
  233.       " << line << ": " << msg << endl;
  234.   }
  235.  
  236.  
  237.   //-----------------------------------------------------------
  238.   //
  239.   //  Report an error with file and line info. The default for
  240.   // cerr is the  standard output stream.
  241.   //
  242.   //-----------------------------------------------------------
  243.  
  244.   void CERR( char * file, unsigned line, char * msg )
  245.   {
  246.       cerr << "~Error: " << file << ",
  247.       " << line << ": " << msg << endl;
  248.   }
  249.  
  250.  
  251.   DISCLAIMER: You have the right to use this technical information
  252.   subject to the terms of the No-Nonsense License Statement that
  253.   you received with the Borland product to which this information
  254.   pertains.
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.