<commit_before><?php
// Jivoo
// Copyright (c) 2015 <NAME> (http://nielssp.dk)
// Licensed under the MIT license.
// See the LICENSE file or http://opensource.org/licenses/MIT for more information.
namespace Jivoo\Core\Log;

use Jivoo\Core\Utilities;
use Psr\Log\LogLevel;

/**
 * File log handler.
 */
class FileHandler extends Handler {
  /**
   * @var string
   */
  private $file;
  
  /**
   * @var resource
   */
  private $stream = null;
  
  /**
   * @var bool
   */
  private $useLocking = false;
  
  /**
   * Construct file log handler.
   * @param string $filePath Log file path.
   * @param string $level Minimum log level, see {@see \Psr\Log\LogLevel}.
   * @param bool $useLocking Whether to lock the file before appending to ensure
   * atomicity of each write.
   */
  public function __construct($filePath, $level = LogLevel::DEBUG, $useLocking = false) {
    parent::__construct($level);
    if (!file_exists($filePath)) {
      $dir = dirname($filePath);
      if (!Utilities::dirExists($dir)) {
        trigger_error(tr('Could not create log directory: %1', $dir), E_USER_WARNING);
      }
      if (!touch($filePath)) {
        trigger_error(tr('Could not create log file: %1', $filePath), E_USER_WARNING);
      }
    }
    $this->file = $filePath;
    $this->useLocking = false;
  }
  
  /**
   * {@inheritdoc}
   */
  public function handle(array $record) {
    if ($this->stream === false)
      return;
    if (!isset($this->stream)) {
      $this->stream = fopen($this->file, 'a');
      if (!$this->stream) {
        $this->stream = false;
        trigger_error(tr('Could not open file: %1', $this->file), E_USER_WARNING);
        return;
      }
    }
    if ($this->useLocking)
      flock($this->stream, LOCK_EX);
    
    fwrite($this->stream, self::format($record));

    if ($this->useLocking)
      flock($this->stream, LOCK_UN);
  }
  
  /**
   * {@inheritdoc}
   */
  public function close() {
    if (is_resource($this->stream))<commit_msg>Fix log file handler.<commit_after><?php
// Jivoo
// Copyright (c) 2015 <NAME> (http://nielssp.dk)
// Licensed under the MIT license.
// See the LICENSE file or http://opensource.org/licenses/MIT for more information.
namespace Jivoo\Core\Log;

use Jivoo\Core\Utilities;
use Psr\Log\LogLevel;

/**
 * File log handler.
 */
class FileHandler extends Handler {
  /**
   * @var string
   */
  private $file;
  
  /**
   * @var resource
   */
  private $stream = null;
  
  /**
   * @var bool
   */
  private $useLocking = false;
  
  /**
   * Construct file log handler.
   * @param string $filePath Log file path.
   * @param string $level Minimum log level, see {@see \Psr\Log\LogLevel}.
   * @param bool $useLocking Whether to lock the file before appending to ensure
   * atomicity of each write.
   */
  public function __construct($filePath, $level = LogLevel::DEBUG, $useLocking = false) {
    parent::__construct($level);
    if (!file_exists($filePath)) {
      $dir = dirname($filePath);
      if (!Utilities::dirExists($dir)) {
        trigger_error(tr('Could not create log directory: %1', $dir), E_USER_WARNING);
        $this->stream = false;
        return;
      }
      if (!touch($filePath)) {
        trigger_error(tr('Could not create log file: %1', $filePath), E_USER_WARNING);
        $this->stream = false;
        return;
      }
    }
    $this->file = realpath($filePath);
    $this->useLocking = $useLocking
  }
  
  /**
   * {@inheritdoc}
   */
  public function handle(array $record) {
    if ($this->stream === false)
      return;
    if (!isset($this->stream)) {
      $this->stream = fopen($this->file, 'a');
      if (!$this->stream) {
        $this->stream = false;
        trigger_error(tr('Could not open file: %1', $this->file), E_USER_WARNING);
        return;
      }
    }
    if ($this->useLocking)
      flock($this->stream, LOCK_EX);
    
    fwrite($this->stream, self::format($record));

    if ($this->useLocking)
      flock($this->stream, LOCK_UN);
  }
  
  /**
   * {@inheritdoc}
   */
  public function close() {
    if (is_resource($this->stream))
{'fraction_non_alphanumeric': 0.16024539877300614, 'fraction_numerical': 0.0034355828220858898, 'mean_word_length': 2.6753832281334535, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3349654', 'n_tokens_mistral': 1348, 'n_tokens_neox': 1341, 'n_words': 330}