openmohaa/code/botlib/l_log.c

174 lines
5.1 KiB
C
Raw Normal View History

2016-03-27 11:49:47 +02:00
/*
===========================================================================
Copyright (C) 1999-2005 Id Software, Inc.
This file is part of Quake III Arena source code.
Quake III Arena source code is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the License,
or (at your option) any later version.
Quake III Arena source code is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
2023-05-24 19:03:05 +02:00
along with Quake III Arena source code; if not, write to the Free Software
2016-03-27 11:49:47 +02:00
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===========================================================================
*/
2023-05-24 19:03:05 +02:00
/*****************************************************************************
* name: l_log.c
*
* desc: log file
*
* $Archive: /MissionPack/CODE/botlib/l_log.c $
*
*****************************************************************************/
2016-03-27 11:49:47 +02:00
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
2023-05-24 19:03:05 +02:00
#include "../qcommon/q_shared.h"
#include "../qcommon/qcommon.h"
#include "botlib.h"
#include "be_interface.h" //for botimport.Print
#include "l_libvar.h"
#include "l_log.h"
2016-03-27 11:49:47 +02:00
#define MAX_LOGFILENAMESIZE 1024
typedef struct logfile_s
{
char filename[MAX_LOGFILENAMESIZE];
FILE *fp;
int numwrites;
} logfile_t;
2023-05-24 19:03:05 +02:00
static logfile_t logfile;
2016-03-27 11:49:47 +02:00
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void Log_Open(char *filename)
{
2023-05-24 19:03:05 +02:00
char *ospath;
if (!LibVarValue("log", "0")) return;
2016-03-27 11:49:47 +02:00
if (!filename || !strlen(filename))
{
2023-05-24 19:03:05 +02:00
botimport.Print(PRT_MESSAGE, "openlog <filename>\n");
2016-03-27 11:49:47 +02:00
return;
} //end if
if (logfile.fp)
{
2023-05-24 19:03:05 +02:00
botimport.Print(PRT_ERROR, "log file %s is already opened\n", logfile.filename);
2016-03-27 11:49:47 +02:00
return;
} //end if
2023-05-24 19:03:05 +02:00
ospath = FS_BuildOSPath(Cvar_VariableString("fs_homepath"), Cvar_VariableString("fs_game"), filename);
logfile.fp = fopen(ospath, "wb");
2016-03-27 11:49:47 +02:00
if (!logfile.fp)
{
2023-05-24 19:03:05 +02:00
botimport.Print(PRT_ERROR, "can't open the log file %s\n", filename);
2016-03-27 11:49:47 +02:00
return;
} //end if
2023-05-24 19:03:05 +02:00
Q_strncpyz(logfile.filename, filename, MAX_LOGFILENAMESIZE);
botimport.Print(PRT_MESSAGE, "Opened log %s\n", logfile.filename);
2016-03-27 11:49:47 +02:00
} //end of the function Log_Create
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void Log_Close(void)
{
2023-05-24 19:03:05 +02:00
if (!logfile.fp) return;
2016-03-27 11:49:47 +02:00
if (fclose(logfile.fp))
{
2023-05-24 19:03:05 +02:00
botimport.Print(PRT_ERROR, "can't close log file %s\n", logfile.filename);
2016-03-27 11:49:47 +02:00
return;
} //end if
logfile.fp = NULL;
2023-05-24 19:03:05 +02:00
botimport.Print(PRT_MESSAGE, "Closed log %s\n", logfile.filename);
2016-03-27 11:49:47 +02:00
} //end of the function Log_Close
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void Log_Shutdown(void)
{
if (logfile.fp) Log_Close();
} //end of the function Log_Shutdown
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
2023-05-24 19:03:05 +02:00
void QDECL Log_Write(char *fmt, ...)
2016-03-27 11:49:47 +02:00
{
va_list ap;
if (!logfile.fp) return;
va_start(ap, fmt);
2023-05-24 19:03:05 +02:00
vfprintf(logfile.fp, fmt, ap);
2016-03-27 11:49:47 +02:00
va_end(ap);
2023-05-24 19:03:05 +02:00
//fprintf(logfile.fp, "\r\n");
2016-03-27 11:49:47 +02:00
fflush(logfile.fp);
} //end of the function Log_Write
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
2023-05-24 19:03:05 +02:00
void QDECL Log_WriteTimeStamped(char *fmt, ...)
2016-03-27 11:49:47 +02:00
{
va_list ap;
if (!logfile.fp) return;
2023-05-24 19:03:05 +02:00
fprintf(logfile.fp, "%d %02d:%02d:%02d:%02d ",
2016-03-27 11:49:47 +02:00
logfile.numwrites,
(int) (botlibglobals.time / 60 / 60),
(int) (botlibglobals.time / 60),
(int) (botlibglobals.time),
(int) ((int) (botlibglobals.time * 100)) -
2023-05-24 19:03:05 +02:00
((int) botlibglobals.time) * 100);
2016-03-27 11:49:47 +02:00
va_start(ap, fmt);
vfprintf(logfile.fp, fmt, ap);
va_end(ap);
2023-05-24 19:03:05 +02:00
fprintf(logfile.fp, "\r\n");
2016-03-27 11:49:47 +02:00
logfile.numwrites++;
fflush(logfile.fp);
} //end of the function Log_Write
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
2023-05-24 19:03:05 +02:00
FILE *Log_FilePointer(void)
2016-03-27 11:49:47 +02:00
{
return logfile.fp;
2023-05-24 19:03:05 +02:00
} //end of the function Log_FilePointer
2016-03-27 11:49:47 +02:00
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void Log_Flush(void)
{
if (logfile.fp) fflush(logfile.fp);
} //end of the function Log_Flush