diff options
author | Daniel Schürmann <daschuer@mixxx.org> | 2015-12-12 23:37:43 +0100 |
---|---|---|
committer | Daniel Schürmann <daschuer@mixxx.org> | 2015-12-12 23:37:43 +0100 |
commit | 16f3e3d86742649d4828fd86248823cbd0b83d5e (patch) | |
tree | ec5c61ea850e61c88e2816fe8065e250ba8d8f54 /src/controllers | |
parent | 5852d021ff57e914cfccb59bd9e6034215e1f90c (diff) |
added ControllerDebug singleton to avoid duplicated code
Diffstat (limited to 'src/controllers')
-rw-r--r-- | src/controllers/bulk/bulkcontroller.cpp | 13 | ||||
-rw-r--r-- | src/controllers/controller.cpp | 18 | ||||
-rw-r--r-- | src/controllers/controller.h | 6 | ||||
-rw-r--r-- | src/controllers/controllerdebug.h | 44 | ||||
-rw-r--r-- | src/controllers/controllerengine.cpp | 33 | ||||
-rw-r--r-- | src/controllers/controllerengine.h | 5 | ||||
-rw-r--r-- | src/controllers/hid/hidcontroller.cpp | 26 | ||||
-rw-r--r-- | src/controllers/midi/hss1394controller.cpp | 6 | ||||
-rw-r--r-- | src/controllers/midi/midicontroller.cpp | 27 | ||||
-rw-r--r-- | src/controllers/midi/midioutputhandler.cpp | 13 | ||||
-rw-r--r-- | src/controllers/midi/portmidicontroller.cpp | 13 |
11 files changed, 102 insertions, 102 deletions
diff --git a/src/controllers/bulk/bulkcontroller.cpp b/src/controllers/bulk/bulkcontroller.cpp index bb48fd5c99..4d038961c5 100644 --- a/src/controllers/bulk/bulkcontroller.cpp +++ b/src/controllers/bulk/bulkcontroller.cpp @@ -10,6 +10,7 @@ #include "controllers/bulk/bulkcontroller.h" #include "controllers/bulk/bulksupported.h" #include "controllers/defs_controllers.h" +#include "controllers/controllerdebug.h" #include "util/compatibility.h" #include "util/trace.h" @@ -210,7 +211,7 @@ int BulkController::close() { disconnect(m_pReader, SIGNAL(incomingData(QByteArray)), this, SLOT(receive(QByteArray))); m_pReader->stop(); - if (debugging()) QDebug(QtDebugMsg) << " Waiting on reader to finish"; + controllerDebug(" Waiting on reader to finish"); m_pReader->wait(); delete m_pReader; m_pReader = NULL; @@ -221,9 +222,7 @@ int BulkController::close() { stopEngine(); // Close device - if (debugging()) { - QDebug(QtDebugMsg) << " Closing device"; - } + controllerDebug(" Closing device"); libusb_close(m_phandle); m_phandle = NULL; setOpen(false); @@ -251,8 +250,8 @@ void BulkController::send(QByteArray data) { if (ret < 0) { qWarning() << "Unable to send data to" << getName() << "serial #" << m_sUID; - } else if (debugging()) { - QDebug(QtDebugMsg) << ret << "bytes sent to" << getName() - << "serial #" << m_sUID; + } else { + controllerDebug(ret << "bytes sent to" << getName() + << "serial #" << m_sUID); } } diff --git a/src/controllers/controller.cpp b/src/controllers/controller.cpp index f618fb819f..c7adce6506 100644 --- a/src/controllers/controller.cpp +++ b/src/controllers/controller.cpp @@ -9,6 +9,7 @@ #include <QScriptValue> #include "controllers/controller.h" +#include "controllers/controllerdebug.h" #include "controllers/defs_controllers.h" Controller::Controller() @@ -17,12 +18,7 @@ Controller::Controller() m_bIsOutputDevice(false), m_bIsInputDevice(false), m_bIsOpen(false), - m_bDebug(false), m_bLearning(false) { - // Get --controllerDebug command line option - QStringList commandLineArgs = QApplication::arguments(); - m_bDebug = commandLineArgs.contains("--controllerDebug", Qt::CaseInsensitive) || - commandLineArgs.contains("--midiDebug", Qt::CaseInsensitive); } Controller::~Controller() { @@ -32,9 +28,7 @@ Controller::~Controller() { void Controller::startEngine() { - if (debugging()) { - QDebug(QtDebugMsg) << " Starting engine"; - } + controllerDebug(" Starting engine"); if (m_pEngine != NULL) { qWarning() << "Controller: Engine already exists! Restarting:"; stopEngine(); @@ -43,9 +37,7 @@ void Controller::startEngine() } void Controller::stopEngine() { - if (debugging()) { - QDebug(QtDebugMsg) << " Shutting down engine"; - } + controllerDebug(" Shutting down engine"); if (m_pEngine == NULL) { qWarning() << "Controller::stopEngine(): No engine exists!"; return; @@ -106,7 +98,7 @@ void Controller::receive(const QByteArray data) { } int length = data.size(); - if (debugging()) { + if (ControllerDebug::enabled()) { // Formatted packet display QString message = QString("%1: %2 bytes:\n").arg(m_sDeviceName).arg(length); for(int i=0; i<length; i++) { @@ -117,7 +109,7 @@ void Controller::receive(const QByteArray data) { .arg((unsigned char)(data.at(i)), 2, 16, QChar('0')).toUpper() .arg(spacer); } - QDebug(QtDebugMsg) << message; + controllerDebug(message); } foreach (QString function, m_pEngine->getScriptFunctionPrefixes()) { diff --git a/src/controllers/controller.h b/src/controllers/controller.h index acd0d9ada7..ea0d84d0c6 100644 --- a/src/controllers/controller.h +++ b/src/controllers/controller.h @@ -58,9 +58,6 @@ class Controller : public QObject, ConstControllerPresetVisitor { inline QString getCategory() const { return m_sDeviceCategory; } - inline bool debugging() const { - return m_bDebug; - } virtual bool isMappable() const = 0; inline bool isLearning() const { return m_bLearning; @@ -150,9 +147,6 @@ class Controller : public QObject, ConstControllerPresetVisitor { bool m_bIsInputDevice; // Indicates whether or not the device has been opened for input/output. bool m_bIsOpen; - // Specifies whether or not we should dump incoming data to the console at - // runtime. This is useful for end-user debugging and script-writing. - bool m_bDebug; bool m_bLearning; friend class ControllerManager; // accesses lots of our stuff, but in the same thread diff --git a/src/controllers/controllerdebug.h b/src/controllers/controllerdebug.h new file mode 100644 index 0000000000..33688230ff --- /dev/null +++ b/src/controllers/controllerdebug.h @@ -0,0 +1,44 @@ +#ifndef CONTROLLERDEBUG_H +#define CONTROLLERDEBUG_H + +#include <QDebug> +#include <QApplication> + +class ControllerDebug { + public: + static ControllerDebug& instance() { + static ControllerDebug instance; + return instance; + } + + static bool enabled() { + return instance().m_enabled; + } + + static void setEnabled(bool enabled) { + instance().m_enabled = enabled; + } + + private: + ControllerDebug() { + // Get --controllerDebug command line option + QStringList commandLineArgs = QApplication::arguments(); + m_enabled = commandLineArgs.contains("--controllerDebug", Qt::CaseInsensitive) || + commandLineArgs.contains("--midiDebug", Qt::CaseInsensitive); + } + + // Specifies whether or not we should dump incoming data to the console at + // runtime. This is useful for end-user debugging and script-writing. + bool m_enabled; +}; + +// Usage +// controllerDebug("hello" << "world"); +#define controllerDebug(stream) \ +{ \ + if (ControllerDebug::enabled()) { \ + QDebug(QtDebugMsg) << stream; \ + } \ +} \ + +#endif // CONTROLLERDEBUG_H diff --git a/src/controllers/controllerengine.cpp b/src/controllers/controllerengine.cpp index 8627a0c8d3..c7f1f19d4c 100644 --- a/src/controllers/controllerengine.cpp +++ b/src/controllers/controllerengine.cpp @@ -9,6 +9,7 @@ #include "controllers/controllerengine.h" #include "controllers/controller.h" +#include "controllers/controllerdebug.h" #include "controlobject.h" #include "controlobjectthread.h" #include "errordialoghandler.h" @@ -31,7 +32,6 @@ const double kAlphaBetaDt = kScratchTimerMs / 1000.0; ControllerEngine::ControllerEngine(Controller* controller) : m_pEngine(NULL), m_pController(controller), - m_bDebug(false), m_bPopups(false), m_pBaClass(NULL) { // Handle error dialog buttons @@ -93,9 +93,7 @@ void ControllerEngine::callFunctionOnObjects(QList<QString> scriptFunctionPrefix qWarning() << "ControllerEngine:" << prefixName << "has no" << function << " method"; continue; } - if (m_bDebug) { - QDebug(QtDebugMsg) << "ControllerEngine: Executing" << prefixName << "." << function; - } + controllerDebug("ControllerEngine: Executing" << prefixName << "." << function); init.call(prefix, args); } } @@ -212,11 +210,7 @@ void ControllerEngine::initializeScriptEngine() { Output: - -------- ------------------------------------------------------ */ void ControllerEngine::loadScriptFiles(QList<QString> scriptPaths, - const QList<ControllerPreset::ScriptFileInfo>& scripts) { - // Set the Debug flag - if (m_pController) - m_bDebug = m_pController->debugging(); - + const QList<ControllerPreset::ScriptFileInfo>& scripts) { qDebug() << "ControllerEngine: Loading & evaluating all script code"; m_lastScriptPaths = scriptPaths; @@ -277,7 +271,7 @@ void ControllerEngine::initializeScripts(const QList<ControllerPreset::ScriptFil QScriptValueList args; args << QScriptValue(m_pController->getName()); - args << QScriptValue(m_bDebug); + args << QScriptValue(ControllerDebug::enabled()); // Call the init method for all the prefixes. callFunctionOnObjects(m_scriptFunctionPrefixes, "init", args); @@ -542,8 +536,9 @@ bool ControllerEngine::checkException() { errorText = tr("Uncaught exception at line %1 in passed code: %2") .arg(QString::number(line), errorMessage); - scriptErrorDialog(m_bDebug ? QString("%1\nBacktrace:\n%2") - .arg(errorText, backtrace.join("\n")) : errorText); + scriptErrorDialog(ControllerDebug::enabled() ? + QString("%1\nBacktrace:\n%2") + .arg(errorText, backtrace.join("\n")) : errorText); return true; } return false; @@ -1104,11 +1099,10 @@ int ControllerEngine::beginTimer(int interval, QScriptValue timerCallback, m_timers[timerId] = info; if (timerId == 0) { qWarning() << "Script timer could not be created"; - } else if (m_bDebug) { - if (oneShot) - QDebug(QtDebugMsg) << "Starting one-shot timer:" << timerId; - else - QDebug(QtDebugMsg) << "Starting timer:" << timerId; + } else if (oneShot) { + controllerDebug("Starting one-shot timer:" << timerId); + } else { + controllerDebug("Starting timer:" << timerId); } return timerId; } @@ -1123,10 +1117,7 @@ void ControllerEngine::stopTimer(int timerId) { qWarning() << "Killing timer" << timerId << ": That timer does not exist!"; return; } - if (m_bDebug) { - QDebug(QtDebugMsg) << "Killing timer:" << timerId; - } - + controllerDebug("Killing timer:" << timerId); killTimer(timerId); m_timers.remove(timerId); } diff --git a/src/controllers/controllerengine.h b/src/controllers/controllerengine.h index 7c1be4b1d2..a8f4424787 100644 --- a/src/controllers/controllerengine.h +++ b/src/controllers/controllerengine.h @@ -70,10 +70,6 @@ class ControllerEngine : public QObject { // Get the errors for a source file that was evaluated()'d const QStringList getErrors(QString filename); - void setDebug(bool bDebug) { - m_bDebug = bDebug; - } - void setPopups(bool bPopups) { m_bPopups = bPopups; } @@ -169,7 +165,6 @@ class ControllerEngine : public QObject { double getDeckRate(const QString& group); Controller* m_pController; - bool m_bDebug; bool m_bPopups; QMultiHash<ConfigKey, ControllerEngineConnection> m_connectedControls; QList<QString> m_scriptFunctionPrefixes; diff --git a/src/controllers/hid/hidcontroller.cpp b/src/controllers/hid/hidcontroller.cpp index c30caf3a79..7175d85b93 100644 --- a/src/controllers/hid/hidcontroller.cpp +++ b/src/controllers/hid/hidcontroller.cpp @@ -14,6 +14,7 @@ #include "controllers/defs_controllers.h" #include "util/compatibility.h" #include "util/trace.h" +#include "controllers/controllerdebug.h" HidReader::HidReader(hid_device* device) : QThread(), @@ -213,17 +214,14 @@ int HidController::open() { } // Open device by path - if (debugging()) { - QDebug(QtDebugMsg) << "Opening HID device" - << getName() << "by HID path" << hid_path; - } + controllerDebug("Opening HID device" << getName() << "by HID path" << hid_path); + m_pHidDevice = hid_open_path(hid_path); // If that fails, try to open device with vendor/product/serial # if (m_pHidDevice == NULL) { - if (debugging()) - QDebug(QtDebugMsg) << "Failed. Trying to open with make, model & serial no:" - << hid_vendor_id << hid_product_id << hid_serial; + controllerDebug("Failed. Trying to open with make, model & serial no:" + << hid_vendor_id << hid_product_id << hid_serial); m_pHidDevice = hid_open(hid_vendor_id, hid_product_id, hid_serial_raw); } @@ -279,7 +277,7 @@ int HidController::close() { this, SLOT(receive(QByteArray))); m_pReader->stop(); hid_set_nonblocking(m_pHidDevice, 1); // Quit blocking - if (debugging()) QDebug(QtDebugMsg) << " Waiting on reader to finish"; + controllerDebug(" Waiting on reader to finish"); m_pReader->wait(); delete m_pReader; m_pReader = NULL; @@ -290,9 +288,7 @@ int HidController::close() { stopEngine(); // Close device - if (debugging()) { - QDebug(QtDebugMsg) << " Closing device"; - } + controllerDebug(" Closing device"); hid_close(m_pHidDevice); setOpen(false); return 0; @@ -317,7 +313,7 @@ void HidController::send(QByteArray data, unsigned int reportID) { int result = hid_write(m_pHidDevice, (unsigned char*)data.constData(), data.size()); if (result == -1) { - if (debugging()) { + if (ControllerDebug::enabled()) { qWarning() << "Unable to send data to" << getName() << "serial #" << hid_serial << ":" << safeDecodeWideString(hid_error(m_pHidDevice), 512); @@ -325,10 +321,10 @@ void HidController::send(QByteArray data, unsigned int reportID) { qWarning() << "Unable to send data to" << getName() << ":" << safeDecodeWideString(hid_error(m_pHidDevice), 512); } - } else if (debugging()) { - QDebug(QtDebugMsg) << result << "bytes sent to" << getName() + } else { + controllerDebug(result << "bytes sent to" << getName() << "serial #" << hid_serial - << "(including report ID of" << reportID << ")"; + << "(including report ID of" << reportID << ")"); } } diff --git a/src/controllers/midi/hss1394controller.cpp b/src/controllers/midi/hss1394controller.cpp index 04484e1cc2..b1bc533459 100644 --- a/src/controllers/midi/hss1394controller.cpp +++ b/src/controllers/midi/hss1394controller.cpp @@ -6,6 +6,7 @@ */ #include "controllers/midi/hss1394controller.h" +#include "controllers/controllerdebug.h" DeviceChannelListener::DeviceChannelListener(QObject* pParent, QString name) : QObject(pParent), @@ -88,9 +89,8 @@ int Hss1394Controller::open() { return -1; } - if (debugging()) { - QDebug(QtDebugMsg) << "Hss1394Controller: Opening" << getName() << "index" << m_iDeviceIndex; - } + controllerDebug("Hss1394Controller: Opening" << getName() << "index" + << m_iDeviceIndex); using namespace hss1394; diff --git a/src/controllers/midi/midicontroller.cpp b/src/controllers/midi/midicontroller.cpp index bc880fb2e4..fe8957b15f 100644 --- a/src/controllers/midi/midicontroller.cpp +++ b/src/controllers/midi/midicontroller.cpp @@ -10,6 +10,7 @@ #include "controllers/midi/midiutils.h" #include "controllers/defs_controllers.h" +#include "controllers/controllerdebug.h" #include "controlobject.h" #include "errordialoghandler.h" #include "playermanager.h" @@ -93,16 +94,14 @@ void MidiController::createOutputHandlers() { double min = mapping.output.min; double max = mapping.output.max; - if (debugging()) { - QDebug(QtDebugMsg) << QString( + controllerDebug(QString( "Creating output handler for %1,%2 between %3 and %4 to MIDI out: 0x%5 0x%6, on: 0x%7 off: 0x%8") - .arg(group, key, - QString::number(min), QString::number(max), - QString::number(status, 16).toUpper(), - QString::number(control, 16).toUpper().rightJustified(2,'0'), - QString::number(on, 16).toUpper().rightJustified(2,'0'), - QString::number(off, 16).toUpper().rightJustified(2,'0')); - } + .arg(group, key, + QString::number(min), QString::number(max), + QString::number(status, 16).toUpper(), + QString::number(control, 16).toUpper().rightJustified(2,'0'), + QString::number(on, 16).toUpper().rightJustified(2,'0'), + QString::number(off, 16).toUpper().rightJustified(2,'0'))); MidiOutputHandler* moh = new MidiOutputHandler(this, mapping); if (!moh->validate()) { @@ -114,7 +113,7 @@ void MidiController::createOutputHandlers() { qWarning() << errorLog; int deckNum = 0; - if (debugging()) { + if (ControllerDebug::enabled()) { failures.append(errorLog); } else if (PlayerManager::isDeckGroup(group, &deckNum)) { int numDecks = PlayerManager::numDecks(); @@ -241,9 +240,7 @@ void MidiController::receive(unsigned char status, unsigned char control, unsigned char channel = MidiUtils::channelFromStatus(status); unsigned char opCode = MidiUtils::opCodeFromStatus(status); - if (debugging()) { - QDebug(QtDebugMsg) << formatMidiMessage(status, control, value, channel, opCode); - } + controllerDebug(formatMidiMessage(status, control, value, channel, opCode)); MidiKey mappingKey(status, control); @@ -503,9 +500,7 @@ QString formatSysexMessage(QString controllerName, const QByteArray& data) { } void MidiController::receive(QByteArray data) { - if (debugging()) { - QDebug(QtDebugMsg) << formatSysexMessage(getName(), data); - } + controllerDebug(formatSysexMessage(getName(), data)); MidiKey mappingKey(data.at(0), 0xFF); diff --git a/src/controllers/midi/midioutputhandler.cpp b/src/controllers/midi/midioutputhandler.cpp index 7aec9a88ac..acb611cde1 100644 --- a/src/controllers/midi/midioutputhandler.cpp +++ b/src/controllers/midi/midioutputhandler.cpp @@ -10,6 +10,7 @@ #include "controllers/midi/midioutputhandler.h" #include "controllers/midi/midicontroller.h" +#include "controllers/controllerdebug.h" #include "controlobject.h" MidiOutputHandler::MidiOutputHandler(MidiController* controller, @@ -24,10 +25,8 @@ MidiOutputHandler::MidiOutputHandler(MidiController* controller, MidiOutputHandler::~MidiOutputHandler() { ConfigKey cKey = m_cot.getKey(); - if (m_pController->debugging()) { - QDebug(QtDebugMsg) << QString("Destroying static MIDI output handler on %1 for %2,%3") - .arg(m_pController->getName(), cKey.group, cKey.item); - } + controllerDebug(QString("Destroying static MIDI output handler on %1 for %2,%3") + .arg(m_pController->getName(), cKey.group, cKey.item)); } bool MidiOutputHandler::validate() { @@ -55,11 +54,9 @@ void MidiOutputHandler::controlChanged(double value) { if (!m_pController->isOpen()) { qWarning() << "MIDI device" << m_pController->getName() << "not open for output!"; } else if (byte3 != 0xFF) { - if (m_pController->debugging()) { - QDebug(QtDebugMsg) << "sending MIDI bytes:" << m_mapping.output.status + controllerDebug("sending MIDI bytes:" << m_mapping.output.status << "," << m_mapping.output.control << "," - << byte3 ; - } + << byte3); m_pController->sendShortMsg(m_mapping.output.status, m_mapping.output.control, byte3); m_lastVal = static_cast<int>(byte3); diff --git a/src/controllers/midi/portmidicontroller.cpp b/src/controllers/midi/portmidicontroller.cpp index 4f1290a7dd..aa75175bac 100644 --- a/src/controllers/midi/portmidicontroller.cpp +++ b/src/controllers/midi/portmidicontroller.cpp @@ -8,6 +8,7 @@ */ #include "controllers/midi/portmidicontroller.h" +#include "controllers/controllerdebug.h" PortMidiController::PortMidiController(const PmDeviceInfo* inputDeviceInfo, const PmDeviceInfo* outputDeviceInfo, @@ -65,11 +66,9 @@ int PortMidiController::open() { if (m_pInputDeviceInfo) { if (isInputDevice()) { - if (debugging()) { - QDebug(QtDebugMsg) << "PortMidiController: Opening" + controllerDebug("PortMidiController: Opening" << m_pInputDeviceInfo->name << "index" - << m_iInputDeviceIndex << "for input"; - } + << m_iInputDeviceIndex << "for input"); err = Pm_OpenInput(&m_pInputStream, m_iInputDeviceIndex, @@ -86,11 +85,9 @@ int PortMidiController::open() { } if (m_pOutputDeviceInfo) { if (isOutputDevice()) { - if (debugging()) { - QDebug(QtDebugMsg) << "PortMidiController: Opening" + controllerDebug("PortMidiController: Opening" << m_pOutputDeviceInfo->name << "index" - << m_iOutputDeviceIndex << "for output"; - } + << m_iOutputDeviceIndex << "for output"); err = Pm_OpenOutput(&m_pOutputStream, m_iOutputDeviceIndex, |