]> Shamusworld >> Repos - thunder/blob - src/log.cpp
Added save states; updated application icon.
[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 bool InitLog(const char * path)
23 {
24         logStream = fopen(path, "wrt");
25
26         if (logStream == NULL)
27                 return false;
28
29         return true;
30 }
31
32 void LogDone(void)
33 {
34         if (logStream)
35                 fclose(logStream);
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 (!logStream)
45                 return;
46
47         va_list arg;
48
49         va_start(arg, text);
50         logSize += vfprintf(logStream, text, arg);
51
52         if (logSize > MAX_LOG_SIZE)
53         {
54                 fflush(logStream);
55                 fclose(logStream);
56                 exit(1);
57         }
58
59         va_end(arg);
60         fflush(logStream);                                      // Make sure that text is written!
61 }