00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00053
00054 #include <stdio.h>
00055 #include <stdarg.h>
00056 #include <windows.h>
00057
00058 #include "ADbg.h"
00059
00060 #if !defined(NDEBUG)
00061
00063
00065
00066 ADbg::ADbg(int level)
00067 :my_level(level)
00068 ,my_time_included(false)
00069 ,my_use_file(false)
00070 ,my_debug_output(true)
00071 ,hFile(NULL)
00072 {
00073 prefix[0] = '\0';
00074 OutPut(-1,"ADbg Creation at debug level = %d (0x%08X)",my_level,this);
00075 }
00076
00077 ADbg::~ADbg()
00078 {
00079 unsetDebugFile();
00080 OutPut(-1,"ADbg Deletion (0x%08X)",this);
00081 }
00082
00083 inline int ADbg::_OutPut(const char * format,va_list params)
00084 {
00085 int result;
00086
00087 char tst[1000];
00088 char myformat[256];
00089
00090 if (my_time_included) {
00091 SYSTEMTIME time;
00092 GetSystemTime(&time);
00093 if (prefix[0] == '\0')
00094 wsprintf(myformat,"%04d/%02d/%02d %02d:%02d:%02d.%03d UTC : %s\r\n",
00095 time.wYear,
00096 time.wMonth,
00097 time.wDay,
00098 time.wHour,
00099 time.wMinute,
00100 time.wSecond,
00101 time.wMilliseconds,
00102 format);
00103 else
00104 wsprintf(myformat,"%04d/%02d/%02d %02d:%02d:%02d.%03d UTC : %s - %s\r\n",
00105 time.wYear,
00106 time.wMonth,
00107 time.wDay,
00108 time.wHour,
00109 time.wMinute,
00110 time.wSecond,
00111 time.wMilliseconds,
00112 prefix,
00113 format);
00114 } else {
00115 if (prefix[0] == '\0')
00116 wsprintf( myformat, "%s\r\n", format);
00117 else
00118 wsprintf( myformat, "%s - %s\r\n", prefix, format);
00119 }
00120
00121 result = vsprintf(tst,myformat,params);
00122
00123 if (my_debug_output)
00124 OutputDebugString(tst);
00125
00126 if (my_use_file && (hFile != NULL)) {
00127 SetFilePointer( hFile, 0, 0, FILE_END );
00128 DWORD written;
00129 WriteFile( hFile, tst, lstrlen(tst), &written, NULL );
00130 }
00131
00132 return result;
00133 }
00134
00135 int ADbg::OutPut(int forLevel, const char * format,...)
00136 {
00137 int result=0;
00138
00139 if (forLevel >= my_level) {
00140 va_list tstlist;
00141 int result;
00142
00143 va_start(tstlist, format);
00144
00145 result = _OutPut(format,tstlist);
00146
00147 }
00148
00149 return result;
00150 }
00151
00152 int ADbg::OutPut(const char * format,...)
00153 {
00154 va_list tstlist;
00155
00156 va_start(tstlist, format);
00157
00158 return _OutPut(format,tstlist);
00159 }
00160
00161 bool ADbg::setDebugFile(const char * NewFilename) {
00162 bool result;
00163 result = unsetDebugFile();
00164
00165 if (result) {
00166 result = false;
00167
00168 hFile = CreateFile(NewFilename, GENERIC_WRITE, FILE_SHARE_WRITE|FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
00169
00170 if (hFile != INVALID_HANDLE_VALUE) {
00171 SetFilePointer( hFile, 0, 0, FILE_END );
00172
00173 result = true;
00174
00175 OutPut(-1,"Debug file Opening succeeded");
00176
00177 }
00178 else
00179 OutPut(-1,"Debug file %s Opening failed",NewFilename);
00180 }
00181
00182 return result;
00183 }
00184
00185 bool ADbg::unsetDebugFile() {
00186 bool result = (hFile == NULL);
00187
00188 if (hFile != NULL) {
00189 result = (CloseHandle(hFile) != 0);
00190
00191 if (result) {
00192 OutPut(-1,"Debug file Closing succeeded");
00193 hFile = NULL;
00194 }
00195 }
00196
00197 return result;
00198 }
00199
00200 #endif // !defined(NDEBUG)