Main Page   Alphabetical List   Compound List   File List   Compound Members   Related Pages  

ADbg.cpp

Go to the documentation of this file.
00001 /************************************************************************
00002 BSD License post 1999 : 
00003 
00004 Copyright (c) 2001, Steve Lhomme
00005 All rights reserved.
00006 
00007 Redistribution and use in source and binary forms, with or without
00008 modification, are permitted provided that the following conditions are met: 
00009 
00010 - Redistributions of source code must retain the above copyright notice, this
00011 list of conditions and the following disclaimer.
00012 
00013 - Redistributions in binary form must reproduce the above copyright notice, 
00014 this list of conditions and the following disclaimer in the documentation
00015 and/or other materials provided with the distribution. 
00016 
00017 - The name of the author may not be used to endorse or promote products derived
00018 from this software without specific prior written permission. 
00019 
00020 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 
00021 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
00022 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 
00023 EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
00024 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
00025 OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
00026 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00027 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
00028 IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
00029 OF SUCH DAMAGE. 
00030 ************************************************************************/
00031 
00032 
00033 /*
00034 
00035 newer versions from http://mukoli.free.fr/
00036 
00037 $Id: ADbg.cpp,v 1.3 2001/08/11 09:31:57 robux4 Exp $ 
00038 
00039 
00040 $Log: ADbg.cpp,v $
00041 Revision 1.3  2001/08/11 09:31:57  robux4
00042 Updated for version 1.4.0
00043 
00044 Revision 1.2  2001/07/31 18:49:39  robux4
00045 Initial Doxygen doc support
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 // Construction/Destruction
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)

Generated at Tue Aug 21 21:10:48 2001 for out_lame by doxygen1.2.9.1 written by Dimitri van Heesch, © 1997-2001