aboutsummaryrefslogtreecommitdiff
path: root/lib/sfIsisImporterLog.class.php
blob: 34594a62f780416db7c6942b1e64b34c4b15e7cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php

/**
 * Logger.
 *
 * @todo $this->caller, $this->section
 */
class sfIsisImporterLog
{
  /**
   * @var object $instance The singleton instance.
   */
  private static $instance = null;

  /**
   * @var object $instance Default loglevel.
   */
  public static $loglevel;

  /**
   * @var int $processed Number of processed entries.
   */
  var $processed = 0;

  /**
   * Get the singleton instance.
   *
   * @param string $loglevel Log level to use
   */
  public static function getInstance($loglevel = null)
  {
    if(self::$instance == null) {
      self::$instance = new self;
    }

    if ($loglevel != null)
    {
      self::$loglevel = $loglevel;
    }

    return self::$instance;
  }

  /**
   * Constructor.
   */
  private function __construct($loglevel = 'info')
  {
    $this->loglevel = $loglevel;
  }

  /**
   * Log dispatcher.
   *
   * @param string $message Log message
   * @param string $level   Log level
   */
  public function log($message, $level = 'info')
  {
    // Available log levels ordered by verbosity.
    $levels = array_flip(array('fatal', 'info', 'warn', 'error', 'debug'));

    // Log level checking.
    if (array_search($level, $levels) === FALSE)
    {
      $this->log("Invalid log level $level.", 'error');
      return;
    }
    elseif ($levels[$level] > $levels[$this->loglevel])
    {
      return;
    }

    // Dispatch.
    if ($this->section == 'action')
    {
      $this->caller->logMessage($message, $level);
    }
    else
    {
      $this->caller->logSection('isisImporter', "[$level] $message");
    }
  }

  /**
   * Progress notifier.
   *
   * @param int $n Row number
   */
  public function progress($n)
  {
    $this->processed = $n;

    if ($this->section == 'action')
    {
      $this->caller->output .= "Saved item $n\n";
    }
    else
    {
      // Progress bar is just shown if loglevel is 'fatal'.
      if ($this->loglevel == 'fatal')
      {
        $this->caller->progressBar($n, $this->entries);
      }
    }

    return $this->processed;
  }
}