-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiskDbg.cpp
More file actions
76 lines (61 loc) · 2.21 KB
/
Copy pathDiskDbg.cpp
File metadata and controls
76 lines (61 loc) · 2.21 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
#include <cctype>
#include <chrono>
#include <cstdio>
#include <fstream>
#include <iostream>
#include <string>
#include <thread>
namespace
{
std::string runCommand(const std::string& cmd)
{
const auto pipe = popen(cmd.c_str(), "r");
if (!pipe) {
std::cerr << "Failed to run `" << cmd << "`\n";
std::exit(EXIT_FAILURE);
}
char buffer[1024];
std::string output;
while (fgets(buffer, sizeof(buffer), pipe)) { output += buffer; }
if (const auto status = pclose(pipe); status != 0) {
std::cerr << "Failed to run `" << cmd << "`\n";
std::exit(EXIT_FAILURE);
}
return output;
}
void commandLoop(const std::string& prog, const std::string& args, const std::string& date,
const std::string& timestamp)
{
const auto cmd = args.empty() ? prog : (prog + ' ' + args);
const auto filename = "/Library/Logs/" + prog + "-" + timestamp + ".txt";
const auto header =
"Command: " + cmd + "\nDate: " + date + "\n#######################################################\n\n";
size_t lastHash = 0;
while (true) {
std::string output = runCommand(cmd);
if (const auto currentHash = std::hash<std::string>{}(output); currentHash != lastHash) {
std::ofstream(filename) << header << output;
lastHash = currentHash;
}
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}
void rtrim(std::string& s)
{
s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) { return !std::isspace(ch); }).base(), s.end());
}
} // namespace
int main()
{
auto date = runCommand("date +\"%Y-%m-%dT%H:%M:%S%z\"");
rtrim(date);
auto timestamp = runCommand("date +%s");
rtrim(timestamp);
std::thread t1(commandLoop, "dmesg", "", date, timestamp);
std::thread t2(commandLoop, "ioreg", "-flxw0", date, timestamp);
std::thread t3(commandLoop, "/System/Library/Extensions/AppleGraphicsControl.kext/Contents/MacOS/AGDCDiagnose", "",
date, timestamp);
t1.join();
t2.join();
t3.join();
}