Files
git-hooks/hooks/syslog.py
Joel Brobecker 23cc50ec72 syshooks/syslog.py: Decode the output returned by the logger
This is another preparation patch for the transition to python 3.x.
With Python 3.x, the output we get from the logger is going to be
a byte string, which we need to then decode into a string.

Change-Id: I2307b90f2e2cccaed8a93fa589f82fba0064c28b
TN: U530-006
2021-10-06 11:27:20 -07:00

42 lines
1.2 KiB
Python

"""Handling of syslog-ing...
"""
from __future__ import print_function
from os import environ
from subprocess import Popen, PIPE, STDOUT
from io_utils import safe_decode
from utils import warn
def syslog(message, tag="style_checker", priority="local0.warn"):
"""Add the given entry to the syslog file.
PARAMETERS
message: The message to file.
tag: Mark every line in the log with the specified tag.
priority: Enter the message with the specified priority.
"""
logger_exe = "logger"
if "GIT_HOOKS_LOGGER" in environ:
logger_exe = environ["GIT_HOOKS_LOGGER"]
p = Popen(
[logger_exe, "-t", tag, "-p", priority, message], stdout=PIPE, stderr=STDOUT
)
out, _ = p.communicate()
out = safe_decode(out)
if p.returncode != 0:
info = [
"Failed to file the following syslog entry:",
" - message: %s" % message,
" - tag: %s" % tag,
" - priority: %s" % priority,
"",
"logger returned with error code %d:" % p.returncode,
] + out.splitlines()
warn(*info)
elif out.rstrip():
print(out.rstrip())