Skip to content

Commit

Permalink
Fix the logger once and for all
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-xo committed Apr 18, 2022
1 parent eb00c3b commit 2d27d63
Show file tree
Hide file tree
Showing 46 changed files with 168 additions and 165 deletions.
5 changes: 5 additions & 0 deletions SSL/HistoryAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@ public function main($argc, array $argv)

try
{
// do this now so that we can still use defailt logging during parsing options
$this->setupLogging();

$this->parseOptions($argv);

// do it again, as parsing options may have altered the logging setup
$this->setupLogging();

// guess history file (always go for the most recently modified)
Expand Down
18 changes: 9 additions & 9 deletions SSL/Logger/L.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,7 @@ public static function setOverrides(array $overrides)
self::$overrides = $overrides;
}

public static function level($log_at_level)
{
return $log_at_level <= self::$threshold;
}

public static function log($log_at_level, $source, $message, array $args=array())
public static function level($log_at_level, $source)
{
if(isset(self::$overrides[$source]))
{
Expand All @@ -87,10 +82,15 @@ public static function log($log_at_level, $source, $message, array $args=array()
{
$threshold = self::$threshold;
}

if($log_at_level <= $threshold)

return $log_at_level <= $threshold;
}

public static function log($log_at_level, $source, $message, array $args=array())
{
if(self::level($log_at_level, $source))
{
self::$logger->log(time(), $log_at_level, $source, vsprintf($message, $args));
}
}
}
}
8 changes: 4 additions & 4 deletions SSL/PCNTL/ParallelRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function spinOff(ParallelTask $t, $task='task')
{
if(function_exists('pcntl_fork') && !defined('SINGLE_THREADED'))
{
L::level(L::DEBUG) &&
L::level(L::DEBUG, __CLASS__) &&
L::log(L::DEBUG, __CLASS__, "Forking %s...",
array($task));

Expand All @@ -40,7 +40,7 @@ public function spinOff(ParallelTask $t, $task='task')
// parent
if($pid == -1)
{
L::level(L::WARNING) &&
L::level(L::WARNING, __CLASS__) &&
L::log(L::WARNING, __CLASS__, "Fork failed! Running %s single-threaded...",
array($task));

Expand All @@ -60,7 +60,7 @@ public function spinOff(ParallelTask $t, $task='task')
}
else
{
L::level(L::DEBUG) &&
L::level(L::DEBUG, __CLASS__) &&
L::log(L::DEBUG, __CLASS__, "PCNTL not supported. Running %s single-threaded. If %s is slow, it will block the app until it is finished.",
array($task, $task));

Expand All @@ -69,4 +69,4 @@ public function spinOff(ParallelTask $t, $task='task')

return false;
}
}
}
4 changes: 2 additions & 2 deletions SSL/PluginManager/PluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function addPlugin(SSLPlugin $plugin)

$this->plugin_wrapper->addPlugin($this->max_plugin_id, $plugin);

L::level(L::DEBUG) &&
L::level(L::DEBUG, __CLASS__) &&
L::log(L::DEBUG, __CLASS__, "added %s plugin with id %d",
array(get_class($plugin), $this->max_plugin_id));

Expand Down Expand Up @@ -126,4 +126,4 @@ public function getObservers()
public function setOptions(array $options) {
$this->plugin_wrapper->setOptions($options);
}
}
}
6 changes: 3 additions & 3 deletions SSL/PluginManager/PluginWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,13 +260,13 @@ protected function addAllObserversFromPlugins()
}
}

L::level(L::INFO) &&
L::level(L::INFO, __CLASS__) &&
L::log(L::INFO, __CLASS__, "%d: %s installed",
array($id, get_class($plugin)));

L::level(L::DEBUG) &&
L::level(L::DEBUG, __CLASS__) &&
L::log(L::DEBUG, __CLASS__, "%s brought %d observers to the table",
array(get_class($plugin), $oc));
}
}
}
}
2 changes: 1 addition & 1 deletion SSL/Plugins/Analyzer/analyzehistory.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,5 @@ function ___autoload($class)
);

$h = new HistoryAnalyzer();
$h->setVerbosityOverride($log_levels, L::INFO);
$h->setVerbosityOverride($log_levels, L::ERROR);
$h->main($argc, $argv);
4 changes: 2 additions & 2 deletions SSL/Plugins/DB/CLIDBPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function addPrompts(array &$argv)

public function addPluginsTo(SSLPluggable $sslpluggable)
{
L::level(L::DEBUG) &&
L::level(L::DEBUG, __CLASS__) &&
L::log(L::DEBUG, __CLASS__, "yielding %d plugins",
array(count($this->plugins)));

Expand All @@ -92,4 +92,4 @@ protected function newDBPlugin($config, $key)
{
return new DBPlugin($config, $key);
}
}
}
8 changes: 4 additions & 4 deletions SSL/Plugins/DBPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function notifyNowPlaying(SSLTrack $track=null)
$placeholders = $this->getPlaceholdersForNoTrack($this->placeholder_map);
}

