]> Shamusworld >> Repos - virtualjaguar/blob - src/log.cpp
Header shuffling
[virtualjaguar] / src / log.cpp
1 //
2 // Log handler
3 //
4 // Originally by David Raingeard (Cal2)
5 // GCC/SDL port by Niels Wagenaar (Linux/WIN32) and Caz (BeOS)
6 // Cleanups/new stuff by James L. Hammons
7 //
8
9 #include <stdlib.h>
10 #include <stdarg.h>
11 #include "types.h"
12 #include "log.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 logSize = 0;
18
19 int log_init(char * path)
20 {
21         log_stream = fopen(path, "wrt");
22
23         if (log_stream == NULL)
24                 return 0;
25
26         return 1;
27 }
28
29 FILE * log_get(void)
30 {
31         return log_stream;
32 }
33
34 void log_done(void)
35 {
36         fclose(log_stream);
37 }
38
39 //
40 // This logger is used mainly to ensure that text gets written to the log file
41 // even if the program crashes. The performance hit is acceptable in this case!
42 //
43 void WriteLog(const char * text, ...)
44 {
45         va_list arg;
46
47         va_start(arg, text);
48         logSize += vfprintf(log_stream, text, arg);
49
50         if (logSize > MAX_LOG_SIZE)
51         {
52                 fflush(log_stream);
53                 fclose(log_stream);
54                 exit(1);
55         }//*/
56
57         va_end(arg);
58         fflush(log_stream);                                     // Make sure that text is written!
59 }