Skip to content

Commit

Permalink
feat: option to flush the log manually/automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
Xenapte committed Feb 24, 2024
1 parent e3b2e40 commit 7331758
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 3 deletions.
2 changes: 2 additions & 0 deletions BallanceMMOCommon/include/utility/misc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ namespace bmmo {
DebugOutput(k_ESteamNetworkingSocketsDebugOutputType_Bug, text);
}

void set_auto_flush_log(bool flush);
void flush_log();
void close_log();
}

Expand Down
17 changes: 16 additions & 1 deletion BallanceMMOCommon/src/utility/misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ namespace bmmo {
false;
#endif

namespace { FILE* log_file = nullptr; }
namespace {
FILE* log_file = nullptr;
bool auto_flush = false;
}

void set_log_file(FILE* file) { log_file = file; }

void LogFileOutput(const char* pMsg) {
Expand All @@ -66,6 +70,7 @@ namespace bmmo {

if (log_file) {
fprintf(log_file, "[%s] %s\n", timeStr, pszMsg);
if (auto_flush) fflush(log_file);
}

if (eType == k_ESteamNetworkingSocketsDebugOutputType_Bug) {
Expand Down Expand Up @@ -100,6 +105,16 @@ namespace bmmo {
DebugOutput(eType, pszMsg, bmmo::ansi::Reset);
}

void set_auto_flush_log(bool flush) {
auto_flush = flush;
}

void flush_log() {
if (!log_file) return;
if (fflush(log_file) == 0)
Printf("Log file flushed successfully.");
}

void close_log() {
if (!log_file) return;
fclose(log_file);
Expand Down
7 changes: 6 additions & 1 deletion BallanceMMOServer/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,7 @@ class client: public role {

// parse command line arguments (server/name/uuid/help/version) with getopt
int parse_args(int argc, char** argv) {
enum option_values { NoSoundFiles = UINT8_MAX + 1 };
enum option_values { NoSoundFiles = UINT8_MAX + 1, AutoFlush };
static struct option long_options[] = {
{"recorder-mode", required_argument, 0, 'r'},
{"server", required_argument, 0, 's'},
Expand All @@ -823,6 +823,7 @@ int parse_args(int argc, char** argv) {
{"help", no_argument, 0, 'h'},
{"version", no_argument, 0, 'v'},
{"no-sound-files", no_argument, 0, NoSoundFiles},
{"auto-flush", no_argument, 0, AutoFlush},
{0, 0, 0, 0}
};
int opt, opt_index = 0;
Expand All @@ -848,13 +849,16 @@ int parse_args(int argc, char** argv) {
options.print_states = true; break;
case NoSoundFiles:
options.save_sound_files = false; break;
case AutoFlush:
bmmo::set_auto_flush_log(true); break;
case 'h':
printf("Usage: %s [OPTION]...\n", argv[0]);
puts("Options:");
puts(" -s, --server=ADDRESS\t Connect to the server at ADDRESS instead (default: 127.0.0.1:26676).");
puts(" -n, --name=NAME\t Set your name to NAME (default: \"MockClient\")");
puts(" -u, --uuid=UUID\t Set your UUID to UUID (default: \"00010002-0003-0004-0005-000600070008\")");
puts(" -l, --log=PATH\t Write log to the file at PATH in addition to stdout.");
puts(" --auto-flush\t Automatically flush the log file after each output.");
puts(" -d, --detail=LEVEL\t Set the detail level (0 to 2, from low to high) of output (default: 0).");
puts(" -r, --recorder-mode\t Record data received from the server and save them to a binary file.");
puts(" --no-sound-files\t Discard sound files sent by the server.");
Expand Down Expand Up @@ -1144,6 +1148,7 @@ int main(int argc, char** argv) {
bmmo::restart_request_msg msg{.content = {.victim = console.get_next_client_id()}};
client.send(msg, k_nSteamNetworkingSend_Reliable);
});
console.register_command("flushlog", bmmo::flush_log);

client.wait_till_started();
std::thread record_thread;
Expand Down
2 changes: 2 additions & 0 deletions BallanceMMOServer/config_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ bool config_manager::load() {
std::vector<std::string> mute_list_vector = yaml_load_value(config_, "mute_list",
decltype(mute_list_vector){"00000001-0002-0003-0004-000000000005"});
muted_players = std::unordered_set(mute_list_vector.begin(), mute_list_vector.end());
bmmo::set_auto_flush_log(yaml_load_value(config_, "auto_flush_log", false));

ifile.close();
save(false);
Expand Down Expand Up @@ -137,6 +138,7 @@ void config_manager::save(bool reload_values) {
<< "# - Log ball-offs: whether to write player ball-off events to the log file.\n"
<< "# - Serious warning as DNF: mark the client's status as Did-Not-Finish upon receiving a serious warning.\n"
<< "# - Options for log levels: important, warning, msg.\n"
<< "# - Auto flush log: whether to automatically flush the log file after each output.\n"
<< "# - Map name list style: \"md5_hash: name\".\n"
<< "# - Op list player data style: \"playername: uuid\".\n"
<< "# - Ban list style: \"uuid: reason\".\n"
Expand Down
3 changes: 2 additions & 1 deletion BallanceMMOServer/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1454,7 +1454,7 @@ class server: public role {
};

// parse arguments (optional port and help/version/log) with getopt
int parse_args(int argc, char** argv, uint16_t& port, std::string& log_path, bool& dry_run) {
static int parse_args(int argc, char** argv, uint16_t& port, std::string& log_path, bool& dry_run) {
enum option_values { DryRun = UINT8_MAX + 1 };
static struct option long_options[] = {
{"port", required_argument, 0, 'p'},
Expand Down Expand Up @@ -1834,6 +1834,7 @@ int main(int argc, char** argv) {
server.broadcast_message(msg);
Printf(bmmo::color_code(msg.code), "Requested to restart #%u's current level.", client);
});
console.register_command("flushlog", bmmo::flush_log);
console.register_command("help", [&] { Printf(console.get_help_string().c_str()); });

server.wait_till_started();
Expand Down

0 comments on commit 7331758

Please sign in to comment.