8efbb40b568648f5c46cf45ab226ade39571ebdd
[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 OK = 0;
35 const NOTICE = 1;
36 const INFO = 2;
37 const WARNING = 3;
38 const ERROR = 4;
39 const DEBUG = 99;
40
41 /**
42 * @var tx_vcc_service_extensionSettingService|NULL
43 */
44 var $extensionSettingService = NULL;
45
46 /**
47 * @var integer
48 */
49 var $debug = 0;
50
51 /**
52 * @var string
53 */
54 var $hash = '';
55
56 /**
57 * Initialize the object
58 *
59 * @return void
60 */
61 public function __construct() {
62 $extensionSettingService = t3lib_div::makeInstance('tx_vcc_service_extensionSettingService');
63 $this->injectExtensionSettingService($extensionSettingService);
64
65 $configuration = $this->extensionSettingService->getConfiguration();
66 $this->debug = $configuration['debug'];
67
68 $this->hash = md5(uniqid('LoggingService', TRUE));
69 }
70
71 /**
72 * Injects the extension setting service
73 *
74 * @param tx_vcc_service_extensionSettingService $extensionSettingService
75 *
76 * @return void
77 */
78 public function injectExtensionSettingService(tx_vcc_service_extensionSettingService $extensionSettingService) {
79 $this->extensionSettingService = $extensionSettingService;
80 }
81
82 /**
83 * @return void
84 */
85 public function log($message, $logData = array(), $type = self::INFO, $pid = 0, $callerDepth = 2, $caller = NULL) {
86 // Get caller if not already set
87 if ($caller === NULL) {
88 $caller = $this->getCallerFromBugtrace($callerDepth);
89 }
90
91 $insertArray = array(
92 'pid' => $pid,
93 'tstamp' => time(),
94 'be_user' => $GLOBALS['BE_USER']->user['uid'],
95 'type' => $type,
96 'message' => $message,
97 'log_data' => serialize($logData),
98 'caller' => serialize($caller),
99 'hash' => $this->hash
100 );
101 $GLOBALS['TYPO3_DB']->exec_INSERTquery('tx_vcc_log', $insertArray);
102 }
103
104 /**
105 * @return void
106 */
107 public function debug($message, $logData = array(), $pid = 0, $callerDepth = 2, $caller = NULL) {
108 if ($this->debug) {
109 // Adjust callerDepth due to debug function
110 $callerDepth++;
111 $this->log($message, $logData, self::DEBUG, $pid, $callerDepth, $caller);
112 }
113 }
114
115 /**
116 * @param integer $callerDepth
117 *
118 * @return array
119 */
120 protected function getCallerFromBugtrace($callerDepth) {
121 // Get trace array
122 $trace = debug_backtrace();
123
124 // Adjust callerDepth due to separate function
125 $callerDepth++;
126 if (isset($trace[$callerDepth])) {
127 return $trace[$callerDepth];
128 }
129
130 return array();
131 }
132 }
133
134 if (defined('TYPO3_MODE') && $GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/vcc/Classes/Service/LoggingService.php']) {
135 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/vcc/Classes/Service/LoggingService.php']);
136 }
137
138 ?>