robmcmullen-apple2/src/log.cpp

70 lines
1.2 KiB
C++
Raw Normal View History

2007-05-29 03:06:33 +00:00
//
// Log handler
//
// by James Hammons
2007-05-29 03:06:33 +00:00
// (C) 2006 Underground Software
//
// JLH = James Hammons <jlhamm@acm.org>
2007-05-29 03:06:33 +00:00
//
// WHO WHEN WHAT
// --- ---------- ------------------------------------------------------------
// JLH 01/03/2006 Moved includes out of header file for faster compilation
//
#include "log.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdint.h>
2007-05-29 03:06:33 +00:00
// Maximum size of log file (10 MB ought to be enough for anybody)
#define MAX_LOG_SIZE 10000000
2007-05-29 03:06:33 +00:00
static FILE * log_stream = NULL;
static uint32_t logSize = 0;
2007-05-29 03:06:33 +00:00
2008-05-07 06:14:40 +00:00
bool InitLog(const char * path)
2007-05-29 03:06:33 +00:00
{
log_stream = fopen(path, "wrt");
if (log_stream == NULL)
return false;
return true;
}
2007-05-29 03:06:33 +00:00
void LogDone(void)
{
if (log_stream)
fclose(log_stream);
}
2007-05-29 03:06:33 +00:00
//
// This logger is used mainly to ensure that text gets written to the log file
// even if the program crashes. The performance hit is acceptable in this case!
//
void WriteLog(const char * text, ...)
{
if (!log_stream)
2007-05-29 03:06:33 +00:00
return;
va_list arg;
va_start(arg, text);
logSize += vfprintf(log_stream, text, arg);
va_end(arg);
fflush(log_stream); // Make sure that text is written!
2007-05-29 03:06:33 +00:00
if (logSize > MAX_LOG_SIZE)
{
fclose(log_stream);
log_stream = NULL;
}
2007-05-29 03:06:33 +00:00
}