<?php
/**
 * Logger
 * 
 * @version:    0.5rc
 * @author:    Alex Yaroshevich
 * @date:    27 June 2006
 * @update:    15 June 2007
 * @link:    http://src.qfox.ru/
 *
 * logger.
 * just fast and furious
*/

/**
 * translation LOG_LEVEL char to numeric for comparing
 * @see L
 * @see L::lvlMax
 * @see L::lvlMin
 * @see L::isShow
 * @see L::logit
 */
$___l2n array_flip( array( 'A''E''W''N''D' ) );


/**
 * translation LOG_LEVEL numeric to name of defined const
 * @see L
 * @see L::logit
 */
$___n2d = array( 'CRIT''ERROR''WARN''NOTICE''DEBUG' ); // for diff files


/**
 * list of registered modules
 * @see L
 * @see L::lvlMax
 * @see L::lvlMin
 * @see L::isShow
 */
$___mods = array( );

class 
{

    
/**
     * Writes data to file
     *
     * @param string $fileName name of log-file
     * @param string $string string, which appending to log-file
     */
    
static function appendFile$fileName$string ) {
        if (
file_exists($fileName) && !is_writable($fileName)) { echo "Файл $fileName недоступен для записи"; return; }
        if (!
$handle fopen($fileName'a')) { echo "Не могу открыть файл ($filename)"; return; }
        if (
fwrite($handle$string) === FALSE) { echo "Не могу произвести запись в файл ($filename)"; return; }
        
fclose$handle );
    }
    
    
/**
     * Main function of logger... Testing for log-level, touching log-file, assembling log-string, and send to file
     *
     * @param string $status log-level. one of { D, N, W, E, A }
     * @param string $string user`s comment
     * @global string current module name
     * @global array used for select file to out
     * @global array used for comparing logLevels
     */
    
static function logit$status$string$advanced = array( ) )
    {
    
// get module name
    
global $___n2d$___l2n;
    
$_module getCurrentModuleName( );
    
$_user getCurrentUserInfo( );

    
// type here tests
    
if( !L::isActive( ) || !defined'FB_LOG_FILE' ) ) return;
    if( !
touchFB_LOG_FILE ) ) return;
    if( !
file_existsFB_LOG_FILE ) ) return;
    
    
// next step
    //echo "!isShow( '{$status{0}}' ) = '".(!L::isShow( $status{0} )) . "'";
    
if( !L::isShow$status{0} ) ) return; // log-level higher. not saving
    
$delimiter defined'FB_LOG_DELIM' ) ? FB_LOG_DELIM " | ";
    
    
// prepare
    
$ssid session_id( );
    
//list( $usec, $sec ) = explode( " ", microtime( ) );
    
$_prefix $status{0} . $delimiter .
            
__log_getTime( ) . $delimiter .
            ( isset( 
$ssid )
                ? ( 
session_id( ) .
                    ( isset( 
$_SESSION['reqs'] ) ? "#".str_pad$_SESSION['reqs'], 20STR_PAD_LEFT ) : "" ) .
                    
$delimiter
                
)
                : 
""
            
) .
            
str_pad$_SERVER'REMOTE_ADDR' ], 15' 'STR_PAD_RIGHT ) . $delimiter .
            
str_pad$_SERVER'HTTP_USER_AGENT' ], 25' 'STR_PAD_RIGHT ) . $delimiter .
            ( isset( 
$_module )
                ? (
str_pad$_module15' 'STR_PAD_RIGHT ) . $delimiter) : "" ) .
            ( isset( 
$_user['id'] )
                ? (
str_padjoin". "$_user ), 15' 'STR_PAD_RIGHT ) . $delimiter) : "" );
    
    if( 
is_array$string ) || is_object$string ) )
        
$string L::a2s$string );
    
    
$_logit "";
    foreach( 
explode"\n"$string ) as $_substr )
            
$_logit .= $_prefix $_substr "\n";
    
//$_logit = substr( $_logit, 0, -1 );
    
    // save
        
L::send$_logit );
        unset( 
$_logit );
    }

    static function 
send$string ) {
    if( 
defined'FB_LOG_BUFFERING' ) )
            
$_SESSION'__buf' ] .= $string;
        else
            
L::appendFileFB_LOG_FILE$string );
    }

    static function 
b_start( ) {
    if( !
defined'FB_LOG_BUFFERING' ) )
            
define'FB_LOG_BUFFERING'true );
    
    
L::b_flush( );
        
L::b_clean( );
    }

    static function 
b_clean( ) {
        
$_SESSION'__buf' ] = "";
    }
    
    static function 
