Descent3/misc/logfile.cpp

74 lines
1.2 KiB
C++
Raw Normal View History

2024-04-16 03:43:29 +00:00
#include "logfile.h"
#include "pstring.h"
#include <stdio.h>
#include <stdarg.h>
#ifdef _DEBUG
static bool log_enable = true;
#else
static bool log_enable = false;
#endif
2024-04-16 18:56:40 +00:00
void log_Enable(bool enable) { log_enable = true; }
2024-04-16 03:43:29 +00:00
2024-04-16 18:56:40 +00:00
void log_Disable() { log_enable = false; }
2024-04-16 03:43:29 +00:00
2024-04-16 18:56:40 +00:00
logfile::logfile() { fp = NULL; }
2024-04-16 03:43:29 +00:00
2024-04-16 18:56:40 +00:00
logfile::~logfile() { end(); }
2024-04-16 03:43:29 +00:00
// restarts the logfile (opens a new one.)
2024-04-16 18:56:40 +00:00
void logfile::start(const char *fname, const char *longname) {
if (log_enable) {
try {
fp = (FILE *)fopen(fname, "wt");
logfile::printf("%s\n", longname);
} catch (...) {
fp = NULL;
}
}
2024-04-16 03:43:29 +00:00
}
2024-04-16 18:56:40 +00:00
void logfile::end() {
if (fp) {
try {
fclose((FILE *)fp);
fp = NULL;
} catch (...) {
fp = NULL;
}
}
2024-04-16 03:43:29 +00:00
}
2024-04-16 18:56:40 +00:00
void logfile::printf(const char *fmt, ...) {
if (fp && fmt) {
char msg[256];
va_list arglist;
va_start(arglist, fmt);
Pvsprintf(msg, sizeof(msg), fmt, arglist);
va_end(arglist);
2024-04-16 03:43:29 +00:00
2024-04-16 18:56:40 +00:00
logfile::puts(msg);
}
2024-04-16 03:43:29 +00:00
}
2024-04-16 18:56:40 +00:00
void logfile::puts(const char *msg) {
if (fp && msg) {
try {
fputs(msg, (FILE *)fp);
} catch (...) {
end();
}
}
2024-04-16 03:43:29 +00:00
}
2024-04-16 18:56:40 +00:00
void logfile::update() {
if (fp) {
try {
fflush((FILE *)fp);
} catch (...) {
end();
}
}
2024-04-16 03:43:29 +00:00
}