Skip to content

Commit f5c696f

Browse files
author
Tom Steer
committed
Add logging of incoming MIDI to file
1 parent a7f9ee1 commit f5c696f

File tree

4 files changed

+142
-4
lines changed

4 files changed

+142
-4
lines changed

UdpMidiTest.pro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
1919
# THE SOFTWARE.
2020

21-
PRODUCT_VERSION = 1.0.1
21+
PRODUCT_VERSION = 1.0.2
2222

2323
QT += core gui network xml svg
2424

src/mainwindow.cpp

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@
2626
#include <QNetworkDatagram>
2727
#include <QMetaEnum>
2828
#include <QDebug>
29+
#include <QFileDialog>
30+
#include <QDate>
31+
#include <QDateTime>
32+
#include <QAbstractEventDispatcher>
33+
#include <QTimer>
2934

3035
QByteArray eosEncode(float value)
3136
{
@@ -53,6 +58,17 @@ QString stringToHex(quint8 value)
5358
return result.toUpper();
5459
}
5560

61+
62+
static int msecsTo(const QTime & at) {
63+
const int msecsPerDay = 24 * 60 * 60 * 1000;
64+
int msecs = QTime::currentTime().msecsTo(at);
65+
if (msecs < 0) msecs += msecsPerDay;
66+
return msecs;
67+
}
68+
69+
70+
71+
5672
MainWindow::MainWindow(QWidget *parent) :
5773
QMainWindow(parent),
5874
ui(new Ui::MainWindow)
@@ -120,6 +136,9 @@ MainWindow::MainWindow(QWidget *parent) :
120136
connect(ui->cbMSCCommand, SIGNAL(currentIndexChanged(int)), this, SLOT(updateMscCommand()));
121137
connect(ui->cbMSCCommandFormat, SIGNAL(currentIndexChanged(int)), this, SLOT(updateMscCommand()));
122138
connect(ui->leMSCData, SIGNAL(textChanged(QString)), this, SLOT(updateMscCommand()));
139+
140+
updateLogFileDisplay();
141+
123142
}
124143

125144
MainWindow::~MainWindow()
@@ -195,6 +214,7 @@ void MainWindow::readData()
195214
{
196215
while (m_rxSocket->hasPendingDatagrams())
197216
{
217+
m_msgCounter++;
198218
QNetworkDatagram datagram = m_rxSocket->receiveDatagram();
199219

200220
if(ui->cbLogAllInput->isChecked())
@@ -205,6 +225,16 @@ void MainWindow::readData()
205225
.arg(QString(datagram.data()))
206226
);
207227
}
228+
if(m_logFile)
229+
{
230+
QString logMsg = QString("%1,%2,%3\r\n")
231+
.arg(QTime::currentTime().toString("hh:mm:ss:zzz"))
232+
.arg(datagram.senderAddress().toString())
233+
.arg(QString::fromLatin1(datagram.data()));
234+
m_logFile->write(logMsg.toUtf8());
235+
m_logFile->flush();
236+
updateLogFileDisplay();
237+
}
208238

209239

210240
midiMessageRecieve(datagram.data());
@@ -443,3 +473,66 @@ void MainWindow::on_btnMSCSend_pressed()
443473
{
444474
midiMessageSend((quint8*)m_mscCommand.data(), m_mscCommand.length());
445475
}
476+
477+
void MainWindow::on_cbLogToFile_pressed()
478+
{
479+
m_msgCounter = 0;
480+
if(!ui->cbLogToFile->isChecked())
481+
{
482+
m_logFileName = QFileDialog::getSaveFileName(this, tr("Save Log File"), QString(), tr("Log Files (*.log)"));
483+
if(m_logFileName.isEmpty())
484+
{
485+
m_logFileName = "";
486+
m_logFile = Q_NULLPTR;
487+
ui->cbLogToFile->setChecked(false);
488+
return;
489+
}
490+
m_logFileName.chop(3); // Remove the .log postfix
491+
rotateLogFile();
492+
ui->cbLogToFile->setChecked(true);
493+
494+
495+
// Rotate the log at midnight
496+
auto timer = new QTimer(QAbstractEventDispatcher::instance());
497+
timer->start(msecsTo(QTime(0,0,0)));
498+
QObject::connect(timer, &QTimer::timeout, [=, &timer]{
499+
this->rotateLogFile();
500+
timer->deleteLater();
501+
});
502+
}
503+
else {
504+
ui->lbLogInfo->clear();
505+
}
506+
}
507+
508+
void MainWindow::rotateLogFile()
509+
{
510+
if(m_logFile)
511+
{
512+
m_logFile->close();
513+
m_logFile = Q_NULLPTR;
514+
}
515+
516+
QString fileName = m_logFileName + QString("%1.log").arg(QDate::currentDate().toString("yy_MM_dd"));
517+
518+
m_logFile = new QFile(fileName);
519+
if(!m_logFile->open(QIODevice::WriteOnly))
520+
{
521+
ui->cbLogToFile->setChecked(false);
522+
m_logFile = Q_NULLPTR;
523+
}
524+
updateLogFileDisplay();
525+
526+
}
527+
528+
void MainWindow::updateLogFileDisplay()
529+
{
530+
if(!m_logFile)
531+
ui->lbLogInfo->clear();
532+
else {
533+
QString logInfo = tr("Logging to %1 : %2 messages")
534+
.arg(m_logFile->fileName())
535+
.arg(m_msgCounter);
536+
ui->lbLogInfo->setText(logInfo);
537+
}
538+
}

