Skip to content

Commit b9bee0c

Browse files
author
Kirill Belousov
committed
Feature: Adds command line arguments to set initial voice and device and to enable logging in release builds
1 parent 6795a5f commit b9bee0c

5 files changed

Lines changed: 61 additions & 23 deletions

File tree

CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@ target_compile_definitions(sim PRIVATE
3939
include(FetchContent)
4040
set(FETCHCONTENT_BASE_DIR ${CMAKE_BINARY_DIR}/_dependencies)
4141

42+
# cli11
43+
FetchContent_Declare(CLI11
44+
GIT_REPOSITORY https://github.com/CLIUtils/CLI11
45+
GIT_TAG v2.5.0
46+
GIT_SHALLOW ON
47+
SYSTEM
48+
)
49+
FetchContent_MakeAvailable(CLI11)
50+
4251
# miniaudio
4352
FetchContent_Declare(miniaudio
4453
GIT_REPOSITORY https://github.com/mackron/miniaudio.git
@@ -129,6 +138,7 @@ target_include_directories(sim PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/src")
129138

130139
target_link_libraries(sim PRIVATE
131140
miniaudio
141+
CLI11::CLI11
132142
SRAL::SRAL_static
133143
spdlog::spdlog_header_only
134144
wx::core

src/loggerSetup.cpp

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,37 @@
1313

1414
constexpr int LOGGER_THREAD_POOL_QUEUE_SIZE = 8192;
1515
constexpr int LOGGER_THREAD_POOL_BACKING_THREAD_COUNT = 1;
16-
#ifndef NDEBUG
17-
constexpr const char* LOG_FORMAT = "%R Level: %l, Thread: %t, Message: %v";
18-
#else
1916
constexpr const char* LOG_FORMAT = "%R Level: %l, Thread: %t, Message: %v";
20-
#endif
2117

22-
void InitializeLogging(int argc, char* argv[]) {
18+
void InitializeLogging(int argc, char* argv[], bool isDebuggingEnabled) {
2319
try {
24-
#ifdef DEBUG
25-
spdlog::set_level(spdlog::level::debug);
20+
#ifndef NDEBUG
21+
bool isDebugBuild = true;
2622
#else
27-
spdlog::set_level(spdlog::level::warn);
23+
bool isDebugBuild = false;
2824
#endif
29-
spdlog::cfg::load_env_levels();
30-
spdlog::cfg::load_argv_levels(argc, argv);
25+
// spdlog::cfg::load_env_levels();
26+
// spdlog::cfg::load_argv_levels(argc, argv);
3127
spdlog::init_thread_pool(LOGGER_THREAD_POOL_QUEUE_SIZE, LOGGER_THREAD_POOL_BACKING_THREAD_COUNT);
3228
auto consoleSink = std::make_shared<spdlog::sinks::stdout_sink_mt>();
3329
auto fileSink = std::make_shared<spdlog::sinks::basic_file_sink_mt>("sim.log", true);
3430
std::vector<spdlog::sink_ptr> sinks{consoleSink, fileSink};
3531
auto logger = std::make_shared<spdlog::async_logger>(
3632
"simlogger", sinks.begin(), sinks.end(), spdlog::thread_pool(), spdlog::async_overflow_policy::block);
37-
logger->set_level(spdlog::level::trace);
3833
logger->set_pattern(LOG_FORMAT);
34+
if (isDebuggingEnabled || isDebugBuild) {
35+
logger->set_level(spdlog::level::trace);
36+
} else {
37+
logger->set_level(spdlog::level::warn);
38+
}
3939
spdlog::register_logger(logger);
4040
spdlog::set_default_logger(logger);
41+
if (isDebugBuild) {
42+
spdlog::debug("Log level is set to trace because of debug build");
43+
}
44+
if (isDebuggingEnabled) {
45+
spdlog::debug("Log level is set to debug via command line parameter");
46+
}
47+
std::atexit(spdlog::shutdown);
4148
} catch (spdlog::spdlog_ex& ex) { std::cerr << "Unable to initialize logger: " << ex.what() << '\n'; }
4249
}

src/loggerSetup.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
#pragma once
22

3-
void InitializeLogging(int argc, char* argv[]);
3+
void InitializeLogging(int argc, char* argv[], bool isDebuggingEnabled);

src/ui.cpp

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,16 @@
55
#include "loggerSetup.h"
66
#include "speech.h"
77

8+
#include <CLI/CLI.hpp>
89
#include <spdlog/spdlog.h>
910
#include <string>
1011
#include <wx/string.h>
1112

12-
MainFrame::MainFrame(const wxString& title) : wxFrame(nullptr, wxID_ANY, title) {
13+
MainFrame::MainFrame(const wxString& title, int cliVoiceIndex, int cliOutputDeviceIndex, bool cliIsHelpRequested)
14+
: wxFrame(nullptr, wxID_ANY, title) {
15+
m_cliVoiceIndex = cliVoiceIndex;
16+
m_cliOutputDeviceIndex = cliOutputDeviceIndex;
17+
m_cliIsHelpRequested = cliIsHelpRequested;
1318
m_panel = new wxPanel(this, wxID_ANY);
1419
auto* mainSizer = new wxBoxSizer(wxVERTICAL);
1520
auto* selectionsSizer = new wxBoxSizer(wxHORIZONTAL);
@@ -80,7 +85,7 @@ void MainFrame::populateVoicesList() {
8085
for (const auto& voiceName : voices) {
8186
m_voicesList->AppendString(wxString::FromUTF8(voiceName));
8287
}
83-
m_voicesList->SetSelection(0);
88+
m_voicesList->SetSelection(m_cliVoiceIndex);
8489
}
8590

8691
void MainFrame::populateDevicesList() {
@@ -94,7 +99,7 @@ void MainFrame::populateDevicesList() {
9499
auto isDefaultStr = device.isDefault ? "[default]" : "";
95100
m_outputDevicesList->AppendString(wxString::FromUTF8(std::format("{} {}", isDefaultStr, device.name)));
96101
}
97-
m_outputDevicesList->SetSelection(0);
102+
m_outputDevicesList->SetSelection(m_cliOutputDeviceIndex);
98103
}
99104

100105
void MainFrame::OnRateSliderChange(wxCommandEvent& event) {
@@ -151,13 +156,24 @@ void MainFrame::OnCharEvent(wxKeyEvent& event) {
151156
}
152157

153158
bool MyApp::OnInit() {
154-
InitializeLogging(MyApp::argc, MyApp::argv);
155-
if (!wxApp::OnInit()) {
156-
spdlog::critical("Failed to initialize WX");
157-
return false;
158-
}
159-
MainFrame* frame = new MainFrame("SIM test");
159+
CLI::App cliApp{"SIM - Speak Instead of Me speech utility"};
160+
auto argv = cliApp.ensure_utf8(MyApp::argv);
161+
bool cliIsDebugEnabled = false;
162+
cliApp.add_flag("-D,--debug", cliIsDebugEnabled, "Enable the debug logging for release builds");
163+
int cliVoiceIndex = 0;
164+
cliApp.add_option("-v,--voice", cliVoiceIndex, "Specify SAPI voice index to be selected at program start");
165+
int cliOutputDeviceIndex = 0;
166+
cliApp.add_option("-d,--device", cliOutputDeviceIndex,
167+
"Specify output device number to be selected at program start");
168+
bool cliIsHelpRequested = false;
169+
CLI11_PARSE(cliApp, MyApp::argc, argv);
170+
InitializeLogging(MyApp::argc, MyApp::argv, cliIsDebugEnabled);
171+
auto* frame = new MainFrame("SIM test", cliVoiceIndex, cliOutputDeviceIndex, cliIsHelpRequested);
160172
frame->Show(true);
161173
spdlog::debug("Main window shown");
162174
return true;
163175
}
176+
177+
void MyApp::OnInitCmdLine(wxCmdLineParser& parser) {
178+
// Left empty to bypass wxWidgets cli parsing
179+
}

src/ui.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55

66
class MainFrame : public wxFrame {
77
public:
8-
MainFrame(const wxString& title);
8+
MainFrame(const wxString& title, int cliVoiceIndex = 0, int cliOutputDeviceIndex = 0,
9+
bool cliIsHelpRequested = false);
910

1011
private:
1112
wxPanel* m_panel;
@@ -14,6 +15,9 @@ class MainFrame : public wxFrame {
1415
wxListBox* m_outputDevicesList;
1516
wxSlider* m_rateSlider;
1617
wxSlider* m_volumeSlider;
18+
int m_cliVoiceIndex = 0;
19+
int m_cliOutputDeviceIndex = 0;
20+
bool m_cliIsHelpRequested = false;
1721

1822
void populateVoicesList();
1923
void populateDevicesList();
@@ -29,5 +33,6 @@ class MainFrame : public wxFrame {
2933

3034
class MyApp : public wxApp {
3135
public:
32-
virtual bool OnInit();
36+
virtual bool OnInit() override;
37+
virtual void OnInitCmdLine(wxCmdLineParser& parser) override;
3338
};

0 commit comments

Comments
 (0)