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