Skip to content

Commit a9e7040

Browse files
committed
improved tty-less progress reporting
Previously when running borg in a systemd service (and similar when piping to a file and co.), these problems occurred: - The carriage return both made it so that journald interpreted the output as binary, therefore not printing the text, while also not buffering correctly, so that log output was only available every once in a while in the form [40k blob data]. This can partially be worked around by using `journalctl -a` to view the logs, which at least prints the text, though only sporadically - The path was getting truncated to a short length, since the default get_terminal_size returns a column width of 80, which isn't relevant when printing to e.g. journald This commit fixes this by introducing a new code path for when stderr is a tty, which always prints the full paths and never ends with a carriage return.
1 parent f8735e6 commit a9e7040

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/borg/archive.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ def osize_fmt(self):
146146
def usize_fmt(self):
147147
return format_file_size(self.usize, iec=self.iec)
148148

149-
def show_progress(self, item=None, final=False, stream=None, dt=None):
149+
def show_progress(self, item=None, final=False, stream=sys.stderr, dt=None):
150150
now = time.monotonic()
151151
if dt is None or now - self.last_progress > dt:
152152
self.last_progress = now
@@ -160,6 +160,13 @@ def show_progress(self, item=None, final=False, stream=None, dt=None):
160160
data.update({"time": time.time(), "type": "archive_progress", "finished": final})
161161
msg = json.dumps(data)
162162
end = "\n"
163+
elif not stream.isatty():
164+
if not final:
165+
msg = "{0.osize_fmt} O {0.csize_fmt} C {0.usize_fmt} D {0.nfiles} N ".format(self)
166+
msg += remove_surrogates(item.path) if item else ""
167+
else:
168+
msg = ""
169+
end = "\n"
163170
else:
164171
columns, lines = get_terminal_size()
165172
if not final:
@@ -174,7 +181,7 @@ def show_progress(self, item=None, final=False, stream=None, dt=None):
174181
else:
175182
msg = " " * columns
176183
end = "\r"
177-
print(msg, end=end, file=stream or sys.stderr, flush=True)
184+
print(msg, end=end, file=stream, flush=True)
178185

179186

180187
def is_special(mode):

0 commit comments

Comments
 (0)