83 lines
1.3 KiB
C
83 lines
1.3 KiB
C
// ============================================================================
|
|
// log.c - Logging to file
|
|
// ============================================================================
|
|
|
|
#include <stdio.h>
|
|
#include <stdarg.h>
|
|
|
|
#include "log.h"
|
|
|
|
static FILE *gLogFile = NULL;
|
|
static LogPreIoFuncT gPreIoHook = NULL;
|
|
|
|
|
|
void logInit(const char *filename)
|
|
{
|
|
if (filename) {
|
|
gLogFile = fopen(filename, "w");
|
|
}
|
|
}
|
|
|
|
|
|
void logErr(const char *fmt, ...)
|
|
{
|
|
va_list ap;
|
|
va_start(ap, fmt);
|
|
logErrV(fmt, ap);
|
|
va_end(ap);
|
|
}
|
|
|
|
|
|
void logErrV(const char *fmt, va_list ap)
|
|
{
|
|
if (gLogFile) {
|
|
if (gPreIoHook) {
|
|
gPreIoHook();
|
|
}
|
|
vfprintf(gLogFile, fmt, ap);
|
|
fflush(gLogFile);
|
|
}
|
|
}
|
|
|
|
|
|
FILE *logGetFile(void)
|
|
{
|
|
return gLogFile;
|
|
}
|
|
|
|
|
|
void logMsg(const char *fmt, ...)
|
|
{
|
|
va_list ap;
|
|
va_start(ap, fmt);
|
|
logMsgV(fmt, ap);
|
|
va_end(ap);
|
|
}
|
|
|
|
|
|
void logMsgV(const char *fmt, va_list ap)
|
|
{
|
|
if (gLogFile) {
|
|
if (gPreIoHook) {
|
|
gPreIoHook();
|
|
}
|
|
vfprintf(gLogFile, fmt, ap);
|
|
fflush(gLogFile);
|
|
}
|
|
}
|
|
|
|
|
|
void logSetPreIoHook(LogPreIoFuncT func)
|
|
{
|
|
gPreIoHook = func;
|
|
}
|
|
|
|
|
|
void logShutdown(void)
|
|
{
|
|
if (gLogFile) {
|
|
fflush(gLogFile);
|
|
fclose(gLogFile);
|
|
gLogFile = NULL;
|
|
}
|
|
}
|