This commit is contained in:
2024-05-20 15:37:46 +03:00
commit 00b7dbd0b7
10404 changed files with 3285853 additions and 0 deletions

View File

@ -0,0 +1,18 @@
{
"name": "shortpixel/log",
"description": "ShortPixel Logging",
"version": "1.1.3",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Bas",
"email": "bas@weblogmechanic.com"
}
],
"minimum-stability": "dev",
"require": {},
"autoload": {
"psr-4": { "ShortPixel\\ShortPixelLogger\\" : "src" }
}
}

View File

@ -0,0 +1,153 @@
<?php
// The data models.
namespace ShortPixel\ShortPixelLogger;
class DebugItem
{
protected $time;
protected $level;
protected $message;
protected $data = array();
protected $caller = false; // array when filled
protected $model;
const LEVEL_ERROR = 1;
const LEVEL_WARN = 2;
const LEVEL_INFO = 3;
const LEVEL_DEBUG = 4;
public function __construct($message, $args)
{
$this->level = $args['level'];
$data = $args['data'];
$this->message = $message;
$this->time = microtime(true);
$this->setCaller();
// Add message to data if it seems to be some debug variable.
if (is_object($this->message) || is_array($this->message))
{
$data[] = $this->message;
$this->message = __('[Data]');
}
if (is_array($data) && count($data) > 0)
{
$dataType = $this->getDataType($data);
if ($dataType == 1) // singular
{
$this->data[] = print_r($data, true);
}
if ($dataType == 2) //array or object.
{
$count = false;
if (gettype($data) == 'array')
$count = count($data);
elseif(gettype($data) == 'object')
$count = count(get_object_vars($data));
$firstLine = ucfirst(gettype($data)) . ':';
if ($count !== false)
$firstLine .= ' (' . $count . ')';
$this->data[] = $firstLine;
foreach($data as $index => $item)
{
if (is_object($item) || is_array($item))
{
$this->data[] = print_r($index, true) . ' ( ' . ucfirst(gettype($item)) . ') => ' . print_r($item, true);
}
}
}
} // if
elseif (! is_array($data)) // this leaves out empty default arrays
{
$this->data[] = print_r($data, true);
}
}
public function getData()
{
return array('time' => $this->time, 'level' => $this->level, 'message' => $this->message, 'data' => $this->data, 'caller' => $this->caller);
}
/** Test Data Array for possible values
*
* Data can be a collection of several debug vars, a single var, or just an normal array. Test if array has single types,
* which is a sign the array is not a collection.
*/
protected function getDataType($data)
{
$single_type = array('integer', 'boolean', 'string');
if (in_array(gettype(reset($data)), $single_type))
{
return 1;
}
else
{
return 2;
}
}
public function getForFormat()
{
$data = $this->getData();
switch($this->level)
{
case self::LEVEL_ERROR:
$level = 'ERR';
$color = "\033[31m";
break;
case self::LEVEL_WARN:
$level = 'WRN';
$color = "\033[33m";
break;
case self::LEVEL_INFO:
$level = 'INF';
$color = "\033[37m";
break;
case self::LEVEL_DEBUG:
$level = 'DBG';
$color = "\033[37m";
break;
}
$color_end = "\033[0m";
$data['color'] = $color;
$data['color_end'] = $color_end;
$data['level'] = $level;
return $data;
//return array('time' => $this->time, 'level' => $level, 'message' => $this->message, 'data' => $this->data, 'color' => $color, 'color_end' => $color_end, 'caller' => $this->caller);
}
protected function setCaller()
{
if(PHP_VERSION_ID < 50400) {
$debug=debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
} else {
$debug=debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS,5);
}
$i = 4;
if (isset($debug[$i]))
{
$info = $debug[$i];
$line = isset($info['line']) ? $info['line'] : 'Line unknown';
$file = isset($info['file']) ? basename($info['file']) : 'File not set';
$this->caller = array('line' => $line, 'file' => $file, 'function' => $info['function']);
}
}
}

View File

