1 /*
2 ===========================================================================
3
4 Return to Castle Wolfenstein single player GPL Source Code
5 Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company.
6
7 This file is part of the Return to Castle Wolfenstein single player GPL Source Code (RTCW SP Source Code).
8
9 RTCW SP Source Code is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 RTCW SP Source Code is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with RTCW SP Source Code. If not, see <http://www.gnu.org/licenses/>.
21
22 In addition, the RTCW SP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW SP Source Code. If not, please request a copy in writing from id Software at the address below.
23
24 If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
25
26 ===========================================================================
27 */
28
29
30 /*****************************************************************************
31 * name: l_log.c
32 *
33 * desc: log file
34 *
35 *
36 *****************************************************************************/
37
38 #include <stdlib.h>
39 #include <stdio.h>
40 #include <string.h>
41
42 #include "../qcommon/q_shared.h"
43 #include "../qcommon/qcommon.h"
44 #include "botlib.h"
45 #include "be_interface.h" //for botimport.Print
46 #include "l_libvar.h"
47 #include "l_log.h"
48
49 #define MAX_LOGFILENAMESIZE 1024
50
51 typedef struct logfile_s
52 {
53 char filename[MAX_LOGFILENAMESIZE];
54 FILE *fp;
55 int numwrites;
56 } logfile_t;
57
58 static logfile_t logfile;
59
60 //===========================================================================
61 //
62 // Parameter: -
63 // Returns: -
64 // Changes Globals: -
65 //===========================================================================
Log_AlwaysOpen(char * filename)66 void Log_AlwaysOpen( char *filename ) {
67 char *ospath;
68
69 if ( !filename || !strlen( filename ) ) {
70 botimport.Print( PRT_MESSAGE, "openlog <filename>\n" );
71 return;
72 } //end if
73 if ( logfile.fp ) {
74 botimport.Print( PRT_ERROR, "log file %s is already opened\n", logfile.filename );
75 return;
76 } //end if
77 ospath = FS_BuildOSPath( Cvar_VariableString( "fs_homepath" ), Cvar_VariableString( "fs_game" ), filename );
78 logfile.fp = fopen(ospath, "wb");
79 if ( !logfile.fp ) {
80 botimport.Print( PRT_ERROR, "can't open the log file %s\n", filename );
81 return;
82 } //end if
83 Q_strncpyz( logfile.filename, filename, MAX_LOGFILENAMESIZE );
84 botimport.Print( PRT_MESSAGE, "Opened log %s\n", logfile.filename );
85 } //end of the function Log_Create
86 //===========================================================================
87 //
88 // Parameter: -
89 // Returns: -
90 // Changes Globals: -
91 //===========================================================================
Log_Open(char * filename)92 void Log_Open( char *filename ) {
93 if ( !LibVarValue( "log", "0" ) ) {
94 return;
95 }
96 Log_AlwaysOpen( filename );
97
98 }
99 //===========================================================================
100 //
101 // Parameter: -
102 // Returns: -
103 // Changes Globals: -
104 //===========================================================================
Log_Close(void)105 void Log_Close( void ) {
106 if ( !logfile.fp ) {
107 return;
108 }
109 if ( fclose( logfile.fp ) ) {
110 botimport.Print( PRT_ERROR, "can't close log file %s\n", logfile.filename );
111 return;
112 } //end if
113 logfile.fp = NULL;
114 botimport.Print( PRT_MESSAGE, "Closed log %s\n", logfile.filename );
115 } //end of the function Log_Close
116 //===========================================================================
117 //
118 // Parameter: -
119 // Returns: -
120 // Changes Globals: -
121 //===========================================================================
Log_Shutdown(void)122 void Log_Shutdown( void ) {
123 if ( logfile.fp ) {
124 Log_Close();
125 }
126 } //end of the function Log_Shutdown
127 //===========================================================================
128 //
129 // Parameter: -
130 // Returns: -
131 // Changes Globals: -
132 //===========================================================================
Log_Write(char * fmt,...)133 void QDECL Log_Write( char *fmt, ... ) {
134 va_list ap;
135
136 if ( !logfile.fp ) {
137 return;
138 }
139 va_start( ap, fmt );
140 vfprintf( logfile.fp, fmt, ap );
141 va_end( ap );
142 //fprintf(logfile.fp, "\r\n");
143 fflush( logfile.fp );
144 } //end of the function Log_Write
145 //===========================================================================
146 //
147 // Parameter: -
148 // Returns: -
149 // Changes Globals: -
150 //===========================================================================
Log_WriteTimeStamped(char * fmt,...)151 void QDECL Log_WriteTimeStamped( char *fmt, ... ) {
152 va_list ap;
153
154 if ( !logfile.fp ) {
155 return;
156 }
157 fprintf( logfile.fp, "%d %02d:%02d:%02d:%02d ",
158 logfile.numwrites,
159 (int) ( botlibglobals.time / 60 / 60 ),
160 (int) ( botlibglobals.time / 60 ),
161 (int) ( botlibglobals.time ),
162 (int) ( (int) ( botlibglobals.time * 100 ) ) -
163 ( (int) botlibglobals.time ) * 100 );
164 va_start( ap, fmt );
165 vfprintf( logfile.fp, fmt, ap );
166 va_end( ap );
167 fprintf( logfile.fp, "\r\n" );
168 logfile.numwrites++;
169 fflush( logfile.fp );
170 } //end of the function Log_Write
171 //===========================================================================
172 //
173 // Parameter: -
174 // Returns: -
175 // Changes Globals: -
176 //===========================================================================
Log_FilePointer(void)177 FILE *Log_FilePointer( void ) {
178 return logfile.fp;
179 } //end of the function Log_FilePointer
180 //===========================================================================
181 //
182 // Parameter: -
183 // Returns: -
184 // Changes Globals: -
185 //===========================================================================
Log_Flush(void)186 void Log_Flush( void ) {
187 if ( logfile.fp ) {
188 fflush( logfile.fp );
189 }
190 } //end of the function Log_Flush
191
192