]> Shamusworld >> Repos - thunder/blob - src/log.cpp
adf8a18a32cb137752e436e5205fbe0533c2a5f0
[thunder] / src / log.cpp
1 //
2 // Log handler
3 //
4 // by James L. Hammons
5 // (c) 2004, 2009 Underground Software
6 //
7 // JLH = James L. Hammons <jlhamm@acm.org>
8 //
9 // WHO  WHEN        WHAT
10 // ---  ----------  ------------------------------------------------------------
11 // JLH  07/23/2009  Added changelog ;-)
12 //
13
14 #include "types.h"
15 #include "log.h"
16
17 #define MAX_LOG_SIZE            10000000                                // Maximum size of log file (10 MB)
18
19 static FILE * log_stream = NULL;
20 static uint32 logSize = 0;
21
22 bool InitLog(const char * path)
23 {
24         log_stream = fopen(path, "wrt");
25
26         if (log_stream == NULL)
27                 return false;
28
29         return true;
30 }
31
32 void LogDone(void)
33 {
34         if (log_stream)
35                 fclose(log_stream);
36 }
37
38 //
39 // This logger is used mainly to ensure that text gets written to the log file
40 // even if the program crashes. The performance hit is acceptable in this case!
41 //
42 void WriteLog(const char * text, ...)
43 {
44         if (!log_stream)
45                 return;
46
47         va_list arg;
48
49         va_start(arg, text);
50         logSize += vfprintf(log_stream, text, arg);
51
52         if (logSize > MAX_LOG_SIZE)
53         {
54                 fflush(log_stream);
55                 fclose(log_stream);
56                 exit(1);
57         }//*/
58
59         va_end(arg);
60         fflush(log_stream);                                     // Make sure that text is written!
61 }