From bc37fcb2af6952dfb3a62d6c65f4d3a12227f77f Mon Sep 17 00:00:00 2001 From: mrkubax10 Date: Sun, 17 Sep 2023 20:21:14 +0200 Subject: [PATCH] Logger: Better application log formatting --- configuration.pm | 4 ++++ logger.pm | 24 ++++++++++++++---------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/configuration.pm b/configuration.pm index beafe42..ee8b8f7 100644 --- a/configuration.pm +++ b/configuration.pm @@ -11,3 +11,7 @@ our $botHostname = "hostname"; our $botName = "Full name of bot"; our $httpServerPort = 8080; + +our $verboseLogging = 0; + +1; diff --git a/logger.pm b/logger.pm index 1d18363..9e4e211 100644 --- a/logger.pm +++ b/logger.pm @@ -164,12 +164,12 @@ sub prepareLogFile { my $outputFilePath = $outputFileFolder."/".localtime->dmy("-").".txt"; open(my $file, ">>", $outputFilePath); if($file) { - printf(":: Logger -> Outputting channel '%s' at '%s' to '%s'\n", $aChannelName, $aServerName, $outputFilePath); + printf("[info] Outputting channel '%s' at '%s' to '%s'\n", $aChannelName, $aServerName, $outputFilePath); $aLogFiles->{$aChannelName}{"file"} = $file; $aLogFiles->{$aChannelName}{"names"} = []; } else { - print(":: Logger -> Failed to open '$outputFilePath' for writing\n"); + print("[error] Failed to open '$outputFilePath' for writing\n"); return 0; } } @@ -182,10 +182,12 @@ sub handlePing { my $aCommandLength = scalar(@$aCommand); if($aCommandLength!=2) { - printf("Encountered invalid PING command (2 arguments expected, %d provided)\n", $aCommandLength); + printf("[error] Encountered invalid PING command (2 arguments expected, %d provided)\n", $aCommandLength); return; } - printf(":: Response -> PONG :%s\n", $aCommand->[1]); + if($configuration::verboseLogging) { + printf("[verbose] Response: PONG :%s\n", $aCommand->[1]); + } $aStream->send(sprintf("PONG :%s\r\n", $aCommand->[1])); } @@ -198,7 +200,7 @@ sub handlePrivMsg { my $aCommandLength = scalar(@$aCommand); if($aCommandLength!=4) { - printf("Encountered invalid PRIVMSG command (4 arguments expected, %d provided)\n", $aCommandLength); + printf("[error] Encountered invalid PRIVMSG command (4 arguments expected, %d provided)\n", $aCommandLength); return; } if(!prepareLogFile($aLogFiles, $aServerName, $aCommand->[1])) { @@ -215,7 +217,7 @@ sub handleJoin { my $aCommandLength = scalar(@$aCommand); if($aCommandLength!=3) { - printf("Encountered invalid JOIN command (3 arguments expected, %d provided)\n", $aCommandLength); + printf("[error] Encountered invalid JOIN command (3 arguments expected, %d provided)\n", $aCommandLength); return; } if(!prepareLogFile($aLogFiles, $aServerName, $aCommand->[1])) { @@ -232,7 +234,7 @@ sub handleQuit { my $aCommandLength = scalar(@$aCommand); if($aCommandLength!=3 && $aCommandLength!=2) { - print("Encountered invalid QUIT command (3 or 2 arguments expected, $aCommandLength provided)\n"); + print("[error] Encountered invalid QUIT command (3 or 2 arguments expected, $aCommandLength provided)\n"); return; } my $reason = ""; @@ -263,7 +265,7 @@ sub handlePart { my $aCommandLength = scalar(@$aCommand); if($aCommandLength!=3) { - print("Encountered invalid PART command (3 arguments expected, $aCommandLength provided)\n"); + print("[error] Encountered invalid PART command (3 arguments expected, $aCommandLength provided)\n"); return; } if(!prepareLogFile($aLogFiles, $aServerName, $aCommand->[1])) { @@ -296,7 +298,7 @@ sub handleNames { my $aCommandLength = scalar(@$aCommand); if($aCommandLength!=6) { - print("Encountered invalid NAMES command (6 arguments expected, $aCommandLength provided)\n"); + print("[error] Encountered invalid NAMES command (6 arguments expected, $aCommandLength provided)\n"); return; } my @names = split(" ", $aCommand->[4]); @@ -344,7 +346,9 @@ sub connectionWorker { $buffer = $remaining; while(length($line)>0) { my @command = parseIRCCommand($line); - printf(":: Server -> %s\n", $line); + if($configuration::verboseLogging) { + printf("[verbose] Server: %s\n", $line); + } given($command[0]) { when("PING") { handlePing($stream, \@command); } when("PRIVMSG") { handlePrivMsg($stream, \@command, $aServerName, $aChannels, \%logFiles); }