Blame ACM/ADbg/ADbg.cpp

Packit 47f805
/************************************************************************
Packit 47f805
Project               : C++ debugging class
Packit 47f805
File version          : 0.4
Packit 47f805
Packit 47f805
BSD License post 1999 : 
Packit 47f805
Packit 47f805
Copyright (c) 2001, Steve Lhomme
Packit 47f805
All rights reserved.
Packit 47f805
Packit 47f805
Redistribution and use in source and binary forms, with or without
Packit 47f805
modification, are permitted provided that the following conditions are met: 
Packit 47f805
Packit 47f805
- Redistributions of source code must retain the above copyright notice, this
Packit 47f805
list of conditions and the following disclaimer.
Packit 47f805
Packit 47f805
- Redistributions in binary form must reproduce the above copyright notice, 
Packit 47f805
this list of conditions and the following disclaimer in the documentation
Packit 47f805
and/or other materials provided with the distribution. 
Packit 47f805
Packit 47f805
- The name of the author may not be used to endorse or promote products derived
Packit 47f805
from this software without specific prior written permission. 
Packit 47f805
Packit 47f805
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 
Packit 47f805
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
Packit 47f805
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 
Packit 47f805
EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
Packit 47f805
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
Packit 47f805
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
Packit 47f805
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
Packit 47f805
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
Packit 47f805
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
Packit 47f805
OF SUCH DAMAGE. 
Packit 47f805
************************************************************************/
Packit 47f805
Packit 47f805
#include <stdio.h>
Packit 47f805
#include <stdarg.h>
Packit 47f805
#include <windows.h>
Packit 47f805
Packit 47f805
#include "ADbg.h"
Packit 47f805
Packit 47f805
#if !defined(NDEBUG)
Packit 47f805
Packit 47f805
//////////////////////////////////////////////////////////////////////
Packit 47f805
// Construction/Destruction
Packit 47f805
//////////////////////////////////////////////////////////////////////
Packit 47f805
Packit 47f805
ADbg::ADbg(int level)
Packit 47f805
:my_level(level)
Packit 47f805
,my_time_included(false)
Packit 47f805
,my_use_file(false)
Packit 47f805
,my_debug_output(true)
Packit 47f805
,hFile(NULL)
Packit 47f805
{
Packit 47f805
	prefix[0] = '\0';
Packit 47f805
	OutPut(-1,"ADbg Creation at debug level = %d (0x%08X)",my_level,this);
Packit 47f805
}
Packit 47f805
Packit 47f805
ADbg::~ADbg()
Packit 47f805
{
Packit 47f805
	unsetDebugFile();
Packit 47f805
	OutPut(-1,"ADbg Deletion (0x%08X)",this);
Packit 47f805
}
Packit 47f805
Packit 47f805
inline int ADbg::_OutPut(const char * format,va_list params) const
Packit 47f805
{
Packit 47f805
	int result;
Packit 47f805
Packit 47f805
	char tst[1000];
Packit 47f805
	char myformat[256];
Packit 47f805
Packit 47f805
	if (my_time_included) {
Packit 47f805
		SYSTEMTIME time;
Packit 47f805
		GetSystemTime(&time);
Packit 47f805
		if (prefix[0] == '\0')
Packit 47f805
			wsprintf(myformat,"%04d/%02d/%02d %02d:%02d:%02d.%03d UTC : %s\r\n",
Packit 47f805
							time.wYear,
Packit 47f805
							time.wMonth,
Packit 47f805
							time.wDay,
Packit 47f805
							time.wHour,
Packit 47f805
							time.wMinute,
Packit 47f805
							time.wSecond,
Packit 47f805
							time.wMilliseconds,
Packit 47f805
							format);
Packit 47f805
		else
Packit 47f805
			wsprintf(myformat,"%04d/%02d/%02d %02d:%02d:%02d.%03d UTC : %s - %s\r\n",
Packit 47f805
							time.wYear,
Packit 47f805
							time.wMonth,
Packit 47f805
							time.wDay,
Packit 47f805
							time.wHour,
Packit 47f805
							time.wMinute,
Packit 47f805
							time.wSecond,
Packit 47f805
							time.wMilliseconds,
Packit 47f805
							prefix,
Packit 47f805
							format);
Packit 47f805
	} else {
Packit 47f805
		if (prefix[0] == '\0')
Packit 47f805
			wsprintf( myformat, "%s\r\n", format);
Packit 47f805
		else
Packit 47f805
			wsprintf( myformat, "%s - %s\r\n", prefix, format);
Packit 47f805
	}
Packit 47f805
Packit 47f805
	result = vsprintf(tst,myformat,params);
Packit 47f805
	
Packit 47f805
	if (my_debug_output)
Packit 47f805
		OutputDebugString(tst);
Packit 47f805
Packit 47f805
	if (my_use_file && (hFile != NULL)) {
Packit 47f805
		SetFilePointer( hFile, 0, 0, FILE_END );
Packit 47f805
		DWORD written;
Packit 47f805
		WriteFile( hFile, tst, lstrlen(tst), &written, NULL );
Packit 47f805
	}
Packit 47f805
Packit 47f805
	return result;
Packit 47f805
}
Packit 47f805
Packit 47f805
int ADbg::OutPut(int forLevel, const char * format,...) const
Packit 47f805
{
Packit 47f805
	int result=0;
Packit 47f805
	
Packit 47f805
	if (forLevel >= my_level) {
Packit 47f805
		va_list tstlist;
Packit 47f805
		int result;
Packit 47f805
Packit 47f805
		va_start(tstlist, format);
Packit 47f805
Packit 47f805
		result = _OutPut(format,tstlist);
Packit 47f805
Packit 47f805
	}
Packit 47f805
Packit 47f805
	return result;
Packit 47f805
}
Packit 47f805
Packit 47f805
int ADbg::OutPut(const char * format,...) const
Packit 47f805
{
Packit 47f805
	va_list tstlist;
Packit 47f805
Packit 47f805
	va_start(tstlist, format);
Packit 47f805
Packit 47f805
	return _OutPut(format,tstlist);
Packit 47f805
}
Packit 47f805
Packit 47f805
bool ADbg::setDebugFile(const char * NewFilename) {
Packit 47f805
	bool result;
Packit 47f805
	result = unsetDebugFile();
Packit 47f805
Packit 47f805
	if (result) {
Packit 47f805
		result = false;
Packit 47f805
Packit 47f805
		hFile = CreateFile(NewFilename, GENERIC_WRITE, FILE_SHARE_WRITE|FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
Packit 47f805
		
Packit 47f805
		if (hFile != INVALID_HANDLE_VALUE) {
Packit 47f805
			SetFilePointer( hFile, 0, 0, FILE_END );
Packit 47f805
Packit 47f805
			result = true;
Packit 47f805
Packit 47f805
			OutPut(-1,"Debug file Opening succeeded");
Packit 47f805
Packit 47f805
		}
Packit 47f805
		else
Packit 47f805
			OutPut(-1,"Debug file %s Opening failed",NewFilename);
Packit 47f805
	}
Packit 47f805
Packit 47f805
	return result;
Packit 47f805
}
Packit 47f805
Packit 47f805
bool ADbg::unsetDebugFile() {
Packit 47f805
	bool result = (hFile == NULL);
Packit 47f805
	
Packit 47f805
	if (hFile != NULL) {
Packit 47f805
		result = (CloseHandle(hFile) != 0);
Packit 47f805
		
Packit 47f805
		if (result) {
Packit 47f805
			OutPut(-1,"Debug file Closing succeeded");
Packit 47f805
			hFile = NULL;
Packit 47f805
		}
Packit 47f805
	}
Packit 47f805
Packit 47f805
	return result;
Packit 47f805
}
Packit 47f805
Packit 47f805
#endif // !defined(NDEBUG)