Source

Kephra / lib / Kephra / Log.pm

use v5.10;
use strict;
use warnings;

package Kephra::Log;

use Kephra::API;

our $level;
our @output;

our $global_switch = 1;    # do_logging
our $GUI_switch = 1;
our $CLI_switch = 1;
our %local_switch;

sub mode {
	my $mode = shift;
	return unless defined $mode;
	return unless $mode eq 'on' or $mode eq 'off'
		or $mode eq 'gui' or $mode eq 'text' or $mode eq 'all';
	$global_switch = $mode eq 'off' ? 0 : 1;
	if ($mode eq 'text'){ $GUI_switch = 0; $CLI_switch = 1 }
	if ($mode eq 'gui') { $GUI_switch = 1; $CLI_switch = 0 }
	if ($mode eq 'all') { $GUI_switch = 1; $CLI_switch = 1 }
}


sub note {
	return unless $global_switch;
	say message( @_ ) if $CLI_switch;
}

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

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

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;
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.