L::level(L::INFO) &&
L::level(L::INFO, __CLASS__) &&
L::log(L::INFO, __CLASS__, 'Sending %s to DB',
array($track ? $track->getFullTitle() : $this->config['empty_string']));

Expand Down Expand Up @@ -188,7 +188,7 @@ protected function executeWithRetries( array $placeholders, $retry_count=0 )
{
if($retry_count > self::RETRY_LIMIT)
{
L::level(L::ERROR) &&
L::level(L::ERROR, __CLASS__) &&
L::log(L::ERROR, __CLASS__, 'Failed to execute database statement; tried %d times',
array(self::RETRY_LIMIT + 1));

Expand All @@ -197,7 +197,7 @@ protected function executeWithRetries( array $placeholders, $retry_count=0 )

if($retry_count > 0)
{
L::level(L::INFO) &&
L::level(L::INFO, __CLASS__) &&
L::log(L::INFO, __CLASS__, 'Retrying database statement. Attempt number %d',
array($retry_count + 1));
}
Expand All @@ -211,7 +211,7 @@ protected function executeWithRetries( array $placeholders, $retry_count=0 )
{
$this->close();

L::level(L::WARNING) &&
L::level(L::WARNING, __CLASS__) &&
L::log(L::WARNING, __CLASS__, 'Statement failed: %s',
array($e->getMessage()));

Expand Down
4 changes: 2 additions & 2 deletions SSL/Plugins/Discord/CLIDiscordPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function addPrompts(array &$argv)

public function addPluginsTo(SSLPluggable $sslpluggable)
{
L::level(L::DEBUG) &&
L::level(L::DEBUG, __CLASS__) &&
L::log(L::DEBUG, __CLASS__, "yielding %d plugins",
array(count($this->plugins)));

Expand All @@ -119,4 +119,4 @@ protected function newDiscordPlugin($config, $sessionname)
{
return new DiscordPlugin($config, $sessionname);
}
}
}
6 changes: 3 additions & 3 deletions SSL/Plugins/Discord/SSLDiscordAdaptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ protected function sendNowPlaying()

try
{
L::level(L::DEBUG) &&
L::level(L::DEBUG, __CLASS__) &&
L::log(L::DEBUG, __CLASS__, 'Sending Now Playing to Discord: %s',
array( $message ));

Expand All @@ -105,7 +105,7 @@ protected function sendNowPlaying()
}
catch(Exception $e)
{
L::level(L::WARNING) &&
L::level(L::WARNING, __CLASS__) &&
L::log(L::WARNING, __CLASS__, 'Could not send Now Playing to Discord: %s',
array( $e->getMessage() ));
}
Expand All @@ -124,4 +124,4 @@ public function run()
public function setSynchronous($synchronous) {
$this->synchronous = $synchronous;
}
}
}
10 changes: 5 additions & 5 deletions SSL/Plugins/DmcaAlerter.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function notifyTrackChange(TrackChangeEventList $events)
$track = $event->getTrack();
if($track)
{
L::level(L::INFO) &&
L::level(L::INFO, __CLASS__) &&
L::log(L::INFO, __CLASS__, "Checking if track would break any Mixcloud DMCA limits",
array( ));

Expand All @@ -82,7 +82,7 @@ public function notifyScrobble(SSLTrack $track=null)
{
if($track)
{
L::level(L::INFO) &&
L::level(L::INFO, __CLASS__) &&
L::log(L::INFO, __CLASS__, "Recording track as played for Mixcloud DMCA accounting",
array( ));

Expand Down Expand Up @@ -123,7 +123,7 @@ protected function recordPlayedTrack(SSLTrack $track)
if(count($this->last_two_albums) > 2)
array_shift($this->last_two_albums);

L::level(L::DEBUG) &&
L::level(L::DEBUG, __CLASS__) &&
L::log(L::DEBUG, __CLASS__, "Last 3 artists: '%s'; Last 2 albums: '%s'",
array( implode("', '", $this->last_three_artists),
implode("', '", $this->last_two_albums) ));
Expand Down Expand Up @@ -161,7 +161,7 @@ protected function checkLimits(SSLTrack $track)

else
{
L::level(L::INFO) &&
L::level(L::INFO, __CLASS__) &&
L::log(L::INFO, __CLASS__, "Nah, we're fine!",
array( ));
}
Expand Down Expand Up @@ -200,7 +200,7 @@ protected function alert($message, $offending_item)
$this->notifier->notify(__CLASS__, "⚠️ Don't play it!", sprintf($message, $offending_item));
}

L::level(L::WARNING) &&
L::level(L::WARNING, __CLASS__) &&
L::log(L::WARNING, __CLASS__, "⚠️ Don't play it! :- " . $message,
array( $offending_item ));
}
Expand Down
10 changes: 5 additions & 5 deletions SSL/Plugins/Growl/SSLEventRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function notifyTrackChange(TrackChangeEventList $events)
// Commented out as this is actually quite noisy
// $this->popup_notifier->notify('alert', 'Track Change', $event);

L::level(L::INFO) &&
L::level(L::INFO, __CLASS__) &&
L::log(L::INFO, __CLASS__, '>> Track change: >> %s',
array($event));
}
Expand All @@ -57,15 +57,15 @@ public function notifyNowPlaying(SSLTrack $track=null)
{
$this->popup_notifier->notify('alert', 'Now Playing', $track->getFullTitle());

L::level(L::INFO) &&
L::level(L::INFO, __CLASS__) &&
L::log(L::INFO, __CLASS__, '>> Now Playing: >> %s',
array($track->getFullTitle()));
}
else
{
$this->popup_notifier->notify('alert', 'Now Playing', '<Playback Stopped>');

L::level(L::INFO) &&
L::level(L::INFO, __CLASS__) &&
L::log(L::INFO, __CLASS__, '>> Now Playing: >> <Playback Stopped>',
array());
}
Expand All @@ -75,8 +75,8 @@ public function notifyScrobble(SSLTrack $track)
{
$this->popup_notifier->notify('alert', 'Scrobbling', $track->getFullTitle());

L::level(L::INFO) &&
L::level(L::INFO, __CLASS__) &&
L::log(L::INFO, __CLASS__, '>> Scrobbling: >> %s',
array($track->getFullTitle()));
}
}
}
4 changes: 2 additions & 2 deletions SSL/Plugins/IrcCat/CLIIrcCatPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function addPrompts(array &$argv)

