forked from aristocratos/btop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbtop_log.cpp
More file actions
160 lines (134 loc) · 3.74 KB
/
Copy pathbtop_log.cpp
File metadata and controls
160 lines (134 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// SPDX-License-Identifier: Apache-2.0
#include "btop_log.hpp"
#include "btop_shared.hpp"
#include <algorithm>
#include <cerrno>
#include <chrono>
#include <cstring>
#include <filesystem>
#include <fstream>
#include <ios>
#include <iterator>
#include <mutex>
#include <optional>
#include <stdexcept>
#include <string>
#include <string_view>
#include <system_error>
#include <utility>
#include <fmt/base.h>
#include <fmt/chrono.h>
#include <fmt/format.h>
#include <fmt/std.h>
#include <sys/types.h>
#include <unistd.h>
namespace fs = std::filesystem;
namespace Logger {
namespace {
constexpr auto ONE_MEGABYTE = 1024 << 10;
struct State {
std::mutex lock {};
std::once_flag print_header;
Level level = Level::ERROR;
std::optional<fs::path> path {};
};
[[nodiscard]] auto get_state() -> State& {
static State state;
return state;
}
auto rotate_log_file(fs::path& path) {
std::error_code errc {};
if (fs::exists(path, errc) && !errc && fs::file_size(path, errc) > ONE_MEGABYTE && !errc) {
auto old_path = path;
old_path += ".1";
if (fs::exists(old_path, errc) && !errc) {
fs::remove(old_path, errc);
}
if (!errc) {
fs::rename(path, old_path, errc);
}
}
return !errc;
}
class DropPrivilegeGuard {
private:
uid_t saved_euid {};
public:
DropPrivilegeGuard() {
saved_euid = geteuid();
auto real_uid = getuid();
if (saved_euid != real_uid && seteuid(real_uid) != 0) {
throw std::runtime_error(
fmt::format("Failed to drop privileges to write log file: {}", strerror(errno))
);
}
}
~DropPrivilegeGuard() noexcept {
if (saved_euid != geteuid()) {
// Silently drop error status.
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-result"
seteuid(saved_euid);
#pragma GCC diagnostic pop
}
}
DropPrivilegeGuard(const DropPrivilegeGuard&) = delete;
DropPrivilegeGuard& operator=(const DropPrivilegeGuard&) = delete;
};
} // namespace
void init(const fs::path& path) {
auto& state = get_state();
std::lock_guard guard { state.lock };
state.path = std::make_optional(path);
}
void set_log_level(Level level) {
auto& state = get_state();
std::lock_guard guard { state.lock };
state.level = level;
}
void set_log_level(const std::string_view level) {
auto& state = get_state();
auto it = std::ranges::find(log_levels, level);
if (it != log_levels.end()) {
std::lock_guard guard { state.lock };
state.level = static_cast<Level>(std::distance(log_levels.begin(), it));
}
}
namespace detail {
[[nodiscard]] auto is_enabled(Level level) -> bool {
auto& state = get_state();
return state.level >= level && state.path.has_value();
}
void log_write(Level level, const std::string_view msg) {
auto& state = get_state();
auto guard = std::lock_guard { state.lock };
if (!is_enabled(level) || !state.path.has_value()) {
return;
}
std::string buffer {};
std::call_once(state.print_header, [&buffer]() {
fmt::format_to(std::back_inserter(buffer), "\n===> btop++ v{}\n", Global::Version);
});
auto now = std::chrono::system_clock::now();
auto seconds = std::chrono::time_point_cast<std::chrono::seconds>(now);
fmt::format_to(
std::back_inserter(buffer),
"{:%FT%T}Z | {}: {}\n",
seconds,
log_levels.at(std::to_underlying(level)),
msg
);
DropPrivilegeGuard privilege_guard {};
auto& path = state.path.value();
if (!rotate_log_file(path)) {
return;
}
auto file = std::ofstream { path, std::ios::app };
if (!file.is_open()) {
return;
}
file << buffer;
}
} // namespace detail
} // namespace Logger
const std::vector<std::string> Logger::log_levels = { "DISABLED", "ERROR", "WARNING", "INFO", "DEBUG" };