2018-04-13 18:23:45 +02:00
|
|
|
/*
|
|
|
|
sv_log.c - server logging in multiplayer
|
|
|
|
Copyright (C) 2017 Uncle Mike
|
|
|
|
|
|
|
|
This program 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 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
#include "server.h"
|
|
|
|
|
|
|
|
void Log_Open( void )
|
|
|
|
{
|
|
|
|
time_t ltime;
|
|
|
|
struct tm *today;
|
|
|
|
char szFileBase[ MAX_OSPATH ];
|
|
|
|
char szTestFile[ MAX_OSPATH ];
|
|
|
|
file_t *fp = NULL;
|
2018-04-23 22:07:54 +02:00
|
|
|
const char *temp;
|
2018-04-13 18:23:45 +02:00
|
|
|
int i;
|
|
|
|
|
|
|
|
if( !svs.log.active )
|
|
|
|
return;
|
|
|
|
|
2021-10-27 18:40:05 +02:00
|
|
|
if( sv_log_onefile.value && svs.log.file )
|
|
|
|
return;
|
|
|
|
|
2018-04-13 18:23:45 +02:00
|
|
|
if( !mp_logfile.value )
|
|
|
|
{
|
|
|
|
Con_Printf( "Server logging data to console.\n" );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Log_Close();
|
|
|
|
|
|
|
|
// Find a new log file slot
|
|
|
|
time( <ime );
|
|
|
|
today = localtime( <ime );
|
|
|
|
temp = Cvar_VariableString( "logsdir" );
|
|
|
|
|
2020-11-29 01:23:50 +01:00
|
|
|
if( COM_CheckString( temp ) && !Q_strchr( temp, ':' ) && !Q_strstr( temp, ".." ))
|
2018-04-13 18:23:45 +02:00
|
|
|
Q_snprintf( szFileBase, sizeof( szFileBase ), "%s/L%02i%02i", temp, today->tm_mon + 1, today->tm_mday );
|
|
|
|
else Q_snprintf( szFileBase, sizeof( szFileBase ), "logs/L%02i%02i", today->tm_mon + 1, today->tm_mday );
|
|
|
|
|
|
|
|
for ( i = 0; i < 1000; i++ )
|
|
|
|
{
|
|
|
|
Q_snprintf( szTestFile, sizeof( szTestFile ), "%s%03i.log", szFileBase, i );
|
|
|
|
|
|
|
|
if( FS_FileExists( szTestFile, false ))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
fp = FS_Open( szTestFile, "w", true );
|
|
|
|
if( fp )
|
|
|
|
{
|
|
|
|
Con_Printf( "Server logging data to file %s\n", szTestFile );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
i = 1000;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( i == 1000 )
|
|
|
|
{
|
|
|
|
Con_Printf( "Unable to open logfiles under %s\nLogging disabled\n", szFileBase );
|
|
|
|
svs.log.active = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( fp ) svs.log.file = fp;
|
2018-04-23 20:36:33 +02:00
|
|
|
Log_Printf( "Log file started (file \"%s\") (game \"%s\") (version \"%i/%s/%d\")\n",
|
2018-04-13 18:23:45 +02:00
|
|
|
szTestFile, Info_ValueForKey( SV_Serverinfo(), "*gamedir" ), PROTOCOL_VERSION, XASH_VERSION, Q_buildnum() );
|
|
|
|
}
|
|
|
|
|
|
|
|
void Log_Close( void )
|
|
|
|
{
|
|
|
|
if( svs.log.file )
|
|
|
|
{
|
|
|
|
Log_Printf( "Log file closed\n" );
|
|
|
|
FS_Close( svs.log.file );
|
|
|
|
}
|
|
|
|
svs.log.file = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
==================
|
|
|
|
Log_Printf
|
|
|
|
|
|
|
|
Prints a frag log message to the server's frag log file, console, and possible a UDP port.
|
|
|
|
==================
|
|
|
|
*/
|
|
|
|
void Log_Printf( const char *fmt, ... )
|
|
|
|
{
|
|
|
|
va_list argptr;
|
|
|
|
static char string[1024];
|
|
|
|
char *p;
|
|
|
|
time_t ltime;
|
2020-11-29 01:23:50 +01:00
|
|
|
struct tm *today;
|
|
|
|
int len;
|
2018-04-13 18:23:45 +02:00
|
|
|
|
|
|
|
if( !svs.log.active )
|
|
|
|
return;
|
|
|
|
|
|
|
|
time( <ime );
|
|
|
|
today = localtime( <ime );
|
|
|
|
|
2020-11-29 01:23:50 +01:00
|
|
|
len = Q_snprintf( string, sizeof( string ), "%02i/%02i/%04i - %02i:%02i:%02i: ",
|
2018-04-13 18:23:45 +02:00
|
|
|
today->tm_mon+1, today->tm_mday, 1900 + today->tm_year, today->tm_hour, today->tm_min, today->tm_sec );
|
|
|
|
|
2020-11-29 01:23:50 +01:00
|
|
|
p = string + len;
|
2018-04-13 18:23:45 +02:00
|
|
|
|
|
|
|
va_start( argptr, fmt );
|
2020-11-29 01:23:50 +01:00
|
|
|
Q_vsnprintf( p, sizeof( string ) - len, fmt, argptr );
|
2018-04-13 18:23:45 +02:00
|
|
|
va_end( argptr );
|
|
|
|
|
|
|
|
if( svs.log.net_log )
|
|
|
|
Netchan_OutOfBandPrint( NS_SERVER, svs.log.net_address, "log %s", string );
|
|
|
|
|
2021-10-27 18:40:05 +02:00
|
|
|
if( svs.log.active && ( svs.maxclients > 1 || sv_log_singleplayer.value != 0.0f ))
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
|
|
|
// echo to server console
|
2021-01-03 02:28:45 +01:00
|
|
|
if( mp_logecho.value )
|
2018-04-13 18:23:45 +02:00
|
|
|
Con_Printf( "%s", string );
|
|
|
|
|
|
|
|
// echo to log file
|
|
|
|
if( svs.log.file && mp_logfile.value )
|
|
|
|
FS_Printf( svs.log.file, "%s", string );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-20 23:05:08 +02:00
|
|
|
static void Log_PrintServerCvar( const char *var_name, const char *var_value, void *unused2, void *unused3 )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
|
|
|
Log_Printf( "Server cvar \"%s\" = \"%s\"\n", var_name, var_value );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
==================
|
|
|
|
Log_PrintServerVars
|
|
|
|
|
|
|
|
==================
|
|
|
|
*/
|
|
|
|
void Log_PrintServerVars( void )
|
|
|
|
{
|
|
|
|
if( !svs.log.active )
|
|
|
|
return;
|
|
|
|
|
|
|
|
Log_Printf( "Server cvars start\n" );
|
2019-05-02 18:12:23 +02:00
|
|
|
Cvar_LookupVars( FCVAR_SERVER, NULL, NULL, (setpair_t)Log_PrintServerCvar );
|
2018-04-13 18:23:45 +02:00
|
|
|
Log_Printf( "Server cvars end\n" );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
====================
|
2021-10-27 18:40:05 +02:00
|
|
|
SV_SetLogAddress_f
|
2018-04-13 18:23:45 +02:00
|
|
|
|
|
|
|
====================
|
|
|
|
*/
|
2021-10-27 18:40:05 +02:00
|
|
|
void SV_SetLogAddress_f( void )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2021-10-27 18:40:05 +02:00
|
|
|
const char *s;
|
|
|
|
int port;
|
|
|
|
string addr;
|
|
|
|
|
|
|
|
if( Cmd_Argc() != 3 )
|
|
|
|
{
|
|
|
|
Con_Printf( "logaddress: usage\nlogaddress ip port\n" );
|
|
|
|
|
|
|
|
if( svs.log.active )
|
|
|
|
Con_Printf( "current: %s\n", NET_AdrToString( svs.log.net_address ));
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
port = Q_atoi( Cmd_Argv( 2 ));
|
|
|
|
if( !port )
|
|
|
|
{
|
|
|
|
Con_Printf( "logaddress: must specify a valid port\n" );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
s = Cmd_Argv( 1 );
|
|
|
|
if( !COM_CheckString( s ))
|
|
|
|
{
|
|
|
|
Con_Printf( "logaddress: unparseable address\n" );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Q_snprintf( addr, sizeof( addr ), "%s:%i", s, port );
|
|
|
|
if( !NET_StringToAdr( addr, &svs.log.net_address ))
|
|
|
|
{
|
|
|
|
Con_Printf( "logaddress: unable to resolve %s\n", addr );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
svs.log.net_log = true;
|
|
|
|
Con_Printf( "logaddress: %s\n", NET_AdrToString( svs.log.net_address ));
|
|
|
|
}
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2021-10-27 18:40:05 +02:00
|
|
|
/*
|
|
|
|
====================
|
|
|
|
SV_ServerLog_f
|
|
|
|
|
|
|
|
====================
|
|
|
|
*/
|
|
|
|
void SV_ServerLog_f( void )
|
|
|
|
{
|
2018-04-13 18:23:45 +02:00
|
|
|
if( Cmd_Argc() != 2 )
|
|
|
|
{
|
2021-10-27 18:40:05 +02:00
|
|
|
Con_Printf("usage: log < on|off >\n" );
|
2018-04-13 18:23:45 +02:00
|
|
|
|
|
|
|
if( svs.log.active )
|
2021-10-27 18:40:05 +02:00
|
|
|
Con_Printf( "currently logging\n" );
|
|
|
|
else Con_Printf( "not currently logging\n" );
|
|
|
|
return;
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if( !Q_stricmp( Cmd_Argv( 1 ), "off" ))
|
|
|
|
{
|
|
|
|
if( svs.log.active )
|
|
|
|
Log_Close();
|
|
|
|
}
|
|
|
|
else if( !Q_stricmp( Cmd_Argv( 1 ), "on" ))
|
|
|
|
{
|
|
|
|
svs.log.active = true;
|
|
|
|
Log_Open();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-10-27 18:40:05 +02:00
|
|
|
Con_Printf( "log: unknown parameter %s\n", Cmd_Argv( 1 ));
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
|
|
|
|
2021-10-27 18:40:05 +02:00
|
|
|
return;
|
2018-04-20 23:05:08 +02:00
|
|
|
}
|