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