Added logging

This commit is contained in:
Torsten Schulz
2024-05-08 21:03:12 +02:00
parent 413dc7ada8
commit 4ce3504688
5 changed files with 46 additions and 3 deletions

View File

@@ -220,7 +220,6 @@ void App::handleLogin(Wt::WLineEdit* userName, Wt::WComboBox* countryWidget, Wt:
validateName(nick);
validateGender(genderWidget);
validateAge(ageWidget);
setUserData(nick, countryWidget, ageWidget, genderWidget);
connectToServer();
startChat();

View File

@@ -6,6 +6,7 @@
#include <Wt/Auth/HashFunction.h>
#include <Wt/WLocalDateTime.h>
#include <Wt/Utils.h>
#include <fstream>
#include <iostream>
#include <sstream>
#include <vector>
@@ -18,6 +19,26 @@ Broadcast::Broadcast(Wt::WServer *server):
downloadCountries();
thread_ = std::thread(std::bind(&Broadcast::run, this));
lastTimeoutCheck_ = Wt::WDateTime::currentDateTime();
checkAndLogStart();
}
void Broadcast::checkAndLogStart() {
const std::string logFilePath = "/opt/ypchat/logs/starts.log";
std::ifstream infile(logFilePath);
if (!infile.good()) {
std::ofstream outfile(logFilePath);
outfile.close();
}
infile.close();
Wt::WDateTime now = Wt::WDateTime::currentDateTime();
std::string timestamp = now.toString("yyyy-MM-dd HH:mm:ss").toUTF8();
std::ofstream outfile(logFilePath, std::ios_base::app);
if (outfile.is_open()) {
outfile << timestamp << std::endl;
outfile.close();
} else {
std::cerr << "Fehler beim Öffnen der Datei: " << logFilePath << std::endl;
}
}
Broadcast::~Broadcast() {
@@ -36,6 +57,25 @@ void Broadcast::connect(Client *client, const std::function<void ()> &fct) {
connection->addBroadcast(userlistBroadcast);
connection->addBroadcast(newUserBroadcast);
}
logClientLogin(client->json());
}
void Broadcast::logClientLogin(const Wt::Json::Object &clientJson) {
const std::string logFilePath = "/opt/ypchat/logs/logins.log";
std::ifstream infile(logFilePath);
if (!infile.good()) {
std::ofstream outfile(logFilePath);
outfile.close();
}
infile.close();
std::string clientData = Wt::Json::serialize(clientJson);
std::ofstream outfile(logFilePath, std::ios_base::app);
if (outfile.is_open()) {
outfile << clientData << std::endl;
outfile.close();
} else {
std::cerr << "Fehler beim Öffnen der Datei: " << logFilePath << std::endl;
}
}
void Broadcast::disconnect(Client *client) {

View File

@@ -147,6 +147,8 @@ private:
void sendBlockedMessage(std::string sessionId, std::string toUserName);
void sendBlockDone(std::string sessionId, std::string toUserName);
void sendUnblockDone(std::string sessionId, std::string toUserName);
void checkAndLogStart();
void logClientLogin(const Wt::Json::Object &clientJson);
};
#endif // BROADCAST_H