| 1 |
/************************************************************************ |
| 2 |
Project : C++ debugging class |
| 3 |
File version : 0.4 |
| 4 |
|
| 5 |
BSD License post 1999 : |
| 6 |
|
| 7 |
Copyright (c) 2001, Steve Lhomme |
| 8 |
All rights reserved. |
| 9 |
|
| 10 |
Redistribution and use in source and binary forms, with or without |
| 11 |
modification, are permitted provided that the following conditions are met: |
| 12 |
|
| 13 |
- Redistributions of source code must retain the above copyright notice, this |
| 14 |
list of conditions and the following disclaimer. |
| 15 |
|
| 16 |
- Redistributions in binary form must reproduce the above copyright notice, |
| 17 |
this list of conditions and the following disclaimer in the documentation |
| 18 |
and/or other materials provided with the distribution. |
| 19 |
|
| 20 |
- The name of the author may not be used to endorse or promote products derived |
| 21 |
from this software without specific prior written permission. |
| 22 |
|
| 23 |
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 24 |
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 25 |
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 26 |
EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 27 |
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT |
| 28 |
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 29 |
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 30 |
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
| 31 |
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY |
| 32 |
OF SUCH DAMAGE. |
| 33 |
************************************************************************/ |
| 34 |
|
| 35 |
#include <stdio.h> |
| 36 |
#include <stdarg.h> |
| 37 |
#include <windows.h> |
| 38 |
|
| 39 |
#include "ADbg.h" |
| 40 |
|
| 41 |
#if !defined(NDEBUG) |
| 42 |
|
| 43 |
////////////////////////////////////////////////////////////////////// |
| 44 |
// Construction/Destruction |
| 45 |
////////////////////////////////////////////////////////////////////// |
| 46 |
|
| 47 |
ADbg::ADbg(int level) |
| 48 |
:my_level(level) |
| 49 |
,my_time_included(false) |
| 50 |
,my_use_file(false) |
| 51 |
,my_debug_output(true) |
| 52 |
,hFile(NULL) |
| 53 |
{ |
| 54 |
prefix[0] = '\0'; |
| 55 |
OutPut(-1,"ADbg Creation at debug level = %d (0x%08X)",my_level,this); |
| 56 |
} |
| 57 |
|
| 58 |
ADbg::~ADbg() |
| 59 |
{ |
| 60 |
unsetDebugFile(); |
| 61 |
OutPut(-1,"ADbg Deletion (0x%08X)",this); |
| 62 |
} |
| 63 |
|
| 64 |
inline int ADbg::_OutPut(const char * format,va_list params) const |
| 65 |
{ |
| 66 |
int result; |
| 67 |
|
| 68 |
char tst[1000]; |
| 69 |
char myformat[256]; |
| 70 |
|
| 71 |
if (my_time_included) { |
| 72 |
SYSTEMTIME time; |
| 73 |
GetSystemTime(&time); |
| 74 |
if (prefix[0] == '\0') |
| 75 |
wsprintf(myformat,"%04d/%02d/%02d %02d:%02d:%02d.%03d UTC : %s\r\n", |
| 76 |
time.wYear, |
| 77 |
time.wMonth, |
| 78 |
time.wDay, |
| 79 |
time.wHour, |
| 80 |
time.wMinute, |
| 81 |
time.wSecond, |
| 82 |
time.wMilliseconds, |
| 83 |
format); |
| 84 |
else |
| 85 |
wsprintf(myformat,"%04d/%02d/%02d %02d:%02d:%02d.%03d UTC : %s - %s\r\n", |
| 86 |
time.wYear, |
| 87 |
time.wMonth, |
| 88 |
time.wDay, |
| 89 |
time.wHour, |
| 90 |
time.wMinute, |
| 91 |
time.wSecond, |
| 92 |
time.wMilliseconds, |
| 93 |
prefix, |
| 94 |
format); |
| 95 |
} else { |
| 96 |
if (prefix[0] == '\0') |
| 97 |
wsprintf( myformat, "%s\r\n", format); |
| 98 |
else |
| 99 |
wsprintf( myformat, "%s - %s\r\n", prefix, format); |
| 100 |
} |
| 101 |
|
| 102 |
result = vsprintf(tst,myformat,params); |
| 103 |
|
| 104 |
if (my_debug_output) |
| 105 |
OutputDebugString(tst); |
| 106 |
|
| 107 |
if (my_use_file && (hFile != NULL)) { |
| 108 |
SetFilePointer( hFile, 0, 0, FILE_END ); |
| 109 |
DWORD written; |
| 110 |
WriteFile( hFile, tst, lstrlen(tst), &written, NULL ); |
| 111 |
} |
| 112 |
|
| 113 |
return result; |
| 114 |
} |
| 115 |
|
| 116 |
int ADbg::OutPut(int forLevel, const char * format,...) const |
| 117 |
{ |
| 118 |
int result=0; |
| 119 |
|
| 120 |
if (forLevel >= my_level) { |
| 121 |
va_list tstlist; |
| 122 |
int result; |
| 123 |
|
| 124 |
va_start(tstlist, format); |
| 125 |
|
| 126 |
result = _OutPut(format,tstlist); |
| 127 |
|
| 128 |
} |
| 129 |
|
| 130 |
return result; |
| 131 |
} |
| 132 |
|
| 133 |
int ADbg::OutPut(const char * format,...) const |
| 134 |
{ |
| 135 |
va_list tstlist; |
| 136 |
|
| 137 |
va_start(tstlist, format); |
| 138 |
|
| 139 |
return _OutPut(format,tstlist); |
| 140 |
} |
| 141 |
|
| 142 |
bool ADbg::setDebugFile(const char * NewFilename) { |
| 143 |
bool result; |
| 144 |
result = unsetDebugFile(); |
| 145 |
|
| 146 |
if (result) { |
| 147 |
result = false; |
| 148 |
|
| 149 |
hFile = CreateFile(NewFilename, GENERIC_WRITE, FILE_SHARE_WRITE|FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL ); |
| 150 |
|
| 151 |
if (hFile != INVALID_HANDLE_VALUE) { |
| 152 |
SetFilePointer( hFile, 0, 0, FILE_END ); |
| 153 |
|
| 154 |
result = true; |
| 155 |
|
| 156 |
OutPut(-1,"Debug file Opening succeeded"); |
| 157 |
|
| 158 |
} |
| 159 |
else |
| 160 |
OutPut(-1,"Debug file %s Opening failed",NewFilename); |
| 161 |
} |
| 162 |
|
| 163 |
return result; |
| 164 |
} |
| 165 |
|
| 166 |
bool ADbg::unsetDebugFile() { |
| 167 |
bool result = (hFile == NULL); |
| 168 |
|
| 169 |
if (hFile != NULL) { |
| 170 |
result = (CloseHandle(hFile) != 0); |
| 171 |
|
| 172 |
if (result) { |
| 173 |
OutPut(-1,"Debug file Closing succeeded"); |
| 174 |
hFile = NULL; |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
return result; |
| 179 |
} |
| 180 |
|
| 181 |
#endif // !defined(NDEBUG) |