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