Source

Kephra / lib / Kephra / Log.pm

Full commit
use strict;
use warnings;
use Kephra::API;

package Kephra::Log;

our $level;
our @output;
our $test_mode = 0;

sub init {}

sub note {
	return if $test_mode;
}

sub warning {
	return if $test_mode;
	my $msg = message( @_ );
	warn $msg unless $test_mode;
	Kephra::API::output('warn', $msg) unless $test_mode;
}

sub error { 
	return if $test_mode;
	my $msg = message( @_ );
	die $msg unless $test_mode;
	Kephra::API::output('warn', 'ERROR: '.$msg) unless $test_mode;
}

sub message     { 
	my $msg = timestamp() . ' ' . calling_sub(3) . ': "'. $_[0].'"';
	$msg .= ', when called by '. calling_sub(3 + $_[1]) if $_[1];
	$msg;
}
sub timestamp   { sprintf("[%02u:%02u:%02u]", (localtime)[2,1,0]) }
sub calling_sub { ( caller($_[0]) )[3] } 

1;