diff --git a/src/Main.cc b/src/Main.cc index 92d2c7da..04097869 100644 --- a/src/Main.cc +++ b/src/Main.cc @@ -26,6 +26,7 @@ using namespace std; FileContentsCache file_cache; +bool use_terminal_colors = false; @@ -279,6 +280,10 @@ void drop_privileges(const string& username) { int main(int, char**) { signal(SIGPIPE, SIG_IGN); + if (isatty(fileno(stderr))) { + use_terminal_colors = true; + } + shared_ptr state(new ServerState()); shared_ptr base(event_base_new(), event_base_free); diff --git a/src/PSOProtocol.cc b/src/PSOProtocol.cc index 1d50c6fc..9e6b6c93 100644 --- a/src/PSOProtocol.cc +++ b/src/PSOProtocol.cc @@ -11,6 +11,10 @@ using namespace std; +extern bool use_terminal_colors; + + + PSOCommandHeader::PSOCommandHeader() { this->bb.size = 0; this->bb.command = 0; @@ -185,7 +189,12 @@ void print_received_command( const void* data, size_t size, GameVersion version, - const char* name) { + const char* name, + TerminalFormat color) { + if (use_terminal_colors) { + print_color_escape(stderr, color, TerminalFormat::BOLD, TerminalFormat::END); + } + string name_token; if (name && name[0]) { name_token = string(" from ") + name; @@ -206,6 +215,10 @@ void print_received_command( w.write(data, size); print_data(stderr, w.str()); + + if (use_terminal_colors) { + print_color_escape(stderr, TerminalFormat::NORMAL, TerminalFormat::END); + } } void check_size_v(size_t size, size_t min_size, size_t max_size) { diff --git a/src/PSOProtocol.hh b/src/PSOProtocol.hh index 7ad2f627..5eeb57ec 100644 --- a/src/PSOProtocol.hh +++ b/src/PSOProtocol.hh @@ -64,7 +64,8 @@ void print_received_command( const void* data, size_t size, GameVersion version, - const char* name = nullptr); + const char* name = nullptr, + TerminalFormat color = TerminalFormat::FG_GREEN); // This function is used in a lot of places to check received command sizes and // cast them to the appropriate type diff --git a/src/ProxyServer.cc b/src/ProxyServer.cc index 68889b5c..bedd8208 100644 --- a/src/ProxyServer.cc +++ b/src/ProxyServer.cc @@ -587,7 +587,7 @@ void ProxyServer::LinkedSession::on_server_input() { for_each_received_command(this->server_bev.get(), this->version, this->server_input_crypt.get(), [&](uint16_t command, uint32_t flag, string& data) { print_received_command(command, flag, data.data(), data.size(), - this->version, this->server_name.c_str()); + this->version, this->server_name.c_str(), TerminalFormat::FG_RED); process_proxy_command( this->server->state, *this, diff --git a/src/ReceiveCommands.cc b/src/ReceiveCommands.cc index f8fe00e6..f8dccb6f 100644 --- a/src/ReceiveCommands.cc +++ b/src/ReceiveCommands.cc @@ -1036,8 +1036,6 @@ void process_quest_ready(shared_ptr s, shared_ptr c, void process_gba_file_request(shared_ptr, shared_ptr c, uint16_t, uint32_t, const string& data) { // D7 - static FileContentsCache file_cache; - string filename(data); strip_trailing_zeroes(filename); auto contents = file_cache.get(filename); diff --git a/src/SendCommands.cc b/src/SendCommands.cc index 1a5a173b..65f808a1 100644 --- a/src/SendCommands.cc +++ b/src/SendCommands.cc @@ -19,7 +19,8 @@ using namespace std; -static FileContentsCache file_cache; +extern bool use_terminal_colors; +extern FileContentsCache file_cache; @@ -88,9 +89,15 @@ void send_command( if (name_str[0]) { name_token = string(" to ") + name_str; } + if (use_terminal_colors) { + print_color_escape(stderr, TerminalFormat::FG_YELLOW, TerminalFormat::BOLD, TerminalFormat::END); + } log(INFO, "Sending%s (version=%d command=%04hX flag=%08X)", name_token.c_str(), static_cast(version), command, flag); print_data(stderr, send_data.data(), send_data.size()); + if (use_terminal_colors) { + print_color_escape(stderr, TerminalFormat::NORMAL, TerminalFormat::END); + } } if (crypt) {