Skip to content

Commit 72d5ddd

Browse files
committed
use the Qt logging system
1 parent 89a9f32 commit 72d5ddd

File tree

9 files changed

+63
-23
lines changed

9 files changed

+63
-23
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ add_subdirectory(note)
7171
add_subdirectory(chord)
7272
add_subdirectory(settings)
7373

74-
qt_add_executable(chordless main.cc)
74+
qt_add_executable(chordless main.cc logging.cpp logging.h)
7575

7676

7777
# Add QML files as resources

chord/chord_matcher_reader.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "chord_matcher_reader.h"
2+
#include "../logging.h"
23

34
#include "chord_matcher.h"
45
#include "chord_matcher_config.h"
@@ -9,15 +10,14 @@
910

1011
#include <QFile>
1112
#include <QFileInfo>
12-
#include <iostream>
1313
#include <memory>
1414

1515
namespace chordless::chord {
16-
void configureChordObserver(ChordObserver &observer, const std::string &file_name) {
17-
// Slurp the file into a string/buffer using Qt's file API (supports qrc://)
16+
void ConfigureChordObserver(ChordObserver &observer, const std::string &file_name) {
17+
// Load chord configuration from file (supports qrc:// resources)
1818
QFile file(QString::fromStdString(file_name));
1919
if (!file.open(QIODevice::ReadOnly)) {
20-
std::cerr << "Failed to read config file\n";
20+
LOG_ERROR(configCategory, "Failed to read chord config file:" << file_name);
2121
return;
2222
}
2323

@@ -51,10 +51,10 @@ namespace chordless::chord {
5151
observer.AddMatcher(std::move(matcher));
5252
}
5353
} catch (const std::exception &e) {
54-
std::cerr << "Failed to parse JSON: " << e.what() << std::endl;
54+
LOG_ERROR(configCategory, "Failed to parse chord configuration JSON:" << e.what());
5555
}
5656
} else {
57-
std::cerr << "Failed to read file data as a string\n";
57+
LOG_ERROR(configCategory, "Chord configuration file is empty");
5858
}
5959
}
6060
}

chord/chord_matcher_reader.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44

55
namespace chordless::chord {
66
class ChordObserver;
7-
8-
void configureChordObserver(ChordObserver&, const std::string &file_name);
7+
8+
void ConfigureChordObserver(ChordObserver&, const std::string &file_name);
99
}

input/alsa/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
add_library(alsa_input alsa_input.cc alsa_input.h ../note_input.h)
2-
target_link_libraries(alsa_input asound note_event)
2+
target_link_libraries(alsa_input PRIVATE asound note_event Qt6::Core)

input/alsa/alsa_input.cc

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,36 @@
11
#include "alsa_input.h"
2+
#include "../../logging.h"
23
#include <utility>
34

