Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Enabled late static binding #8

Merged
merged 1 commit into from
Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 40 additions & 40 deletions src/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,23 +100,23 @@ class Logger {
* Add a log entry with a diagnostic message for the developer.
*/
public static function debug( $message, $name = '' ) {
return self::add( $message, $name, 'debug' );
return static::add( $message, $name, 'debug' );
}


/**
* Add a log entry with an informational message for the user.
*/
public static function info( $message, $name = '' ) {
return self::add( $message, $name, 'info' );
return static::add( $message, $name, 'info' );
}


/**
* Add a log entry with a warning message.
*/
public static function warning( $message, $name = '' ) {
return self::add( $message, $name, 'warning' );
return static::add( $message, $name, 'warning' );
}


Expand All @@ -125,7 +125,7 @@ public static function warning( $message, $name = '' ) {
* script termination.
*/
public static function error( $message, $name = '' ) {
return self::add( $message, $name, 'error' );
return static::add( $message, $name, 'error' );
}


Expand All @@ -138,12 +138,12 @@ public static function error( $message, $name = '' ) {
public static function time( string $name = null ) {

if ( $name === null ) {
$name = self::$default_timer;
$name = static::$default_timer;
}

if ( ! isset( self::$time_tracking[ $name ] ) ) {
self::$time_tracking[ $name ] = microtime( true );
return self::$time_tracking[ $name ];
if ( ! isset( static::$time_tracking[ $name ] ) ) {
static::$time_tracking[ $name ] = microtime( true );
return static::$time_tracking[ $name ];
}
else {
return false;
Expand All @@ -163,19 +163,19 @@ public static function timeEnd( string $name = null, int $decimals = 6, $level =
$is_default_timer = $name === null;

if ( $is_default_timer ) {
$name = self::$default_timer;
$name = static::$default_timer;
}

if ( isset( self::$time_tracking[ $name ] ) ) {
$start = self::$time_tracking[ $name ];
if ( isset( static::$time_tracking[ $name ] ) ) {
$start = static::$time_tracking[ $name ];
$end = microtime( true );
$elapsed_time = number_format( ( $end - $start), $decimals );
unset( self::$time_tracking[ $name ] );
unset( static::$time_tracking[ $name ] );
if ( ! $is_default_timer ) {
self::add( "$elapsed_time seconds", "Elapsed time for '$name'", $level );
static::add( "$elapsed_time seconds", "Elapsed time for '$name'", $level );
}
else {
self::add( "$elapsed_time seconds", "Elapsed time", $level );
static::add( "$elapsed_time seconds", "Elapsed time", $level );
}
return $elapsed_time;
}
Expand All @@ -193,7 +193,7 @@ public static function timeEnd( string $name = null, int $decimals = 6, $level =
private static function add( $message, $name = '', $level = 'debug' ) {

/* Check if the logging level severity warrants writing this log */
if ( self::$log_level_integers[$level] > self::$log_level_integers[self::$log_level] ){
if ( static::$log_level_integers[$level] > static::$log_level_integers[static::$log_level] ){
return;
}

Expand All @@ -206,17 +206,17 @@ private static function add( $message, $name = '', $level = 'debug' ) {
];

/* Add the log entry to the incremental log */
self::$log[] = $log_entry;
static::$log[] = $log_entry;

/* Initialize the logger if it hasn't been done already */
if ( ! self::$logger_ready ) {
self::init();
if ( ! static::$logger_ready ) {
static::init();
}

/* Write the log to output, if requested */
if ( self::$logger_ready && count( self::$output_streams ) > 0 ) {
$output_line = self::format_log_entry( $log_entry ) . PHP_EOL;
foreach ( self::$output_streams as $key => $stream ) {
if ( static::$logger_ready && count( static::$output_streams ) > 0 ) {
$output_line = static::format_log_entry( $log_entry ) . PHP_EOL;
foreach ( static::$output_streams as $key => $stream ) {
fputs( $stream, $output_line );
}
}
Expand Down Expand Up @@ -260,33 +260,33 @@ public static function format_log_entry( array $log_entry ) : string {
*/
public static function init() {

if ( ! self::$logger_ready ) {
if ( ! static::$logger_ready ) {

/* Print to screen */
if ( true === self::$print_log ) {
self::$output_streams[ 'stdout' ] = STDOUT;
if ( true === static::$print_log ) {
static::$output_streams[ 'stdout' ] = STDOUT;
}

/* Build log file path */
if ( file_exists( self::$log_dir ) ) {
self::$log_file_path = implode( DIRECTORY_SEPARATOR, [ self::$log_dir, self::$log_file_name ] );
if ( ! empty( self::$log_file_extension ) ) {
self::$log_file_path .= "." . self::$log_file_extension;
if ( file_exists( static::$log_dir ) ) {
static::$log_file_path = implode( DIRECTORY_SEPARATOR, [ static::$log_dir, static::$log_file_name ] );
if ( ! empty( static::$log_file_extension ) ) {
static::$log_file_path .= "." . static::$log_file_extension;
}
}

/* Print to log file */
if ( true === self::$write_log ) {
if ( file_exists( self::$log_dir ) ) {
$mode = self::$log_file_append ? "a" : "w";
self::$output_streams[ self::$log_file_path ] = fopen ( self::$log_file_path, $mode );
if ( true === static::$write_log ) {
if ( file_exists( static::$log_dir ) ) {
$mode = static::$log_file_append ? "a" : "w";
static::$output_streams[ static::$log_file_path ] = fopen ( static::$log_file_path, $mode );
}
}
}

/* Now that we have assigned the output stream, this function does not need
to be called anymore */
self::$logger_ready = true;
static::$logger_ready = true;

}

Expand All @@ -306,16 +306,16 @@ public static function init() {
public static function dump_to_file( $file_path='' ) {

if ( ! $file_path ) {
$file_path = self::$log_file_path;
$file_path = static::$log_file_path;
}

if ( file_exists( dirname( $file_path ) ) ) {

$mode = self::$log_file_append ? "a" : "w";
$mode = static::$log_file_append ? "a" : "w";
$output_file = fopen( $file_path, $mode );

foreach ( self::$log as $log_entry ) {
$log_line = self::format_log_entry( $log_entry );
foreach ( static::$log as $log_entry ) {
$log_line = static::format_log_entry( $log_entry );
fwrite( $output_file, $log_line . PHP_EOL );
}

Expand All @@ -333,8 +333,8 @@ public static function dump_to_string() {

$output = '';

foreach ( self::$log as $log_entry ) {
$log_line = self::format_log_entry( $log_entry );
foreach ( static::$log as $log_entry ) {
$log_line = static::format_log_entry( $log_entry );
$output .= $log_line . PHP_EOL;
}

Expand All @@ -345,7 +345,7 @@ public static function dump_to_string() {
* Empty the log
*/
public static function clear_log() {
self::$log = [];
static::$log = [];
}

}
133 changes: 133 additions & 0 deletions tests/CustomLoggerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php
/**
* Same tests as in LoggerTest, but subclassing the logger
* instead of setting the static properties at run time.
*/

namespace Idearia\Tests;

use Idearia\Logger;
use PHPUnit\Framework\TestCase;

class DebugLevelLogger extends Logger
{
public static $log_level = 'debug';
public static $print_log = false;
}

class InfoLevelLogger extends Logger
{
public static $log_level = 'info';
public static $print_log = false;
}

class WarningLevelLogger extends Logger
{
public static $log_level = 'warning';
public static $print_log = false;
}

class ErrorLevelLogger extends Logger
{
public static $log_level = 'error';
public static $print_log = false;
}

class CustomLoggerTest extends TestCase
{
public function testDebug()
{
DebugLevelLogger::clear_log();
$msg = "variable x is false";
DebugLevelLogger::debug( $msg );
$result = DebugLevelLogger::dump_to_string();

$this->assertStringContainsString( $msg, $result );
$this->assertStringContainsStringIgnoringCase( 'debug', $result );
}

public function testInfo()
{
InfoLevelLogger::clear_log();
$msg = "variable x is false";
InfoLevelLogger::info( $msg );
$result = InfoLevelLogger::dump_to_string();

$this->assertStringContainsString( $msg, $result );
$this->assertStringContainsStringIgnoringCase( 'info', $result );
}

public function testWarning()
{
WarningLevelLogger::clear_log();
$msg = "variable x is false";
WarningLevelLogger::warning( $msg );
$result = WarningLevelLogger::dump_to_string();

$this->assertStringContainsString( $msg, $result );
$this->assertStringContainsStringIgnoringCase( 'warning', $result );
}

public function testError()
{
ErrorLevelLogger::clear_log();
$msg = "variable x is false";
ErrorLevelLogger::error( $msg );
$result = ErrorLevelLogger::dump_to_string();

$this->assertStringContainsString( $msg, $result );
$this->assertStringContainsStringIgnoringCase( 'error', $result );
}

public function testLogLevelTooLow()
{
InfoLevelLogger::clear_log();
$msg = "variable x is false";
InfoLevelLogger::debug( $msg );
$result = InfoLevelLogger::dump_to_string();

$this->assertSame( '', $result );
}

public function testSingleTimer()
{
$seconds = 0.5;

$microSeconds = $seconds * 1e6;
DebugLevelLogger::clear_log();
DebugLevelLogger::time( 'Testing the timing' );
usleep($microSeconds);
$result = DebugLevelLogger::timeEnd( 'Testing the timing', 6, 'debug' );

$this->assertEqualsWithDelta($seconds, $result, 0.01);
}

public function testMultipleTimers()
{
$seconds = 1;

DebugLevelLogger::clear_log();
DebugLevelLogger::time('outer timer');
sleep($seconds);
DebugLevelLogger::time('inner timer');
sleep($seconds);
$result_2 = DebugLevelLogger::timeEnd('inner timer', 6, 'debug' );
$result_1 = DebugLevelLogger::timeEnd('outer timer', 6, 'debug' );

$this->assertEqualsWithDelta(2*$seconds, $result_1, 0.01);
$this->assertEqualsWithDelta($seconds, $result_2, 0.01);
}

public function testTimingWithDefaultParameters()
{
$seconds = 1;

$microSeconds = $seconds * 1e6;
DebugLevelLogger::clear_log();
DebugLevelLogger::time();
usleep($microSeconds);
$result = DebugLevelLogger::timeEnd();

$this->assertEqualsWithDelta($seconds, $result, 0.01);
}
}
5 changes: 5 additions & 0 deletions tests/LoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

class LoggerTest extends TestCase
{
protected function setUp(): void
{
Logger::$print_log = false;
}

protected function tearDown(): void
{
Logger::clear_log();
Expand Down