<?php
    
/**
    * o------------------------------------------------------------------------------o
    * | This package is licensed under the Phpguru license 2008. A quick summary is  |
    * | that the code is free to use for non-commercial purposes. For commercial     |
    * | purposes of any kind there is a small license fee to pay. You can read more  |
    * | at:                                                                          |
    * |                  http://www.phpguru.org/static/license.html                  |
    * o------------------------------------------------------------------------------o
    *
    * © Copyright 2008 Richard Heyes
    */

/**
* Logging class that easily switch between email, files, and the screen.
* Should work with PHP4, but I've not tried it.
*/

class Log
{
    
/**
    * Log file
    */
    
var $file;

    
/**
    * Constructor
    *
    * @param  $file string The name of the log file
    * @return       bool   Success or not 
    */
    
function __construct($file null$default_email null)
    {
        
// Default log file
        
$this->file $file $file 'log-' date('Y-m-d') . '.txt';
        
        
// Default email
        
if ($default_email) {
            
$this->email $default_email;
        }
    }
    
    
/**
    * PHP4 constructor
    */
    
function Log($file null)
    {
        return 
$this->__constuct($file);
    }

    
/**
    * Builds a log line
    *
    * @param  $msg   string The message to log
    * @param  $level string The level to log. This is purely for indication only
    */
    
function getMessage($msg$level_txt$date true$level true)
    {
        return (
$date date('[Y-m-d H:i:s] ') : ' ') . ($level "{$level_txt} " '') . "{$msg}\r\n";
    }

    
/**
    * Logs a message to the log file
    *
    * @param  $msg string The message to log
    * @return      bool Success or not
    */
    
function toFile($msg$level '[NORMAL]')
    {
        if (
$fp fopen($this->file'a')) {
            return 
fwrite($fp$this->getMessage($msg$level));
        }
        
        return 
false;
    }

    
/**
    * Logs a message to the log file
    *
    * @param  $msg   string The message to log
    * @param  $level string The level to log. This is purely for indication only
    * @return        bool Success or not
    */
    
function toMail ($msg$level '[NORMAL]'$email null)
    {
        
$msg $this->getMessage($msg$levelfalsefalse);

        
$date date('D, j M Y H:i:s O'); // Date: 1 Aug 2007 21:07:50 -0000

        
return mail($email $email $this->email"Log message, level: {$level}"$msg"From: Logging System\r\nDate: " $date);
    }

    
/**
    * Logs a message to the screen. Primarily for debug purposes
    *
    * @param  $msg   string The message to log
    * @param  $level string The level to log. This is purely for indication only
    * @return        bool Success or not
    */
    
function toScreen($msg$level '[NORMAL]'$html false)
    {
        
$line $this->getMessage($msg$level);

        if (
$html) {
            
$line nl2br($line);
        }

        echo 
$line;

        return 
true;
    }
}
?>