45
namespace chordless {
56
namespace alsa {
67
AlsaInput::AlsaInput() {
78
error_ = snd_seq_open(&seq_handle_, "default", SND_SEQ_OPEN_INPUT, SND_SEQ_NONBLOCK);
8-
if (error_ != 0) {
9-
return;
9+
if (error_ < 0) {
10+
LOG_ERROR(alsaCategory, "Failed to open sequencer:" << snd_strerror(error_));
11+
return;
1012
}
11-
snd_seq_set_client_name(seq_handle_, "chordless");
1213

14+
// Set client name
15+
int name_result = snd_seq_set_client_name(seq_handle_, "chordless");
16+
if (name_result < 0) {
17+
LOG_WARNING(alsaCategory, "Failed to set client name:" << snd_strerror(name_result));
18+
// Continue anyway, this is not fatal
19+
}
20+
21+
// Create port
1322
error_ = snd_seq_create_simple_port(seq_handle_, "Input MIDI",
14-
SND_SEQ_PORT_CAP_WRITE | SND_SEQ_PORT_CAP_SUBS_WRITE,
15-
SND_SEQ_PORT_TYPE_MIDI_GENERIC );
23+
SND_SEQ_PORT_CAP_WRITE | SND_SEQ_PORT_CAP_SUBS_WRITE,
24+
SND_SEQ_PORT_TYPE_MIDI_GENERIC);
25+
if (error_ < 0) {
26+
LOG_ERROR(alsaCategory, "Failed to create MIDI port:" << snd_strerror(error_));
27+
snd_seq_close(seq_handle_);
28+
seq_handle_ = nullptr;
29+
return;
30+
}
31+
32+
LOG_INFO(alsaCategory, "ALSA initialized successfully (client:"
33+
<< snd_seq_client_id(seq_handle_) << ", port:" << error_ << ")");
1634
}
1735

1836
AlsaInput::~AlsaInput() {

logging.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include "logging.h"
2+
3+
// Define logging categories
4+
Q_LOGGING_CATEGORY(alsaCategory, "chordless.alsa")
5+
Q_LOGGING_CATEGORY(chordCategory, "chordless.chord")
6+
Q_LOGGING_CATEGORY(configCategory, "chordless.config")
7+
Q_LOGGING_CATEGORY(generalCategory, "chordless.general")

logging.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#pragma once
2+
3+
#include <QLoggingCategory>
4+
5+
// Declare logging categories for different subsystems
6+
Q_DECLARE_LOGGING_CATEGORY(alsaCategory)
7+
Q_DECLARE_LOGGING_CATEGORY(chordCategory)
8+
Q_DECLARE_LOGGING_CATEGORY(configCategory)
9+
Q_DECLARE_LOGGING_CATEGORY(generalCategory)
10+
11+
// Convenience macros for common logging patterns
12+
#define LOG_INFO(category, msg) qCInfo(category) << msg
13+
#define LOG_WARNING(category, msg) qCWarning(category) << msg
14+
#define LOG_ERROR(category, msg) qCCritical(category) << msg
15+
#define LOG_DEBUG(category, msg) qCDebug(category) << msg

main.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "note/note_reader.h"
77
#include "note/scientific_note_namer.h"
88
#include "settings/settings.h"
9+
#include "logging.h"
910

1011
#include <QApplication>
1112
#include <QCoreApplication>
@@ -15,7 +16,6 @@
1516
#include <QQuickView>
1617
#include <QQmlEngine>
1718

18-
#include <iostream>
1919
#include <memory>
2020
#include <string>
2121
#include <utility>
@@ -34,11 +34,11 @@ constexpr int chord_label_w = full_voice_label_w;
3434
constexpr int chord_label_h = full_voice_label_h;
3535

3636
int main(int argc, char **argv) {
37-
std::cout << "chordless v" << CHORDLESS_VERSION << std::endl;
37+
LOG_INFO(generalCategory, "chordless v" << CHORDLESS_VERSION);
3838

3939
chordless::alsa::AlsaInput alsa_input;
4040
if (!alsa_input.IsValid()) {
41-
std::cerr << "Failed to open ALSA sequencer/port" << std::endl;
41+
LOG_ERROR(alsaCategory, "Failed to open ALSA sequencer/port");
4242
exit(EXIT_FAILURE);
4343
}
4444

@@ -59,7 +59,7 @@ int main(int argc, char **argv) {
5959
return config_result;
6060
}
6161

62-
chordless::chord::configureChordObserver(chord_observer, settings.ChordsFile());
62+
chordless::chord::ConfigureChordObserver(chord_observer, settings.ChordsFile());
6363

6464
note_reader.start();
6565

settings/settings.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#include "settings.h"
2+
#include "../logging.h"
23

34
#include <boost/program_options.hpp>
45

5-
#include <iostream>
66
#include <filesystem>
77

88
namespace chordless::settings {
@@ -20,17 +20,17 @@ namespace chordless::settings {
2020
po::store(po::parse_command_line(argc, argv, desc), vm);
2121
po::notify(vm);
2222
} catch (...) {
23-
std::cerr << desc << std::endl;
23+
LOG_ERROR(configCategory, "Invalid command line options. Usage information available with --help");
2424
return 1;
2525
}
2626
if (vm.count("config")) {
2727
// User provided a config file - make it absolute for local files
2828
chords_file_ = std::filesystem::absolute(vm["config"].as<std::string>());
29-
std::cout << "Using chord config file: " << chords_file_ << std::endl;
29+
LOG_INFO(configCategory, "Using chord config file:" << chords_file_);
3030
} else {
3131
// Use embedded resource as default
3232
chords_file_ = ":/chords.json";
33-
std::cout << "Using default chord config" << std::endl;
33+
LOG_INFO(configCategory, "Using default chord config");
3434
}
3535

3636
sharp_ = vm["sharp"].as<bool>();

0 commit comments

Comments
 (0)