b_get_contents( ) {
        if( !isset( 
$_SESSION'__buf' ] ) || empty( $_SESSION'__buf' ] ) )
            return 
"";
        return 
$_SESSION'__buf' ];
    }

    static function 
b_flush( ) {
        if( !isset( 
$_SESSION'__buf' ] ) || empty( $_SESSION'__buf' ] ) )
            return;

        
L::appendFileFB_LOG_FILE$_SESSION'__buf' ] );
        
        
L::b_clean( );
    }

    static function 
b_end( ) {
        
L::b_clean( );
        
define'FB_LOG_BUFFERING' );
    }

    
// fixme: 
    
static function a2s$arr ) { return print_r$arr); }
    
    static function 
debug$string ) { L::logit'D'$string ); }
    static function 
d$string ) { L::debug$string ); }
    
    static function 
notice$string ) { L::logit'N'$string ); }
    static function 
note$string ) { L::notice$string ); }
    static function 
n$string ) { L::notice$string ); }
    
    static function 
warning$string ) { L::logit'W'$string ); }
    static function 
warn$string ) { L::warning$string ); }
    static function 
w$string ) { L::warning$string ); }
    
    static function 
error$string ) { L::logit'E'$string ); }
    static function 
err$string ) { L::error$string ); }
    static function 
e$string ) { L::error$string ); }
    
    static function 
alert$string ) { L::logit'A'$string ); }
    static function 
a$string ) { L::alert$string ); }
    static function 
crit$string ) { L::alert$string ); }
    static function 
c$string ) { L::alert$string ); }


/**
 * function specially for corelogic`s cite-engine
 *
 */


    /**
     * Returns level with most priority
     *
     * @global array used for comparing log-levels
     * @param string $a left-side log-level
     * @param string $b right-side log-level
     * @return string max( a, b )
     */
    
static function lvlMax$a$b ) {
    global 
$___l2n;
    return ( 
$___l2n[$a] > $___l2n[$b] ) ? $a $b;
    }


    
/**
     * Returns level with less priority
     *
     * @global array used for comparing log-levels
     * @param string $a left-side log-level
     * @param string $b right-side log-level
     * @return string min( a, b )
     */
    
static function lvlMin$a$b ) {
    global 
$___l2n;
    return ( 
$___l2n[$a] < $___l2n[$b] ) ? $a $b;
    }


    
/**
     * Returns true, if log-level of message is available for log now, else false
     *
     * @global array used for comparing logLevels
     * @global string current module name
     * @global array list of registered modules
     * @param string $status log-level of called function ( d(), n(), ... )
     */
    
static function isShow$status ) {
    global 
$___l2n$___mods;
    
$_module getCurrentModuleName( );
    
// echo "<pre>{ a, b } <==> { {$___l2n[$status]}, {$___l2n[$___mods[$_module]]} }\n{$___l2n[$status]}&lt;={$___l2n[$___mods[$_module]]} is ".( $___l2n[ $status ] <= $___l2n[ $___mods[ $_module ] ] )."</pre>";
    
return ( $___l2n$status ] <= $___l2n$___mods$_module ] ] ) ? true false;
    }

    
/**
     * Registers module in logger with some logLevel
     *
     * @see L::on
     * @global string current module name
     * @global array list of registered modules
     * @param string $logLevel log-level
     */
    
static function registerModule$logLevel ) {
    
//setup debug for module $module_name, or 'global', if not isset
    
global $___mods;
    
$_module getCurrentModuleName( );
    
$___mods$_module ] = $logLevel;
    }

    
/**
     * Unregister module from logger
     *
     * @see L::off
     * @global string current module name
     * @global array list of registered modules
     */
    
static function unregisterModule( ) {
    
//setup debug for module $module_name, or 'global', if not isset
    
global $module_name$___mods;
    
$_module getCurrentModuleName( );
    
    
$___mods$_module ] = 'A';
    }

    
/**
     * Returns true, if module exist in list of registered
     *
     * @global array used for comparing logLevels
     * @return boolean true, if module is registered
     */
    
static function isActive( ) {
    global 
$___mods;
    return !empty( 
$___mods );
    }
    
    
    
/**
     * redirect to registerModule method
     *
     * @param string $logLevel
     */
    
static function on$logLevel null ) {
    
    if( !isset( 
$logLevel ) || $logLevel == null )
        
$logLevel FB_LOG_LEVEL;
    
        
// register module
    
L::registerModule$logLevel );
    }
    
    
/**
     * redirect to unregisterModule method
     *
     */
    
static function off( ) {
    
    
// unregister module
    
L::unregisterModule( );
    
    }

}



?>