forked from pop-os/distinst
-
Notifications
You must be signed in to change notification settings - Fork 2
/
json-progress.patch
186 lines (174 loc) · 5.91 KB
/
json-progress.patch
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
From 2a82ebb244969585e4f6e6166a522d783d178474 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Maciej=20Kr=C3=BCger?= <[email protected]>
Date: Tue, 14 Jul 2020 17:50:44 +0200
Subject: [PATCH] feat: add a JSON progress bar to fd 3
---
src/libmain/progress-bar.cc | 90 ++++++++++++++++++++++++++++++++++++-
src/libutil/logging.cc | 12 +++++
src/libutil/logging.hh | 1 +
3 files changed, 101 insertions(+), 2 deletions(-)
diff --git a/src/libmain/progress-bar.cc b/src/libmain/progress-bar.cc
index 0e5432fca..86abeaad2 100644
--- a/src/libmain/progress-bar.cc
+++ b/src/libmain/progress-bar.cc
@@ -8,6 +8,7 @@
#include <map>
#include <thread>
#include <iostream>
+#include <nlohmann/json.hpp>
namespace nix {
@@ -88,13 +89,14 @@ public:
: printBuildLogs(printBuildLogs)
, isTTY(isTTY)
{
- state_.lock()->active = isTTY;
+ state_.lock()->active = true;
updateThread = std::thread([&]() {
auto state(state_.lock());
while (state->active) {
if (!state->haveUpdate)
state.wait(updateCV);
draw(*state);
+ dumpState(*state);
state.wait_for(quitCV, std::chrono::milliseconds(50));
}
});
@@ -139,6 +141,10 @@ public:
void log(State & state, Verbosity lvl, const std::string & s)
{
if (state.active) {
+ dumpState(state);
+ }
+
+ if (state.active && isTTY) {
writeToStderr("\r\e[K" + filterANSIEscapes(s, !isTTY) + ANSI_NORMAL "\n");
draw(state);
} else {
@@ -324,10 +330,46 @@ public:
updateCV.notify_one();
}
+ void dumpState(State & state)
+ {
+ state.haveUpdate = false;
+ if (!state.active || !std::getenv("DUMP_JSON")) return;
+
+ auto s = nlohmann::json::object();
+
+ s["status"] = getStatusJSON(state);
+
+ s["activities"] = nlohmann::json::array();
+
+ auto I = 0;
+
+ if (!state.activities.empty()) {
+ auto i = state.activities.rbegin();
+
+ while (i != state.activities.rend() && (!i->visible || (i->s.empty() && i->lastLine.empty())))
+ ++i;
+
+ if (i != state.activities.rend()) {
+ auto act = nlohmann::json::object();
+ s["activites"][I] = act;
+ I++;
+ act["s"] = i->s;
+ if (!i->phase.empty()) {
+ act["phase"] = i->phase;
+ }
+ if (!i->lastLine.empty()) {
+ act["lastLine"] = i->lastLine;
+ }
+ }
+ }
+
+ writeToAlien(s.dump());
+ }
+
void draw(State & state)
{
state.haveUpdate = false;
- if (!state.active) return;
+ if (!state.active || !isTTY) return;
std::string line;
@@ -365,6 +407,50 @@ public:
writeToStderr("\r" + filterANSIEscapes(line, false, width) + ANSI_NORMAL + "\e[K");
}
+ nlohmann::json getStatusJSON(State & state) {
+ auto status = nlohmann::json::object();
+
+ auto MiB = 1024.0 * 1024.0;
+
+ auto renderActivity = [&](ActivityType type, const std::string & key, const std::string & itemFmt, const std::string & numberFmt = "%d", double unit = 1) {
+ auto & act = state.activitiesByType[type];
+ uint64_t done = act.done, expected = act.done, running = 0, failed = act.failed;
+ for (auto & j : act.its) {
+ done += j.second->done;
+ expected += j.second->expected;
+ running += j.second->running;
+ failed += j.second->failed;
+ }
+
+ expected = std::max(expected, act.expected);
+
+ status[key] = nlohmann::json::object();
+
+ status[key]["unit"] = unit;
+ status[key]["running"] = running;
+ status[key]["done"] = done;
+ status[key]["expected"] = expected;
+ status[key]["failed"] = failed;
+
+ status[key]["itemFmt"] = itemFmt;
+ status[key]["numberFmt"] = numberFmt;
+ };
+
+ renderActivity(actCopyPaths, "copyPaths", "%s copied");
+ renderActivity(actCopyPath, "copyPath", "%s MiB", "%.1f", MiB);
+ renderActivity(actFileTransfer, "fileTransfer", "%s MiB DL", "%.1f", MiB);
+ renderActivity(actBuilds, "builds", "%s built");
+ renderActivity(actOptimiseStore, "optimiseStore", "%s paths optimised");
+ renderActivity(actVerifyPaths, "verifyPaths", "%s paths verified");
+
+ status["corruptedPaths"] = state.corruptedPaths;
+ status["untrustedPaths"] = state.untrustedPaths;
+ status["bytesLinked"] = state.bytesLinked;
+ status["filesLinked"] = state.filesLinked;
+
+ return status;
+ }
+
std::string getStatus(State & state)
{
auto MiB = 1024.0 * 1024.0;
diff --git a/src/libutil/logging.cc b/src/libutil/logging.cc
index d2e801175..1670fc993 100644
--- a/src/libutil/logging.cc
+++ b/src/libutil/logging.cc
@@ -125,6 +125,18 @@ void writeToStderr(const string & s)
}
}
+void writeToAlien(const string & s)
+{
+ try {
+ writeFull(3, s, false);
+ } catch (SysError & e) {
+ /* Ignore failing writes to stderr. We need to ignore write
+ errors to ensure that cleanup code that logs to stderr runs
+ to completion if the other side of stderr has been closed
+ unexpectedly. */
+ }
+}
+
Logger * makeSimpleLogger(bool printBuildLogs)
{
return new SimpleLogger(printBuildLogs);
diff --git a/src/libutil/logging.hh b/src/libutil/logging.hh
index 96ad69790..cd0cde856 100644
--- a/src/libutil/logging.hh
+++ b/src/libutil/logging.hh
@@ -216,5 +216,6 @@ inline void warn(const std::string & fs, const Args & ... args)
void warnOnce(bool & haveWarned, const FormatOrString & fs);
void writeToStderr(const string & s);
+void writeToAlien(const string & s);
}
--
2.30.0