]> Shamusworld >> Repos - virtualjaguar/blob - src/log.cpp
2782c930160ad1e8b3a042981ff90d8d8bf0d7b0
[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 Hammons
7 // (C) 2010 Underground Software
8 //
9 // JLH = James Hammons <jlhamm@acm.org>
10 //
11 // Who  When        What
12 // ---  ----------  -------------------------------------------------------------
13 // JLH  01/16/2010  Created this log ;-)
14 // JLH  07/11/2011  Instead of dumping out on max log file size being reached, we
15 //                  now just silently ignore any more output. 10 megs ought to be
16 //                  enough for anybody. ;-) Except when it isn't. :-P
17 //
18
19 #include "log.h"
20
21 #include <stdlib.h>
22 #include <stdarg.h>
23 #include "types.h"
24
25 #define MAX_LOG_SIZE            10000000                                // Maximum size of log file (10 MB)
26
27 static FILE * log_stream = NULL;
28 static uint32 logSize = 0;
29
30 int LogInit(const char * path)
31 {
32         log_stream = fopen(path, "w");
33
34         if (log_stream == NULL)
35                 return 0;
36
37         return 1;
38 }
39
40 FILE * LogGet(void)
41 {
42         return log_stream;
43 }
44
45 void LogDone(void)
46 {
47         if (log_stream != NULL)
48                 fclose(log_stream);
49 }
50
51 //
52 // This logger is used mainly to ensure that text gets written to the log file
53 // even if the program crashes. The performance hit is acceptable in this case!
54 //
55 void WriteLog(const char * text, ...)
56 {
57         va_list arg;
58         va_start(arg, text);
59
60         if (log_stream == NULL)
61         {
62                 va_end(arg);
63                 return;
64         }
65
66         logSize += vfprintf(log_stream, text, arg);
67
68         if (logSize > MAX_LOG_SIZE)
69         {
70                 // Instead of dumping out, we just close the file and ignore any more output.
71                 fflush(log_stream);
72                 fclose(log_stream);
73                 log_stream = NULL;
74         }
75
76         va_end(arg);
77         fflush(log_stream);                                     // Make sure that text is written!
78 }