2
0
forked from Wavyzz/dolibarr

Enhance the module debugbar. Do not scan the log anymore.

This commit is contained in:
Laurent Destailleur
2019-03-23 14:37:54 +01:00
parent 05c2ae6778
commit df9e121ac6
7 changed files with 65 additions and 241 deletions

View File

@@ -1,5 +1,5 @@
<?php
/* Copyright (C) 2005-2012 Laurent Destailleur <eldy@users.sourceforge.net>
/* Copyright (C) 2005-2019 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2005-2009 Regis Houssin <regis.houssin@inodbox.com>
* Copyright (C) 2007 Rodolphe Quiedeville <rodolphe@quiedeville.org>
* Copyright (C) 2013 Juanjo Menent <jmenent@2byte.es>

View File

@@ -982,7 +982,7 @@ function dol_strtoupper($utf8_string)
*/
function dol_syslog($message, $level = LOG_INFO, $ident = 0, $suffixinfilename = '', $restricttologhandler = '')
{
global $conf, $user;
global $conf, $user, $debugbar;
// If syslog module enabled
if (empty($conf->syslog->enabled)) return;
@@ -998,8 +998,8 @@ function dol_syslog($message, $level = LOG_INFO, $ident = 0, $suffixinfilename =
if (! empty($message))
{
// Test log level
$logLevels = array(LOG_EMERG, LOG_ALERT, LOG_CRIT, LOG_ERR, LOG_WARNING, LOG_NOTICE, LOG_INFO, LOG_DEBUG);
if (!in_array($level, $logLevels, true))
$logLevels = array(LOG_EMERG=>'EMERG', LOG_ALERT=>'ALERT', LOG_CRIT=>'CRITICAL', LOG_ERR=>'ERR', LOG_WARNING=>'WARN', LOG_NOTICE=>'NOTICE', LOG_INFO=>'INFO', LOG_DEBUG=>'DEBUG');
if (! array_key_exists($level, $logLevels))
{
throw new Exception('Incorrect log level');
}
@@ -1008,9 +1008,10 @@ function dol_syslog($message, $level = LOG_INFO, $ident = 0, $suffixinfilename =
$message = preg_replace('/password=\'[^\']*\'/', 'password=\'hidden\'', $message); // protection to avoid to have value of password in log
// If adding log inside HTML page is required
if (! empty($_REQUEST['logtohtml']) && (! empty($conf->global->MAIN_ENABLE_LOG_TO_HTML) || ! empty($conf->global->MAIN_LOGTOHTML))) // MAIN_LOGTOHTML kept for backward compatibility
if ((! empty($_REQUEST['logtohtml']) && ! empty($conf->global->MAIN_ENABLE_LOG_TO_HTML))
|| (! empty($user->rights->debugbar->read) && is_object($debugbar)))
{
$conf->logbuffer[] = dol_print_date(time(), "%Y-%m-%d %H:%M:%S")." ".$message;
$conf->logbuffer[] = dol_print_date(time(), "%Y-%m-%d %H:%M:%S")." ".$logLevels[$level]." ".$message;
}
//TODO: Remove this. MAIN_ENABLE_LOG_INLINE_HTML should be deprecated and use a log handler dedicated to HTML output
@@ -7164,7 +7165,7 @@ function complete_head_from_modules($conf, $langs, $object, &$head, &$h, $type,
*/
function printCommonFooter($zone = 'private')
{
global $conf, $hookmanager, $user;
global $conf, $hookmanager, $user, $debugbar;
global $action;
global $micro_start_time;
@@ -7331,8 +7332,21 @@ function printCommonFooter($zone = 'private')
print_r(xdebug_get_code_coverage());
}
// If there is some logs in buffer to show
if (count($conf->logbuffer))
// Add DebugBar data
if (! empty($user->rights->debugbar->read) && is_object($debugbar))
{
$debugbar['time']->stopMeasure('pageaftermaster');
/*foreach($conf->logbuffer as $logline)
{
//print $logline."<br>\n";
//$debugbar['log']->addMessage($logline, 'ERR', false);
}*/
print '<!-- Output debugbar data -->'."\n";
print $debugbar->getRenderer()->render();
}
elseif (count($conf->logbuffer)) // If there is some logs in buffer to show
{
print "\n";
print "<!-- Start of log output\n";

View File

@@ -54,18 +54,7 @@ class modDebugBar extends DolibarrModules
$this->const_name = 'MAIN_MODULE_'.strtoupper($this->name);
$this->picto='technic';
$this->module_parts = array(
// Set here all hooks context managed by module. To find available hook context, make a "grep -r '>initHooks(' *" on source code. You can also set hook context 'all'
'hooks' => array(
'data' => array(
'main',
'login',
),
'entity' => '0',
),
// Set this to 1 if feature of module are opened to external users
'moduleforexternal' => 0,
);
$this->module_parts = array('moduleforexternal' => 0);
// Data directories to create when module is enabled
$this->dirs = array();
@@ -80,9 +69,7 @@ class modDebugBar extends DolibarrModules
// Constants
// Example: $this->const=array(0=>array('MYMODULE_MYNEWCONST1','chaine','myvalue','This is a constant to add',0),
// 1=>array('MYMODULE_MYNEWCONST2','chaine','myvalue','This is another constant to add',0) );
$this->const = array(
0 => array('DEBUGBAR_LOGS_LINES_NUMBER', 'chaine', '100', 'Number of log lines to show in debug bar', 1)
);
$this->const = array();
// Boxes
$this->boxes = array();

View File

@@ -17,7 +17,7 @@ class DolLogsCollector extends MessagesCollector
/**
* @var int number of lines to show
*/
protected $lines;
protected $maxnboflines;
/**
* Constructor
@@ -31,8 +31,9 @@ class DolLogsCollector extends MessagesCollector
parent::__construct($name);
$this->path = $path ?: $this->getLogsFile();
$this->lines = empty($conf->global->DEBUGBAR_LOGS_LINES_NUMBER) ? 250 : $conf->global->DEBUGBAR_LOGS_LINES_NUMBER; // This slow seriously output
//$this->path = $path ?: $this->getLogsFile();
$this->nboflines=0;
$this->maxnboflines = empty($conf->global->DEBUGBAR_LOGS_LINES_NUMBER) ? 250 : $conf->global->DEBUGBAR_LOGS_LINES_NUMBER; // High number slows seriously output
}
/**
@@ -68,104 +69,27 @@ class DolLogsCollector extends MessagesCollector
*/
public function collect()
{
$this->getStorageLogs($this->path);
//$this->getStorageLogs($this->path);
global $conf;
//var_dump($conf->logbuffer);
return parent::collect();
}
/**
* Get the path to the logs file
*
* @return string Path of log file
*/
public function getLogsFile()
{
// default dolibarr log file
$path = DOL_DATA_ROOT . '/dolibarr.log';
return $path;
}
/**
* Get logs
*
* @param string $path Path
* @return array
*/
public function getStorageLogs($path)
{
if (! file_exists($path)) {
return;
}
// Load the latest lines
$file = implode("", $this->tailFile($path, $this->lines));
foreach ($this->getLogs($file) as $log) {
$this->addMessage($log['line'], $log['level'], false);
}
}
/**
* Get latest file lines
*
* @param string $file File
* @param int $lines Lines
* @return array Array
*/
protected function tailFile($file, $lines)
{
$handle = fopen($file, "r");
$linecounter = $lines;
$pos = -2;
$beginning = false;
$text = [];
while ($linecounter > 0) {
$t = " ";
while ($t != "\n") {
if (fseek($handle, $pos, SEEK_END) == -1) {
$beginning = true;
break;
}
$t = fgetc($handle);
$pos--;
}
$linecounter--;
if ($beginning) {
rewind($handle);
}
$text[$lines - $linecounter - 1] = fgets($handle);
if ($beginning) {
break;
}
}
fclose($handle);
return array_reverse($text);
}
/**
* Search a string for log entries
*
* @param string $file File
* @return array Lines of logs
*/
public function getLogs($file)
{
$pattern = "/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}.*/";
$log = array();
$log_levels = $this->getLevels();
preg_match_all($pattern, $file, $matches);
$log = [];
foreach ($matches as $lines) {
foreach ($lines as $line) {
foreach ($conf->logbuffer as $line) {
if ($this->nboflines >= $this->maxnboflines)
{
break;
}
foreach ($log_levels as $level_key => $level) {
if (strpos(strtolower($line), strtolower($level_key)) == 20) {
$log[] = ['level' => $level, 'line' => $line];
$this->nboflines++;
$this->addMessage($line, $level, false);
}
}
}
}
$log = array_reverse($log);
return $log;
return parent::collect();
}
/**

View File

@@ -31,12 +31,12 @@ class DolibarrDebugBar extends DebugBar
global $conf;
//$this->addCollector(new PhpInfoCollector());
$this->addCollector(new DolMessagesCollector());
//$this->addCollector(new DolMessagesCollector());
$this->addCollector(new DolRequestDataCollector());
$this->addCollector(new DolConfigCollector());
$this->addCollector(new DolTimeDataCollector());
$this->addCollector(new DolMemoryCollector());
$this->addCollector(new DolExceptionsCollector());
//$this->addCollector(new DolExceptionsCollector());
$this->addCollector(new DolQueryCollector());
$this->addCollector(new DolibarrCollector());
if ($conf->syslog->enabled) {

View File

@@ -1,113 +0,0 @@
<?php
/**
* ActionsDebugBar class
*/
class ActionsDebugBar
{
/**
* Load Debug bar
*
* @return void
*/
protected function loadDebugBar()
{
global $conf, $debugbar;
dol_include_once('/debugbar/class/DebugBar.php');
$debugbar = new DolibarrDebugBar();
$renderer = $debugbar->getRenderer();
$conf->global->MAIN_HTML_HEADER .= $renderer->renderHead();
}
/**
* Overloading the afterLogin function
*
* @param array $parameters Hook metadatas (context, etc...)
* @param CommonObject $object The object to process (an invoice if you are in invoice module, a propale in propale's module, etc...)
* @param string $action Current action (if set). Generally create or edit or null
* @param HookManager $hookmanager Hook manager propagated to allow calling another hook
* @return int < 0 on error, 0 on success, 1 to replace standard code
*/
public function afterLogin($parameters, &$object, &$action, $hookmanager)
{
$error = 0; // Error counter
if (in_array('login', explode(':', $parameters['context'])))
{
$this->loadDebugBar();
}
if (! $error)
{
return 0; // or return 1 to replace standard code
}
else
{
return -1;
}
}
/**
* Overloading the updateSession function
*
* @param array $parameters Hook metadatas (context, etc...)
* @param CommonObject $object The object to process (an invoice if you are in invoice module, a propale in propale's module, etc...)
* @param string $action Current action (if set). Generally create or edit or null
* @param HookManager $hookmanager Hook manager propagated to allow calling another hook
* @return int < 0 on error, 0 on success, 1 to replace standard code
*/
public function updateSession($parameters, &$object, &$action, $hookmanager)
{
$error = 0; // Error counter
if (in_array('main', explode(':', $parameters['context'])))
{
$this->loadDebugBar();
}
if (! $error)
{
return 0; // or return 1 to replace standard code
}
else
{
return -1;
}
}
/**
* Overloading the printCommonFooter function
*
* @param array $parameters Hook metadatas (context, etc...)
* @param CommonObject $object The object to process (an invoice if you are in invoice module, a propale in propale's module, etc...)
* @param string $action Current action (if set). Generally create or edit or null
* @param HookManager $hookmanager Hook manager propagated to allow calling another hook
* @return int <0 on error, 0 on success, 1 to replace standard code
*/
public function printCommonFooter($parameters, &$object, &$action, $hookmanager)
{
global $user, $debugbar, $langs;
$error = 0; // Error counter
$context = explode(':', $parameters['context']);
if (in_array('main', $context) || in_array('login', $context))
{
if ($user->rights->debugbar->read && is_object($debugbar)) {
$renderer = $debugbar->getRenderer();
echo $renderer->render();
}
}
if (! $error)
{
return 0; // or return 1 to replace standard code
}
else
{
return -1;
}
}
}

View File

@@ -194,7 +194,7 @@ if (! empty($_SERVER['DOCUMENT_ROOT']) && substr($_SERVER['DOCUMENT_ROOT'], -6)
set_include_path($_SERVER['DOCUMENT_ROOT'] . '/htdocs');
}
// Include the conf.php and functions.lib.php
// Include the conf.php and functions.lib.php. This defined the constants like DOL_DOCUMENT_ROOT, DOL_DATA_ROOT, DOL_URL_ROOT...
require_once 'filefunc.inc.php';
// If there is a POST parameter to tell to save automatically some POST parameters into cookies, we do it.
@@ -242,12 +242,24 @@ if (! defined('NOSESSION'))
}*/
}
// Init the 5 global objects, this include will make the new and set properties for: $conf, $db, $langs, $user, $mysoc
// Init the 5 global objects, this include will make the 'new Xxx()' and set properties for: $conf, $db, $langs, $user, $mysoc
require_once 'master.inc.php';
// Activate end of page function
register_shutdown_function('dol_shutdown');
// Load debugbar
if (! empty($conf->debugbar->enabled))
{
global $debugbar;
include_once DOL_DOCUMENT_ROOT.'/debugbar/class/DebugBar.php';
$debugbar = new DolibarrDebugBar();
$renderer = $debugbar->getRenderer();
$conf->global->MAIN_HTML_HEADER .= $renderer->renderHead();
$debugbar['time']->startMeasure('pageaftermaster', 'Page generation (after environment init)');
}
// Detection browser
if (isset($_SERVER["HTTP_USER_AGENT"]))
{