diff --git a/LogManager.cpp b/LogManager.cpp index 079381d6..5af9d09c 100644 --- a/LogManager.cpp +++ b/LogManager.cpp @@ -9,7 +9,7 @@ #include "filesystem.h" -const char* LogManager::log_codes[] = {"FATAL:", "ERROR:", "Warning:", "Info:", "Verbose:", "Debug:", "Trace:"}; +const char* LogManager::log_codes[] = {"FATAL:", "ERROR:", "Warning:", "Info:", "Verbose:", "Debug:", "Trace:", "Dialog:"}; LogManager::LogManager() { @@ -154,7 +154,7 @@ void LogManager::_flush() { for(size_t msg = 0; msg < temp_messages.size(); ++msg) { - if(temp_messages[msg]->level <= loglevel) + if(temp_messages[msg]->level <= loglevel || temp_messages[msg]->level == LL_DIALOG) { // Put the timestamp here std::chrono::milliseconds counter = std::chrono::duration_cast(temp_messages[msg]->counted_second); @@ -225,12 +225,24 @@ void LogManager::_append(const char* filename, int line, unsigned int level, con mes->line = line; mes->counted_second = std::chrono::steady_clock::now() - base_clock; + /*-------------------------------------------------*\ + | If this is a dialog message, call the dialog show | + | callback | + \*-------------------------------------------------*/ + if(level == LL_DIALOG) + { + for(size_t idx = 0; idx < dialog_show_callbacks.size(); idx++) + { + dialog_show_callbacks[idx](dialog_show_callback_args[idx], mes); + } + } + /*-------------------------------------------------*\ | If the message is within the current verbosity, | | print it on the screen | | TODO: Put the timestamp here | \*-------------------------------------------------*/ - if(level <= verbosity) + if(level <= verbosity || level == LL_DIALOG) { std::cout << mes->buffer; if(print_source) @@ -254,21 +266,6 @@ void LogManager::_append(const char* filename, int line, unsigned int level, con | Flush the queues | \*-------------------------------------------------*/ _flush(); - - /*-------------------------------------------------*\ - | If the message level is LL_WARNING or lower, add | - | it to the error queue | - | | - | Commented out - It is causing OpenRGB to crash | - | according to #1537 | - \*-------------------------------------------------*/ - //if(level <= LL_WARNING) - //{ - // for(size_t idx = 0; idx < error_callbacks.size(); ++idx) - // { - // error_callbacks[idx].first(error_callbacks[idx].second, mes); - // } - //} } std::vector LogManager::messages() @@ -337,22 +334,21 @@ void LogManager::setPrintSource(bool v) print_source = v; } -void LogManager::registerErrorCallback(LogErrorCallback callback, void* receiver) +void LogManager::RegisterDialogShowCallback(LogDialogShowCallback callback, void* receiver) { - std::lock_guard grd(entry_mutex); - - error_callbacks.push_back(LogErrorBlock(callback, receiver)); + LOG_DEBUG("dialog show callback registered"); + dialog_show_callbacks.push_back(callback); + dialog_show_callback_args.push_back(receiver); } -void LogManager::unregisterErrorCallback(LogErrorCallback callback, void* receiver) +void LogManager::UnregisterDialogShowCallback(LogDialogShowCallback callback, void* receiver) { - std::lock_guard grd(entry_mutex); - - for(size_t idx = 0; idx < error_callbacks.size(); ++idx) + for(size_t idx = 0; idx < dialog_show_callbacks.size(); idx++) { - if(error_callbacks[idx].first == callback && error_callbacks[idx].second == receiver) + if(dialog_show_callbacks[idx] == callback && dialog_show_callback_args[idx] == receiver) { - error_callbacks.erase(error_callbacks.begin() + idx); + dialog_show_callbacks.erase(dialog_show_callbacks.begin() + idx); + dialog_show_callback_args.erase(dialog_show_callback_args.begin() + idx); } } -} +} \ No newline at end of file diff --git a/LogManager.h b/LogManager.h index 44a702bd..94232874 100644 --- a/LogManager.h +++ b/LogManager.h @@ -25,7 +25,8 @@ enum LL_INFO, // Initialization messages, significant actions and follow-up information LL_VERBOSE, // Tracing of commands and performed actions, usually for debug purposes, comments on the higher priority messages LL_DEBUG, // Deep tracing, "printf-style debugging" alternative, for debug purposes. Such messages should be put all over the code instead of comments - LL_TRACE + LL_TRACE, + LL_DIALOG // Log messages to be shown in a GUI dialog box }; struct LogMessage @@ -38,8 +39,7 @@ struct LogMessage // int timestamp or float time_offset? TBD }; typedef std::shared_ptr PLogMessage; -typedef void(*LogErrorCallback)(void*, PLogMessage); -typedef std::pair LogErrorBlock; +typedef void(*LogDialogShowCallback)(void*, PLogMessage); class LogManager { @@ -52,7 +52,8 @@ private: std::mutex section_mutex; std::ofstream log_stream; - std::vector error_callbacks; + std::vector dialog_show_callbacks; + std::vector dialog_show_callback_args; // A temporary log message storage to hold them until the stream opens std::vector temp_messages; @@ -86,8 +87,8 @@ public: void setLoglevel(unsigned int); void setVerbosity(unsigned int); void setPrintSource(bool); - void registerErrorCallback(LogErrorCallback callback, void* receiver); - void unregisterErrorCallback(LogErrorCallback callback, void* receiver); + void RegisterDialogShowCallback(LogDialogShowCallback callback, void* receiver); + void UnregisterDialogShowCallback(LogDialogShowCallback callback, void* receiver); unsigned int getLoglevel(); unsigned int getVerbosity() {return verbosity;} void clearMessages(); @@ -98,12 +99,13 @@ public: }; #define LogAppend(level, ...) LogManager::get()->append(__FILE__, __LINE__, level, __VA_ARGS__) -#define LOG_FATAL(...) LogAppend(LL_FATAL, __VA_ARGS__) +#define LOG_FATAL(...) LogAppend(LL_FATAL, __VA_ARGS__) #define LOG_ERROR(...) LogAppend(LL_ERROR, __VA_ARGS__) #define LOG_WARNING(...) LogAppend(LL_WARNING, __VA_ARGS__) -#define LOG_INFO(...) LogAppend(LL_INFO, __VA_ARGS__) +#define LOG_INFO(...) LogAppend(LL_INFO, __VA_ARGS__) #define LOG_VERBOSE(...) LogAppend(LL_VERBOSE, __VA_ARGS__) #define LOG_DEBUG(...) LogAppend(LL_DEBUG, __VA_ARGS__) #define LOG_TRACE(...) LogAppend(LL_TRACE, __VA_ARGS__) +#define LOG_DIALOG(...) LogAppend(LL_DIALOG, __VA_ARGS__) #endif // LOGMANAGER_H diff --git a/main.cpp b/main.cpp index c2fede07..6af67f65 100644 --- a/main.cpp +++ b/main.cpp @@ -18,7 +18,6 @@ #include #include #include -#include #include "OpenRGBDialog2.h" @@ -147,50 +146,6 @@ bool AttemptLocalConnection() return success; } -/******************************************************************************************\ -* * -* MessageBoxCallback * -* * -* Displays a message box when an error occurs. Only call once GUI is initialized * -* * -\******************************************************************************************/ - -void MessageBoxCallback(void*, PLogMessage message) -{ - /*---------------------------------------------------------*\ - | Create a message box | - \*---------------------------------------------------------*/ - QMessageBox box; - - /*---------------------------------------------------------*\ - | Set the box main text to the log message text | - \*---------------------------------------------------------*/ - box.setText(QString::fromStdString(message->buffer)); - - /*---------------------------------------------------------*\ - | Set the informative text from the message information | - \*---------------------------------------------------------*/ - QString info = "Occured in "; - info += message->filename; - info += " on line " + QVariant(message->line).toString(); - box.setInformativeText(info); - - /*---------------------------------------------------------*\ - | Set the message box icon according to message level | - \*---------------------------------------------------------*/ - switch(message->level) - { - case LL_FATAL: box.setIcon(QMessageBox::Critical); break; - case LL_ERROR: box.setIcon(QMessageBox::Warning); break; - case LL_WARNING: box.setIcon(QMessageBox::Information); break; - } - - /*---------------------------------------------------------*\ - | Show the message box | - \*---------------------------------------------------------*/ - box.exec(); -} - /******************************************************************************************\ * * * main * @@ -295,11 +250,6 @@ int main(int argc, char* argv[]) QApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QApplication a(argc, argv); - /*---------------------------------------------------------*\ - | Register the message box callback with the log manager | - \*---------------------------------------------------------*/ - LogManager::get()->registerErrorCallback(&MessageBoxCallback, nullptr); - Ui::OpenRGBDialog2 dlg; if(ret_flags & RET_FLAG_I2C_TOOLS) diff --git a/qt/OpenRGBConsolePage.cpp b/qt/OpenRGBConsolePage.cpp index ad5b7edf..85c0c3a2 100644 --- a/qt/OpenRGBConsolePage.cpp +++ b/qt/OpenRGBConsolePage.cpp @@ -37,7 +37,7 @@ void OpenRGBConsolePage::Refresh() { unsigned int message_level = message.get()->level; - if(message_level <= current_level) + if(message_level <= current_level || message_level == LL_DIALOG) { log += "["; log += LogManager::log_codes[message_level]; diff --git a/qt/OpenRGBDialog2.cpp b/qt/OpenRGBDialog2.cpp index 5d695b10..cacc57e8 100644 --- a/qt/OpenRGBDialog2.cpp +++ b/qt/OpenRGBDialog2.cpp @@ -1,4 +1,5 @@ #include "OpenRGBDialog2.h" +#include "LogManager.h" #include "PluginManager.h" #include "OpenRGBDevicePage.h" #include "OpenRGBDeviceInfoPage.h" @@ -119,6 +120,14 @@ static void DetectionEndedCallback(void * this_ptr) QMetaObject::invokeMethod(this_obj, "onDetectionEnded", Qt::QueuedConnection); } +static void DialogShowCallback(void * this_ptr, PLogMessage msg) +{ + OpenRGBDialog2 * this_obj = (OpenRGBDialog2 *)this_ptr; + + this_obj->SetDialogMessage(msg); + QMetaObject::invokeMethod(this_obj, "onShowDialogMessage", Qt::QueuedConnection); +} + bool OpenRGBDialog2::IsDarkTheme() { #ifdef _WIN32 @@ -269,6 +278,11 @@ OpenRGBDialog2::OpenRGBDialog2(QWidget *parent) : QMainWindow(parent), ui(new Op ResourceManager::get()->RegisterDeviceListChangeCallback(UpdateDeviceListCallback, this); ResourceManager::get()->RegisterDetectionEndCallback(DetectionEndedCallback, this); + /*-----------------------------------------------------*\ + | Register dialog show callback with log manager | + \*-----------------------------------------------------*/ + LogManager::get()->RegisterDialogShowCallback(DialogShowCallback, this); + /*-----------------------------------------------------*\ | Initialize page pointers | \*-----------------------------------------------------*/ @@ -1265,6 +1279,11 @@ void OpenRGBDialog2::UpdateDevicesList() } } +void OpenRGBDialog2::SetDialogMessage(PLogMessage msg) +{ + dialog_message = QString::fromStdString(msg->buffer); +} + void OpenRGBDialog2::UpdateProfileList() { ProfileManager* profile_manager = ResourceManager::get()->GetProfileManager(); @@ -1410,6 +1429,17 @@ void OpenRGBDialog2::on_ShowHide() } } +void OpenRGBDialog2::onShowDialogMessage() +{ + QMessageBox box; + + box.setInformativeText(dialog_message); + + box.exec(); + + dialog_message.clear(); +} + void OpenRGBDialog2::on_ReShow(QSystemTrayIcon::ActivationReason reason) { if (reason == QSystemTrayIcon::DoubleClick) diff --git a/qt/OpenRGBDialog2.h b/qt/OpenRGBDialog2.h index fc631f9f..fb5e20e6 100644 --- a/qt/OpenRGBDialog2.h +++ b/qt/OpenRGBDialog2.h @@ -19,6 +19,7 @@ #include #include "i2c_smbus.h" +#include "LogManager.h" #include "RGBController.h" #include "ProfileManager.h" #include "NetworkClient.h" @@ -56,6 +57,8 @@ public: static bool IsDarkTheme(); static bool IsMinimizeOnClose(); + void SetDialogMessage(PLogMessage msg); + private: /*-------------------------------------*\ | Page pointers | @@ -115,6 +118,7 @@ private: PluginManager* plugin_manager = nullptr; QAction* actionExit; + QString dialog_message; private slots: void on_Exit(); @@ -132,6 +136,7 @@ private slots: void on_SetAllDevices(unsigned char red, unsigned char green, unsigned char blue); void on_SaveSizeProfile(); void on_ShowHide(); + void onShowDialogMessage(); void on_ReShow(QSystemTrayIcon::ActivationReason reason); void on_ProfileSelected(); void on_ButtonLoadProfile_clicked();