2
# -*- mode: python; c-basic-offset: 2; indent-tabs-mode: nil; -*-
3
# vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
5
# Copyright (C) 2009 Sun Microsystems
6
# Copyright (C) 2011 Patrick Crews
10
# Jay Pipes <joinfu@sun.com>
11
# Monty Taylor <mordred@sun.com>
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.
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.
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
29
# This code is modified from the logging module used in the
30
# drizzle-automation project - https://launchpad.net/drizzle-automation
33
""" Simple replacement for python logging module that doesn't suck """
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.
44
Also, this is the cleanest way I can think of to deal
45
with test-reporting (again, multi-threaded and such
49
def __init__(self, variables):
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']
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)))
65
def setOutput(self,file_name):
66
if file_name == 'stdout':
67
self.log_file= sys.stdout
69
self.log_file= open(variables['log_file'],'w+')
72
self._write_message("INFO", msg)
74
def warning(self,msg):
75
self._write_message("WARNING", msg)
78
self._write_message("ERROR", msg)
80
def verbose(self,msg):
82
self._write_message("VERBOSE", msg)
86
self._write_message("DEBUG", msg)
88
def test_debug(self,msg):
89
if self.test_debug_flag:
90
self._write_message("TEST_DEBUG", msg)
92
def debug_class(self,codeClass):
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)))
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
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
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:
119
self._write_message("",line)
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()
126
def write_thin_line(self):
127
self._write_message("",self.thin_line)
129
def write_thick_line(self):
130
self._write_message("",self.thick_line)
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))
138
def subunit_stop(self, test_name, retcode, output):
139
""" We log the return of a test appropriately:
140
success, skip, failure, etc
143
result_map = {'pass':'success'
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"))
150
output_string = " [\n%s]\n" %(output)
152
output_string = "\n" # we just want a newline if nothing here
153
subunit_outfile.write("%s: %s%s" %( result