www.pudn.com > fbt-a3-20041206.zip > log.c, change:2004-03-06,size:3259b
// Copyright (c) 2004, Antony C. Roberts
// Use of this file is subject to the terms
// described in the LICENSE.TXT file that
// accompanies this file.
//
// Your use of this file indicates your
// acceptance of the terms described in
// LICENSE.TXT.
//
// http://www.freebt.net
#include <stdio.h>
#include <time.h>
#include <process.h>
#include <windows.h>
#include "fbtlog.h"
#include "fbtxcpt.h"
char _fbt_log_file[1024];
unsigned int _fbt_log_level;
void fbtLog(unsigned int nLevel, const char *szText, ...)
{
__try
{
FILE *fd=NULL;
va_list valist;
struct tm *newtime;
time_t long_time;
char *szFile=_fbt_log_file;
static BOOL bInitialised;
static CRITICAL_SECTION CriticalSection;
if (nLevel!=fbtLog_Exception && _fbt_log_level<nLevel)
return;
if (!bInitialised)
{
InitializeCriticalSection(&CriticalSection);
bInitialised=TRUE;
}
EnterCriticalSection(&CriticalSection);
fd=fopen(szFile, "a+");
if (fd!=NULL)
{
time(&long_time);
newtime=localtime(&long_time);
// Use a critical section to stop the various threads from trampling
// over each other. Note this makes the log file a major contention
// point and thus slows processing considerably
fprintf(fd, "%04d-%02d-%02d %02d.%02d:%02d (%d): ",
newtime->tm_year+1900, newtime->tm_mon+1, newtime->tm_mday,
newtime->tm_hour, newtime->tm_min, newtime->tm_sec, GetTickCount());
va_start(valist, szText);
vfprintf(fd, szText, valist);
va_end(valist);
fprintf(fd, "\n");
fflush(fd);
fclose(fd);
}
LeaveCriticalSection(&CriticalSection);
}
__except(EXCEPTION_EXECUTE_HANDLER)
{
// We can't do anything here!
return;
}
}
void fbtLogClear()
{
FBT_TRY
DeleteFile(_fbt_log_file);
FBT_CATCH_NORETURN
}
unsigned int fbtLogGetLevel()
{
FBT_TRY
return _fbt_log_level;
FBT_CATCH_RETURN(0)
}
void fbtLogSetLevel(unsigned int nLevel)
{
FBT_TRY
fbtLog(fbtLog_Enter, "Entered fbtLogSetLevel(%d)", nLevel);
_fbt_log_level=nLevel;
fbtLog(fbtLog_Exit, "Leaving fbtLogSetLevel");
FBT_CATCH_NORETURN
}
boolean fbtLogGetFile(char *szLogFile, unsigned int nSize)
{
FBT_TRY
if (nSize<strlen(_fbt_log_file))
{
szLogFile[0]='\0';
fbtLog(fbtLog_Exit, "Leaving fbtLogGetFile=FALSE (Buffer too small)");
return FALSE;
}
memcpy(szLogFile, _fbt_log_file, strlen(_fbt_log_file)+1);
return TRUE;
FBT_CATCH_RETURN(FALSE)
}
BOOL fbtLogSetFile(char *szDebugFile)
{
FBT_TRY
fbtLog(fbtLog_Enter, "Entered fbtLogSetFile(%s)", szDebugFile);
if (IsBadStringPtr(szDebugFile, 1024))
{
fbtLog(fbtLog_Exit, "Leaving fbtLogSetFile=FALSE (Log file too long)");
return FALSE;
}
memcpy(_fbt_log_file, szDebugFile, strlen(szDebugFile)+1);
fbtLog(fbtLog_Exit, "Leaving fbtLogSetFile=TRUE");
return TRUE;
FBT_CATCH_RETURN(FALSE)
}