aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGuillermo Ramos2019-06-24 17:40:07 +0200
committerGuillermo Ramos2019-06-24 17:40:25 +0200
commit0e3e79e3d72de5118c74b8515bcffb38e7db8fad (patch)
tree064c55fa83358148b799c67f8f170a5c840ae0ac
parent57f8e009519b305294d002784967239e7a41ef5e (diff)
downloadtgutils-0e3e79e3d72de5118c74b8515bcffb38e7db8fad.tar.gz
TgLib.Logger
-rw-r--r--lib/TgLib/Logger.pm24
-rwxr-xr-xtgrecv16
-rwxr-xr-xtgsend7
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;
diff --git a/tgrecv b/tgrecv
index 2c612aa..0cb0036 100755
--- a/tgrecv
+++ b/tgrecv
@@ -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;
}
diff --git a/tgsend b/tgsend
index 10a447a..d4b3445 100755
--- a/tgsend
+++ b/tgsend
@@ -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);