Repository navigation
Expand file tree
/
Copy pathcapture.cpp
More file actions
64 lines (60 loc) · 2.09 KB
/
Copy pathcapture.cpp
File metadata and controls
64 lines (60 loc) · 2.09 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
#include "capture.hpp"
#include <cctype>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <unistd.h>
std::string quote_pipe(std::string_view text) {
bool quote = false;
size_t clean = 0;
for (char c : text) {
if (c == '\0') continue;
clean++;
if (c == '|' || c == '"' || c == '\r' || c == '\n') quote = true;
}
if (clean == 0) return {};
std::string out;
out.reserve(clean + (quote ? 2 : 0));
if (quote) out.push_back('"');
for (char c : text) {
if (c == '\0') continue;
if (c == '"' && quote) out.push_back('"');
out.push_back(c);
}
if (quote) out.push_back('"');
return out;
}
std::string sanitize_host(std::string_view host) {
std::string out;
out.reserve(host.size());
for (char c : host) {
unsigned char uc = static_cast<unsigned char>(c);
out.push_back(std::isalnum(uc) || c == '-' || c == '_' ? c : '_');
}
return out.empty() ? std::string("host") : out;
}
bool Capture::toggle() {
if (file_) { file_.reset(); return false; }
char host[64] = "host";
if (gethostname(host, sizeof(host) - 1) != 0)
std::snprintf(host, sizeof(host), "host");
auto now = std::time(nullptr);
std::tm tm{};
char name[160];
if (!localtime_r(&now, &tm))
std::snprintf(name, sizeof(name), "strace_%s.log", sanitize_host(host).c_str());
else
std::snprintf(name, sizeof(name), "strace_%s_%04d%02d%02d_%02d%02d%02d.log",
sanitize_host(host).c_str(), tm.tm_year + 1900, tm.tm_mon + 1,
tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
file_.reset(std::fopen(name, "w"));
if (file_) std::fputs("Time|PID|Process|SysID|Syscall|Parameters\n", file_.get());
return static_cast<bool>(file_);
}
void Capture::write(const UiEvent &e) {
if (!file_) return;
std::fprintf(file_.get(), "%s|%d|%s|%d|%s|%s\n",
quote_pipe(std::to_string(e.ts_ns)).c_str(), e.pid,
quote_pipe(e.comm).c_str(), e.nr,
quote_pipe(e.sys_name).c_str(), quote_pipe(e.extra).c_str());
}