ADbg.cpp 4.4 KB

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