Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages

LogFile.cpp

Go to the documentation of this file.
00001 /*
00002  * LogFile.cpp
00003  *
00004  * Part of Fly! Legacy project
00005  *
00006  * Copyright 2000-2004 Chris Wallace
00007  *
00008  * Fly! Legacy is free software; you can redistribute it and/or modify
00009  *   it under the terms of the GNU General Public License as published by
00010  *   the Free Software Foundation; either version 2 of the License, or
00011  *   (at your option) any later version.
00012  *
00013  * Fly! Legacy is distributed in the hope that it will be useful,
00014  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  *   GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  *   along with Fly! Legacy; if not, write to the Free Software
00020  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021  */
00022 
00027 #include <sys/types.h>
00028 #include <sys/timeb.h>
00029 #include <time.h>
00030 #include <string.h>
00031 #include "../Include/LogFile.h"
00032 
00033 
00034 //
00035 // Constructor 
00036 //
00037 CLogFile::CLogFile (const char* filename, const char* mode)
00038 {
00039   // Attempt to open log file
00040   f = fopen (filename, mode);
00041 }
00042 
00043 
00044 //
00045 // Destructor
00046 //
00047 CLogFile::~CLogFile (void)
00048 {
00049   // Close file handle
00050   if (f) {
00051     fclose (f);
00052     f = NULL;
00053   }
00054 }
00055 
00056 //
00057 // Return file reference
00058 //
00059 FILE *CLogFile::GetFile (void)
00060 {
00061   return f;
00062 }
00063 
00064 //
00065 // Write timestamped output to the log file
00066 //
00067 void CLogFile::Write (const char* fmt, ...)
00068 {
00069   if (f) {
00070     // Prefix format string with timestamp in fixed-length 26-byte format:
00071     //   <YYYY-MM-DD HH:MM:SS.MMMM  >
00072     int nChars = strlen(fmt) + 40;
00073     char *newfmt = new char[nChars];
00074 
00075     // Get local computer date/time
00076     struct timeb t;
00077     ftime (&t);
00078     struct tm* local = localtime (&t.time);
00079     if (local) {
00080       sprintf (newfmt, "%04d-%02d-%02d %02d:%02d:%02d.%03d  %s\n",
00081         1900 + local->tm_year, local->tm_mon+1, local->tm_mday,
00082         local->tm_hour, local->tm_min, local->tm_sec, t.millitm, fmt);
00083     } else {
00084       strcpy (newfmt, fmt);
00085     }
00086 
00087     va_list argp;
00088     va_start(argp, fmt);
00089     vfprintf(f, newfmt, argp);
00090     va_end(argp);
00091   
00092     delete newfmt;
00093   }
00094   fflush (f);
00095 }
00096 
SourceForge.net Logo Documentation generated by doxygen