~drizzle-trunk/drizzle/development

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#! /usr/bin/env python
# -*- mode: python; c-basic-offset: 2; indent-tabs-mode: nil; -*-
# vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
#
# Copyright (C) 2009 Sun Microsystems
# Copyright (C) 2011 Patrick Crews
#
# Authors:
#
#  Jay Pipes <joinfu@sun.com>
#  Monty Taylor <mordred@sun.com>
#  Patrick Crews 
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
#
# This code is modified from the logging module used in the 
# drizzle-automation project - https://launchpad.net/drizzle-automation


""" Simple replacement for python logging module that doesn't suck """

import time, sys


class loggingManager():
    """ Class to deal with logging
        We make a class just because we forsee ourselves being
        multi-threaded and it will be nice to have a single
        point of control for managing i/o.

        Also, this is the cleanest way I can think of to deal
        with test-reporting (again, multi-threaded and such

    """

    def __init__(self, variables):

        self.log_file = sys.stdout
        self.report_fmt = '{0:<42} {1} {2:>8}'
        self.report_started = 0
        self.thick_line = '='*(80 - len("20110420-105402  "))
        self.thin_line = '-'*(80- len("20110420-105402  "))
        self.verbose_flag = variables['verbose']
        self.debug_flag = variables['debug']

    def _write_message(self,level, msg):
      self.log_file.write("%s %s %s\n" % (time.strftime("%Y%m%d-%H%M%S"), level, str(msg)))
      self.log_file.flush()

    def setOutput(self,file_name):
      if file_name == 'stdout':
        self.log_file= sys.stdout
      else:
        self.log_file= open(variables['log_file'],'w+')

    def info(self,msg):
      self._write_message("INFO", msg)

    def warning(self,msg):
      self._write_message("WARNING", msg)

    def error(self,msg):
      self._write_message("ERROR", msg)

    def verbose(self,msg):
      if self.verbose_flag:
          self._write_message("VERBOSE", msg)

    def debug(self,msg):
      if self.debug_flag:
          self._write_message("DEBUG", msg)
 
    def debug_class(self,codeClass):
      if self.debug_flag:
        self._write_message("DEBUG**",codeClass)
        skip_keys = ['skip_keys', 'debug', 'verbose']
        for key, item in sorted(vars(codeClass).items()):
            if key not in codeClass.skip_keys and key not in skip_keys:
                self._write_message("DEBUG**",("%s: %s" %(key, item)))



    def test_report( self, test_name, test_status
                   , execution_time, additional_output = None
                   , report_output = False):
        """ We use this method to deal with writing out the test report

        """
        if not self.report_started:
            self.report_started = 1
            self.write_report_header()
        test_status = "[ %s ]" %(test_status)
        msg = self.report_fmt.format( test_name, test_status
                                    , execution_time)
        self._write_message("", msg)
        if additional_output and report_output:
            additional_output=additional_output.split('\n')
            for line in additional_output:
                line = line.strip()
                self._write_message("",line)

    def write_report_header(self):
        self.write_thick_line()
        self.test_report("TEST NAME", "RESULT", "TIME (ms)")
        self.write_thick_line()

    def write_thin_line(self):
        self._write_message("",self.thin_line)

    def write_thick_line(self):
        self._write_message("",self.thick_line)