diff options
-rw-r--r-- | lib/TgLib/Logger.pm | 24 | ||||
-rwxr-xr-x | tgrecv | 16 | ||||
-rwxr-xr-x | tgsend | 7 |
3 files changed, 37 insertions, 10 deletions
diff --git a/lib/TgLib/Logger.pm b/lib/TgLib/Logger.pm new file mode 100644 index 0000000..fe5d6a9 --- /dev/null +++ b/lib/TgLib/Logger.pm @@ -0,0 +1,24 @@ +package TgLib::Logger; + +use parent qw<Exporter>; +our @EXPORT = qw<new debug info>; + +my %level = (info => 1, debug => 2); + +sub log_level { + my ($self, $msg, $level) = @_; + print STDERR "[$level] $msg" if $level{$level} <= $self->{'level'}; +} + +################################################################################ +# Public + +sub new { + my ($class, $verbose) = @_; + return bless { level => $verbose }, $class; +} + +sub debug { log_level(@_, 'debug'); } +sub info { log_level(@_, 'info'); } + +1; @@ -22,7 +22,8 @@ use FindBin; use lib "$FindBin::Bin/lib"; use TgLib qw<fetch_token>; use TgLib::Env qw<$HOME $CONFIG_HOME $CACHE_HOME>; -use TgLib::Cache; +require TgLib::Cache; +require TgLib::Logger; my $TOKEN; my $OUTPUT; @@ -41,13 +42,15 @@ GetOptions("token=s" => \$TOKEN, "help" => \$HELP); pod2usage(-verbose => $VERBOSE+1) if $HELP; +my $logger = TgLib::Logger->new($VERBOSE); + # If token was not specified in CLI, get it from ENV/file $TOKEN ||= fetch_token() or pod2usage(-message => "ERROR: Unable to get bot token ($!).\n", -verbose => 99, -sections => "AUTHENTICATION"); my $cache = TgLib::Cache->new; -print STDERR "Using cache:\n", Dumper($cache), "\n" if $VERBOSE > 1; +$logger->debug("Using cache: " . Dumper($cache)); # Get offset from cache if --auto-offset is enabled (and no --offset provided) if ($AUTO_OFFSET && ! $OFFSET) { @@ -58,12 +61,10 @@ my $ua = LWP::UserAgent->new; my $uri = "https://api.telegram.org/bot$TOKEN/getUpdates?timeout=$TIMEOUT"; $uri = $uri . "&offset=$OFFSET" if $OFFSET; my $req = HTTP::Request->new("GET", $uri); -if ($VERBOSE) { - print STDERR "Request:\n", Dumper($req), "\n" if $VERBOSE > 1; -} +$logger->debug("Request: " . Dumper($req)); my $resp = $ua->request($req); -print STDERR "Response:\n", Dumper($resp), "\n" if $VERBOSE > 1; +$logger->debug("Response: " . Dumper($resp)); if ($resp->is_error()) { die $resp->message; } else { @@ -77,8 +78,7 @@ if ($resp->is_error()) { if ($AUTO_OFFSET && @$results) { # Update cache: set offset to last update id +1 $cache->offset($results->[-1]{'update_id'}+1); - printf STDERR "Cached offset %s (--auto-offset)\n", - $cache->offset if $VERBOSE > 1; + $logger->debug(sprintf "Cached offset %s (--auto-offset)\n", $cache->offset); $cache->save; } @@ -22,6 +22,7 @@ use FindBin; use lib "$FindBin::Bin/lib"; use TgLib qw<fetch_token>; use TgLib::Env qw<$HOME $CONFIG_HOME $CACHE_HOME>; +require TgLib::Logger; my $TOKEN; my $PRETEND; @@ -34,6 +35,8 @@ GetOptions("token=s" => \$TOKEN, "help" => \$HELP); pod2usage(-verbose => $VERBOSE+1) if $HELP or ! @ARGV; +my $logger = TgLib::Logger->new($VERBOSE); + # If token was not specified in CLI, get it from ENV/file $TOKEN ||= fetch_token() or pod2usage(-message => "ERROR: Unable to get bot token ($!).\n", @@ -51,8 +54,8 @@ sub send_message { my $req = HTTP::Request->new("POST", $URI, ["Content-Type", "application/json"], $content); if ($VERBOSE || $PRETEND) { - print STDERR "Sending to $chat_id:\n====\n$TEXT\n====\n"; - print STDERR "Request:\n", Dumper($req), "\n" if $VERBOSE > 1; + $logger->info("Sending to $chat_id:\n====\n$TEXT\n====\n"); + $logger->debug(sprintf "Request:\n%s\n", Dumper($req)); } unless ($PRETEND) { my $resp = $ua->request($req); |