Skip to content

Instantly share code, notes, and snippets.

@gesslar
Created April 3, 2021 20:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gesslar/3a68fe425826d4a858ea1c898deeb6f2 to your computer and use it in GitHub Desktop.
Save gesslar/3a68fe425826d4a858ea1c898deeb6f2 to your computer and use it in GitHub Desktop.
log file sefun with log file rotation
void log_file( string file, string text )
{
string pathfile ;
if( !stringp( file ) || !stringp( text ) ) return ; // syntax error
if( strsrch( file, ".." ) != -1 ) return ; // security error
if( text[ <1 .. ] != "\n") text += "\n" ;
if (previous_object())
{
if ( base_name( previous_object() ) == USER_OB || base_name( previous_object() ) == CONNECTION || base_name( previous_object() ) == CMD_ED )
{
seteuid(ROOT_UID) ;
}
else
{
seteuid(geteuid(previous_object())) ;
}
}
pathfile = LOG_DIR + file;
if( file_size( pathfile ) > MAX_LOG_SIZE )
{
string attic_dir, attic_file ;
string dir ;
int pos ;
mixed *ltime = localtime(time());
int version = 0 ;
pos = strsrch( pathfile, "/", -1 ) ;
dir = pathfile[ 0 .. pos - 1 ] ;
file = pathfile[ pos + 1 .. ] ;
// First, left's find out if there's an attic directory
attic_dir = dir + "/attic" ;
if( file_size( attic_dir ) != -2 )
{
if( mkdir( attic_dir ) == 0 ) attic_dir = dir ;
}
attic_file = sprintf("%s/%s.old.%s", attic_dir, file, sprintf("%04d%02d%02d", ltime[LT_YEAR], ltime[LT_MON], ltime[LT_MON])) ;
if( file_size( attic_file ) != -1 )
{
version ++ ;
while( file_size( sprintf("%s.%d", attic_file, version ) ) != -1 ) version ++ ;
}
if( version > 0 ) attic_file = sprintf( "%s.%d", attic_file, version ) ;
rename( pathfile, attic_file ) ;
}
write_file( pathfile, text );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment