| 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 <stdio.h> |
| 23 | #include <stdarg.h> |
| 24 | #include <stdint.h> |
| 25 | |
| 26 | |
| 27 | //#define MAX_LOG_SIZE 10000000 // Maximum size of log file (10 MB) |
| 28 | #define MAX_LOG_SIZE 100000000 // Maximum size of log file (100 MB) |
| 29 | |
| 30 | static FILE * log_stream = NULL; |
| 31 | static uint32_t logSize = 0; |
| 32 | |
| 33 | int LogInit(const char * path) |
| 34 | { |
| 35 | log_stream = fopen(path, "w"); |
| 36 | |
| 37 | if (log_stream == NULL) |
| 38 | return 0; |
| 39 | |
| 40 | return 1; |
| 41 | } |
| 42 | |
| 43 | FILE * LogGet(void) |
| 44 | { |
| 45 | return log_stream; |
| 46 | } |
| 47 | |
| 48 | void LogDone(void) |
| 49 | { |
| 50 | if (log_stream != NULL) |
| 51 | fclose(log_stream); |
| 52 | } |
| 53 | |
| 54 | // |
| 55 | // This logger is used mainly to ensure that text gets written to the log file |
| 56 | // even if the program crashes. The performance hit is acceptable in this case! |
| 57 | // |
| 58 | void WriteLog(const char * text, ...) |
| 59 | { |
| 60 | va_list arg; |
| 61 | va_start(arg, text); |
| 62 | |
| 63 | if (log_stream == NULL) |
| 64 | { |
| 65 | va_end(arg); |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | logSize += vfprintf(log_stream, text, arg); |
| 70 | |
| 71 | if (logSize > MAX_LOG_SIZE) |
| 72 | { |
| 73 | // Instead of dumping out, we just close the file and ignore any more output. |
| 74 | fflush(log_stream); |
| 75 | fclose(log_stream); |
| 76 | log_stream = NULL; |
| 77 | } |
| 78 | |
| 79 | va_end(arg); |
| 80 | fflush(log_stream); // Make sure that text is written! |
| 81 | } |