diff options
author | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2016-10-17 04:00:00 -0400 |
---|---|---|
committer | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2016-10-17 04:10:06 -0400 |
commit | 5a5063bac0b856b9ad8a0fdad3ab1e2517e34f73 (patch) | |
tree | 1d0c32e814b4837da4bf224725f23db00674a8d9 /systemd | |
parent | 52d9f51b9b2f73c478ecb5325be4196350d9b7a1 (diff) | |
download | python-systemd-5a5063bac0b856b9ad8a0fdad3ab1e2517e34f73.tar.gz |
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.
Diffstat (limited to 'systemd')
-rw-r--r-- | systemd/journal.py | 15 |
1 files 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) |