Revision 46fbdd04

b/lib/cli.py
27 27
import os.path
28 28
import copy
29 29
import time
30
import logging
30 31
from cStringIO import StringIO
31 32

  
32 33
from ganeti import utils
......
50 51
           "FormatError", "SplitNodeOption", "SubmitOrSend",
51 52
           "JobSubmittedException", "FormatTimestamp", "ParseTimespec",
52 53
           "ValidateBeParams",
54
           "ToStderr", "ToStdout",
53 55
           ]
54 56

  
55 57

  
......
626 628
  msg = str(err)
627 629
  if isinstance(err, errors.ConfigurationError):
628 630
    txt = "Corrupt configuration file: %s" % msg
629
    logger.Error(txt)
631
    logging.error(txt)
630 632
    obuf.write(txt + "\n")
631 633
    obuf.write("Aborting.")
632 634
    retcode = 2
......
718 720
  utils.debug = options.debug
719 721

  
720 722
  if old_cmdline:
721
    logger.Info("run with arguments '%s'" % old_cmdline)
723
    logging.info("run with arguments '%s'", old_cmdline)
722 724
  else:
723
    logger.Info("run with no arguments")
725
    logging.info("run with no arguments")
724 726

  
725 727
  try:
726 728
    result = func(options, args)
727 729
  except (errors.GenericError, luxi.ProtocolError), err:
728 730
    result, err_msg = FormatError(err)
729
    logger.ToStderr(err_msg)
731
    logging.exception("Error durring command processing")
732
    ToStderr(err_msg)
730 733

  
731 734
  return result
732 735

  
......
858 861
    except ValueError:
859 862
      raise errors.OpPrereqError("Invalid time specification '%s'" % value)
860 863
  return value
864

  
865

  
866
def _ToStream(stream, txt, *args):
867
  """Write a message to a stream, bypassing the logging system
868

  
869
  @type stream: file object
870
  @param stream: the file to which we should write
871
  @type txt: str
872
  @param txt: the message
873

  
874
  """
875
  if args:
876
    args = tuple(args)
877
    stream.write(txt % args)
878
  else:
879
    stream.write(txt)
880
  stream.write('\n')
881
  stream.flush()
882

  
883

  
884
def ToStdout(txt, *args):
885
  """Write a message to stdout only, bypassing the logging system
886

  
887
  This is just a wrapper over _ToStream.
888

  
889
  @type txt: str
890
  @param txt: the message
891

  
892
  """
893
  _ToStream(sys.stdout, txt, *args)
894

  
895

  
896
def ToStderr(txt, *args):
897
  """Write a message to stderr only, bypassing the logging system
898

  
899
  This is just a wrapper over _ToStream.
900

  
901
  @type txt: str
902
  @param txt: the message
903

  
904
  """
905
  _ToStream(sys.stderr, txt, *args)
b/test/ganeti.cli_unittest.py
22 22
"""Script for unittesting the cli module"""
23 23

  
24 24
import unittest
25
from cStringIO import StringIO
25 26

  
26 27
import ganeti
27 28
import testutils
......
75 76
                            "option", data)
76 77

  
77 78

  
79
class TestToStream(unittest.TestCase):
80
  """Thes the ToStream functions"""
81

  
82
  def testBasic(self):
83
    for data in ["foo",
84
                 "foo %s",
85
                 "foo %(test)s",
86
                 "foo %s %s",
87
                 "",
88
                 ]:
89
      buf = StringIO()
90
      cli._ToStream(buf, data)
91
      self.failUnlessEqual(buf.getvalue(), data+'\n')
92

  
93
  def testParams(self):
94
      buf = StringIO()
95
      cli._ToStream(buf, "foo %s", 1)
96
      self.failUnlessEqual(buf.getvalue(), "foo 1\n")
97
      buf = StringIO()
98
      cli._ToStream(buf, "foo %s", (15,16))
99
      self.failUnlessEqual(buf.getvalue(), "foo (15, 16)\n")
100
      buf = StringIO()
101
      cli._ToStream(buf, "foo %s %s", "a", "b")
102
      self.failUnlessEqual(buf.getvalue(), "foo a b\n")
103

  
78 104
if __name__ == '__main__':
79 105
  unittest.main()

Also available in: Unified diff