GME  13
MgaEventLogger.cpp
Go to the documentation of this file.
00001 // MgaEventLogger.cpp: implementation of the CMgaEventLogger class.
00002 //
00004 
00005 #include "stdafx.h"
00006 #include "MgaEventLogger.h"
00007 
00009 // Construction/Destruction
00011 
00012 CMgaEventLogger::CMgaEventLogger():initialized(false),newLine(true)
00013 {
00014 }
00015 
00016 CMgaEventLogger::~CMgaEventLogger()
00017 {
00018 
00019 }
00020 
00021 STDMETHODIMP CMgaEventLogger::LogEvent(BSTR eventMsg)
00022 {
00023         if(initialized)
00024         {
00025                 CString s(eventMsg);
00026                 if(newLine)
00027                 {
00028                         CTime time = CTime::GetCurrentTime();
00029                         CString CurrentTime = time.Format(_T("%b %d, %H:%M:%S "));
00030                         _ftprintf(EventLog,CurrentTime);
00031                         newLine = false;
00032                 }
00033                 if(s.Find(_T("\r\n"))!=-1)
00034                 {
00035                         newLine = true;
00036                 }
00037                 _ftprintf(EventLog, _T("%s"), static_cast<const TCHAR*>(s));
00038                 fflush(EventLog);
00039         }
00040         return S_OK;
00041 }
00042 
00043 STDMETHODIMP CMgaEventLogger::StartLogging()
00044 {
00045         if(!initialized) //if already initialized, don't do anything
00046         {
00047                 CString path;
00048                 TCHAR gmepath[MAX_PATH];
00049                 if(SHGetSpecialFolderPath( NULL, gmepath, CSIDL_APPDATA, true)) //most likely C:\Documents and Settings<username>\Application Data
00050                 {
00051                         path = CString(gmepath) + _T("\\GME"); // add \GME to the path
00052                         _tmkdir(path.GetBuffer(4)); //in case GME dir not there, make it, if this function fails because GME already exists, don't care
00053                         CTime time = CTime::GetCurrentTime(); //to make unique logfile names
00054                         CString CurrentTime = time.Format( _T("\\GME_%b-%d_%H.%M.%S.log") );
00055                         EventLog = _tfopen(path+CurrentTime,_T("w"));
00056 
00057                         if (EventLog != NULL) //fopen succeded
00058                         {       
00059                                 initialized = true;
00060                                 newLine = true;
00061                                 CComBSTR b = L"CMgaEventLogger::StartLogging\r\n";
00062                                 LogEvent(b);
00063                         }
00064                 }
00065         }
00066         return S_OK;
00067 }
00068 
00069 STDMETHODIMP CMgaEventLogger::StopLogging()
00070 {
00071         if(initialized)
00072         {
00073                 CComBSTR b = L"CMgaEventLogger::StopLogging\r\n";
00074                 LogEvent(b);
00075                 fflush(EventLog);
00076                 fclose(EventLog);
00077         }
00078         initialized = false;
00079         EventLog = NULL;
00080         return S_OK;
00081 }
00082 
00083 STDMETHODIMP CMgaEventLogger::EmergencyEvent()
00084 {
00085         CComBSTR b = L"CMgaEventLogger::EmergencyEvent\r\n";
00086         LogEvent(b);
00087         StopLogging();
00088         return S_OK;
00089 }