~ubuntu-branches/ubuntu/trusty/drizzle/trusty

« back to all changes in this revision

Viewing changes to tests/kewpie/lib/sys_mgmt/logging_management.py

  • Committer: Package Import Robot
  • Author(s): Clint Byrum
  • Date: 2012-06-19 10:46:49 UTC
  • mfrom: (1.1.6)
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20120619104649-e2l0ggd4oz3um0f4
Tags: upstream-7.1.36-stable
ImportĀ upstreamĀ versionĀ 7.1.36-stable

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! /usr/bin/env python
 
2
# -*- mode: python; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
3
# vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
4
#
 
5
# Copyright (C) 2009 Sun Microsystems
 
6
# Copyright (C) 2011 Patrick Crews
 
7
#
 
8
# Authors:
 
9
#
 
10
#  Jay Pipes <joinfu@sun.com>
 
11
#  Monty Taylor <mordred@sun.com>
 
12
#  Patrick Crews 
 
13
#
 
14
# This program is free software; you can redistribute it and/or modify
 
15
# it under the terms of the GNU General Public License as published by
 
16
# the Free Software Foundation; either version 2 of the License, or
 
17
# (at your option) any later version.
 
18
#
 
19
# This program is distributed in the hope that it will be useful,
 
20
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
21
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
22
# GNU General Public License for more details.
 
23
#
 
24
# You should have received a copy of the GNU General Public License
 
25
# along with this program; if not, write to the Free Software
 
26
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
27
#
 
28
#
 
29
# This code is modified from the logging module used in the 
 
30
# drizzle-automation project - https://launchpad.net/drizzle-automation
 
31
 
 
32
 
 
33
""" Simple replacement for python logging module that doesn't suck """
 
34
 
 
35
import time, sys
 
36
 
 
37
 
 
38
class loggingManager():
 
39
    """ Class to deal with logging
 
40
        We make a class just because we forsee ourselves being
 
41
        multi-threaded and it will be nice to have a single
 
42
        point of control for managing i/o.
 
43
 
 
44
        Also, this is the cleanest way I can think of to deal
 
45
        with test-reporting (again, multi-threaded and such
 
46
 
 
47
    """
 
48
 
 
49
    def __init__(self, variables):
 
50
 
 
51
        self.log_file = sys.stdout
 
52
        self.subunit_file = variables['subunitoutfile']
 
53
        self.report_fmt = '{0:<42} {1} {2:>8}'
 
54
        self.report_started = 0
 
55
        self.thick_line = '='*(80 - len("20110420-105402  "))
 
56
        self.thin_line = '-'*(80- len("20110420-105402  "))
 
57
        self.verbose_flag = variables['verbose']
 
58
        self.debug_flag = variables['debug']
 
59
        self.test_debug_flag = variables['testdebug']
 
60
 
 
61
    def _write_message(self,level, msg):
 
62
      self.log_file.write("%s %s %s\n" % (time.strftime("%Y%m%d-%H%M%S"), level, str(msg)))
 
63
      self.log_file.flush()
 
64
 
 
65
    def setOutput(self,file_name):
 
66
      if file_name == 'stdout':
 
67
        self.log_file= sys.stdout
 
68
      else:
 
69
        self.log_file= open(variables['log_file'],'w+')
 
70
 
 
71
    def info(self,msg):
 
72
      self._write_message("INFO", msg)
 
73
 
 
74
    def warning(self,msg):
 
75
      self._write_message("WARNING", msg)
 
76
 
 
77
    def error(self,msg):
 
78
      self._write_message("ERROR", msg)
 
79
 
 
80
    def verbose(self,msg):
 
81
      if self.verbose_flag:
 
82
          self._write_message("VERBOSE", msg)
 
83
 
 
84
    def debug(self,msg):
 
85
      if self.debug_flag:
 
86
          self._write_message("DEBUG", msg)
 
87
 
 
88
    def test_debug(self,msg):
 
89
        if self.test_debug_flag:
 
90
            self._write_message("TEST_DEBUG", msg)
 
91
 
 
92
    def debug_class(self,codeClass):
 
93
      if self.debug_flag:
 
94
        self._write_message("DEBUG**",codeClass)
 
95
        skip_keys = ['skip_keys', 'debug', 'verbose']
 
96
        for key, item in sorted(vars(codeClass).items()):
 
97
            if key not in codeClass.skip_keys and key not in skip_keys:
 
98
                self._write_message("DEBUG**",("%s: %s" %(key, item)))
 
99
 
 
100
 
 
101
 
 
102
    def test_report( self, test_name, test_status
 
103
                   , execution_time, additional_output = None
 
104
                   , report_output = False):
 
105
        """ We use this method to deal with writing out the test report
 
106
 
 
107
        """
 
108
        if not self.report_started:
 
109
            self.report_started = 1
 
110
            self.write_report_header()
 
111
        test_status = "[ %s ]" %(test_status)
 
112
        msg = self.report_fmt.format( test_name, test_status
 
113
                                    , execution_time)
 
114
        self._write_message("", msg)
 
115
        if additional_output and report_output:
 
116
            additional_output=additional_output.split('\n')
 
117
            for line in additional_output:
 
118
                line = line.strip()
 
119
                self._write_message("",line)
 
120
 
 
121
    def write_report_header(self):
 
122
        self.write_thick_line()
 
123
        self.test_report("TEST NAME", "RESULT", "TIME (ms)")
 
124
        self.write_thick_line()
 
125
 
 
126
    def write_thin_line(self):
 
127
        self._write_message("",self.thin_line)
 
128
 
 
129
    def write_thick_line(self):
 
130
        self._write_message("",self.thick_line)
 
131
 
 
132
    def subunit_start(self,test_name):
 
133
        """ We log a test being started for subunit output """
 
134
        with open(self.subunit_file,'a') as subunit_outfile:
 
135
            subunit_outfile.write("time: %sZ\n" %(time.strftime("%Y-%m-%d-%H:%M:%S")))
 
136
            subunit_outfile.write("test: %s\n" %(test_name))
 
137
 
 
138
    def subunit_stop(self, test_name, retcode, output):
 
139
        """ We log the return of a test appropriately:
 
140
            success, skip, failure, etc
 
141
 
 
142
        """
 
143
        result_map = {'pass':'success'
 
144
                     ,'fail':'failure'
 
145
                     }
 
146
        result = result_map[retcode]
 
147
        with open(self.subunit_file,'a') as subunit_outfile:
 
148
            subunit_outfile.write(time.strftime("time: %Y-%m-%d-%H:%M:%SZ\n"))
 
149
            if output:
 
150
                output_string = " [\n%s]\n" %(output)
 
151
            else:
 
152
                output_string = "\n" # we just want a newline if nothing here 
 
153
            subunit_outfile.write("%s: %s%s" %( result
 
154
                                              , test_name
 
155
                                              , output_string))
 
156
            
 
157