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