From 6e1e847750dc593bcb2571c9775b2db11661cdb2 Mon Sep 17 00:00:00 2001 From: oldmud0 Date: Thu, 30 Dec 2021 20:44:59 -0600 Subject: [PATCH] Add and refine debug log messages (#625) Since these are going to be visible to the user now, at least let's properly format them and make them somewhat helpful. --- src/aoimage.cpp | 12 +++++----- src/charselect.cpp | 9 ++++---- src/courtroom.cpp | 29 ++++++++++-------------- src/demoserver.cpp | 8 +++---- src/discord_rich_presence.cpp | 4 ++-- src/emotes.cpp | 1 - src/lobby.cpp | 2 +- src/packet_distribution.cpp | 7 +++--- src/path_functions.cpp | 42 ++++++++++++++++++++++++++++++++--- src/text_file_functions.cpp | 7 ++++-- 10 files changed, 78 insertions(+), 43 deletions(-) diff --git a/src/aoimage.cpp b/src/aoimage.cpp index b84a279..42fd5b8 100644 --- a/src/aoimage.cpp +++ b/src/aoimage.cpp @@ -26,14 +26,16 @@ AOImage::~AOImage() {} bool AOImage::set_image(QString p_image, QString p_misc) { - p_image = ao_app->get_image(p_image, ao_app->current_theme, ao_app->get_subtheme(), - ao_app->default_theme, p_misc, "", "", is_static || !ao_app->get_animated_theme()); + QString p_image_resolved = ao_app->get_image(p_image, ao_app->current_theme, ao_app->get_subtheme(), + ao_app->default_theme, p_misc, "", "", + is_static || !ao_app->get_animated_theme()); - if (!file_exists(p_image)) { - qWarning() << "Image" << p_image << "not found! Can't set!"; + if (!file_exists(p_image_resolved)) { + qWarning() << "could not find image" << p_image; return false; } - path = p_image; + + path = p_image_resolved; if (!is_static) { movie->stop(); movie->setFileName(path); diff --git a/src/charselect.cpp b/src/charselect.cpp index 3109558..ea0a4c4 100644 --- a/src/charselect.cpp +++ b/src/charselect.cpp @@ -164,15 +164,16 @@ void Courtroom::char_clicked(int n_char) { if (n_char != -1) { + QString char_name = char_list.at(n_char).name; QString char_ini_path = ao_app->get_real_path( - ao_app->get_character_path(char_list.at(n_char).name, "char.ini")); - - qDebug() << "char_ini_path" << char_ini_path; + ao_app->get_character_path(char_name, "char.ini")); if (!file_exists(char_ini_path)) { - call_error("Could not find " + char_ini_path); + call_error(tr("Could not find character (char.ini) for %1").arg(char_name)); return; } + + qDebug() << "Found char.ini for" << char_name << "at" << char_ini_path; } if (n_char != m_cid || n_char == -1) { diff --git a/src/courtroom.cpp b/src/courtroom.cpp index 486cb1c..ff78cea 100644 --- a/src/courtroom.cpp +++ b/src/courtroom.cpp @@ -1253,7 +1253,7 @@ void Courtroom::set_size_and_pos(QWidget *p_widget, QString p_identifier, QStrin ao_app->get_element_dimensions(p_identifier, filename, p_misc); if (design_ini_result.width < 0 || design_ini_result.height < 0) { - qWarning() << "could not find \"" << p_identifier << "\" in " << filename; + qWarning() << "could not find" << p_identifier << "in" << filename; p_widget->hide(); } else { @@ -1265,8 +1265,7 @@ void Courtroom::set_size_and_pos(QWidget *p_widget, QString p_identifier, QStrin void Courtroom::set_taken(int n_char, bool p_taken) { if (n_char >= char_list.size()) { - qDebug() - << "W: set_taken attempted to set an index bigger than char_list size"; + qWarning() << "set_taken attempted to set an index bigger than char_list size"; return; } @@ -1287,7 +1286,7 @@ QPoint Courtroom::get_theme_pos(QString p_identifier) ao_app->get_element_dimensions(p_identifier, filename); if (design_ini_result.width < 0 || design_ini_result.height < 0) { - qWarning() << "could not find \"" << p_identifier << "\" in " << filename; + qWarning() << "could not find" << p_identifier << "in" << filename; return QPoint(0, 0); } else { @@ -3149,7 +3148,7 @@ void Courtroom::append_ic_text(QString p_text, QString p_name, QString p_action, ui_ic_chatlog->textCursor().insertText( "[" + timestamp.toString(log_timestamp_format) + "] ", normal); } else { - qDebug() << "could not insert invalid timestamp"; + qCritical() << "could not insert invalid timestamp" << timestamp; } } @@ -3265,7 +3264,7 @@ void Courtroom::play_preanim(bool immediate) else anim_state = 1; preanim_done(); - qWarning() << "could not find " + anim_to_find; + qWarning() << "could not find preanim" << f_preanim << "for character" << f_char; return; } ui_vp_player_char->set_static_duration(preanim_duration); @@ -5504,7 +5503,7 @@ void Courtroom::truncate_label_text(QWidget *p_widget, QString p_identifier) if (p_checkbox == nullptr && p_label == nullptr) { // i.e. the given p_widget isn't a QLabel or a QCheckBox - qWarning() << "W: Tried to truncate an unsupported widget:" << p_identifier; + qWarning() << "Tried to truncate an unsupported widget:" << p_identifier; return; } // translate the text for the widget we're working with so we truncate the right string @@ -5525,14 +5524,12 @@ void Courtroom::truncate_label_text(QWidget *p_widget, QString p_identifier) p_widget->fontMetrics().boundingRect(label_text_tr).width(); // pixel width of our translated text if (!p_widget->toolTip().startsWith(label_text_tr)) // don't want to append this multiple times p_widget->setToolTip(label_text_tr + "\n" + p_widget->toolTip()); - // qInfo() << "I: Width of label text: " << label_px_width << "px. Theme's - // width: " << label_theme_width << "px."; // we can't do much with a 0-width widget, and there's no need to truncate if // the theme gives us enough space if (label_theme_width <= 0 || label_px_width < label_theme_width) { - qInfo() << "I: Truncation aborted for label text " << label_text_tr - << ", either theme width <= 0 or label width < theme width."; + qDebug().nospace() << "Truncation aborted for label text " << label_text_tr + << ", either theme width <= 0 or label width < theme width."; return; } @@ -5541,8 +5538,6 @@ void Courtroom::truncate_label_text(QWidget *p_widget, QString p_identifier) while (truncated_px_width > label_theme_width && truncated_label != "…") { truncated_label.chop(2); truncated_label.append("…"); - // qInfo() << "I: Attempted to truncate label to string: " << - // truncated_label; truncated_px_width = p_widget->fontMetrics().boundingRect(truncated_label).width(); } @@ -5550,7 +5545,7 @@ void Courtroom::truncate_label_text(QWidget *p_widget, QString p_identifier) // Safeguard against edge case where label text is shorter in px than '…', // causing an infinite loop. Additionally, having just an ellipse for a // label looks strange, so we don't set the new label. - qWarning() << "W: Potential infinite loop prevented: Label text " + qWarning() << "Potential infinite loop prevented: Label text " << label_text_tr << "truncated to '…', so truncation was aborted."; return; @@ -5559,9 +5554,9 @@ void Courtroom::truncate_label_text(QWidget *p_widget, QString p_identifier) p_label->setText(truncated_label); else if (p_checkbox != nullptr) p_checkbox->setText(truncated_label); - qInfo() << "I: Truncated label text from " << label_text_tr << " (" - << label_px_width << "px ) to " << truncated_label << " (" - << truncated_px_width << "px )"; + qDebug().nospace() << "Truncated label text from " << label_text_tr << " (" + << label_px_width << "px) to " << truncated_label << " (" + << truncated_px_width << "px)"; } Courtroom::~Courtroom() diff --git a/src/demoserver.cpp b/src/demoserver.cpp index db5e1bc..800d282 100644 --- a/src/demoserver.cpp +++ b/src/demoserver.cpp @@ -21,7 +21,7 @@ void DemoServer::start_server() return; } this->port = tcp_server->serverPort(); - qDebug() << "Server started"; + qInfo() << "Demo server started at port" << port; server_started = true; } @@ -63,7 +63,7 @@ void DemoServer::accept_connection() if (client_sock) { // Client is already connected... - qDebug() << "Multiple connections to demo server disallowed."; + qWarning() << "Multiple connections to demo server disallowed."; QTcpSocket* temp_socket = tcp_server->nextPendingConnection(); connect(temp_socket, &QAbstractSocket::disconnected, temp_socket, &QObject::deleteLater); temp_socket->disconnectFromHost(); @@ -269,7 +269,7 @@ void DemoServer::load_demo(QString filename) // No-shenanigans 2.9.0 demo file with the dreaded demo desync bug detected https://github.com/AttorneyOnline/AO2-Client/pull/496 // If we don't start with the SC packet this means user-edited weirdo shenanigans. Don't screw around with those. if (demo_data.head().startsWith("SC#") && demo_data.last().startsWith("wait#")) { - qDebug() << "Loaded a broken pre-2.9.1 demo file, with the wait desync issue!"; + qInfo() << "Loaded a broken pre-2.9.1 demo file, with the wait desync issue!"; QMessageBox *msgBox = new QMessageBox; msgBox->setAttribute(Qt::WA_DeleteOnClose); msgBox->setTextFormat(Qt::RichText); @@ -281,7 +281,7 @@ void DemoServer::load_demo(QString filename) QQueue p_demo_data; switch (ret) { case QMessageBox::Yes: - qDebug() << "Making a backup of the broken demo..."; + qInfo() << "Making a backup of the broken demo..."; QFile::copy(filename, filename + ".backup"); while (!demo_data.isEmpty()) { QString current_packet = demo_data.dequeue(); diff --git a/src/discord_rich_presence.cpp b/src/discord_rich_presence.cpp index aea07e5..7306655 100644 --- a/src/discord_rich_presence.cpp +++ b/src/discord_rich_presence.cpp @@ -63,8 +63,8 @@ void Discord::state_character(std::string name) QString(name.c_str()).toLower().replace(' ', '_').toStdString(); auto name_friendly = QString(name.c_str()).replace('_', ' ').toStdString(); const std::string playing_as = "Playing as " + name_friendly; - qDebug() << "Discord RPC: Setting character state (" << playing_as.c_str() - << ")"; + qDebug().nospace() << "Discord RPC: Setting character state (\"" << playing_as.c_str() + << "\")"; DiscordRichPresence presence; std::memset(&presence, 0, sizeof(presence)); diff --git a/src/emotes.cpp b/src/emotes.cpp index 67a243b..53f2b06 100644 --- a/src/emotes.cpp +++ b/src/emotes.cpp @@ -201,7 +201,6 @@ void Courtroom::on_emote_left_clicked() void Courtroom::on_emote_right_clicked() { - qDebug() << "emote right clicked"; ++current_emote_page; set_emote_page(); diff --git a/src/lobby.cpp b/src/lobby.cpp index e9d95e0..a18e5c2 100644 --- a/src/lobby.cpp +++ b/src/lobby.cpp @@ -205,7 +205,7 @@ void Lobby::set_size_and_pos(QWidget *p_widget, QString p_identifier) ao_app->get_element_dimensions(p_identifier, filename); if (design_ini_result.width < 0 || design_ini_result.height < 0) { - qWarning() << "could not find " << p_identifier << " in " << filename; + qWarning() << "could not find" << p_identifier << "in" << filename; p_widget->hide(); } else { diff --git a/src/packet_distribution.cpp b/src/packet_distribution.cpp index bc7ae82..5777821 100644 --- a/src/packet_distribution.cpp +++ b/src/packet_distribution.cpp @@ -469,10 +469,10 @@ void AOApplication::server_packet_received(AOPacket *p_packet) goto end; if (courtroom_constructed) { - qDebug() << f_contents; - if (f_contents.size() >= - 2) // We have a pos included in the background packet! + if (f_contents.size() >= 2) { + // We have a pos included in the background packet! w_courtroom->set_side(f_contents.at(1)); + } w_courtroom->set_background(f_contents.at(0), f_contents.size() >= 2); append_to_demofile(f_packet_encoded); } @@ -645,7 +645,6 @@ void AOApplication::server_packet_received(AOPacket *p_packet) // The time as displayed on the clock, in milliseconds. // If the number received is negative, stop the timer. qint64 timer_value = f_contents.at(2).toLongLong(); - qDebug() << "timer:" << timer_value; if (timer_value > 0) { if (type == 0) diff --git a/src/path_functions.cpp b/src/path_functions.cpp index 6fd06e7..fe0a77e 100644 --- a/src/path_functions.cpp +++ b/src/path_functions.cpp @@ -19,6 +19,15 @@ #define CASE_SENSITIVE_FILESYSTEM #endif +static bool is_power_2(unsigned int n) { + unsigned int r = 0; + while (n) { + r += n & 1; + n >>= 1; + } + return r == 1; +} + QString AOApplication::get_base_path() { QString base_path = ""; @@ -172,20 +181,41 @@ QString AOApplication::get_config_value(QString p_identifier, QString p_config, QString AOApplication::get_asset(QString p_element, QString p_theme, QString p_subtheme, QString p_default_theme, QString p_misc, QString p_character, QString p_placeholder) { - return get_asset_path(get_asset_paths(p_element, p_theme, p_subtheme, p_default_theme, p_misc, p_character, p_placeholder)); + QString ret = get_asset_path(get_asset_paths(p_element, p_theme, p_subtheme, p_default_theme, p_misc, p_character, p_placeholder)); + if (ret.isEmpty()) { + qWarning().nospace() << "could not find asset " << p_element + << " (theme = " << p_theme + << ", misc = " << p_misc + << ", char = " << p_character << ")"; + } + return ret; } QString AOApplication::get_image(QString p_element, QString p_theme, QString p_subtheme, QString p_default_theme, QString p_misc, QString p_character, QString p_placeholder, bool static_image) { - return get_image_path(get_asset_paths(p_element, p_theme, p_subtheme, p_default_theme, p_misc, p_character, p_placeholder), static_image); + QString ret = get_image_path(get_asset_paths(p_element, p_theme, p_subtheme, p_default_theme, p_misc, p_character, p_placeholder), static_image); + if (ret.isEmpty()) { + qWarning().nospace() << "could not find image " << p_element + << " (theme = " << p_theme + << ", misc = " << p_misc + << ", char = " << p_character + << ", static = " << static_image << ")"; + } + return ret; } QString AOApplication::get_sfx(QString p_sfx, QString p_misc, QString p_character) { QVector pathlist = get_asset_paths(p_sfx, current_theme, get_subtheme(), default_theme, p_misc, p_character); pathlist += get_sounds_path(p_sfx); // Sounds folder path - return get_sfx_path(pathlist); + QString ret = get_sfx_path(pathlist); + if (ret.isEmpty()) { + qWarning().nospace() << "could not find sfx " << p_sfx + << " (char = " << p_character + << ", misc = " << p_misc << ")"; + } + return ret; } QString AOApplication::get_case_sensitive_path(QString p_file) @@ -249,6 +279,9 @@ QString AOApplication::get_real_path(const VPath &vpath) { path = get_case_sensitive_path(path); if (exists(path)) { asset_lookup_cache.insert(qHash(vpath), path); + unsigned int cache_size = asset_lookup_cache.size(); + if (is_power_2(cache_size)) + qDebug() << "lookup cache has reached" << cache_size << "entries"; return path; } } @@ -291,6 +324,9 @@ QString AOApplication::get_real_suffixed_path(const VPath &vpath, path = get_case_sensitive_path(path); if (exists(path)) { asset_lookup_cache.insert(qHash(vpath), path); + unsigned int cache_size = asset_lookup_cache.size(); + if (is_power_2(cache_size)) + qDebug() << "lookup cache has reached" << cache_size << "entries"; return path; } } diff --git a/src/text_file_functions.cpp b/src/text_file_functions.cpp index 6ea82f5..46e7511 100644 --- a/src/text_file_functions.cpp +++ b/src/text_file_functions.cpp @@ -164,11 +164,14 @@ QStringList AOApplication::get_list_file(QString p_file) QString AOApplication::read_file(QString filename) { + if (filename.isEmpty()) + return QString(); + QFile f_log(filename); if (!f_log.open(QIODevice::ReadOnly | QIODevice::Text)) { - qWarning() << "Couldn't open" << filename; - return ""; + qWarning() << "Couldn't open" << filename << "for reading"; + return QString(); } QTextStream in(&f_log);