diff options
| author | ilotterytea <iltsu@alright.party> | 2024-05-17 23:23:37 +0500 |
|---|---|---|
| committer | ilotterytea <iltsu@alright.party> | 2024-05-17 23:23:37 +0500 |
| commit | 646ca655501dace981001843289d896657ee6b82 (patch) | |
| tree | a657b0f383d0dc7c3881d3eb8427313d982f9523 | |
| parent | 68f981047c8cec031f55b5ee571c25b9ccd1fcd9 (diff) | |
upd: replaced cout with log + removed unused #include
| -rw-r--r-- | src/commands/response_error.hpp | 1 | ||||
| -rw-r--r-- | src/config.cpp | 8 | ||||
| -rw-r--r-- | src/handlers.cpp | 9 | ||||
| -rw-r--r-- | src/irc/client.cpp | 18 | ||||
| -rw-r--r-- | src/main.cpp | 20 | ||||
| -rw-r--r-- | src/stream.cpp | 4 | ||||
| -rw-r--r-- | src/utils/string.cpp | 2 |
7 files changed, 35 insertions, 27 deletions
diff --git a/src/commands/response_error.hpp b/src/commands/response_error.hpp index e91a4fd..ae2c3ee 100644 --- a/src/commands/response_error.hpp +++ b/src/commands/response_error.hpp @@ -1,7 +1,6 @@ #pragma once #include <exception> -#include <iostream> #include <optional> #include <string> #include <type_traits> diff --git a/src/config.cpp b/src/config.cpp index a8bf3fb..ec55913 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -2,19 +2,19 @@ #include <cctype> #include <fstream> -#include <iostream> #include <optional> #include <sstream> #include <string> +#include "logger.hpp" + namespace bot { std::optional<Configuration> parse_configuration_from_file( const std::string &file_path) { std::ifstream ifs(file_path); if (!ifs.is_open()) { - std::cerr << "*** Failed to open the configuration file: " << file_path - << "!\n"; + log::error("Configuration", "Failed to open the file at " + file_path); return std::nullopt; } @@ -77,6 +77,8 @@ namespace bot { cfg.twitch_credentials = ttv_crd_cfg; cfg.database = db_cfg; + log::info("Configuration", + "Successfully loaded the file from '" + file_path + "'"); return cfg; } } diff --git a/src/handlers.cpp b/src/handlers.cpp index add59f0..c7820b4 100644 --- a/src/handlers.cpp +++ b/src/handlers.cpp @@ -1,7 +1,6 @@ #include "handlers.hpp" #include <exception> -#include <iostream> #include <optional> #include <pqxx/pqxx> #include <string> @@ -13,6 +12,7 @@ #include "commands/request_util.hpp" #include "irc/message.hpp" #include "localization/line_id.hpp" +#include "logger.hpp" #include "utils/string.hpp" namespace bot::handlers { @@ -22,10 +22,9 @@ namespace bot::handlers { const irc::Message<irc::MessageType::Privmsg> &message, pqxx::connection &conn) { if (utils::string::string_contains_sql_injection(message.message)) { - std::cout << "[TWITCH HANDLER] Attempted to process the message, but it " - "seems to contain SQL " - "injection symbols: " - << message.message << "\n"; + log::warn("PrivateMessageHandler", + "Received the message in #" + message.source.login + + " with SQL injection: " + message.message); return; } diff --git a/src/irc/client.cpp b/src/irc/client.cpp index eac41d5..018736e 100644 --- a/src/irc/client.cpp +++ b/src/irc/client.cpp @@ -4,11 +4,11 @@ #include <ixwebsocket/IXWebSocketMessageType.h> #include <algorithm> -#include <iostream> #include <optional> #include <string> #include <vector> +#include "../logger.hpp" #include "cpr/api.h" #include "cpr/cprtypes.h" #include "cpr/response.h" @@ -32,7 +32,9 @@ Client::Client(std::string client_id, std::string token) { cpr::Header{{"Client-Id", this->client_id}}); if (response.status_code != 200) { - std::cout << "* Failed to get bot username from Twitch API!\n"; + log::warn("IRC", "Failed to get bot username from Twitch API: " + + std::to_string(response.status_code) + " " + + response.status_line); } else { nlohmann::json j = nlohmann::json::parse(response.text); @@ -47,7 +49,7 @@ void Client::run() { [this](const ix::WebSocketMessagePtr &msg) { switch (msg->type) { case ix::WebSocketMessageType::Message: { - std::cout << "Got a message: " << msg->str << std::endl; + log::debug("IRC", "Received message: " + msg->str); std::vector<std::string> lines = utils::string::split_text(msg->str, '\n'); @@ -81,7 +83,7 @@ void Client::run() { break; } case ix::WebSocketMessageType::Open: { - std::cout << "Connected to Twitch IRC!\n"; + log::info("IRC", "Connected to Twitch IRC"); this->is_connected = true; this->authorize(); for (const auto &msg : this->pool) { @@ -91,7 +93,7 @@ void Client::run() { break; } case ix::WebSocketMessageType::Close: { - std::cout << "Twitch IRC Connection closed!\n"; + log::info("IRC", "Twitch IRC connection closed"); this->is_connected = false; for (const auto &x : this->joined_channels) { @@ -111,6 +113,7 @@ void Client::run() { void Client::say(const std::string &channel_login, const std::string &message) { this->raw("PRIVMSG #" + channel_login + " :" + message); + log::debug("IRC", "Sent '" + message + "' in #" + channel_login); } bool Client::join(const std::string &channel_login) { @@ -121,6 +124,7 @@ bool Client::join(const std::string &channel_login) { if (!already_joined) { this->raw("JOIN #" + channel_login); this->joined_channels.push_back(channel_login); + log::info("IRC", "Joined #" + channel_login); } return !already_joined; @@ -137,11 +141,11 @@ void Client::raw(const std::string &raw_message) { void Client::authorize() { if (this->username.empty() || this->token.empty()) { - std::cout << "Bot username and token must be set!\n"; + log::error("IRC", "Bot username and token must be set for authorization!"); return; } - std::cout << "Authorizing on Twitch IRC servers...\n"; + log::info("IRC", "Authorizing on Twitch IRC servers..."); this->raw("PASS oauth:" + this->token); this->raw("NICK " + this->username); diff --git a/src/main.cpp b/src/main.cpp index a582df4..88b56cd 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,4 +1,3 @@ -#include <iostream> #include <optional> #include <pqxx/pqxx> #include <string> @@ -12,34 +11,37 @@ #include "irc/client.hpp" #include "irc/message.hpp" #include "localization/localization.hpp" +#include "logger.hpp" #include "stream.hpp" #include "timer.hpp" int main(int argc, char *argv[]) { - std::cout << "hi world\n"; + bot::log::info("Main", "Starting up..."); std::optional<bot::Configuration> o_cfg = bot::parse_configuration_from_file(".env"); if (!o_cfg.has_value()) { - return -1; + return 1; } bot::Configuration cfg = o_cfg.value(); if (cfg.twitch_credentials.client_id.empty() || cfg.twitch_credentials.token.empty()) { - std::cerr << "*** TWITCH_CREDENTIALS.CLIENT_ID and " - "TWITCH_CREDENTIALS.TOKEN must be set!\n"; - return -1; + bot::log::error("Main", + "TWITCH_CREDENTIALS.CLIENT_ID and TWITCH_CREDENTIALS.TOKEN " + "must be set in environmental file!"); + return 1; } if (cfg.database.name.empty() || cfg.database.user.empty() || cfg.database.password.empty() || cfg.database.host.empty() || cfg.database.port.empty()) { - std::cerr - << "*** DB_NAME, DB_USER, DB_PASSWORD, DB_HOST, DB_PORT must be set!\n"; - return -1; + bot::log::error("Main", + "DB_NAME, DB_USER, DB_PASSWORD, DB_HOST, DB_PORT " + "must be set in environmental file!"); + return 1; } bot::irc::Client client(cfg.twitch_credentials.client_id, diff --git a/src/stream.cpp b/src/stream.cpp index ad7df68..818d4de 100644 --- a/src/stream.cpp +++ b/src/stream.cpp @@ -11,6 +11,7 @@ #include "api/twitch/schemas/stream.hpp" #include "config.hpp" +#include "logger.hpp" #include "schemas/stream.hpp" #include "utils/string.hpp" @@ -180,6 +181,9 @@ namespace bot::stream { continue; } + log::info("TwitchStreamListener", + "Listening stream events for ID " + std::to_string(id)); + this->ids.push_back(id); } diff --git a/src/utils/string.cpp b/src/utils/string.cpp index b8ba269..71c06bf 100644 --- a/src/utils/string.cpp +++ b/src/utils/string.cpp @@ -1,7 +1,5 @@ #include "string.hpp" -#include <algorithm> -#include <iostream> #include <sstream> #include <string> #include <vector> |