@ -0,0 +1,390 @@
<?php
namespace ShortPixel\ShortPixelLogger;
/*** Logger class
*
* Class uses the debug data model for keeping log entries.
* Logger should not be called before init hook!
*/
class ShortPixelLogger
{
static protected $instance = null;
protected $start_time;
protected $is_active = false;
protected $is_manual_request = false;
protected $show_debug_view = false;
protected $items = array();
protected $logPath = false;
protected $logMode = FILE_APPEND;
protected $logLevel;
protected $format = "[ %%time%% ] %%color%% %%level%% %%color_end%% \t %%message%% \t %%caller%% ( %%time_passed%% )";
protected $format_data = "\t %%data%% ";
protected $hooks = array();
private $logFile; // pointer resource to the logFile.
/* protected $hooks = array(
'shortpixel_image_exists' => array('numargs' => 3),
'shortpixel_webp_image_base' => array('numargs' => 2),
'shortpixel_image_urls' => array('numargs' => 2),
); // @todo monitor hooks, but this should be more dynamic. Do when moving to module via config.
*/
// utility
private $namespace;
private $view;
protected $template = 'view-debug-box';
/** Debugger constructor
* Two ways to activate the debugger. 1) Define SHORTPIXEL_DEBUG in wp-config.php. Either must be true or a number corresponding to required LogLevel
* 2) Put SHORTPIXEL_DEBUG in the request. Either true or number.
*/
public function __construct()
{
$this->start_time = microtime(true);
$this->logLevel = DebugItem::LEVEL_WARN;
$ns = __NAMESPACE__;
$this->namespace = substr($ns, 0, strpos($ns, '\\')); // try to get first part of namespace
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- This is not a form
if (isset($_REQUEST['SHORTPIXEL_DEBUG'])) // manual takes precedence over constants
{
$this->is_manual_request = true;
$this->is_active = true;
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- This is not a form
if ($_REQUEST['SHORTPIXEL_DEBUG'] === 'true')
{
$this->logLevel = DebugItem::LEVEL_INFO;
}
else {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- This is not a form
$this->logLevel = intval($_REQUEST['SHORTPIXEL_DEBUG']);
}
}
else if ( (defined('SHORTPIXEL_DEBUG') && SHORTPIXEL_DEBUG > 0) )
{
$this->is_active = true;
if (SHORTPIXEL_DEBUG === true)
$this->logLevel = DebugItem::LEVEL_INFO;
else {
$this->logLevel = intval(SHORTPIXEL_DEBUG);
}
}
if (defined('SHORTPIXEL_DEBUG_TARGET') && SHORTPIXEL_DEBUG_TARGET || $this->is_manual_request)
{
if (defined('SHORTPIXEL_LOG_OVERWRITE')) // if overwrite, do this on init once.
file_put_contents($this->logPath,'-- Log Reset -- ' .PHP_EOL);
}
if ($this->is_active)
{
/* On Early init, this function might not exist, then queue it when needed */
if (! function_exists('wp_get_current_user'))
add_action('init', array($this, 'initView'));
else
$this->initView();
}
if ($this->is_active && count($this->hooks) > 0)
$this->monitorHooks();
}
/** Init the view when needed. Private function ( public because of WP_HOOK )
* Never call directly */
public function initView()
{
$user_is_administrator = (current_user_can('manage_options')) ? true : false;
if ($this->is_active && $this->is_manual_request && $user_is_administrator )
{
$content_url = content_url();
$logPath = $this->logPath;
$pathpos = strpos($logPath, 'wp-content') + strlen('wp-content');
$logPart = substr($logPath, $pathpos);
$logLink = $content_url . $logPart;
$this->view = new \stdClass;
$this->view->logLink = $logLink;
add_action('admin_footer', array($this, 'loadView'));
}
}
public static function getInstance()
{
if ( self::$instance === null)
{
self::$instance = new ShortPixelLogger();
}
return self::$instance;
}
public function setLogPath($logPath)
{
$this->logPath = $logPath;
$this->getWriteFile(true); // reset the writeFile here.
}
protected function addLog($message, $level, $data = array())
{
// $log = self::getInstance();
// don't log anything too low or when not active.
if ($this->logLevel < $level || ! $this->is_active)
{
return;
}
// Force administrator on manuals.
if ( $this->is_manual_request )
{
if (! function_exists('wp_get_current_user')) // not loaded yet
return false;
$user_is_administrator = (current_user_can('manage_options')) ? true : false;
if (! $user_is_administrator)
return false;
}
// Check where to log to.
if ($this->logPath === false)
{
$upload_dir = wp_upload_dir(null,false,false);
$this->logPath = $this->setLogPath($upload_dir['basedir'] . '/' . $this->namespace . ".log");
}
$arg = array();
$args['level'] = $level;
$args['data'] = $data;
$newItem = new DebugItem($message, $args);
$this->items[] = $newItem;
if ($this->is_active)
{
$this->write($newItem);
}
}
/** Writes to log File. */
protected function write($debugItem, $mode = 'file')
{
$items = $debugItem->getForFormat();
$items['time_passed'] = round ( ($items['time'] - $this->start_time), 5);
$items['time'] = date('Y-m-d H:i:s', (int) $items['time'] );
if ( ($items['caller']) && is_array($items['caller']) && count($items['caller']) > 0)
{
$caller = $items['caller'];
$items['caller'] = $caller['file'] . ' in ' . $caller['function'] . '(' . $caller['line'] . ')';
}
$line = $this->formatLine($items);
$file = $this->getWriteFile();
// try to write to file. Don't write if directory doesn't exists (leads to notices)
if ($file )
{
fwrite($file, $line);
// file_put_contents($this->logPath,$line, FILE_APPEND);
}
else {
// error_log($line);
}
}
protected function getWriteFile($reset = false)
{
if (! is_null($this->logFile) && $reset === false)
{
return $this->logFile;
}
elseif(is_object($this->logFile))
{
fclose($this->logFile);
}
$logDir = dirname($this->logPath);
if (! is_dir($logDir) || ! is_writable($logDir))
{
error_log('ShortpixelLogger: Log Directory is not writable');
$this->logFile = false;
return false;
}
$file = fopen($this->logPath, 'a');
if ($file === false)
{
error_log('ShortpixelLogger: File could not be opened / created: ' . $this->logPath);
$this->logFile = false;
return $file;
}
$this->logFile = $file;
return $file;
}
protected function formatLine($args = array() )
{
$line= $this->format;
foreach($args as $key => $value)
{
if (! is_array($value) && ! is_object($value))
$line = str_replace('%%' . $key . '%%', $value, $line);
}
$line .= PHP_EOL;
if (isset($args['data']))
{
$data = array_filter($args['data']);
if (count($data) > 0)
{
foreach($data as $item)
{
$line .= $item . PHP_EOL;
}
}
}
return $line;
}
protected function setLogLevel($level)
{
$this->logLevel = $level;
}
protected function getEnv($name)
{
if (isset($this->{$name}))
{
return $this->{$name};
}
else {
return false;
}
}
public static function addError($message, $args = array())
{
$level = DebugItem::LEVEL_ERROR;
$log = self::getInstance();
$log->addLog($message, $level, $args);
}
public static function addWarn($message, $args = array())
{
$level = DebugItem::LEVEL_WARN;
$log = self::getInstance();
$log->addLog($message, $level, $args);
}
// Alias, since it goes wrong so often.
public static function addWarning($message, $args = array())
{
self::addWarn($message, $args);
}
public static function addInfo($message, $args = array())
{
$level = DebugItem::LEVEL_INFO;
$log = self::getInstance();
$log->addLog($message, $level, $args);
}
public static function addDebug($message, $args = array())
{
$level = DebugItem::LEVEL_DEBUG;
$log = self::getInstance();
$log->addLog($message, $level, $args);
}
/** These should be removed every release. They are temporary only for d'bugging the current release */
public static function addTemp($message, $args = array())
{
self::addDebug($message, $args);
}
public static function logLevel($level)
{
$log = self::getInstance();
static::addInfo('Changing Log level' . $level);
$log->setLogLevel($level);
}
public static function getLogLevel()
{
$log = self::getInstance();
return $log->getEnv('logLevel');
}
public static function isManualDebug()
{
$log = self::getInstance();
return $log->getEnv('is_manual_request');
}
public static function getLogPath()
{
$log = self::getInstance();
return $log->getEnv('logPath');
}
/** Function to test if the debugger is active
* @return boolean true when active.
*/
public static function debugIsActive()
{
$log = self::getInstance();
return $log->getEnv('is_active');
}
protected function monitorHooks()
{
foreach($this->hooks as $hook => $data)
{
$numargs = isset($data['numargs']) ? $data['numargs'] : 1;
$prio = isset($data['priority']) ? $data['priority'] : 10;
add_filter($hook, function($value) use ($hook) {
$args = func_get_args();
return $this->logHook($hook, $value, $args); }, $prio, $numargs);
}
}
public function logHook($hook, $value, $args)
{
array_shift($args);
self::addInfo('[Hook] - ' . $hook . ' with ' . var_export($value,true), $args);
return $value;
}
public function loadView()
{
// load either param or class template.
$template = $this->template;
$view = $this->view;
$view->namespace = $this->namespace;
$controller = $this;
$template_path = __DIR__ . '/' . $this->template . '.php';
if (file_exists($template_path))
{
include($template_path);
}
else {
self::addError("View $template for ShortPixelLogger could not be found in " . $template_path,
array('class' => get_class($this)));
}
}
} // class debugController

View File

@ -0,0 +1,57 @@
<?php
// Debug Box to load Log File
namespace ShortPixel\ShortPixelLogger;
wp_enqueue_script( 'jquery-ui-draggable' );
?>
<style>
.sp_debug_wrap
{
position: relative;
clear: both;
}
.sp_debug_box
{
position: absolute;
right: 0px;
top: 50px;
background-color: #fff;
width: 150px;
z-index: 1000000;
border: 1px solid #000;
}
.sp_debug_box .header
{
min-height: 10px;
background: #000;
color: #fff;
padding: 8px
}
.sp_debug_box .content_box
{
background: #ccc;
}
.content_box
{
padding: 8px;
}
</style>
<script language='javascript'>
jQuery(document).ready(function($)
{
$( ".sp_debug_box" ).draggable();
});
</script>
<div class='sp_debug_box'>
<div class='header'><?php echo esc_html($view->namespace) ?> Debug Box </div>
<a target="_blank" href='<?php echo esc_url($view->logLink) ?>'>Logfile</a>
<div class='content_box'>
</div>
</div>