Source

Kephra / lib / Kephra / Log.pm

Full commit
use v5.10;
use strict;
use warnings;
use Kephra::API;

package Kephra::Log;

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

sub init {}
sub on {}
sub off {$test_mode = 1}

sub note {
	return if $test_mode;
	#say message( @_ );
}

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

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

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

1;