summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorRoland Reichwein <mail@reichwein.it>2025-01-03 18:36:05 +0100
committerRoland Reichwein <mail@reichwein.it>2025-01-03 18:36:05 +0100
commit1f1e71e0d9e1a60eac38040ea5e6a49c14d81b71 (patch)
treebb924a1605e66974de6fe8bfc890b0cda312aef3
parent7d98b5d410233fd9608ed5682f5a98b283f83d12 (diff)
Added log
-rw-r--r--Makefile3
-rw-r--r--UI.cpp8
-rw-r--r--config.h1
-rw-r--r--log.cpp4
-rw-r--r--log.h85
-rw-r--r--main.cpp19
6 files changed, 110 insertions, 10 deletions
diff --git a/Makefile b/Makefile
index 1fc0bbd..4c58223 100644
--- a/Makefile
+++ b/Makefile
@@ -11,7 +11,8 @@ SRCS= \
UI.cpp \
Timer.cpp \
debug.cpp \
- cpuload.cpp
+ cpuload.cpp \
+ log.cpp
HEADERS=$(SRCS:.cpp=.h)
diff --git a/UI.cpp b/UI.cpp
index f9b2512..57c79ec 100644
--- a/UI.cpp
+++ b/UI.cpp
@@ -1,6 +1,7 @@
#include "UI.h"
#include "cpuload.h"
+#include "log.h"
#include <algorithm>
#include <iostream>
@@ -33,6 +34,8 @@ void UI::draw()
std::vector<int> cpuloads = get_cpu_loads();
int main_loops_per_second = get_main_loops_per_second();
+ // clear screen
+ std::cout << "\x1B[2J\x1B[H";
std::cout << std::endl;
std::cout << "- -- BPM +" << std::endl;
std::cout << "Mode: Click __/__ (Clock Internal)" << std::endl;
@@ -54,11 +57,14 @@ void UI::draw()
std::cout << " Click: ____ BPM" << std::endl;
std::cout << fmt::format(" Main loops/s: {}", main_loops_per_second) << std::endl;
+
+ std::cout << "Log:" << std::endl;
+ std::cout << log_cout.get_log() << std::endl;
}
void UI::count_main_loops()
{
++m_main_loops;
- //std::cout << "DEBUG:" << m_main_loops << std::endl;
+ debug_cout << "DEBUG:" << m_main_loops << std::endl;
}
diff --git a/config.h b/config.h
index 8c6f1ce..b1ee6a4 100644
--- a/config.h
+++ b/config.h
@@ -8,4 +8,5 @@ static const int CLICK_CHANNEL = 4;
extern const char *device; // playback device
const snd_pcm_sframes_t nframes = 1024; // ~1/44th sec buffer size
const unsigned int f_sample = 44100;
+const int log_lines = 10;
diff --git a/log.cpp b/log.cpp
new file mode 100644
index 0000000..7698e4f
--- /dev/null
+++ b/log.cpp
@@ -0,0 +1,4 @@
+#include "log.h"
+
+log_stream log_cout;
+
diff --git a/log.h b/log.h
new file mode 100644
index 0000000..cfc4d4c
--- /dev/null
+++ b/log.h
@@ -0,0 +1,85 @@
+#pragma once
+
+#include <iostream>
+#include <ostream>
+#include <sstream>
+#include <string>
+
+class log_stream
+{
+public:
+ log_stream(): m_active(false), m_buffer(), m_log_lines() {}
+
+ void log_lines(int n) {
+ m_log_lines = n;
+ }
+
+ std::string get_log() {
+ return m_buffer.str();
+ }
+
+ // log to buffer
+ void activate()
+ {
+ m_active = true;
+ }
+
+ // log to plain console
+ void deactivate()
+ {
+ m_active = false;
+ }
+
+ template<typename T>
+ log_stream& operator<<(const T& arg) {
+ if (m_active) {
+ m_buffer << arg;
+ trim_buffer();
+ }
+ else
+ {
+ std::cout << arg;
+ }
+ return *this;
+ }
+
+ log_stream& operator<<(
+ std::basic_ostream<char>& (*func)
+ (std::basic_ostream<char>&) ) {
+ if (m_active) {
+ m_buffer << *func;
+ trim_buffer();
+ }
+ else
+ {
+ std::cout << *func;
+ }
+ return *this;
+ }
+
+private:
+ void trim_buffer()
+ {
+ std::string s = m_buffer.str();
+ size_t pos = s.npos;
+ for (int i = 0; i <= m_log_lines; ++i) {
+ pos = s.rfind("\n", pos);
+ if (pos == s.npos) {
+ // too few lines
+ return;
+ }
+ if (pos > 0) {
+ --pos;
+ }
+ }
+
+ m_buffer.str(s.substr((pos <= (s.size() - 2)) ? pos + 2 : pos));
+ }
+
+ bool m_active;
+ std::stringstream m_buffer;
+ int m_log_lines;
+};
+
+extern log_stream log_cout;
+
diff --git a/main.cpp b/main.cpp
index 4c33bc4..8fb4822 100644
--- a/main.cpp
+++ b/main.cpp
@@ -4,6 +4,7 @@
#include "Timer.h"
#include "UI.h"
#include "config.h"
+#include "log.h"
#include <chrono>
#include <cmath>
@@ -32,6 +33,8 @@ int main(void)
{
try {
//debug_cout.activate();
+ log_cout.activate();
+ log_cout.log_lines(log_lines);
MIDI midi;
ClickStream stream;
@@ -40,19 +43,19 @@ int main(void)
pcm.write();
- Timer timer_300ms(300ms, true);
- timer_300ms.start();
+ Timer timer_500ms(500ms, true);
+ timer_500ms.start();
// Main signals
boost::signals2::signal<void()> signal_count_loops;
// Signal-Slot Connections:
midi.signal_click.connect([&](){stream.click();});
- timer_300ms.elapsed.connect([&](){ui.draw();});
+ timer_500ms.elapsed.connect([&](){ui.draw();});
signal_count_loops.connect([&](){ui.count_main_loops();});
while (true) {
- //std::cout << "Main loop entered." << std::endl;
+ debug_cout << "Main loop entered." << std::endl;
signal_count_loops();
fd_set read_set;
@@ -67,14 +70,14 @@ int main(void)
#endif
struct timeval timeout;
- timeout.tv_sec = 1;
- timeout.tv_usec = 300000;
+ timeout.tv_sec = 0;
+ timeout.tv_usec = 20000;
int result = select(FD_SETSIZE, &read_set, nullptr/*&write_set*/, nullptr, &timeout);
if (result < 0) {
throw std::runtime_error("select() failed");
} else if (result == 0) {
- std::cout << "select() timeout" << std::endl;
+ debug_cout << "select() timeout" << std::endl;
}
while (midi.event_ready())
@@ -91,7 +94,7 @@ int main(void)
}
// handle timers, TODO: make updates more efficient at scale
- timer_300ms.update();
+ timer_500ms.update();
}
} catch (const std::exception& ex) {
std::cerr << "Error: " << ex.what() << std::endl;