]> Shamusworld >> Repos - virtualjaguar/blob - src/log.cpp
5b2f76317f49b08b1d5433fe37a6a739dc143912
[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 "log.h"
10
11 #include <stdlib.h>
12 #include <stdarg.h>
13 #include "types.h"
14
15 #define MAX_LOG_SIZE            10000000                                // Maximum size of log file (10 MB)
16
17 static FILE * log_stream = NULL;
18 static uint32 logSize = 0;
19
20 int LogInit(const char * path)
21 {
22         log_stream = fopen(path, "wrt");
23
24         if (log_stream == NULL)
25                 return 0;
26
27         return 1;
28 }
29
30 FILE * LogGet(void)
31 {
32         return log_stream;
33 }
34
35 void LogDone(void)
36 {
37         fclose(log_stream);
38 }
39
40 //
41 // This logger is used mainly to ensure that text gets written to the log file
42 // even if the program crashes. The performance hit is acceptable in this case!
43 //
44 void WriteLog(const char * text, ...)
45 {
46         va_list arg;
47
48         va_start(arg, text);
49         logSize += vfprintf(log_stream, text, arg);
50
51         if (logSize > MAX_LOG_SIZE)
52         {
53                 fflush(log_stream);
54                 fclose(log_stream);
55                 exit(1);
56         }//*/
57
58         va_end(arg);
59         fflush(log_stream);                                     // Make sure that text is written!
60 }