<?php
/**
 * Ce fichier contient ...
 *
 * @package openmairie
 * @link http://www.openmairie.org/
 * @version SVN : $Id$
 */

/**
 *
 */
(defined("PATH_OPENMAIRIE") ? "" : define("PATH_OPENMAIRIE", ""));
require_once PATH_OPENMAIRIE."om_locales.inc.php";
require_once PATH_OPENMAIRIE."om_debug.inc.php";
(defined("DEBUG") ? "" : define("DEBUG", PRODUCTION_MODE));

/**
 *
 */
class logger {

    /**
     *
     */
    private static $_instance;

    /**
     *
     */
    private function __construct() {
    }

    /**
     *
     */
    static function instance() {
        //
        if (!isset(self::$_instance)) {
            $c = __CLASS__;
            self::$_instance = new $c;
        }
        //
        return self::$_instance;
    }

    /**
     * Prevent users to clone the instance
     */
    public function __clone() {
        throw new Exception('Cannot clone the logger object.');
    }

    /**
     *
     */
    public function log($message = "", $type = DEBUG_MODE) {
        //
        $types_to_show = array(DEBUG_MODE => "DEBUG",
                               VERBOSE_MODE => "VERBOSE",
                               EXTRA_VERBOSE_MODE => "EXTRA_VERBOSE");
        //
        if (DEBUG >= $type && ($type == DEBUG_MODE || $type == VERBOSE_MODE || $type == EXTRA_VERBOSE_MODE)) {
            //
            array_push($this->storage, "<span class=\"".strtolower($types_to_show[$type])."\"><span class=\"message\">".$message."</span>&nbsp;<span class=\"type\">".$types_to_show[$type]."</span></span>");
        }
    }

    /**
     *
     */
    var $storage = array();

    /**
     *
     */
    var $display_log = true;
    /**
     *
     */
    function displayLog() {
        //
        if ($this->display_log == true && DEBUG > PRODUCTION_MODE
            && count($this->storage) > 0) {
            //
            echo "\n<div class=\"log-box\">\n";
            //
            echo "<fieldset class=\"cadre ui-widget-content ui-corner-all\">\n";
            //
            echo "<legend class=\"ui-corner-all ui-widget-content ui-state-active\">";
            echo _("Logger");
            echo "</legend>\n";
            //
            echo "<div class=\"even\"><span class=\"url\">".htmlentities($_SERVER['REQUEST_URI'])."</span></div>\n";
            foreach($this->storage as $key => $log) {
                echo "<div class=\"".($key % 2 == 0 ? "odd" : "even")."\">".$log."</div>\n";
            }
            //
            echo "</fieldset>\n";
            //
            echo "</div>\n";
        }
    }

}

?>
