Commit 8a7f1c61 authored by Michael Hanselmann's avatar Michael Hanselmann

Add function to format all job log messages

Just calling utils.SafeEncode on the log message failed when it
wasn't of the type ELOG_MESSAGE and not a string. Now non-message
log entries are formatted using repr().
Signed-off-by: default avatarMichael Hanselmann <hansmi@google.com>
Reviewed-by: default avatarIustin Pop <iustin@google.com>
parent e51ca051
......@@ -161,6 +161,7 @@ __all__ = [
"GenerateTable",
"AskUser",
"FormatTimestamp",
"FormatLogMessage",
# Tags functions
"ListTags",
"AddTags",
......@@ -1474,7 +1475,7 @@ class StdioJobPollReportCb(JobPollReportCbBase):
"""
ToStdout("%s %s", time.ctime(utils.MergeTime(timestamp)),
utils.SafeEncode(log_msg))
FormatLogMessage(log_type, log_msg))
def ReportNotChanged(self, job_id, status):
"""Called if a job hasn't changed in a while.
......@@ -1492,7 +1493,16 @@ class StdioJobPollReportCb(JobPollReportCbBase):
self.notified_waitlock = True
def PollJob(job_id, cl=None, feedback_fn=None):
def FormatLogMessage(log_type, log_msg):
"""Formats a job message according to its type.
"""
if log_type != constants.ELOG_MESSAGE:
log_msg = str(log_msg)
return utils.SafeEncode(log_msg)
"""Function to poll for the result of a job.
@type job_id: job identified
......
......@@ -321,7 +321,7 @@ def ShowJobs(opts, args):
format_msg(3, "Execution log:")
for serial, log_ts, log_type, log_msg in log:
time_txt = FormatTimestamp(log_ts)
encoded = utils.SafeEncode(log_msg)
encoded = FormatLogMessage(log_type, log_msg)
format_msg(4, "%s:%s:%s %s" % (serial, time_txt, log_type, encoded))
return 0
......
......@@ -418,5 +418,16 @@ class TestGenericPollJob(testutils.GanetiTestCase):
cbs.CheckEmpty()
class TestFormatLogMessage(unittest.TestCase):
def test(self):
self.assertEqual(cli.FormatLogMessage(constants.ELOG_MESSAGE,
"Hello World"),
"Hello World")
self.assertRaises(TypeError, cli.FormatLogMessage,
constants.ELOG_MESSAGE, [1, 2, 3])
self.assert_(cli.FormatLogMessage("some other type", (1, 2, 3)))
if __name__ == '__main__':
testutils.GanetiTestProgram()
......@@ -250,7 +250,7 @@ class MoveJobPollReportCb(cli.JobPollReportCbBase):
return
logging.info("[%s] %s", time.ctime(utils.MergeTime(timestamp)),
utils.SafeEncode(log_msg))
cli.FormatLogMessage(log_type, log_msg))
def ReportNotChanged(self, job_id, status):
"""Called if a job hasn't changed in a while.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment