Revert "Delete file.cpp"
[clinton/Virtual-Jaguar-Rx.git] / src / log.cpp
1 //
2 // Log handler
3 //
4 // Originally by David Raingeard (Cal2)
5 // GCC/SDL port by Niels Wagenaar (Linux/WIN32) and Caz (BeOS)
6 // Cleanups/new stuff by James Hammons
7 // (C) 2010 Underground Software
8 //
9 // JLH = James Hammons <jlhamm@acm.org>
10 //
11 // Who When What
12 // --- ---------- -------------------------------------------------------------
13 // JLH 01/16/2010 Created this log ;-)
14 // JLH 07/11/2011 Instead of dumping out on max log file size being reached, we
15 // now just silently ignore any more output. 10 megs ought to be
16 // enough for anybody. ;-) Except when it isn't. :-P
17 //
18
19 #include "log.h"
20
21 #include <stdlib.h>
22 #include <stdarg.h>
23 #include <stdint.h>
24
25
26 //#define MAX_LOG_SIZE 10000000 // Maximum size of log file (10 MB)
27 #define MAX_LOG_SIZE 100000000 // Maximum size of log file (100 MB)
28
29 static FILE * log_stream = NULL;
30 static uint32_t logSize = 0;
31
32 int LogInit(const char * path)
33 {
34 log_stream = fopen(path, "w");
35
36 if (log_stream == NULL)
37 return 0;
38
39 return 1;
40 }
41
42 FILE * LogGet(void)
43 {
44 return log_stream;
45 }
46
47 void LogDone(void)
48 {
49 if (log_stream != NULL)
50 fclose(log_stream);
51 }
52
53 //
54 // This logger is used mainly to ensure that text gets written to the log file
55 // even if the program crashes. The performance hit is acceptable in this case!
56 //
57 void WriteLog(const char * text, ...)
58 {
59 va_list arg;
60 va_start(arg, text);
61
62 if (log_stream == NULL)
63 {
64 va_end(arg);
65 return;
66 }
67
68 logSize += vfprintf(log_stream, text, arg);
69
70 if (logSize > MAX_LOG_SIZE)
71 {
72 // Instead of dumping out, we just close the file and ignore any more output.
73 fflush(log_stream);
74 fclose(log_stream);
75 log_stream = NULL;
76 }
77
78 va_end(arg);
79 fflush(log_stream); // Make sure that text is written!
80 }