]> Shamusworld >> Repos - apple2/blob - src/log.cpp
4a01f8001153f0fb3868382ec3483a2f921043c5
[apple2] / src / log.cpp
1 //
2 // Log handler
3 //
4 // by James L. Hammons
5 // (C) 2006 Underground Software
6 //
7 // JLH = James L. 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 static bool logDone = false;
27
28
29 bool InitLog(const char * path)
30 {
31         log_stream = fopen(path, "wrt");
32
33         if (log_stream == NULL)
34                 return false;
35
36         return true;
37 }
38
39
40 void LogDone(void)
41 {
42         if (log_stream)
43                 fclose(log_stream);
44 }
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         if (!log_stream || logDone)
54                 return;
55
56         va_list arg;
57
58         va_start(arg, text);
59         logSize += vfprintf(log_stream, text, arg);
60         va_end(arg);
61
62         fflush(log_stream);                                     // Make sure that text is written!
63
64         if (logSize > MAX_LOG_SIZE)
65         {
66                 fclose(log_stream);
67                 log_stream = NULL;
68                 logDone = true;
69         }
70 }
71