00001 
00002 
00004 
#include "stdafx.h"
00005 
#include "Logger.h"
00006 
00008 
00010 
00011 CLogger::CLogger()
00012 {
00013         m_pFile = NULL;
00014 }
00015 
00016 CLogger::CLogger(
char *pcFilename, 
bool bKeepOpen, 
bool bAppend)
00017 {
00018         m_pFile = NULL;
00019         
Open(pcFilename, bKeepOpen, bAppend);
00020 }
00021 
00022 CLogger::~CLogger()
00023 {
00024         
Close();
00025 }
00026 
00027 int CLogger::Open(
char* pcFilename, 
bool bKeepOpen, 
bool bAppend)
00028 {
00029         
if(pcFilename == NULL)
00030                 
return -1;
00031 
00032         m_bKeepOpen = bKeepOpen;
00033         
00034         
if(bAppend)
00035                 m_pFile = fopen(pcFilename, 
"a+t");
00036         
else
00037                 m_pFile = fopen(pcFilename, 
"w+t");
00038 
00039         
if(!bKeepOpen)  
00040         {
00041                 
Close();
00042 
00043                 
if(strlen(pcFilename) > 256)
00044                         strncpy(m_strFilename, pcFilename, 256);
00045                 
else 
00046                         strcpy(m_strFilename, pcFilename);
00047         }
00048 
00049         
return 1;
00050 }
00051 
00053 int CLogger::Write(
const char *pcFormat, ...)
00054 {
00055         
if(pcFormat == NULL)                                                                    
00056                 
return -1;                                                                              
00057 
00058         
char            text[256];                                                              
00059         va_list         ap;                                                                             
00060 
00061         va_start(ap, pcFormat);                                                         
00062             vsprintf(text, pcFormat, ap);                                               
00063         va_end(ap);
00064 
00065         
if(m_pFile == NULL)
00066         {
00067                 
if(m_strFilename == NULL)
00068                         
return -1;
00069 
00070                 m_pFile = fopen(m_strFilename, 
"w+t");
00071                 
if(m_pFile == NULL)
00072                         
return -1;
00073 
00074                 fprintf(m_pFile, 
"%s", text);
00075                 
00076                 
if(!m_bKeepOpen)
00077                         
Close();
00078 
00079                 
00080                 
return 1;
00081         }
00082         
else
00083         {
00084                 fprintf(m_pFile, 
"%s", text);
00085 
00086                 
if(!m_bKeepOpen)
00087                         
Close();
00088 
00089                 
return 1;
00090         }
00091 
00092         
00093         
return -1;
00094 }
00095 
00096 int CLogger::Append(
const char *fmt, ...)
00097 {
00098         
if(fmt == NULL)                                                                         
00099                 
return -1;                                                                              
00100 
00101         
char            text[256];                                                              
00102         va_list         ap;                                                                             
00103 
00104         va_start(ap, fmt);                                                                      
00105             vsprintf(text, fmt, ap);                                            
00106         va_end(ap);
00107 
00108         
if(m_pFile == NULL)
00109         {
00110                 
if(m_strFilename == NULL)
00111                         
return -1;
00112 
00113                 m_pFile = fopen(m_strFilename, 
"a+t");
00114                 
if(m_pFile == NULL)
00115                         
return -1;
00116 
00117                 fprintf(m_pFile, 
"%s", text);
00118                 
00119                 
if(!m_bKeepOpen)
00120                         
Close();
00121 
00122                 
00123                 
return 1;
00124         }
00125         
else
00126         {
00127                 fprintf(m_pFile, 
"%s", text);
00128 
00129                 
if(!m_bKeepOpen)
00130                         
Close();
00131 
00132                 
return 1;
00133         }
00134 
00135         
return -1;
00136 }
00137 
00138 int CLogger::Erase()
00139 {
00140         
if(m_pFile == NULL)
00141                 
return -1;
00142 
00143         fseek(m_pFile, 0, SEEK_SET);
00144         fprintf(m_pFile, 
"");
00145 
00146         
return 1;
00147 }
00148 
00149 
00150 int CLogger::Close()
00151 {
00152         
if(m_pFile == NULL)
00153                 
return -1;
00154 
00155         fclose(m_pFile);
00156         m_pFile = NULL;
00157 
00158         
return 1;
00159 }
00160 
00161 int CLogger::GetError()
00162 {
00163         
if(m_pFile == NULL)
00164                 
return -1;
00165 
00166         
return ferror(m_pFile);
00167 }