1 /*
2 ===========================================================================
3 Copyright (C) 1999 - 2005, Id Software, Inc.
4 Copyright (C) 2000 - 2013, Raven Software, Inc.
5 Copyright (C) 2001 - 2013, Activision, Inc.
6 Copyright (C) 2013 - 2015, OpenJK contributors
7 
8 This file is part of the OpenJK source code.
9 
10 OpenJK is free software; you can redistribute it and/or modify it
11 under the terms of the GNU General Public License version 2 as
12 published by the Free Software Foundation.
13 
14 This program 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 this program; if not, see <http://www.gnu.org/licenses/>.
21 ===========================================================================
22 */
23 
24 /*****************************************************************************
25  * name:		l_log.c
26  *
27  * desc:		log file
28  *
29  * $Archive: /MissionPack/CODE/botlib/l_log.c $
30  * $Author: Raduffy $
31  * $Revision: 1 $
32  * $Modtime: 12/20/99 8:43p $
33  * $Date: 3/08/00 11:28a $
34  *
35  *****************************************************************************/
36 
37 #include <stdlib.h>
38 #include <stdio.h>
39 #include <string.h>
40 
41 #include "qcommon/q_shared.h"
42 #include "botlib.h"
43 #include "be_interface.h"			//for botimport.Print
44 #include "l_libvar.h"
45 #include "l_log.h"
46 
47 #define MAX_LOGFILENAMESIZE		1024
48 
49 typedef struct logfile_s
50 {
51 	char filename[MAX_LOGFILENAMESIZE];
52 	FILE *fp;
53 	int numwrites;
54 } logfile_t;
55 
56 static logfile_t logfile;
57 
58 //===========================================================================
59 //
60 // Parameter:				-
61 // Returns:					-
62 // Changes Globals:		-
63 //===========================================================================
Log_Open(char * filename)64 void Log_Open(char *filename)
65 {
66 	if (!LibVarValue("log", "0")) return;
67 	if (!filename || !strlen(filename))
68 	{
69 		botimport.Print(PRT_MESSAGE, "openlog <filename>\n");
70 		return;
71 	} //end if
72 	if (logfile.fp)
73 	{
74 		botimport.Print(PRT_ERROR, "log file %s is already opened\n", logfile.filename);
75 		return;
76 	} //end if
77 	logfile.fp = fopen(filename, "wb");
78 	if (!logfile.fp)
79 	{
80 		botimport.Print(PRT_ERROR, "can't open the log file %s\n", filename);
81 		return;
82 	} //end if
83 	strncpy(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_Close(void)92 void Log_Close(void)
93 {
94 	if (!logfile.fp) return;
95 	if (fclose(logfile.fp))
96 	{
97 		botimport.Print(PRT_ERROR, "can't close log file %s\n", logfile.filename);
98 		return;
99 	} //end if
100 	logfile.fp = NULL;
101 	botimport.Print(PRT_MESSAGE, "Closed log %s\n", logfile.filename);
102 } //end of the function Log_Close
103 //===========================================================================
104 //
105 // Parameter:				-
106 // Returns:					-
107 // Changes Globals:		-
108 //===========================================================================
Log_Shutdown(void)109 void Log_Shutdown(void)
110 {
111 	if (logfile.fp) Log_Close();
112 } //end of the function Log_Shutdown
113 //===========================================================================
114 //
115 // Parameter:				-
116 // Returns:					-
117 // Changes Globals:		-
118 //===========================================================================
Log_Write(char * fmt,...)119 void QDECL Log_Write(char *fmt, ...)
120 {
121 	va_list ap;
122 
123 	if (!logfile.fp) return;
124 	va_start(ap, fmt);
125 	vfprintf(logfile.fp, fmt, ap);
126 	va_end(ap);
127 	//fprintf(logfile.fp, "\r\n");
128 	fflush(logfile.fp);
129 } //end of the function Log_Write
130 //===========================================================================
131 //
132 // Parameter:				-
133 // Returns:					-
134 // Changes Globals:		-
135 //===========================================================================
Log_WriteTimeStamped(char * fmt,...)136 void QDECL Log_WriteTimeStamped(char *fmt, ...)
137 {
138 	va_list ap;
139 
140 	if (!logfile.fp) return;
141 	fprintf(logfile.fp, "%d   %02d:%02d:%02d:%02d   ",
142 					logfile.numwrites,
143 					(int) (botlibglobals.time / 60 / 60),
144 					(int) (botlibglobals.time / 60),
145 					(int) (botlibglobals.time),
146 					(int) ((int) (botlibglobals.time * 100)) -
147 							((int) botlibglobals.time) * 100);
148 	va_start(ap, fmt);
149 	vfprintf(logfile.fp, fmt, ap);
150 	va_end(ap);
151 	fprintf(logfile.fp, "\r\n");
152 	logfile.numwrites++;
153 	fflush(logfile.fp);
154 } //end of the function Log_Write
155 //===========================================================================
156 //
157 // Parameter:				-
158 // Returns:					-
159 // Changes Globals:		-
160 //===========================================================================
Log_FilePointer(void)161 FILE *Log_FilePointer(void)
162 {
163 	return logfile.fp;
164 } //end of the function Log_FilePointer
165 //===========================================================================
166 //
167 // Parameter:				-
168 // Returns:					-
169 // Changes Globals:		-
170 //===========================================================================
Log_Flush(void)171 void Log_Flush(void)
172 {
173 	if (logfile.fp) fflush(logfile.fp);
174 } //end of the function Log_Flush
175 
176