Viewing File: /usr/local/cpanel/base/frontend/jupiter/ls_web_cache_manager/core/Lsc/UserLogger.php
<?php
/** ******************************************
* LiteSpeed Web Cache Management Plugin for cPanel
*
* @author Michael Alegre
* @copyright 2018-2023 LiteSpeed Technologies, Inc.
* ******************************************* */
namespace LsUserPanel\Lsc;
use LsUserPanel\Lsc\Context\UserContextOption;
/**
* UserLogger is a singleton
*/
class UserLogger
{
/**
* @var int
*/
const L_NONE = 0;
/**
* @var int
*/
const L_ERROR = 1;
/**
* @var int
*/
const L_WARN = 2;
/**
* @var int
*/
const L_NOTICE = 3;
/**
* @var int
*/
const L_INFO = 4;
/**
* @var int
*/
const L_VERBOSE = 5;
/**
* @var int
*/
const L_DEBUG = 9;
/**
* @var int
*/
const UI_INFO = 0;
/**
* @var int
*/
const UI_SUCC = 1;
/**
* @var int
*/
const UI_ERR = 2;
/**
* @var int
*/
const UI_WARN = 3;
/**
* @var null|UserLogger
*/
private static $instance;
/**
* @var int Highest log message level allowed to be logged. Set to the
* higher value between $this->logFileLvl and $this->logEchoLvl.
*/
private $logLvl;
/**
* @var string File that log messages will be written to (if writable).
*/
private $logFile;
/**
* @var int Highest log message level allowed to be written to the log
* file.
*/
private $logFileLvl;
/**
* @var bool When set to true, log messages will not be written to the log
* file until this UserLogger object is destroyed.
*/
private $bufferedWrite;
/**
* @var UserLogEntry[] Stores created LogEntry objects.
*/
private $msgQueue = array();
/**
* @var int Highest log message level allowed to echo.
*/
private $logEchoLvl;
/**
* @var bool When set to true, echoing of log messages is suppressed.
*/
private $bufferedEcho;
/**
* @var string[][] Leveraged by Control Panel GUI to store and retrieve
* display messages.
*/
private $uiMsgs = array(
self::UI_INFO => array(),
self::UI_SUCC => array(),
self::UI_ERR => array(),
self::UI_WARN => array()
);
/**
*
* @param UserContextOption $ctxOption
*/
private function __construct( UserContextOption $ctxOption )
{
$this->logFile = $ctxOption->getLogFile();
$this->logFileLvl = $ctxOption->getLogFileLvl();
$this->bufferedWrite = $ctxOption->isBufferedWrite();
$this->logEchoLvl = $ctxOption->getLogEchoLvl();
$this->bufferedEcho = $ctxOption->isBufferedEcho();
$this->logLvl = max($this->logEchoLvl, $this->logFileLvl);
}
public function __destruct()
{
if ( $this->bufferedWrite ) {
$this->writeToFile($this->msgQueue);
}
}
/**
*
* @param UserContextOption $contextOption
*
* @throws UserLSCMException Thrown when UserLogger object has already been
* initialized.
*/
public static function Initialize( UserContextOption $contextOption )
{
if ( self::$instance != null ) {
throw new UserLSCMException(
'UserLogger cannot be initialized twice.',
UserLSCMException::E_PROGRAM
);
}
self::$instance = new self($contextOption);
}
/**
*
* @param int $type
*
* @return string[]
*
* @throws UserLSCMException Thrown indirectly by self::me() call.
*/
public static function getUiMsgs( $type )
{
switch ($type) {
case self::UI_INFO:
case self::UI_SUCC:
case self::UI_ERR:
case self::UI_WARN:
return self::me()->uiMsgs[$type];
default:
return array();
}
}
/**
*
* @param string $msg
* @param int $type
*
* @throws UserLSCMException Thrown indirectly by self::me() call.
*/
public static function addUiMsg( $msg, $type )
{
switch ($type) {
case self::UI_INFO:
case self::UI_ERR:
case self::UI_SUCC:
case self::UI_WARN:
self::me()->uiMsgs[$type][] = $msg;
break;
//no default
}
}
/**
*
* @param string $msg
* @param int $lvl
*
* @throws UserLSCMException Thrown indirectly by self::me() call.
*/
public static function logMsg( $msg, $lvl )
{
self::me()->log($msg, $lvl);
}
/**
*
* @since 2.1
*
* @param string $msg
*
* @throws UserLSCMException Thrown indirectly by static::logMsg() call.
*/
public static function error( $msg )
{
static::logMsg($msg, static::L_ERROR);
}
/**
*
* @since 2.1
*
* @param string $msg
*
* @throws UserLSCMException Thrown indirectly by static::logMsg() call.
*/
public static function warn( $msg )
{
static::logMsg($msg, static::L_WARN);
}
/**
*
* @since 2.1
*
* @param string $msg
*
* @throws UserLSCMException Thrown indirectly by static::logMsg() call.
*/
public static function notice( $msg )
{
static::logMsg($msg, static::L_NOTICE);
}
/**
*
* @param string $msg
*
* @throws UserLSCMException Thrown indirectly by self::logMsg() call.
*/
public static function info( $msg )
{
self::logMsg($msg, static::L_INFO);
}
/**
*
* @since 2.1
*
* @param string $msg
*
* @throws UserLSCMException Thrown indirectly by static::logMsg() call.
*/
public static function verbose( $msg )
{
static::logMsg($msg, static::L_VERBOSE);
}
/**
*
* @param string $msg
*
* @throws UserLSCMException Thrown indirectly by self::logMsg() call.
*/
public static function debug( $msg )
{
self::logMsg($msg, static::L_DEBUG);
}
/**
*
* @return UserLogger
*
* @throws UserLSCMException Thrown when UserLogger object has not been
* initialized yet.
*/
private static function me()
{
if ( self::$instance == null ) {
throw new UserLSCMException(
'UserLogger Uninitialized.',
UserLSCMException::E_PROGRAM
);
}
return self::$instance;
}
/**
*
* @param string $msg
* @param int $lvl
*/
protected function log( $msg, $lvl )
{
$entry = new UserLogEntry($msg, $lvl);
$this->msgQueue[] = $entry;
if ( !$this->bufferedWrite ) {
$this->writeToFile(array( $entry ));
}
if ( !$this->bufferedEcho ) {
$this->echoEntries(array( $entry ));
}
}
/**
*
* @param UserLogEntry[] $entries
*/
protected function writeToFile( array $entries )
{
$content = '';
foreach ( $entries as $e ) {
$content .= $e->getOutput($this->logFileLvl);
}
if ( $content != '' ) {
if ( $this->logFile ) {
file_put_contents(
$this->logFile,
$content,
FILE_APPEND | LOCK_EX
);
}
else {
error_log($content);
}
}
}
/**
*
* @param UserLogEntry[] $entries
*/
protected function echoEntries( array $entries )
{
foreach ( $entries as $entry ) {
if ( ($msg = $entry->getOutput($this->logEchoLvl)) !== '' ) {
echo $msg;
}
}
}
/**
*
* @param int $lvl
*
* @return string
*/
public static function getLvlDescr( $lvl )
{
switch ($lvl) {
case self::L_ERROR:
return 'ERROR';
case self::L_WARN:
return 'WARN';
case self::L_NOTICE:
return 'NOTICE';
case self::L_INFO:
return 'INFO';
case self::L_VERBOSE:
return 'DETAIL';
case self::L_DEBUG:
return 'DEBUG';
default:
/**
* Do silently.
*/
return '';
}
}
}
Back to Directory
File Manager