summaryrefslogtreecommitdiff
path: root/src/debug/RollingLogFollow.hpp
diff options
context:
space:
mode:
authorAlexander <51529891+Truenya@users.noreply.github.com>2024-06-14 13:11:40 +0300
committerGitHub <noreply@github.com>2024-06-14 12:11:40 +0200
commitb2590b58c51094424a9651d8df37dfab838b5bbb (patch)
treedfd12a27cddd7b19de2eec4415630db1bd5b3c84 /src/debug/RollingLogFollow.hpp
parent9cd5b3587cb1e3d42b647fa230024cd0153ea9cb (diff)
hyprctl: added --follow option to rolliglog (#6325)
Co-authored-by: Крылов Александр <aleksandr.krylov@hyperus.team>
Diffstat (limited to 'src/debug/RollingLogFollow.hpp')
-rw-r--r--src/debug/RollingLogFollow.hpp65
1 files changed, 65 insertions, 0 deletions
diff --git a/src/debug/RollingLogFollow.hpp b/src/debug/RollingLogFollow.hpp
new file mode 100644
index 00000000..5ff018af
--- /dev/null
+++ b/src/debug/RollingLogFollow.hpp
@@ -0,0 +1,65 @@
+#pragma once
+
+#include <shared_mutex>
+
+namespace Debug {
+ struct RollingLogFollow {
+ std::unordered_map<int, std::string> socketToRollingLogFollowQueue;
+ std::shared_mutex m;
+ bool running = false;
+ static constexpr size_t ROLLING_LOG_FOLLOW_TOO_BIG = 8192;
+
+ // Returns true if the queue is empty for the given socket
+ bool isEmpty(int socket) {
+ std::shared_lock<std::shared_mutex> r(m);
+ return socketToRollingLogFollowQueue[socket].empty();
+ }
+
+ std::string DebugInfo() {
+ std::shared_lock<std::shared_mutex> r(m);
+ return std::format("RollingLogFollow, got {} connections", socketToRollingLogFollowQueue.size());
+ }
+
+ std::string GetLog(int socket) {
+ std::unique_lock<std::shared_mutex> w(m);
+
+ const std::string ret = socketToRollingLogFollowQueue[socket];
+ socketToRollingLogFollowQueue[socket] = "";
+
+ return ret;
+ };
+
+ void AddLog(std::string log) {
+ std::unique_lock<std::shared_mutex> w(m);
+ running = true;
+ std::vector<int> to_erase;
+ for (const auto& p : socketToRollingLogFollowQueue)
+ socketToRollingLogFollowQueue[p.first] += log + "\n";
+ }
+
+ bool IsRunning() {
+ std::shared_lock<std::shared_mutex> r(m);
+ return running;
+ }
+
+ void StopFor(int socket) {
+ std::unique_lock<std::shared_mutex> w(m);
+ socketToRollingLogFollowQueue.erase(socket);
+ if (socketToRollingLogFollowQueue.empty())
+ running = false;
+ }
+
+ void StartFor(int socket) {
+ std::unique_lock<std::shared_mutex> w(m);
+ socketToRollingLogFollowQueue[socket] = std::format("[LOG] Following log to socket: {} started\n", socket);
+ running = true;
+ }
+
+ static RollingLogFollow& Get() {
+ static RollingLogFollow instance;
+ static std::mutex gm;
+ std::lock_guard<std::mutex> lock(gm);
+ return instance;
+ };
+ };
+}