All Data Structures Functions Variables Pages
MessageLogger.php
1 <?php
2  /*********************************************************************************
3  * Zurmo is a customer relationship management program developed by
4  * Zurmo, Inc. Copyright (C) 2017 Zurmo Inc.
5  *
6  * Zurmo is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License version 3 as published by the
8  * Free Software Foundation with the addition of the following permission added
9  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10  * IN WHICH THE COPYRIGHT IS OWNED BY ZURMO, ZURMO DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  *
13  * Zurmo is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Affero General Public License along with
19  * this program; if not, see http://www.gnu.org/licenses or write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301 USA.
22  *
23  * You can contact Zurmo, Inc. with a mailing address at 27 North Wacker Drive
24  * Suite 370 Chicago, IL 60606. or at email address contact@zurmo.com.
25  *
26  * The interactive user interfaces in original and modified versions
27  * of this program must display Appropriate Legal Notices, as required under
28  * Section 5 of the GNU Affero General Public License version 3.
29  *
30  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31  * these Appropriate Legal Notices must retain the display of the Zurmo
32  * logo and Zurmo copyright notice. If the display of the logo is not reasonably
33  * feasible for technical reasons, the Appropriate Legal Notices must display the words
34  * "Copyright Zurmo Inc. 2017. All rights reserved".
35  ********************************************************************************/
36 
41  {
45  const ERROR = 1;
46 
50  const INFO = 2;
51 
55  const DEBUG = 3;
56 
60  public $logDateTimeStamp = true;
61 
65  protected $errorMessagePresent = false;
66 
70  protected $messages = array();
71 
75  protected $messageStreamer;
76 
77  protected $messageStreamers = array();
78 
85  public function __construct($messageStreamer = null)
86  {
87  assert('$messageStreamer == null || $messageStreamer instanceof MessageStreamer || $messageStreamer instanceof JobManagerFileLogRouteMessageStreamer || is_array($messageStreamer)');
88  $messageStreamers = array();
89  if ($messageStreamer instanceof MessageStreamer || $messageStreamer instanceof JobManagerFileLogRouteMessageStreamer)
90  {
91  $messageStreamers[] = $messageStreamer;
92  }
93  elseif (is_array($messageStreamer) && !empty($messageStreamer))
94  {
95  foreach ($messageStreamer as $messageStreamerItem)
96  {
97  assert('$messageStreamerItem instanceof MessageStreamer || $messageStreamerItem instanceof JobManagerFileLogRouteMessageStreamer');
98  $messageStreamers[] = $messageStreamerItem;
99  }
100  }
101  $this->messageStreamers = $messageStreamers;
102  }
103 
108  public function addInfoMessage($message)
109  {
110  $this->add(array(MessageLogger::INFO, $message, static::getFormattedDateTimeStampForNow()));
111  }
112 
117  public function addErrorMessage($message)
118  {
119  $this->errorMessagePresent = true;
120  $this->add(array(MessageLogger::ERROR, $message, static::getFormattedDateTimeStampForNow()));
121  }
122 
127  public function addDebugMessage($message)
128  {
129  $this->add(array(MessageLogger::DEBUG, $message, static::getFormattedDateTimeStampForNow()));
130  }
131 
132  protected function add($message)
133  {
134  assert('is_array($message)');
135  $this->messages[] = $message;
136  if (!empty($this->messageStreamers))
137  {
138  if ($message[0] != MessageLogger::DEBUG ||
139  ($this->shouldPrintDebugMessages() && $message[0] == MessageLogger::DEBUG))
140  {
141  foreach ($this->messageStreamers as $messageStreamer)
142  {
143  if ($this->logDateTimeStamp)
144  {
145  $prefixContent = $message[2] . ' ';
146  }
147  else
148  {
149  $prefixContent = null;
150  }
151  $messageStreamer->add($prefixContent . static::getTypeLabel($message[0]) . ' - ' . $message[1]);
152  }
153  }
154  }
155  }
156 
157  public function getMessages()
158  {
159  return $this->messages;
160  }
161 
167  public function printMessages($return = false, $errorOnly = false)
168  {
169  $content = '';
170  foreach ($this->messages as $messageInfo)
171  {
172  if (!$errorOnly || ($errorOnly && $messageInfo[0] == MessageLogger::ERROR))
173  {
174  if ($messageInfo[0] != MessageLogger::DEBUG ||
175  ($this->shouldPrintDebugMessages() && $messageInfo[0] == MessageLogger::DEBUG))
176  {
177  $content .= static::getTypeLabel($messageInfo[0]) . ' - ' . $messageInfo[1] . "\n";
178  }
179  }
180  }
181  if ($return)
182  {
183  return $content;
184  }
185  echo $content;
186  }
187 
192  public static function getTypeLabel($type)
193  {
194  assert('$type == MessageLogger::ERROR || $type == MessageLogger::INFO || $type == MessageLogger::DEBUG');
195  if ($type == MessageLogger::ERROR)
196  {
197  return str_pad(Zurmo::t('Core', 'Error'), 5);
198  }
199  elseif ($type == MessageLogger::INFO)
200  {
201  return str_pad(Zurmo::t('Core', 'Info'), 5);
202  }
203  else
204  {
205  return str_pad(Zurmo::t('Core', 'Debug'), 5);
206  }
207  }
208 
212  public function isErrorMessagePresent()
213  {
214  return $this->errorMessagePresent;
215  }
216 
220  protected function shouldPrintDebugMessages()
221  {
222  if (YII_DEBUG)
223  {
224  return true;
225  }
226  return false;
227  }
228 
233  protected static function getFormattedDateTimeStampForNow()
234  {
235  return @date('Y/m/d H:i:s', microtime(true));
236  }
237  }
238 ?>
addErrorMessage($message)
static getTypeLabel($type)
__construct($messageStreamer=null)
addDebugMessage($message)
addInfoMessage($message)
static getFormattedDateTimeStampForNow()
printMessages($return=false, $errorOnly=false)
Generated on Sat Mar 28 2020 07:10:38