Liberty Unleashed

Scripting => Script Snippets => Topic started by: Windlord on April 27, 2011, 07:15:55 pm

Title: /var/log type logging for scripts
Post by: Windlord on April 27, 2011, 07:15:55 pm
Code: (squirrel) [Select]
DEBUG <- true;

// Function to add timestamps to debug messages
function debug ( msg )
{
// Check if debug is turned on
if ( !DEBUG ) return false;
else
{
// Get date data table
local dt = date();
local timestamp = format( "%02i/%02i %02i:%02i:%02i", dt.day, dt.month, dt.hour, dt.min, dt.sec );
msg = timestamp +" :: "+ msg;

// Initiate file stream if not already done
if ( !getroottable().rawin( "DEBUG_STREAM" ) )
{
DEBUG_STREAM <- file( "debug.log", "a" );
debug_newline();
}

// Write debug message to file stream
foreach ( idx, chr in msg )
DEBUG_STREAM.writen( chr, 'b' );

// Write newline to make debug.log readable
debug_newline();

print( "\r"+ msg );
return true;
}
}


function debug_newline ()
{
DEBUG_STREAM.writen( '\r', 'b' );
DEBUG_STREAM.writen( '\n', 'b' );
}

This code allows you to do a:
Code: (bash) [Select]
tail -f debug.logas you can do with Linux log files located in /var/log
Title: Re: /var/log type logging for scripts
Post by: Thijn on April 27, 2011, 10:21:38 pm
ow looks very smexy <3
Title: Re: /var/log type logging for scripts
Post by: Windlord on April 28, 2011, 01:40:15 am
Thanks Thijn :)

Cleaned it up a bit and made the output use CRLF for Windows-friendliness...