Skip to content

Commit 61c8979

Browse files
authored
Remove debug globals (librenms#12811)
* Remove $debug global and $vdebug global makes these variables more accessible and protects from collisions. * the on boot set sends application as the first parameter, just handle that * Relocate other debug related functions * Log debug to stdout * Wrong output * remove stupid constants * Fix lint and style issues
1 parent 2cdd762 commit 61c8979

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+287
-278
lines changed

LibreNMS/Authentication/RadiusAuthorizer.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Dapphp\Radius\Radius;
66
use LibreNMS\Config;
77
use LibreNMS\Exceptions\AuthenticationException;
8+
use LibreNMS\Util\Debug;
89

910
class RadiusAuthorizer extends MysqlAuthorizer
1011
{
@@ -22,13 +23,11 @@ public function __construct()
2223

2324
public function authenticate($credentials)
2425
{
25-
global $debug;
26-
2726
if (empty($credentials['username'])) {
2827
throw new AuthenticationException('Username is required');
2928
}
3029

31-
if ($debug) {
30+
if (Debug::isEnabled()) {
3231
$this->radius->setDebug(true);
3332
}
3433

LibreNMS/Config.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use Illuminate\Support\Arr;
3131
use Illuminate\Support\Str;
3232
use LibreNMS\DB\Eloquent;
33+
use LibreNMS\Util\Debug;
3334
use Log;
3435

3536
class Config
@@ -259,8 +260,7 @@ public static function persist($key, $value)
259260
if (class_exists(Log::class)) {
260261
Log::error($e);
261262
}
262-
global $debug;
263-
if ($debug) {
263+
if (Debug::isEnabled()) {
264264
echo $e;
265265
}
266266

@@ -485,8 +485,7 @@ private static function setDefault($key, $value, $format_values = [])
485485
private static function deprecatedVariable($old, $new)
486486
{
487487
if (self::has($old)) {
488-
global $debug;
489-
if ($debug) {
488+
if (Debug::isEnabled()) {
490489
echo "Copied deprecated config $old to $new\n";
491490
}
492491
self::set($new, self::get($old));

LibreNMS/Data/Store/Rrd.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use LibreNMS\Data\Measure\Measurement;
3030
use LibreNMS\Exceptions\FileExistsException;
3131
use LibreNMS\Proc;
32+
use LibreNMS\Util\Debug;
3233
use LibreNMS\Util\Rewrite;
3334
use Log;
3435

@@ -356,7 +357,6 @@ public function dirFromHost($host)
356357
*/
357358
private function command($command, $filename, $options)
358359
{
359-
global $vdebug;
360360
$stat = Measurement::start($this->coalesceStatisticType($command));
361361
$output = null;
362362

@@ -391,7 +391,7 @@ private function command($command, $filename, $options)
391391
Log::error('rrdtool could not start');
392392
}
393393

394-
if ($vdebug) {
394+
if (Debug::isVerbose()) {
395395
echo 'RRDtool Output: ';
396396
echo $output[0];
397397
echo $output[1];

LibreNMS/Util/Debug.php

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<?php
2+
/*
3+
* Debug.php
4+
*
5+
* -Description-
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*
20+
* @package LibreNMS
21+
* @link http://librenms.org
22+
* @copyright 2021 Tony Murray
23+
* @author Tony Murray <[email protected]>
24+
*/
25+
26+
namespace LibreNMS\Util;
27+
28+
use App;
29+
use Illuminate\Database\Events\QueryExecuted;
30+
use LibreNMS\DB\Eloquent;
31+
use Log;
32+
33+
class Debug
34+
{
35+
private static $debug = false;
36+
private static $verbose = false;
37+
38+
public static function set($debug = true, bool $silence = false): bool
39+
{
40+
self::$debug = (bool) $debug;
41+
42+
restore_error_handler(); // disable Laravel error handler
43+
44+
if (self::$debug) {
45+
ini_set('display_errors', '1');
46+
ini_set('display_startup_errors', '1');
47+
ini_set('log_errors', '0');
48+
error_reporting(E_ALL & ~E_NOTICE);
49+
50+
self::enableCliDebugOutput();
51+
self::enableQueryDebug();
52+
} else {
53+
ini_set('display_errors', '0');
54+
ini_set('display_startup_errors', '0');
55+
ini_set('log_errors', '1');
56+
error_reporting($silence ? 0 : E_ERROR);
57+
58+
self::disableCliDebugOutput();
59+
self::disableQueryDebug();
60+
}
61+
62+
return self::$debug;
63+
}
64+
65+
/**
66+
* Set debug without configuring error reporting.
67+
*/
68+
public static function setOnly(bool $debug = true): bool
69+
{
70+
return self::$debug = $debug;
71+
}
72+
73+
public static function setVerbose(bool $verbose = true): void
74+
{
75+
self::$verbose = $verbose;
76+
}
77+
78+
public static function isEnabled(): bool
79+
{
80+
return self::$debug;
81+
}
82+
83+
public static function isVerbose(): bool
84+
{
85+
return self::$verbose;
86+
}
87+
88+
public static function disableQueryDebug()
89+
{
90+
$db = Eloquent::DB();
91+
92+
if ($db) {
93+
// remove all query executed event handlers
94+
$db->getEventDispatcher()->flush('Illuminate\Database\Events\QueryExecuted');
95+
}
96+
}
97+
98+
public static function enableCliDebugOutput()
99+
{
100+
if (Laravel::isBooted() && App::runningInConsole()) {
101+
Log::setDefaultDriver('console');
102+
}
103+
}
104+
105+
public static function disableCliDebugOutput()
106+
{
107+
if (Laravel::isBooted()) {
108+
Log::setDefaultDriver('stack');
109+
}
110+
}
111+
112+
public static function enableQueryDebug()
113+
{
114+
static $sql_debug_enabled;
115+
$db = Eloquent::DB();
116+
117+
if ($db && ! $sql_debug_enabled) {
118+
$db->listen(function (QueryExecuted $query) {
119+
// collect bindings and make them a little more readable
120+
$bindings = collect($query->bindings)->map(function ($item) {
121+
if ($item instanceof \Carbon\Carbon) {
122+
return $item->toDateTimeString();
123+
}
124+
125+
return $item;
126+
})->toJson();
127+
128+
if (Laravel::isBooted()) {
129+
Log::debug("SQL[%Y{$query->sql} %y$bindings%n {$query->time}ms] \n", ['color' => true]);
130+
} else {
131+
c_echo("SQL[%Y{$query->sql} %y$bindings%n {$query->time}ms] \n");
132+
}
133+
});
134+
$sql_debug_enabled = true;
135+
}
136+
}
137+
}

LibreNMS/Util/Laravel.php

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@
2424

2525
namespace LibreNMS\Util;
2626

27-
use App;
28-
use Illuminate\Database\Events\QueryExecuted;
29-
use LibreNMS\DB\Eloquent;
30-
use Log;
3127
use Symfony\Component\HttpFoundation\HeaderBag;
3228

3329
class Laravel
@@ -77,56 +73,6 @@ public static function isBooted()
7773
return function_exists('app') && ! empty(app()->isAlias('Illuminate\Foundation\Application')) && app()->isBooted();
7874
}
7975

80-
public static function enableQueryDebug()
81-
{
82-
static $sql_debug_enabled;
83-
$db = Eloquent::DB();
84-
85-
if ($db && ! $sql_debug_enabled) {
86-
$db->listen(function (QueryExecuted $query) {
87-
// collect bindings and make them a little more readable
88-
$bindings = collect($query->bindings)->map(function ($item) {
89-
if ($item instanceof \Carbon\Carbon) {
90-
return $item->toDateTimeString();
91-
}
92-
93-
return $item;
94-
})->toJson();
95-
96-
if (self::isBooted()) {
97-
Log::debug("SQL[%Y{$query->sql} %y$bindings%n {$query->time}ms] \n", ['color' => true]);
98-
} else {
99-
c_echo("SQL[%Y{$query->sql} %y$bindings%n {$query->time}ms] \n");
100-
}
101-
});
102-
$sql_debug_enabled = true;
103-
}
104-
}
105-
106-
public static function disableQueryDebug()
107-
{
108-
$db = Eloquent::DB();
109-
110-
if ($db) {
111-
// remove all query executed event handlers
112-
$db->getEventDispatcher()->flush('Illuminate\Database\Events\QueryExecuted');
113-
}
114-
}
115-
116-
public static function enableCliDebugOutput()
117-
{
118-
if (self::isBooted() && App::runningInConsole()) {
119-
Log::setDefaultDriver('console');
120-
}
121-
}
122-
123-
public static function disableCliDebugOutput()
124-
{
125-
if (self::isBooted()) {
126-
Log::setDefaultDriver('stack');
127-
}
128-
}
129-
13076
/**
13177
* Add prefix and strip .php to make the url helper work in non-laravel php scripts
13278
*

LibreNMS/Util/ModuleTestHelper.php

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -181,21 +181,21 @@ public function captureFromDevice($device_id, $write = true, $prefer_new = false
181181

182182
private function collectOids($device_id)
183183
{
184-
global $debug, $vdebug, $device;
184+
global $device;
185185

186186
$device = device_by_id_cache($device_id);
187187
DeviceCache::setPrimary($device_id);
188188

189189
// Run discovery
190190
ob_start();
191-
$save_debug = $debug;
192-
$save_vedbug = $vdebug;
193-
$debug = true;
194-
$vdebug = false;
191+
$save_debug = Debug::isEnabled();
192+
$save_vedbug = Debug::isEnabled();
193+
Debug::set();
194+
Debug::setVerbose();
195195
discover_device($device, $this->parseArgs('discovery'));
196196
poll_device($device, $this->parseArgs('poller'));
197-
$debug = $save_debug;
198-
$vdebug = $save_vedbug;
197+
Debug::set($save_debug);
198+
Debug::setVerbose($save_vedbug);
199199
$collection_output = ob_get_contents();
200200
ob_end_clean();
201201

@@ -518,7 +518,7 @@ private function cleanSnmprecData(&$data)
518518
*/
519519
public function generateTestData(Snmpsim $snmpsim, $no_save = false)
520520
{
521-
global $device, $debug, $vdebug;
521+
global $device;
522522
Config::set('rrd.enable', false); // disable rrd
523523

524524
if (! is_file($this->snmprec_file)) {
@@ -552,20 +552,20 @@ public function generateTestData(Snmpsim $snmpsim, $no_save = false)
552552
$data = []; // array to hold dumped data
553553

554554
// Run discovery
555-
$save_debug = $debug;
556-
$save_vedbug = $vdebug;
555+
$save_debug = Debug::isEnabled();
556+
$save_vedbug = Debug::isVerbose();
557557
if ($this->quiet) {
558-
$debug = true;
559-
$vdebug = true;
558+
Debug::setOnly();
559+
Debug::setVerbose();
560560
}
561561
ob_start();
562562

563563
discover_device($device, $this->parseArgs('discovery'));
564564

565565
$this->discovery_output = ob_get_contents();
566566
if ($this->quiet) {
567-
$debug = $save_debug;
568-
$vdebug = $save_vedbug;
567+
Debug::setOnly($save_debug);
568+
Debug::setVerbose($save_vedbug);
569569
} else {
570570
ob_flush();
571571
}
@@ -583,17 +583,17 @@ public function generateTestData(Snmpsim $snmpsim, $no_save = false)
583583

584584
// Run the poller
585585
if ($this->quiet) {
586-
$debug = true;
587-
$vdebug = true;
586+
Debug::setOnly();
587+
Debug::setVerbose();
588588
}
589589
ob_start();
590590

591591
poll_device($device, $this->parseArgs('poller'));
592592

593593
$this->poller_output = ob_get_contents();
594594
if ($this->quiet) {
595-
$debug = $save_debug;
596-
$vdebug = $save_vedbug;
595+
Debug::setOnly($save_debug);
596+
Debug::setVerbose($save_vedbug);
597597
} else {
598598
ob_flush();
599599
}
@@ -608,8 +608,7 @@ public function generateTestData(Snmpsim $snmpsim, $no_save = false)
608608

609609
// Remove the test device, we don't need the debug from this
610610
if ($device['hostname'] == $snmpsim->getIp()) {
611-
global $debug;
612-
$debug = false;
611+
Debug::set(false);
613612
delete_device($device['device_id']);
614613
}
615614

alerts.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,14 @@
2828
*/
2929

3030
use LibreNMS\Alert\RunAlerts;
31+
use LibreNMS\Util\Debug;
3132

3233
$init_modules = ['alerts', 'laravel'];
3334
require __DIR__ . '/includes/init.php';
3435

3536
$options = getopt('d::');
3637

37-
if (set_debug(isset($options['d']))) {
38+
if (Debug::set(isset($options['d']))) {
3839
echo "DEBUG!\n";
3940
}
4041

0 commit comments

Comments
 (0)