Code reformatting
[TYPO3CMS/Extensions/vcc.git] / Classes / Service / LoggingService.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2012 Nicole Cordes <cordes@cps-it.de>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25 /**
26 * Service to log requests and responses
27 *
28 * @author Nicole Cordes <cordes@cps-it.de>
29 * @package TYPO3
30 * @subpackage vcc
31 */
32 class tx_vcc_service_loggingService implements t3lib_Singleton {
33
34 const DEBUG = 99;
35
36 const ERROR = 4;
37
38 const INFO = 2;
39
40 const NOTICE = 1;
41
42 const OK = 0;
43
44 const WARNING = 3;
45
46 /**
47 * @var integer
48 */
49 var $debug = 0;
50
51 /**
52 * @var tx_vcc_service_extensionSettingService|NULL
53 */
54 var $extensionSettingService = NULL;
55
56 /**
57 * @var string
58 */
59 var $hash = '';
60
61 /**
62 * Initialize the object
63 *
64 * @return void
65 */
66 public function __construct() {
67 $extensionSettingService = t3lib_div::makeInstance('tx_vcc_service_extensionSettingService');
68 $this->injectExtensionSettingService($extensionSettingService);
69
70 $configuration = $this->extensionSettingService->getConfiguration();
71 $this->debug = $configuration['debug'];
72
73 $this->hash = md5(uniqid('LoggingService', TRUE));
74 }
75
76 /**
77 * Injects the extension setting service
78 *
79 * @param tx_vcc_service_extensionSettingService $extensionSettingService
80 *
81 * @return void
82 */
83 public function injectExtensionSettingService(tx_vcc_service_extensionSettingService $extensionSettingService) {
84 $this->extensionSettingService = $extensionSettingService;
85 }
86
87 /**
88 * @return void
89 */
90 public function debug($message, $logData = array(), $pid = 0, $callerDepth = 2, $caller = NULL) {
91 if ($this->debug) {
92 // Adjust callerDepth due to debug function
93 $callerDepth++;
94 $this->log($message, $logData, self::DEBUG, $pid, $callerDepth, $caller);
95 }
96 }
97
98 /**
99 * @return void
100 */
101 public function log($message, $logData = array(), $type = self::INFO, $pid = 0, $callerDepth = 2, $caller = NULL) {
102 // Get caller if not already set
103 if ($caller === NULL) {
104 $caller = $this->getCallerFromBugtrace($callerDepth);
105 }
106
107 $insertArray = array(
108 'pid' => $pid,
109 'tstamp' => time(),
110 'be_user' => $GLOBALS['BE_USER']->user['uid'],
111 'type' => $type,
112 'message' => $message,
113 'log_data' => serialize($logData),
114 'caller' => serialize($caller),
115 'hash' => $this->hash
116 );
117 $GLOBALS['TYPO3_DB']->exec_INSERTquery('tx_vcc_log', $insertArray);
118 }
119
120 /**
121 * @param integer $callerDepth
122 *
123 * @return array
124 */
125 protected function getCallerFromBugtrace($callerDepth) {
126 // Get trace array
127 $trace = debug_backtrace();
128
129 // Adjust callerDepth due to separate function
130 $callerDepth++;
131 if (isset($trace[$callerDepth])) {
132 return $trace[$callerDepth];
133 }
134
135 return array();
136 }
137 }
138
139 if (defined('TYPO3_MODE') && $GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/vcc/Classes/Service/LoggingService.php']) {
140 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/vcc/Classes/Service/LoggingService.php']);
141 }
142
143 ?>