This is the result of 10 minutes of pounding on the keyboard after yet another disappointing experience with trying to get logrotate to do something vaguely more flexible.

This simple script scans all normal files in a log directory, and if they are older than a certain cutoff, moves them into a holding directory for old logs. Future passes will check files in the old directory for another age setting and will delete them. That's all there is to it.

Configure your cutoffs, directories of interest, and optionally plug in a better logging mechanism and you're set. (Oh, and change the #! if necessary, of course).

#!/usr/bin/php
<?
$cutoff_rotate = "3 days";
$cutoff_delete = "7 days";
$dir_log = "/logs";
$dir_old = "/logs.old";

function trace( $msg, $debug = FALSE ) {
    // appropriate logging mechanism can be plugged in here
    echo "[] $msg\n";
}

// scan old files for deletion
if( is_dir($dir_old) ) {
    $dh = opendir( $dir_old );
    if( $dh !== FALSE ) {
        chdir( $dir_old );
        trace( "scanning $dir_old for logs more than $cutoff_delete old" );
        $cutoff = strtotime( "-$cutoff_delete" );
        trace( "cutoff is ".date('r',$cutoff) );
        while( ($file = readdir($dh)) !== FALSE ) {
            if( is_dir($file) ) {
                trace( "skipping $file", true );
                continue;
            } else {
                $ts = filemtime($file);
                if( $ts <$cutoff ) {
                    trace( "deleting $file, ".date('r',$ts) );
                    $succ = @unlink($file);
                    if( !$succ )
                        trace( "failed to unlink $file!" );
                } else {
                    trace( "ignoring $file, ".date('r',$ts), true );
                }
            }
        }
    }
    closedir( $dh );
} else {
    trace( "no old log dir $dir_old to scan yet" );
}

// scan current files for rotation
if( file_exists($dir_old) ) {
    trace( "creating $dir_old" );
    $succ = @mkdir( $dir_old, 0775, true );
    if( !$succ ) {
        trace( "mkdir failed, aborting rotation" );
        exit( 1 );
    }
}
if( is_dir($dir_log) ) {
    $dh = opendir( $dir_log );
    if( $dh !== FALSE ) {
        chdir( $dir_log );
        trace( "scanning $dir_log for logs more than $cutoff_rotate old" );
        $cutoff = strtotime( "-$cutoff_delete" );
        trace( "cutoff is ".date('r',$cutoff) );
        while( ($file = readdir($dh)) !== FALSE ) {
            if( is_dir($file) ) {
                trace( "skipping $file", true );
                continue;
            } else {
                $ts = filemtime($file);
                if( $ts <$cutoff ) {
                    trace( "rotating $file, ".date('r',$ts), true );
                    $succ = @rename( $file, $dir_old );
                    if( !$succ )
                        trace( "failed to rotate $file!" );
                } else {
                    trace( "ignoring $file, ".date('r',$ts), true );
                }
            }
        }
    }
    closedir( $dh );
}
?>

Caveman profiling with a side of "where were you at 9pm on the night in question?" As always, season to taste.

<?
$_profile_log = "/tmp/php-profile.log";

function _profile() {
    static $fh;
    if( !isset($fh) ) {
        global $_profile_log;
        if( !file_exists($_profile_log) ) {
            @touch( $_profile_log );
            @chmod( $_profile_log, 0664 );
        }
        $fh = @fopen( $_profile_log, "a" );
    }
    if( !$fh )
        return false;

    $stack = debug_backtrace();
    if( $stack[1] )
        $base = $stack[1];
    else
        $base = $stack[0];
    $buf = $base['file'].":".$base['line'].", ";
    if( $base['class'] )
        $buf .= $base['class'].$base['type'];
    $buf .= $base['function'];

    $buf = sprintf("[%s] %s\n",date("H:i:s"),$buf);
    return @fwrite( $fh, $buf );
}
?>

Read the rest of this entry »

I've gone through all of my old posts (~260 of them in my 5+ year history) and tagged them.

I've got a theme that doesn't make me want to tear my eyes out, but it's not what I want yet.

I've acknowledged all missing images that I noticed by tagging the post as 'broken images', and I'll be going through them again later to see if I have any remaining local copies of the files in question.

I've installed a modern syntax hiliting plugin, so code should be readable again.

Sooo close.

I can't type. I Just managed to delete my entire blog content dir and plugins. This means all of my images and any uploaded zips are gone.

Also, my company has recently had to make some dramatic changes that I'm still coping with. I am still employed, but I'm not much in the mood to continue with any of my previous article series at present.

On the bright side, this gives me an excuse upgrade to WP2.9 and dig through my 250+ post archive and clean things up.

Please excuse the mess while I unbork everything. Happy new year.

Well, five weeks in a row was pretty good. I'm going to have to take a break from this series until some time after the new year for reasons of not making my wife hate me over the holiday :)

I plan on making my Florensia update some time next week and hope to resume regular posting for most of January.

Time was actually short this week. I spent a lot of time alternatively between work, a closed beta, and Borderlands. Lots of Borderlands. And some Dawn of War, come to think of it... but anyhow.

The game I had wanted to look at this week is Gatheryn. They're nominally in "open" beta, but put testers under a clickthrough NDA. So... they go on the list with all of my other closed beta games of interest in stead.

After a lot of debate and consideration, I've decided to just go ahead and give Florensia a shot and be done with it.

Week 5: Florensia

character portrait

Florensia looks like a pretty generic Asian sort of MMO. The Western publication of the game is handled by the same folks who're publishing Ragnarok Online and Hello Kitty Online in english, so I figure it's worth a look for that reason alone.

Read the rest of this entry »