<?php
/**
 * @copyright (c) Kufer Software GmbH, Loisachstrasse 4, D-84453 Mühldorf
 */


class DateiLogger
{
  public function __construct()
  {
    $logPath = ConstantOperations::getPathForLog();
    $this->logFilePath = $logPath . 'debug.txt';
  }

  private static $instance;
  private $logFilePath;

  public static function getInstance()
  {
    if (self::$instance == null) {
      self::$instance = new DateiLogger();
    }

    return self::$instance;
  }

  public function getLogFilePath()
  {
    return $this->logFilePath;
  }

  public function setLogFilePath($logFilePath)
  {
    $this->logFilePath = $logFilePath;
  }

  public function log($pMessage, $pSeverity = 0, $showObjectsAndArraysInOneLine = false)
  {
    if ($pSeverity >= Logger::getSeverity()) {
      $logfile = $this->getLogFilePath();
      $handle = @fopen($logfile, 'a');
      if ($handle === false) { 
        return;
      }

      if (is_object($pMessage) || is_array($pMessage)) {
        if ($showObjectsAndArraysInOneLine) {
          $pMessage = DebugOperations::getVarString($pMessage, true);
        } else {
          $pMessage = "\n" . print_r($pMessage, true);
        }
      }

      $backtrace = debug_backtrace();
      $line = @$backtrace[1]['line'];
      $file = @pathinfo(@$backtrace[1]['file'], PATHINFO_BASENAME);
      $class = @$backtrace[2]['class'];
      $function = @$backtrace[2]['function'];
      
      fwrite($handle, date('Y.M.d H:i:s: ') . "($pSeverity) message='$pMessage' file='$file' class='$class' function='$function' line='$line'\r\n");
      fclose($handle);
    }
  }

  
  public static function logg($pMessage, $pSeverity = 0, $showObjectsAndArraysInOneLine = false)
  {
    $dateilogger = self::getInstance();
    $dateilogger->log($pMessage, $pSeverity, $showObjectsAndArraysInOneLine);
  }
}