mirror of
https://github.com/OpenRCT2/OpenRCT2
synced 2025-12-24 00:03:11 +01:00
Refactor chat / server stream logging
This commit is contained in:
committed by
Michał Janiszewski
parent
f69e7ac89e
commit
8bb15a70de
@@ -49,6 +49,7 @@ extern "C"
|
|||||||
#include "intro.h"
|
#include "intro.h"
|
||||||
#include "localisation/localisation.h"
|
#include "localisation/localisation.h"
|
||||||
#include "network/http.h"
|
#include "network/http.h"
|
||||||
|
#include "network/network.h"
|
||||||
#include "object_list.h"
|
#include "object_list.h"
|
||||||
#include "rct1.h"
|
#include "rct1.h"
|
||||||
#include "rct2.h"
|
#include "rct2.h"
|
||||||
@@ -218,6 +219,7 @@ namespace OpenRCT2
|
|||||||
}
|
}
|
||||||
|
|
||||||
http_init();
|
http_init();
|
||||||
|
network_set_env(_env);
|
||||||
theme_manager_initialise();
|
theme_manager_initialise();
|
||||||
|
|
||||||
rct2_interop_setup_hooks();
|
rct2_interop_setup_hooks();
|
||||||
|
|||||||
@@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
#include "../core/Guard.hpp"
|
#include "../core/Guard.hpp"
|
||||||
#include "../OpenRCT2.h"
|
#include "../OpenRCT2.h"
|
||||||
|
#include "../PlatformEnvironment.h"
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#include "../platform/platform.h"
|
#include "../platform/platform.h"
|
||||||
@@ -125,6 +126,11 @@ Network::~Network()
|
|||||||
Close();
|
Close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Network::SetEnvironment(IPlatformEnvironment * env)
|
||||||
|
{
|
||||||
|
_env = env;
|
||||||
|
}
|
||||||
|
|
||||||
bool Network::Init()
|
bool Network::Init()
|
||||||
{
|
{
|
||||||
if (!InitialiseWSA()) {
|
if (!InitialiseWSA()) {
|
||||||
@@ -211,8 +217,8 @@ bool Network::BeginClient(const char* host, uint16 port)
|
|||||||
status = NETWORK_STATUS_CONNECTING;
|
status = NETWORK_STATUS_CONNECTING;
|
||||||
_lastConnectStatus = SOCKET_STATUS_CLOSED;
|
_lastConnectStatus = SOCKET_STATUS_CLOSED;
|
||||||
|
|
||||||
BeginChatLog(_chatLogDirectory, _chatLogFilenameFormat);
|
BeginChatLog();
|
||||||
BeginServerLog(_serverLogDirectory, gConfigNetwork.server_name, _serverLogFilenameFormat);
|
BeginServerLog();
|
||||||
|
|
||||||
utf8 keyPath[MAX_PATH];
|
utf8 keyPath[MAX_PATH];
|
||||||
network_get_private_key_path(keyPath, sizeof(keyPath), gConfigNetwork.player_name);
|
network_get_private_key_path(keyPath, sizeof(keyPath), gConfigNetwork.player_name);
|
||||||
@@ -315,8 +321,8 @@ bool Network::BeginServer(uint16 port, const char* address)
|
|||||||
|
|
||||||
cheats_reset();
|
cheats_reset();
|
||||||
LoadGroups();
|
LoadGroups();
|
||||||
BeginChatLog(_chatLogDirectory, _chatLogFilenameFormat);
|
BeginChatLog();
|
||||||
BeginServerLog(_serverLogDirectory, ServerName, _serverLogFilenameFormat);
|
BeginServerLog();
|
||||||
|
|
||||||
NetworkPlayer *player = AddPlayer(gConfigNetwork.player_name, "");
|
NetworkPlayer *player = AddPlayer(gConfigNetwork.player_name, "");
|
||||||
player->Flags |= NETWORK_PLAYER_FLAG_ISSERVER;
|
player->Flags |= NETWORK_PLAYER_FLAG_ISSERVER;
|
||||||
@@ -827,108 +833,95 @@ void Network::LoadGroups()
|
|||||||
group_list.at(0)->ActionsAllowed.fill(0xFF);
|
group_list.at(0)->ActionsAllowed.fill(0xFF);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string Network::BeginLog(const char* directory, const char* filename_format)
|
std::string Network::BeginLog(const std::string &directory, const std::string &filenameFormat)
|
||||||
{
|
{
|
||||||
utf8 filename[32];
|
utf8 filename[256];
|
||||||
time_t timer;
|
time_t timer;
|
||||||
struct tm * tmInfo;
|
|
||||||
time(&timer);
|
time(&timer);
|
||||||
tmInfo = localtime(&timer);
|
auto tmInfo = localtime(&timer);
|
||||||
strftime(filename, sizeof(filename), filename_format, tmInfo);
|
if (strftime(filename, sizeof(filename), filenameFormat.c_str(), tmInfo) == 0) {
|
||||||
|
throw std::runtime_error("strftime failed");
|
||||||
|
}
|
||||||
|
|
||||||
utf8 path[MAX_PATH];
|
return Path::Combine(directory, filename);
|
||||||
platform_get_user_directory(path, directory, sizeof(path));
|
|
||||||
Path::Append(path, sizeof(path), filename);
|
|
||||||
|
|
||||||
return std::string(path);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Network::AppendLog(const utf8 *logPath, const utf8 *text)
|
void Network::AppendLog(const std::string &logPath, const std::string &s)
|
||||||
{
|
{
|
||||||
utf8 directory[MAX_PATH];
|
std::string directory = Path::GetDirectory(logPath);
|
||||||
Path::GetDirectory(directory, sizeof(directory), logPath);
|
if (platform_ensure_directory_exists(directory.c_str())) {
|
||||||
if (platform_ensure_directory_exists(directory)) {
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
_chatLogStream = new FileStream(chatLogPath, FILE_MODE_APPEND);
|
auto fs = FileStream(logPath, FILE_MODE_APPEND);
|
||||||
_logStream = SDL_RWFromFile(logPath, "a");
|
|
||||||
if (_logStream != nullptr) {
|
|
||||||
utf8 buffer[256];
|
utf8 buffer[256];
|
||||||
time_t timer;
|
time_t timer;
|
||||||
struct tm * tmInfo;
|
|
||||||
time(&timer);
|
time(&timer);
|
||||||
tmInfo = localtime(&timer);
|
auto tmInfo = localtime(&timer);
|
||||||
strftime(buffer, sizeof(buffer), "[%Y/%m/%d %H:%M:%S] ", tmInfo);
|
if (strftime(buffer, sizeof(buffer), "[%Y/%m/%d %H:%M:%S] ", tmInfo) != 0) {
|
||||||
|
String::Append(buffer, sizeof(buffer), s.c_str());
|
||||||
|
utf8_remove_formatting(buffer, false);
|
||||||
|
String::Append(buffer, sizeof(buffer), PLATFORM_NEWLINE);
|
||||||
|
|
||||||
String::Append(buffer, sizeof(buffer), text);
|
fs.Write(buffer, strlen(buffer));
|
||||||
utf8_remove_formatting(buffer, false);
|
}
|
||||||
String::Append(buffer, sizeof(buffer), PLATFORM_NEWLINE);
|
|
||||||
|
|
||||||
_chatLogStream->Write(buffer, strlen(buffer));
|
|
||||||
delete _chatLogStream;
|
|
||||||
_chatLogStream = nullptr;
|
|
||||||
}
|
}
|
||||||
catch (const Exception &)
|
catch (const Exception &ex)
|
||||||
{
|
{
|
||||||
SDL_RWwrite(_logStream, buffer, strlen(buffer), 1);
|
log_error("%s", ex.GetMessage());
|
||||||
SDL_RWclose(_logStream);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Network::BeginChatLog(const char* directory, const char* filename_format)
|
void Network::BeginChatLog()
|
||||||
{
|
{
|
||||||
_chatLogPath = BeginLog(directory, filename_format);
|
auto directory = _env->GetDirectoryPath(DIRBASE::USER, DIRID::LOG_CHAT);
|
||||||
|
_chatLogPath = BeginLog(directory, _chatLogFilenameFormat);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Network::AppendChatLog(const utf8 *text)
|
void Network::AppendChatLog(const std::string &s)
|
||||||
{
|
{
|
||||||
if (!gConfigNetwork.log_chat) {
|
if (gConfigNetwork.log_chat) {
|
||||||
return;
|
AppendLog(_chatLogPath, s);
|
||||||
}
|
}
|
||||||
|
|
||||||
AppendLog(_chatLogPath.c_str(), text);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Network::CloseChatLog()
|
void Network::CloseChatLog()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void Network::BeginServerLog(const char* directory, std::string server_name, const char* filename_format)
|
void Network::BeginServerLog()
|
||||||
{
|
{
|
||||||
server_name.append(filename_format);
|
auto directory = _env->GetDirectoryPath(DIRBASE::USER, DIRID::LOG_SERVER);
|
||||||
char* filename = (char *) server_name.c_str();
|
_serverLogPath = BeginLog(directory, (ServerName + _serverLogFilenameFormat));
|
||||||
_serverLogPath = BeginLog(directory, filename);
|
|
||||||
|
|
||||||
// Log server start event
|
// Log server start event
|
||||||
char log_msg[256];
|
utf8 logMessage[256];
|
||||||
if (GetMode() == NETWORK_MODE_CLIENT) {
|
if (GetMode() == NETWORK_MODE_CLIENT) {
|
||||||
format_string(log_msg, 256, STR_LOG_CLIENT_STARTED, NULL);
|
format_string(logMessage, sizeof(logMessage), STR_LOG_CLIENT_STARTED, NULL);
|
||||||
} else if (GetMode() == NETWORK_MODE_SERVER) {
|
} else if (GetMode() == NETWORK_MODE_SERVER) {
|
||||||
format_string(log_msg, 256, STR_LOG_SERVER_STARTED, NULL);
|
format_string(logMessage, sizeof(logMessage), STR_LOG_SERVER_STARTED, NULL);
|
||||||
}
|
}
|
||||||
AppendServerLog(log_msg);
|
AppendServerLog(logMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Network::AppendServerLog(const utf8 *text)
|
void Network::AppendServerLog(const std::string &s)
|
||||||
{
|
{
|
||||||
if (!gConfigNetwork.log_server_actions) {
|
if (gConfigNetwork.log_server_actions) {
|
||||||
return;
|
AppendLog(_serverLogPath.c_str(), s);
|
||||||
}
|
}
|
||||||
|
|
||||||
AppendLog(_serverLogPath.c_str(), text);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Network::CloseServerLog()
|
void Network::CloseServerLog()
|
||||||
{
|
{
|
||||||
// Log server stopped event
|
// Log server stopped event
|
||||||
char log_msg[256];
|
char logMessage[256];
|
||||||
if (GetMode() == NETWORK_MODE_CLIENT) {
|
if (GetMode() == NETWORK_MODE_CLIENT) {
|
||||||
format_string(log_msg, 256, STR_LOG_CLIENT_STOPPED, NULL);
|
format_string(logMessage, sizeof(logMessage), STR_LOG_CLIENT_STOPPED, NULL);
|
||||||
} else if (GetMode() == NETWORK_MODE_SERVER) {
|
} else if (GetMode() == NETWORK_MODE_SERVER) {
|
||||||
format_string(log_msg, 256, STR_LOG_SERVER_STOPPED, NULL);
|
format_string(logMessage, sizeof(logMessage), STR_LOG_SERVER_STOPPED, NULL);
|
||||||
}
|
}
|
||||||
AppendServerLog(log_msg);
|
AppendServerLog(logMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Network::Client_Send_TOKEN()
|
void Network::Client_Send_TOKEN()
|
||||||
@@ -2196,9 +2189,9 @@ void Network::Client_Handle_GAMEINFO(NetworkConnection& connection, NetworkPacke
|
|||||||
network_chat_show_server_greeting();
|
network_chat_show_server_greeting();
|
||||||
}
|
}
|
||||||
|
|
||||||
sint32 network_init()
|
void network_set_env(void * env)
|
||||||
{
|
{
|
||||||
return gNetwork.Init();
|
gNetwork.SetEnvironment((IPlatformEnvironment *)env);
|
||||||
}
|
}
|
||||||
|
|
||||||
void network_close()
|
void network_close()
|
||||||
@@ -2867,6 +2860,7 @@ sint32 network_get_pickup_peep_old_x(uint8 playerid) { return _pickup_peep_old_x
|
|||||||
void network_send_chat(const char* text) {}
|
void network_send_chat(const char* text) {}
|
||||||
void network_send_password(const char* password) {}
|
void network_send_password(const char* password) {}
|
||||||
void network_close() {}
|
void network_close() {}
|
||||||
|
void network_set_env(void * env) {}
|
||||||
void network_shutdown_client() {}
|
void network_shutdown_client() {}
|
||||||
void network_set_password(const char* password) {}
|
void network_set_password(const char* password) {}
|
||||||
uint8 network_get_current_player_id() { return 0; }
|
uint8 network_get_current_player_id() { return 0; }
|
||||||
|
|||||||
@@ -85,13 +85,15 @@ enum {
|
|||||||
NETWORK_TICK_FLAG_CHECKSUMS = 1 << 0,
|
NETWORK_TICK_FLAG_CHECKSUMS = 1 << 0,
|
||||||
};
|
};
|
||||||
|
|
||||||
struct ObjectRepositoryItem;
|
interface IPlatformEnvironment;
|
||||||
|
struct ObjectRepositoryItem;
|
||||||
|
|
||||||
class Network
|
class Network
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Network();
|
Network();
|
||||||
~Network();
|
~Network();
|
||||||
|
void SetEnvironment(IPlatformEnvironment * env);
|
||||||
bool Init();
|
bool Init();
|
||||||
void Close();
|
void Close();
|
||||||
bool BeginClient(const char* host, uint16 port);
|
bool BeginClient(const char* host, uint16 port);
|
||||||
@@ -120,15 +122,15 @@ public:
|
|||||||
void SaveGroups();
|
void SaveGroups();
|
||||||
void LoadGroups();
|
void LoadGroups();
|
||||||
|
|
||||||
std::string BeginLog(const char* directory, const char* filename_format);
|
std::string BeginLog(const std::string &directory, const std::string &filenameFormat);
|
||||||
void AppendLog(const utf8 *logPath, const utf8 *text);
|
void AppendLog(const std::string &logPath, const std::string &s);
|
||||||
|
|
||||||
void BeginChatLog(const char* directory, const char* filename_format);
|
void BeginChatLog();
|
||||||
void AppendChatLog(const utf8 *text);
|
void AppendChatLog(const std::string &s);
|
||||||
void CloseChatLog();
|
void CloseChatLog();
|
||||||
|
|
||||||
void BeginServerLog(const char* directory, std::string server_name, const char* filename_format);
|
void BeginServerLog();
|
||||||
void AppendServerLog(const utf8 *text);
|
void AppendServerLog(const std::string &s);
|
||||||
void CloseServerLog();
|
void CloseServerLog();
|
||||||
|
|
||||||
void Client_Send_TOKEN();
|
void Client_Send_TOKEN();
|
||||||
@@ -225,13 +227,11 @@ private:
|
|||||||
uint32 server_connect_time = 0;
|
uint32 server_connect_time = 0;
|
||||||
uint8 default_group = 0;
|
uint8 default_group = 0;
|
||||||
uint32 game_commands_processed_this_tick = 0;
|
uint32 game_commands_processed_this_tick = 0;
|
||||||
IStream * _chatLogStream = nullptr;
|
|
||||||
std::string _chatLogPath;
|
std::string _chatLogPath;
|
||||||
const char* _chatLogDirectory = "/chatlogs";
|
std::string _chatLogFilenameFormat = "%Y%m%d-%H%M%S.txt";
|
||||||
const char* _chatLogFilenameFormat = "%Y%m%d-%H%M%S.txt";
|
|
||||||
std::string _serverLogPath;
|
std::string _serverLogPath;
|
||||||
const char* _serverLogDirectory = "/serverlogs";
|
std::string _serverLogFilenameFormat = "-%Y%m%d-%H%M%S.txt";
|
||||||
const char* _serverLogFilenameFormat = "-%Y%m%d-%H%M%S.txt";
|
IPlatformEnvironment * _env;
|
||||||
|
|
||||||
void UpdateServer();
|
void UpdateServer();
|
||||||
void UpdateClient();
|
void UpdateClient();
|
||||||
@@ -274,7 +274,7 @@ private:
|
|||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif // __cplusplus
|
#endif // __cplusplus
|
||||||
sint32 network_init();
|
void network_set_env(void * env);
|
||||||
void network_close();
|
void network_close();
|
||||||
void network_shutdown_client();
|
void network_shutdown_client();
|
||||||
sint32 network_begin_client(const char *host, sint32 port);
|
sint32 network_begin_client(const char *host, sint32 port);
|
||||||
|
|||||||
Reference in New Issue
Block a user