<?php namespace Majestic\Logger;
/**
 * @copyright NetMonsters <team@netmonsters.ru>
 * @link http://netmonsters.ru
 * @package Majestic
 * @subpackage Logger
 * @since 21-11-2011
 * @user: agrebnev
 */

class FileLogger extends Logger
{

    protected $file_path = '';

    /**
     * @var resource
     */
    protected $handler = null;

    protected function __construct()
    {
        $this->file_path = \Majestic\Config::get('Logger')->filepath;
    }

    protected function generateOutString($message)
    {
        return microtime(true) . "   \t: " . $this->pid . trim($message) . "\r\n";
    }

    protected function concreteLog($message)
    {
        $out = $this->generateOutString($message);

        if (!$this->handler) {
            $this->handler = @fopen($this->file_path, "a");
            if (!$this->handler) {
                throw new \Majestic\Exception\GeneralException('Could not open file ' . $this->file_path);
            }
        }
        fwrite($this->handler, $out);
    }

    public function __destruct()
    {
        if ($this->handler) {
            if (fclose($this->handler)) {
                $this->handler = null;
            }
        }
    }
}