Created
April 3, 2021 20:49
-
-
Save gesslar/3a68fe425826d4a858ea1c898deeb6f2 to your computer and use it in GitHub Desktop.
log file sefun with log file rotation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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