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