From 5a5063bac0b856b9ad8a0fdad3ab1e2517e34f73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Mon, 17 Oct 2016 04:00:00 -0400 Subject: journal: allow stream() to be used without any arguments It's fairly easy to provide a reasonable default for the first argument. Let's do that. Also, the documentation was misleading, suggesting that the function itself can be passed as file parameter to print(). Use a different name for the temporary variable to clarify that. --- systemd/journal.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/systemd/journal.py b/systemd/journal.py index dbe39e3..a43b837 100644 --- a/systemd/journal.py +++ b/systemd/journal.py @@ -446,7 +446,7 @@ def send(MESSAGE, MESSAGE_ID=None, args.extend(_make_line(key, val) for key, val in kwargs.items()) return sendv(*args) -def stream(identifier, priority=LOG_INFO, level_prefix=False): +def stream(identifier=None, priority=LOG_INFO, level_prefix=False): r"""Return a file object wrapping a stream to journal. Log messages written to this file as simple newline sepearted text strings @@ -465,10 +465,13 @@ def stream(identifier, priority=LOG_INFO, level_prefix=False): SYSLOG_IDENTIFIER=myapp MESSAGE=message... - Using the interface with print might be more convenient: + If identifier is None, a suitable default based on sys.argv[0] will be used. + + This interface can be used conveniently with the print function: >>> from __future__ import print_function - >>> print('message...', file=stream) # doctest: +SKIP + >>> fileobj = journal.stream() + >>> print('message...', file=fileobj) # doctest: +SKIP priority is the syslog priority, one of `LOG_EMERG`, `LOG_ALERT`, `LOG_CRIT`, `LOG_ERR`, `LOG_WARNING`, `LOG_NOTICE`, `LOG_INFO`, `LOG_DEBUG`. @@ -477,6 +480,12 @@ def stream(identifier, priority=LOG_INFO, level_prefix=False): (such as '<1>') are interpreted. See sd-daemon(3) for more information. """ + if identifier is None: + if not _sys.argv or not _sys.argv[0] or _sys.argv[0] == '-c': + identifier = 'python' + else: + identifier = _sys.argv[0] + fd = stream_fd(identifier, priority, level_prefix) return _os.fdopen(fd, 'w', 1) -- cgit v1.2.1