<?php
/**
* o------------------------------------------------------------------------------o
* | This package is dual licensed as GPL and a commercial license.               |
* | If you use the code commercially (or if you don't want to be restricted by   |
* | the GPL license), you will need the commercial license. It's only £49 (GBP - |
* | roughly $98 depending on the exchange rate) and helps me out a lot. Thanks.  |
* o------------------------------------------------------------------------------o
*
* © Copyright 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, $level, false, false);

        
$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;
    }
}
?>