"Fossies" - the Fresh Open Source Software Archive

Member "vfu-4.18/vslib/dlog.cpp" (23 Aug 2015, 2026 Bytes) of package /linux/privat/vfu-4.18.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "dlog.cpp" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 4.16_vs_4.18.

    1 /****************************************************************************
    2  *
    3  * (c) Vladi Belperchinov-Shabanski "Cade" 1996-2015
    4  * http://cade.datamax.bg/  <cade@biscom.net> <cade@bis.bg> <cade@datamax.bg>
    5  *
    6  * SEE `README',`LICENSE' OR `COPYING' FILE FOR LICENSE AND OTHER DETAILS!
    7  *
    8  ****************************************************************************/
    9 
   10 #include <time.h>
   11 #include <string.h>
   12 #include "dlog.h"
   13 
   14 TLogFile::TLogFile()
   15 {
   16   f = NULL;
   17   log_fn[0] = 0;
   18   keep_open = 0;
   19   on_stdout = 0;
   20   on_stderr = 0;
   21 }
   22 
   23 TLogFile::~TLogFile()
   24 {
   25   close();
   26 }
   27 
   28 void TLogFile::create( const char *fname, int pkeep_open )
   29 {
   30   strcpy( log_fn, fname );
   31   f = NULL;
   32   keep_open = pkeep_open;
   33   open();
   34   fprintf( f, "\n" );
   35   if (!keep_open) close();
   36 }
   37 
   38 void TLogFile::open()
   39 {
   40   if ( f ) fclose( f );
   41   f = fopen( log_fn, "at" );
   42 }
   43 
   44 void TLogFile::close()
   45 {
   46   if ( f ) fclose( f );
   47   f = NULL;
   48 }
   49 
   50 void TLogFile::log( const char *fname, int line, const char *msg )
   51 {
   52   char tmp[1024];
   53   if (!keep_open) open();
   54 
   55   time_t now;
   56   time(&now);
   57   char stime[32];
   58   strcpy(stime, asctime(localtime(&now)));
   59   if (stime[strlen(stime) - 1] == '\n') stime[strlen(stime) - 1] = 0;
   60 
   61 
   62   if ( fname == NULL || line == -1 )
   63     sprintf( tmp, "%s : %s", stime, msg);
   64   else
   65     sprintf( tmp, "%s [%10s:%-5d] %s", stime, fname, line, msg);
   66 
   67   while(tmp[strlen(tmp) - 1] == '\n') tmp[strlen(tmp) - 1] = 0;
   68   strcat( tmp, "\n" );
   69 
   70   fputs( tmp, f );
   71   if (on_stdout) fputs( tmp, stdout );
   72   if (on_stderr) fputs( tmp, stderr );
   73 
   74   if (!keep_open && f != NULL) close();
   75 
   76 }
   77 
   78 void TLogFile::log( const char *msg )
   79 {
   80   log( NULL, -1, msg );
   81 }
   82 
   83 void TLogFile::log( const char *msg, int n )
   84 {
   85   char tmp[255];
   86   sprintf( tmp, msg, n );
   87   log( NULL, -1, tmp );
   88 }
   89 
   90 void TLogFile::log( const char *msg, const char *arg )
   91 {
   92   char tmp[255];
   93   sprintf( tmp, msg, arg );
   94   log( NULL, -1, tmp );
   95 }
   96 
   97 void TLogFile::log( const char *fname, int line, const char *msg, int n )
   98 {
   99   char tmp[255];
  100   sprintf( tmp, msg, n );
  101   log( fname, line, tmp );
  102 }
  103 
  104