]> Shamusworld >> Repos - legend/blob - src/log.cpp
Initial commit for the Legend of A... project!
[legend] / src / log.cpp
1 //
2 // Log handler
3 //
4 // by James Hammons
5 // (C) 2006 Underground Software
6 //
7 // JLH = James Hammons <jlhamm@acm.org>
8 //
9 // WHO  WHEN        WHAT
10 // ---  ----------  ------------------------------------------------------------
11 // JLH  01/03/2006  Moved includes out of header file for faster compilation
12 //
13
14 #include "log.h"
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <stdarg.h>
19 #include <stdint.h>
20
21 // Maximum size of log file (10 MB ought to be enough for anybody)
22 #define MAX_LOG_SIZE            10000000
23
24 static FILE * log_stream = NULL;
25 static uint32_t logSize = 0;
26
27
28 bool InitLog(const char * path)
29 {
30         log_stream = fopen(path, "wrt");
31
32         if (log_stream == NULL)
33                 return false;
34
35         return true;
36 }
37
38
39 void LogDone(void)
40 {
41         if (log_stream)
42                 fclose(log_stream);
43 }
44
45
46 //
47 // This logger is used mainly to ensure that text gets written to the log file
48 // even if the program crashes. The performance hit is acceptable in this case!
49 //
50 void WriteLog(const char * text, ...)
51 {
52         if (!log_stream)
53                 return;
54
55         va_list arg;
56
57         va_start(arg, text);
58         logSize += vfprintf(log_stream, text, arg);
59         va_end(arg);
60
61         fflush(log_stream);                                     // Make sure that text is written!
62
63         if (logSize > MAX_LOG_SIZE)
64         {
65                 fclose(log_stream);
66                 log_stream = NULL;
67         }
68 }
69