aboutsummaryrefslogtreecommitdiff
path: root/lib/sfIsisImporterLog.class.php
blob: 4ad79aff16071c02f22f51c356b01aac25487b63 (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php

/**
 * sfIsisImporterLog provides basic logging facilities for import procedures.
 *
 * @todo Support for http://pear.php.net/package/Log
 */
class sfIsisImporterLog
{
  /**
   * @var object $instance The singleton instance
   */
  private static $instance = null;

  /**
   * @var object $loglevel Log level
   */
  public static $loglevel;

  /**
   * @var object $section Log section
   */
  public static $section = false;

  /**
   * @var object $caller Log caller
   */
  public static $caller = false;

  /**
   * @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 = 'info')
  {
    if(self::$instance == null) {
      self::$instance = new self($loglevel);
    }

    return self::$instance;
  }

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

  /**
   * Set the log level.
   *
   * @param string $loglevel Log level to use
   */
  public function setLevel($loglevel)
  {
    $this->loglevel = $loglevel;
  }

  /**
   * Set the log section.
   *
   * @param string $section Log section to use
   */
  public function setSection($section)
  {
    $this->section = $section;
  }

  /**
   * Set the log caller.
   *
   * @param string $caller Log caller to use
   */
  public function setCaller($caller)
  {
    $this->caller = $caller;
  }

  /**
   * Get the available log levels ordered by verbosity.
   *
   * @return array Log levels
   */
  public function levels()
  {
    return array_flip(array('notice', 'fatal', 'info', 'warning', 'error', 'debug'));
  }

  /**
   * Log dispatcher.
   *
   * @param string $message Log message
   * @param string $level   Log level
   */
  public function log($message, $level = 'info')
  {
    // Get the available log levels.
    $levels = $this->levels();

    // 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
    {
      $message = "[$level] $message";

      if ($this->caller)
      {
        $this->caller->logSection('isisImporter', $message);
      }
      else
      {
        echo ("$message\n");
      }
    }
  }

  /**
   * Progress notifier.
   *
   * @param int $entry   Entry number
   * @param int $entries Total number of entries
   */
  public function progress($entry, $entries)
  {
    $this->processed = $entry;
    $levels          = $this->levels();

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

    return $this->processed;
  }
}