src/mainwindow.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <QMainWindow>
2525
#include <QTimer>
2626
#include <QUdpSocket>
27+
#include <QFile>
2728
#include "windows.h"
2829

2930
namespace Ui {
@@ -48,7 +49,10 @@ private slots:
4849
void on_leMSCData_textChanged(const QString &text);
4950
void updateMscCommand();
5051
void on_btnMSCSend_pressed();
52+
void on_cbLogToFile_pressed();
5153
private:
54+
void rotateLogFile();
55+
void updateLogFileDisplay();
5256
Ui::MainWindow *ui;
5357
QUdpSocket *m_txSocket;
5458
QUdpSocket *m_rxSocket;
@@ -57,6 +61,9 @@ private slots:
5761
void midiMessageRecieve(const QByteArray &data);
5862
QByteArray m_mscCommand;
5963
QByteArray m_mscData;
64+
QString m_logFileName;
65+
QFile *m_logFile = Q_NULLPTR;
66+
int m_msgCounter = 0;
6067
};
6168

6269

src/mainwindow.ui

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@
100100
<item>
101101
<widget class="QTabWidget" name="tabWidget">
102102
<property name="currentIndex">
103-
<number>1</number>
103+
<number>0</number>
104104
</property>
105105
<widget class="QWidget" name="tab">
106106
<attribute name="title">
@@ -119,7 +119,7 @@
119119
<item row="1" column="0">
120120
<widget class="QTabWidget" name="tabWidget_2">
121121
<property name="currentIndex">
122-
<number>1</number>
122+
<number>0</number>
123123
</property>
124124
<widget class="QWidget" name="tab_3">
125125
<attribute name="title">
@@ -319,10 +319,48 @@ font: 8pt &quot;Courier New&quot;;</string>
319319
<item>
320320
<widget class="QCheckBox" name="cbLogAllInput">
321321
<property name="text">
322-
<string>Log All Messages</string>
322+
<string>Display All Messages</string>
323323
</property>
324324
</widget>
325325
</item>
326+
<item>
327+
<layout class="QHBoxLayout" name="horizontalLayout_3">
328+
<item>
329+
<widget class="QCheckBox" name="cbLogToFile">
330+
<property name="text">
331+
<string>Log to file</string>
332+
</property>
333+
</widget>
334+
</item>
335+
<item>
336+
<widget class="Line" name="line">
337+
<property name="orientation">
338+
<enum>Qt::Vertical</enum>
339+
</property>
340+
</widget>
341+
</item>
342+
<item>
343+
<widget class="QLabel" name="lbLogInfo">
344+
<property name="text">
345+
<string>Logging to : %1 : 1234 Messages</string>
346+
</property>
347+
</widget>
348+
</item>
349+
<item>
350+
<spacer name="horizontalSpacer_4">
351+
<property name="orientation">
352+
<enum>Qt::Horizontal</enum>
353+
</property>
354+
<property name="sizeHint" stdset="0">
355+
<size>
356+
<width>40</width>
357+
<height>20</height>
358+
</size>
359+
</property>
360+
</spacer>
361+
</item>
362+
</layout>
363+
</item>
326364
<item>
327365
<widget class="QListWidget" name="lvRxMessages"/>
328366
</item>

0 commit comments

Comments
 (0)