]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/protected/Class/Logging.php
Add Baculum
[bacula/bacula] / gui / baculum / protected / Class / Logging.php
1 <?php
2 /**
3  * Bacula® - The Network Backup Solution
4  * Baculum - Bacula web interface
5  *
6  * Copyright (C) 2013-2014 Marcin Haba
7  *
8  * The main author of Baculum is Marcin Haba.
9  * The main author of Bacula is Kern Sibbald, with contributions from many
10  * others, a complete list can be found in the file AUTHORS.
11  *
12  * You may use this file and others of this release according to the
13  * license defined in the LICENSE file, which includes the Affero General
14  * Public License, v3.0 ("AGPLv3") and some additional permissions and
15  * terms pursuant to its AGPLv3 Section 7.
16  *
17  * Bacula® is a registered trademark of Kern Sibbald.
18  */
19
20 class Logging extends TModule {
21
22         private $debugEnabled = false;
23
24         const CATEGORY_EXECUTE = 'Execute';
25         const CATEGORY_EXTERNAL = 'External';
26         const CATEGORY_APPLICATION = 'Application';
27         const CATEGORY_GENERAL = 'General';
28         const CATEGORY_SECURITY = 'Security';
29
30         public function init($config) {
31                 $this->debugEnabled = $this->isDebugOn();
32         }
33
34         public function isDebugOn() {
35                 $isDebug = false;
36                 $cfgModule = $this->Application->getModule('configuration');
37                 if($cfgModule->isApplicationConfig() === true) {
38                         $config = $cfgModule->getApplicationConfig();
39                         $isDebug = (isset($config['baculum']['debug']) && intval($config['baculum']['debug']) === 1);
40                 } else {
41                         $isDebug = true;
42                 }
43                 return $isDebug;
44         }
45
46         public function enableDebug($enable) {
47                 $result = false;
48                 $cfgModule = $this->Application->getModule('configuration');
49                 if($cfgModule->isApplicationConfig() === true) {
50                         $config = $cfgModule->getApplicationConfig();
51                         $config['baculum']['debug'] = ($enable === true) ? "1" : "0";
52                         $result = $cfgModule->setApplicationConfig($config);
53                 }
54                 return $result;
55         }
56
57         private function getLogCategories() {
58                 $categories = array(
59                         self::CATEGORY_EXECUTE,
60                         self::CATEGORY_EXTERNAL,
61                         self::CATEGORY_APPLICATION,
62                         self::CATEGORY_GENERAL,
63                         self::CATEGORY_SECURITY
64                 );
65                 return $categories;
66         }
67
68         public function log($cmd, $output, $category, $file, $line) {
69                 if($this->debugEnabled !== true) {
70                         return;
71                 }
72
73                 if(!in_array($category, $this->getLogCategories())) {
74                         $category = self::CATEGORY_SECURITY;
75                 }
76
77                 $log = sprintf(
78                         'Command=%s, Output=%s, File=%s, Line=%d',
79                         $cmd,
80                         print_r($output, true),
81                         $file,
82                         intval($line)
83                 );
84
85                 Prado::trace($log, $category);
86         }
87 }
88
89 ?>