Remove debug globals (#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
This commit is contained in:
Tony Murray
2021-04-29 22:42:18 -05:00
committed by GitHub
parent 2cdd762656
commit 61c89794e4
51 changed files with 287 additions and 278 deletions

137
LibreNMS/Util/Debug.php Normal file
View File

@@ -0,0 +1,137 @@
<?php
/*
* Debug.php
*
* -Description-
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package LibreNMS
* @link http://librenms.org
* @copyright 2021 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/
namespace LibreNMS\Util;
use App;
use Illuminate\Database\Events\QueryExecuted;
use LibreNMS\DB\Eloquent;
use Log;
class Debug
{
private static $debug = false;
private static $verbose = false;
public static function set($debug = true, bool $silence = false): bool
{
self::$debug = (bool) $debug;
restore_error_handler(); // disable Laravel error handler
if (self::$debug) {
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
ini_set('log_errors', '0');
error_reporting(E_ALL & ~E_NOTICE);
self::enableCliDebugOutput();
self::enableQueryDebug();
} else {
ini_set('display_errors', '0');
ini_set('display_startup_errors', '0');
ini_set('log_errors', '1');
error_reporting($silence ? 0 : E_ERROR);
self::disableCliDebugOutput();
self::disableQueryDebug();
}
return self::$debug;
}
/**
* Set debug without configuring error reporting.
*/
public static function setOnly(bool $debug = true): bool
{
return self::$debug = $debug;
}
public static function setVerbose(bool $verbose = true): void
{
self::$verbose = $verbose;
}
public static function isEnabled(): bool
{
return self::$debug;
}
public static function isVerbose(): bool
{
return self::$verbose;
}
public static function disableQueryDebug()
{
$db = Eloquent::DB();
if ($db) {
// remove all query executed event handlers
$db->getEventDispatcher()->flush('Illuminate\Database\Events\QueryExecuted');
}
}
public static function enableCliDebugOutput()
{
if (Laravel::isBooted() && App::runningInConsole()) {
Log::setDefaultDriver('console');
}
}
public static function disableCliDebugOutput()
{
if (Laravel::isBooted()) {
Log::setDefaultDriver('stack');
}
}
public static function enableQueryDebug()
{
static $sql_debug_enabled;
$db = Eloquent::DB();
if ($db && ! $sql_debug_enabled) {
$db->listen(function (QueryExecuted $query) {
// collect bindings and make them a little more readable
$bindings = collect($query->bindings)->map(function ($item) {
if ($item instanceof \Carbon\Carbon) {
return $item->toDateTimeString();
}
return $item;
})->toJson();
if (Laravel::isBooted()) {
Log::debug("SQL[%Y{$query->sql} %y$bindings%n {$query->time}ms] \n", ['color' => true]);
} else {
c_echo("SQL[%Y{$query->sql} %y$bindings%n {$query->time}ms] \n");
}
});
$sql_debug_enabled = true;
}
}
}

View File

@@ -24,10 +24,6 @@
namespace LibreNMS\Util;
use App;
use Illuminate\Database\Events\QueryExecuted;
use LibreNMS\DB\Eloquent;
use Log;
use Symfony\Component\HttpFoundation\HeaderBag;
class Laravel
@@ -77,56 +73,6 @@ class Laravel
return function_exists('app') && ! empty(app()->isAlias('Illuminate\Foundation\Application')) && app()->isBooted();
}
public static function enableQueryDebug()
{
static $sql_debug_enabled;
$db = Eloquent::DB();
if ($db && ! $sql_debug_enabled) {
$db->listen(function (QueryExecuted $query) {
// collect bindings and make them a little more readable
$bindings = collect($query->bindings)->map(function ($item) {
if ($item instanceof \Carbon\Carbon) {
return $item->toDateTimeString();
}
return $item;
})->toJson();
if (self::isBooted()) {
Log::debug("SQL[%Y{$query->sql} %y$bindings%n {$query->time}ms] \n", ['color' => true]);
} else {
c_echo("SQL[%Y{$query->sql} %y$bindings%n {$query->time}ms] \n");
}
});
$sql_debug_enabled = true;
}
}
public static function disableQueryDebug()
{
$db = Eloquent::DB();
if ($db) {
// remove all query executed event handlers
$db->getEventDispatcher()->flush('Illuminate\Database\Events\QueryExecuted');
}
}
public static function enableCliDebugOutput()
{
if (self::isBooted() && App::runningInConsole()) {
Log::setDefaultDriver('console');
}
}
public static function disableCliDebugOutput()
{
if (self::isBooted()) {
Log::setDefaultDriver('stack');
}
}
/**
* Add prefix and strip .php to make the url helper work in non-laravel php scripts
*

View File

@@ -181,21 +181,21 @@ class ModuleTestHelper
private function collectOids($device_id)
{
global $debug, $vdebug, $device;
global $device;
$device = device_by_id_cache($device_id);
DeviceCache::setPrimary($device_id);
// Run discovery
ob_start();
$save_debug = $debug;
$save_vedbug = $vdebug;
$debug = true;
$vdebug = false;
$save_debug = Debug::isEnabled();
$save_vedbug = Debug::isEnabled();
Debug::set();
Debug::setVerbose();
discover_device($device, $this->parseArgs('discovery'));
poll_device($device, $this->parseArgs('poller'));
$debug = $save_debug;
$vdebug = $save_vedbug;
Debug::set($save_debug);
Debug::setVerbose($save_vedbug);
$collection_output = ob_get_contents();
ob_end_clean();
@@ -518,7 +518,7 @@ class ModuleTestHelper
*/
public function generateTestData(Snmpsim $snmpsim, $no_save = false)
{
global $device, $debug, $vdebug;
global $device;
Config::set('rrd.enable', false); // disable rrd
if (! is_file($this->snmprec_file)) {
@@ -552,11 +552,11 @@ class ModuleTestHelper
$data = []; // array to hold dumped data
// Run discovery
$save_debug = $debug;
$save_vedbug = $vdebug;
$save_debug = Debug::isEnabled();
$save_vedbug = Debug::isVerbose();
if ($this->quiet) {
$debug = true;
$vdebug = true;
Debug::setOnly();
Debug::setVerbose();
}
ob_start();
@@ -564,8 +564,8 @@ class ModuleTestHelper
$this->discovery_output = ob_get_contents();
if ($this->quiet) {
$debug = $save_debug;
$vdebug = $save_vedbug;
Debug::setOnly($save_debug);
Debug::setVerbose($save_vedbug);
} else {
ob_flush();
}
@@ -583,8 +583,8 @@ class ModuleTestHelper
// Run the poller
if ($this->quiet) {
$debug = true;
$vdebug = true;
Debug::setOnly();
Debug::setVerbose();
}
ob_start();
@@ -592,8 +592,8 @@ class ModuleTestHelper
$this->poller_output = ob_get_contents();
if ($this->quiet) {
$debug = $save_debug;
$vdebug = $save_vedbug;
Debug::setOnly($save_debug);
Debug::setVerbose($save_vedbug);
} else {
ob_flush();
}
@@ -608,8 +608,7 @@ class ModuleTestHelper
// Remove the test device, we don't need the debug from this
if ($device['hostname'] == $snmpsim->getIp()) {
global $debug;
$debug = false;
Debug::set(false);
delete_device($device['device_id']);
}