User Tools


Differences

This shows you the differences between two versions of the page.

Link to this comparison view

public:paste:logger.php5 [2014-07-10 20:21] – created Johannes Graënpublic:paste:logger.php5 [2023-09-15 20:33] (current) – external edit 127.0.0.1
Line 1: Line 1:
 +<file php>
 +<?php
  
 +class Logger
 +{
 +    private $loglimit = 0;
 +    // some nice colors for 256 color terminals:
 +    private $colors = array(197,199,203,172,114,202,129,94,205,112,194,46);
 +    private $counter = 0;
 +
 +    public function __construct($loglimit)
 +    {
 +        $this->loglimit = $loglimit;
 +    }
 +
 +    // print colored debug information
 +    public function message($section='_',$loglevel=0,$message,$color=null)
 +    {
 +        if($loglevel <= $this->loglimit)
 +        {
 +            $color = current(array_filter(array($color,$this->colors[$loglevel%count($this->colors)])));
 +            printf("\x1b[33m%s%d%'-8d\x1b[0m\t%s\x1b[38;5;%dm%s\x1b[0m\n",$section,$loglevel,
 +                ++$this->counter,str_repeat('   ',$loglevel),$color,$message);
 +        }
 +    }
 +
 +    // highlight a string (by inverting colors)
 +    public function highlight($s)
 +    {
 +        return "\x1b[7m".$s."\x1b[27m";
 +    }
 +
 +    // underline a string
 +    public function underline($s)
 +    {
 +        return "\x1b[4m".$s."\x1b[24m";
 +    }
 +}
 +
 +?>
 +</file>

CL Wiki

Institute of Computational Linguistics – University of Zurich