tweaks to make work

This commit is contained in:
2026-07-23 22:41:47 -05:00
parent 86becee951
commit d091879773
2 changed files with 15 additions and 4 deletions

View File

@@ -1,6 +1,8 @@
#include "TcpClient.hpp" #include "TcpClient.hpp"
#include <arpa/inet.h>
TcpClient::TcpClient(ConfigService* config, LoggerService* logger) : TcpBase(logger) { TcpClient::TcpClient(ConfigService* config, LoggerService* logger) : TcpBase(logger) {
if(!(config->getConfig<ClientConfig>("TcpClient", "main", &configuration_))) { if(!(config->getConfig<ClientConfig>("TcpClient", "main", &configuration_))) {
@@ -33,7 +35,7 @@ ErrorCode TcpClient::init() {
// parse the hostname string into uint8s // parse the hostname string into uint8s
host = gethostbyname(configuration_.hostname.c_str()); host = gethostbyname(configuration_.hostname.c_str());
if(host == NULL) { if(host == NULL) {
logger_->log("TcpClient", LogFlag::Debug, "Unable to parse hostname."); logger_->log("TcpClient", LogFlag::Error, "Unable to parse hostname.");
close(socketFd_); // TODO: probably on a destructor close(socketFd_); // TODO: probably on a destructor
return ErrorCode::Error; return ErrorCode::Error;
} }
@@ -41,7 +43,7 @@ ErrorCode TcpClient::init() {
// create tcp socket // create tcp socket
socketFd_ = socket(AF_INET, SOCK_STREAM, 0); socketFd_ = socket(AF_INET, SOCK_STREAM, 0);
if(socketFd_ < 0) { if(socketFd_ < 0) {
logger_->log("TcpClient", LogFlag::Debug, "Unable to open client socket."); logger_->log("TcpClient", LogFlag::Error, "Unable to open client socket.");
close(socketFd_); close(socketFd_);
return ErrorCode::Error; return ErrorCode::Error;
} }
@@ -52,9 +54,13 @@ ErrorCode TcpClient::init() {
serverAddress.sin_port = htons(configuration_.port); serverAddress.sin_port = htons(configuration_.port);
serverAddress.sin_addr = *((struct in_addr*)host->h_addr); serverAddress.sin_addr = *((struct in_addr*)host->h_addr);
char ip[INET_ADDRSTRLEN];
inet_ntop(AF_INET, host->h_addr_list[0], ip, sizeof(ip));
logger_->log("TcpClient", LogFlag::Error, "Connecting to {}:{}", ip, configuration_.port);
// connect to server // connect to server
if(connect(socketFd_, (struct sockaddr*)&serverAddress, sizeof(struct sockaddr)) < 0) { if(connect(socketFd_, (struct sockaddr*)&serverAddress, sizeof(struct sockaddr)) < 0) {
logger_->log("TcpClient", LogFlag::Debug, "Unable to connect to server."); logger_->log("TcpClient", LogFlag::Error, "Unable to connect to server.");
close(socketFd_); close(socketFd_);
return ErrorCode::Error; return ErrorCode::Error;
} }

View File

@@ -10,6 +10,8 @@
#include <filesystem> #include <filesystem>
#include <algorithm> #include <algorithm>
#include <cstdarg> #include <cstdarg>
#include <cerrno>
#include <cstring>
namespace fs = std::filesystem; namespace fs = std::filesystem;
@@ -87,6 +89,9 @@ void LoggerService::write(std::string component, LogFlag flag, std::string messa
// level.append(7 - level.length(), ' ') pads out the level string with whitespace so every line is aligned the sam // level.append(7 - level.length(), ' ') pads out the level string with whitespace so every line is aligned the sam
finalmessage = finalmessage + "[" + level + "] "; finalmessage = finalmessage + "[" + level + "] ";
finalmessage = finalmessage + message + " "; finalmessage = finalmessage + message + " ";
if(flag == LogFlag::Error) {
finalmessage += std::format("[errno = {} ({})]", errno, std::strerror(errno));
}
/* Removed because didn't want to bother with variadics + default arguments /* Removed because didn't want to bother with variadics + default arguments
if (configuration_.showSourceTrace) { if (configuration_.showSourceTrace) {