public function addPluginsTo(SSLPluggable $sslpluggable)
{
L::level(L::DEBUG) &&
L::level(L::DEBUG, __CLASS__) &&
L::log(L::DEBUG, __CLASS__, "yielding %d plugins",
array(count($this->plugins)));

Expand All @@ -100,4 +100,4 @@ protected function newIrcCatPlugin($config, $host, $port, $channel)
{
return new IrcCatPlugin($config, $host, $port, $channel);
}
}
}
4 changes: 2 additions & 2 deletions SSL/Plugins/IrcCatPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function notifyNowPlaying(SSLTrack $track=null)
$sock = fsockopen($this->host, $this->port, $errno, $errstr, $timeout=1);
if($sock===false)
{
L::level(L::ERROR) &&
L::level(L::ERROR, __CLASS__) &&
L::log(L::ERROR, __CLASS__, "couldn't connect to IRCCat: (%d) %s",
array($errno, $errstr));

Expand All @@ -77,7 +77,7 @@ public function notifyNowPlaying(SSLTrack $track=null)

$message = sprintf($this->config['message'], $track->getFullTitle());

L::level(L::INFO) &&
L::level(L::INFO, __CLASS__) &&
L::log(L::INFO, __CLASS__, "sending '%s' -> %s:%d#%s",
array($message, $this->host, $this->port, $this->channel));

Expand Down
8 changes: 4 additions & 4 deletions SSL/Plugins/JsonServer/CLIJsonServerPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,11 @@ public function parseOption($arg, array &$argv)

$this->config['template_fields'] = $matches[1];

L::level(L::INFO) &&
L::level(L::INFO, __CLASS__) &&
L::log(L::INFO, __CLASS__, "loaded HTML template from %s with %d templated fields",
array($template_filename, $match_count));

L::level(L::DEBUG) &&
L::level(L::DEBUG, __CLASS__) &&
L::log(L::DEBUG, __CLASS__, "Fields in this template: %s",
array(implode(', ', $matches[1])));

Expand All @@ -126,7 +126,7 @@ public function addPrompts(array &$argv)

public function addPluginsTo(SSLPluggable $sslpluggable)
{
L::level(L::DEBUG) &&
L::level(L::DEBUG, __CLASS__) &&
L::log(L::DEBUG, __CLASS__, "yielding %d plugins",
array(count($this->plugins)));

Expand All @@ -141,4 +141,4 @@ protected function newJsonServerPlugin($config, $port)
{
return new JsonServerPlugin($config, $port);
}
}
}
8 changes: 4 additions & 4 deletions SSL/Plugins/JsonServerPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ public function onStart()

socket_set_nonblock($this->socket);

L::level(L::DEBUG) &&
L::level(L::DEBUG, __CLASS__) &&
L::log(L::DEBUG, __CLASS__, "Installed JSON / HTML listener...",
array());

if(L::level(L::INFO))
if(L::level(L::INFO, __CLASS__))
{
L::log(L::INFO, __CLASS__, "now playing info will be available at:",
array());
Expand Down Expand Up @@ -251,7 +251,7 @@ protected function handleRequest($conn)
$bytes = socket_recv($conn, $request, 16384, 0);
if($bytes === false)
{
L::level(L::DEBUG) &&
L::level(L::DEBUG, __CLASS__) &&
L::log(L::DEBUG, __CLASS__, "Problem reading from socket: %s",
array(socket_last_error($conn)));

Expand Down Expand Up @@ -294,7 +294,7 @@ protected function handleRequest($conn)

socket_write($conn, implode("\n", $lines));
socket_close($conn);
L::level(L::DEBUG) &&
L::level(L::DEBUG, __CLASS__) &&
L::log(L::DEBUG, __CLASS__, "Finished handling %s request.",
array($route_name));
}
Expand Down
Loading

0 comments on commit 2d27d63

Please sign in to comment.