Skip to content

Commit

Permalink
Merge pull request #1337 from aryoda/PR/log_only_to_stderr
Browse files Browse the repository at this point in the history
Write all log output to stderr (do not pollute stdout)
  • Loading branch information
emtiu committed Oct 21, 2022
2 parents ad4e91c + 2e44f98 commit 4848708
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Upcoming Release
* GUI change: Remove Exit button from the toolbar (#172)
* GUI change: Define accelerator keys for menu bar and tabs, as well as toolbar shortcuts (#1104)
* Desktop integration: Update .desktop file to mark Back In Time as a single main window program (#1258)
* Improvement: Write all log output to stderr; do not pollute stdout with INFO and WARNING messages anymore (#1337)
* Bugfix: AttributeError in "Diff Options" dialog (#898)
* Bugfix: Settings GUI: "Save password to Keyring" was disabled due to "no appropriate keyring found" (#1321)
* Bugfix: Back in Time did not start with D-Bus error
Expand Down
10 changes: 6 additions & 4 deletions common/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
import tools
import bcolors

DEBUG = False
APP_NAME = 'backintime'
DEBUG = False # Set to "True" when passing "--debug" as cmd arg
APP_NAME = 'backintime' # TODO Duplicated code (see Config.APP_NAME)

def openlog():
name = os.getenv('LOGNAME', 'unknown')
Expand Down Expand Up @@ -55,14 +55,16 @@ def warning(msg , parent = None, traceDepth = 0):
def info(msg , parent = None, traceDepth = 0):
if DEBUG:
msg = '%s %s' %(_debugHeader(parent, traceDepth), msg)
print('%sINFO%s: %s' %(bcolors.OKGREEN, bcolors.ENDC, msg), file=sys.stdout)
print('%sINFO%s: %s' %(bcolors.OKGREEN, bcolors.ENDC, msg), file=sys.stderr)
for line in tools.wrapLine(msg):
syslog.syslog(syslog.LOG_INFO, 'INFO: ' + line)

def debug(msg, parent = None, traceDepth = 0):
if DEBUG:
msg = '%s %s' %(_debugHeader(parent, traceDepth), msg)
print('%sDEBUG%s: %s' %(bcolors.OKBLUE, bcolors.ENDC, msg), file = sys.stdout)
# Why does this code differ from eg. "error()"
# (where the following lines are NOT part of the "if")?
print('%sDEBUG%s: %s' %(bcolors.OKBLUE, bcolors.ENDC, msg), file=sys.stderr)
for line in tools.wrapLine(msg):
syslog.syslog(syslog.LOG_DEBUG, 'DEBUG: %s' %line)

Expand Down
12 changes: 9 additions & 3 deletions common/test/test_backintime.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,17 @@ def test_local_snapshot_is_successful(self):
Back In Time comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions; type `backintime --license' for details.
''', re.MULTILINE))

INFO: Lock
# The log output completely goes to stderr
self.assertRegex(error.decode(), re.compile(r'''INFO: Lock
INFO: Take a new snapshot. Profile: 1 Main profile
INFO: Call rsync to take the snapshot
INFO: Save config file
INFO: Save permissions
INFO: Create info file
INFO: Unlock''', re.MULTILINE))
INFO: Unlock
''', re.MULTILINE))

# get snapshot id
subprocess.check_output(["./backintime",
Expand Down Expand Up @@ -167,8 +170,11 @@ def test_local_snapshot_is_successful(self):
This is free software, and you are welcome to redistribute it
under certain conditions; type `backintime --license' for details.
''', re.MULTILINE))

INFO: Restore: /tmp/test/testfile to: /tmp/restored.*''', re.MULTILINE))
# The log output completely goes to stderr
self.assertRegex(error.decode(), re.compile(r'''INFO: Restore: /tmp/test/testfile to: /tmp/restored.*''',
re.MULTILINE))

# verify that files restored are the same as those backed up
subprocess.check_output(["diff",
Expand Down

0 comments on commit 4848708

Please sign in to comment.