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