Commit 46fbdd04 authored by Iustin Pop's avatar Iustin Pop
Browse files

Convert to logging

We also add two function for printing messages, so that scripts won't
have to import logger to get these. They are a simple extension over the
logger ones, as they accept the call style from logging:
 ToStdout("Message: %s", msg)
(instead of requiring formatting by the client.

Reviewed-by: imsnah
parent 61be6ba4
......@@ -27,6 +27,7 @@ import textwrap
import os.path
import copy
import time
import logging
from cStringIO import StringIO
from ganeti import utils
......@@ -50,6 +51,7 @@ __all__ = ["DEBUG_OPT", "NOHDR_OPT", "SEP_OPT", "GenericMain",
"FormatError", "SplitNodeOption", "SubmitOrSend",
"JobSubmittedException", "FormatTimestamp", "ParseTimespec",
"ToStderr", "ToStdout",
......@@ -626,7 +628,7 @@ def FormatError(err):
msg = str(err)
if isinstance(err, errors.ConfigurationError):
txt = "Corrupt configuration file: %s" % msg
obuf.write(txt + "\n")
retcode = 2
......@@ -718,15 +720,16 @@ def GenericMain(commands, override=None, aliases=None):
utils.debug = options.debug
if old_cmdline:
logger.Info("run with arguments '%s'" % old_cmdline)"run with arguments '%s'", old_cmdline)
logger.Info("run with no arguments")"run with no arguments")
result = func(options, args)
except (errors.GenericError, luxi.ProtocolError), err:
result, err_msg = FormatError(err)
logging.exception("Error durring command processing")
return result
......@@ -858,3 +861,45 @@ def ParseTimespec(value):
except ValueError:
raise errors.OpPrereqError("Invalid time specification '%s'" % value)
return value
def _ToStream(stream, txt, *args):
"""Write a message to a stream, bypassing the logging system
@type stream: file object
@param stream: the file to which we should write
@type txt: str
@param txt: the message
if args:
args = tuple(args)
stream.write(txt % args)
def ToStdout(txt, *args):
"""Write a message to stdout only, bypassing the logging system
This is just a wrapper over _ToStream.
@type txt: str
@param txt: the message
_ToStream(sys.stdout, txt, *args)
def ToStderr(txt, *args):
"""Write a message to stderr only, bypassing the logging system
This is just a wrapper over _ToStream.
@type txt: str
@param txt: the message
_ToStream(sys.stderr, txt, *args)
......@@ -22,6 +22,7 @@
"""Script for unittesting the cli module"""
import unittest
from cStringIO import StringIO
import ganeti
import testutils
......@@ -75,5 +76,30 @@ class TestSplitKeyVal(unittest.TestCase):
"option", data)
class TestToStream(unittest.TestCase):
"""Thes the ToStream functions"""
def testBasic(self):
for data in ["foo",
"foo %s",
"foo %(test)s",
"foo %s %s",
buf = StringIO()
cli._ToStream(buf, data)
self.failUnlessEqual(buf.getvalue(), data+'\n')
def testParams(self):
buf = StringIO()
cli._ToStream(buf, "foo %s", 1)
self.failUnlessEqual(buf.getvalue(), "foo 1\n")
buf = StringIO()
cli._ToStream(buf, "foo %s", (15,16))
self.failUnlessEqual(buf.getvalue(), "foo (15, 16)\n")
buf = StringIO()
cli._ToStream(buf, "foo %s %s", "a", "b")
self.failUnlessEqual(buf.getvalue(), "foo a b\n")
if __name__ == '__main__':
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