| 1 | /* |
| 2 | =========================================================================== |
| 3 | Copyright (C) 1999-2005 Id Software, Inc. |
| 4 | |
| 5 | This file is part of Quake III Arena source code. |
| 6 | |
| 7 | Quake III Arena source code is free software; you can redistribute it |
| 8 | and/or modify it under the terms of the GNU General Public License as |
| 9 | published by the Free Software Foundation; either version 2 of the License, |
| 10 | or (at your option) any later version. |
| 11 | |
| 12 | Quake III Arena source code is distributed in the hope that it will be |
| 13 | useful, but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with Quake III Arena source code; if not, write to the Free Software |
| 19 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | =========================================================================== |
| 21 | */ |
| 22 | |
| 23 | /***************************************************************************** |
| 24 | * name: l_log.c |
| 25 | * |
| 26 | * desc: log file |
| 27 | * |
| 28 | * $Archive: /MissionPack/CODE/botlib/l_log.c $ |
| 29 | * |
| 30 | *****************************************************************************/ |
| 31 | |
| 32 | #include <stdlib.h> |
| 33 | #include <stdio.h> |
| 34 | #include <string.h> |
| 35 | |
| 36 | #include "../qcommon/q_shared.h" |
| 37 | #include "../qcommon/qcommon.h" |
| 38 | #include "botlib.h" |
| 39 | #include "be_interface.h" //for botimport.Print |
| 40 | #include "l_libvar.h" |
| 41 | #include "l_log.h" |
| 42 | |
| 43 | #define MAX_LOGFILENAMESIZE 1024 |
| 44 | |
| 45 | typedef struct logfile_s |
| 46 | { |
| 47 | char filename[MAX_LOGFILENAMESIZE]; |
| 48 | FILE *fp; |
| 49 | int numwrites; |
| 50 | } logfile_t; |
| 51 | |
| 52 | static logfile_t logfile; |
| 53 | |
| 54 | //=========================================================================== |
| 55 | // |
| 56 | // Parameter: - |
| 57 | // Returns: - |
| 58 | // Changes Globals: - |
| 59 | //=========================================================================== |
| 60 | void Log_Open(char *filename) |
| 61 | { |
| 62 | char *ospath; |
| 63 | if (!LibVarValue("log", "0")) return; |
| 64 | if (!filename || !strlen(filename)) |
| 65 | { |
| 66 | botimport.Print(PRT_MESSAGE, "openlog <filename>\n"); |
| 67 | return; |
| 68 | } //end if |
| 69 | if (logfile.fp) |
| 70 | { |
| 71 | botimport.Print(PRT_ERROR, "log file %s is already opened\n", logfile.filename); |
| 72 | return; |
| 73 | } //end if |
| 74 | ospath = FS_BuildOSPath(Cvar_VariableString("fs_homestatepath"), Cvar_VariableString("fs_game"), filename); |
| 75 | logfile.fp = fopen(ospath, "wb"); |
| 76 | if (!logfile.fp) |
| 77 | { |
| 78 | botimport.Print(PRT_ERROR, "can't open the log file %s\n", filename); |
| 79 | return; |
| 80 | } //end if |
| 81 | Q_strncpyz(logfile.filename, filename, MAX_LOGFILENAMESIZE); |
| 82 | botimport.Print(PRT_MESSAGE, "Opened log %s\n", logfile.filename); |
| 83 | } //end of the function Log_Create |
| 84 | //=========================================================================== |
| 85 | // |
| 86 | // Parameter: - |
| 87 | // Returns: - |
| 88 | // Changes Globals: - |
| 89 | //=========================================================================== |
| 90 | void Log_Close(void) |
| 91 | { |
| 92 | if (!logfile.fp) return; |
| 93 | if (fclose(logfile.fp)) |
| 94 | { |
| 95 | botimport.Print(PRT_ERROR, "can't close log file %s\n", logfile.filename); |
| 96 | return; |
| 97 | } //end if |
| 98 | logfile.fp = NULL; |
| 99 | botimport.Print(PRT_MESSAGE, "Closed log %s\n", logfile.filename); |
| 100 | } //end of the function Log_Close |
| 101 | //=========================================================================== |
| 102 | // |
| 103 | // Parameter: - |
| 104 | // Returns: - |
| 105 | // Changes Globals: - |
| 106 | //=========================================================================== |
| 107 | void Log_Shutdown(void) |
| 108 | { |
| 109 | if (logfile.fp) Log_Close(); |
| 110 | } //end of the function Log_Shutdown |
| 111 | //=========================================================================== |
| 112 | // |
| 113 | // Parameter: - |
| 114 | // Returns: - |
| 115 | // Changes Globals: - |
| 116 | //=========================================================================== |
| 117 | void QDECL Log_Write(char *fmt, ...) |
| 118 | { |
| 119 | va_list ap; |
| 120 | |
| 121 | if (!logfile.fp) return; |
| 122 | va_start(ap, fmt); |
| 123 | vfprintf(logfile.fp, fmt, ap); |
| 124 | va_end(ap); |
| 125 | //fprintf(logfile.fp, "\r\n"); |
| 126 | fflush(logfile.fp); |
| 127 | } //end of the function Log_Write |
| 128 | //=========================================================================== |
| 129 | // |
| 130 | // Parameter: - |
| 131 | // Returns: - |
| 132 | // Changes Globals: - |
| 133 | //=========================================================================== |
| 134 | void QDECL Log_WriteTimeStamped(char *fmt, ...) |
| 135 | { |
| 136 | va_list ap; |
| 137 | |
| 138 | if (!logfile.fp) return; |
| 139 | fprintf(logfile.fp, "%d %02d:%02d:%02d:%02d ", |
| 140 | logfile.numwrites, |
| 141 | (int) (botlibglobals.time / 60 / 60), |
| 142 | (int) (botlibglobals.time / 60), |
| 143 | (int) (botlibglobals.time), |
| 144 | (int) ((int) (botlibglobals.time * 100)) - |
| 145 | ((int) botlibglobals.time) * 100); |
| 146 | va_start(ap, fmt); |
| 147 | vfprintf(logfile.fp, fmt, ap); |
| 148 | va_end(ap); |
| 149 | fprintf(logfile.fp, "\r\n"); |
| 150 | logfile.numwrites++; |
| 151 | fflush(logfile.fp); |
| 152 | } //end of the function Log_Write |
| 153 | //=========================================================================== |
| 154 | // |
| 155 | // Parameter: - |
| 156 | // Returns: - |
| 157 | // Changes Globals: - |
| 158 | //=========================================================================== |
| 159 | FILE *Log_FilePointer(void) |
| 160 | { |
| 161 | return logfile.fp; |
| 162 | } //end of the function Log_FilePointer |
| 163 | //=========================================================================== |
| 164 | // |
| 165 | // Parameter: - |
| 166 | // Returns: - |
| 167 | // Changes Globals: - |
| 168 | //=========================================================================== |
| 169 | void Log_Flush(void) |
| 170 | { |
| 171 | if (logfile.fp) fflush(logfile.fp); |
| 172 | } //end of the function Log_Flush |
| 173 |