source: rtems-source-builder/source-builder/sb/log.py @ c914e1d

4.104.114.95
Last change on this file since c914e1d was c914e1d, checked in by Chris Johns <chrisj@…>, on 05/01/13 at 00:08:36

Add tail support to the log. Email the log tail in a failure.

  • Property mode set to 100755
File size: 5.7 KB
Line 
1#
2# RTEMS Tools Project (http://www.rtems.org/)
3# Copyright 2010-2012 Chris Johns (chrisj@rtems.org)
4# All rights reserved.
5#
6# This file is part of the RTEMS Tools package in 'rtems-testing'.
7#
8# Permission to use, copy, modify, and/or distribute this software for any
9# purpose with or without fee is hereby granted, provided that the above
10# copyright notice and this permission notice appear in all copies.
11#
12# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19
20#
21# Log output to stdout and/or a file.
22#
23
24import os
25import sys
26
27import error
28
29#
30# A global log.
31#
32default = None
33
34#
35# Global parameters.
36#
37tracing = False
38quiet = False
39
40def set_default_once(log):
41    if default is None:
42        default = log
43
44def _output(text = os.linesep, log = None):
45    """Output the text to a log if provided else send it to stdout."""
46    if text is None:
47        text = os.linesep
48    if type(text) is list:
49        _text = ''
50        for l in text:
51            _text += l + os.linesep
52        text = _text
53    if log:
54        log.output(text)
55    elif default is not None:
56        default.output(text)
57    else:
58        for l in text.replace(chr(13), '').splitlines():
59            print l
60
61def stderr(text = os.linesep, log = None):
62    for l in text.replace(chr(13), '').splitlines():
63        print >> sys.stderr, l
64
65def output(text = os.linesep, log = None):
66    if not quiet:
67        _output(text, log)
68
69def notice(text = os.linesep, log = None):
70    if not quiet and default is not None and not default.has_stdout():
71        for l in text.replace(chr(13), '').splitlines():
72            print l
73    _output(text, log)
74
75def trace(text = os.linesep, log = None):
76    if tracing:
77        _output(text, log)
78
79def warning(text = os.linesep, log = None):
80    for l in text.replace(chr(13), '').splitlines():
81        _output('warning: %s' % (l), log)
82
83def flush(log = None):
84    if log:
85        log.flush()
86    elif default is not None:
87        default.flush()
88
89class log:
90    """Log output to stdout or a file."""
91    def __init__(self, streams = None, tail_size = 100):
92        self.tail = []
93        self.tail_size = tail_size
94        self.fhs = [None, None]
95        if streams:
96            for s in streams:
97                if s == 'stdout':
98                    self.fhs[0] = sys.stdout
99                elif s == 'stderr':
100                    self.fhs[1] = sys.stderr
101                else:
102                    try:
103                        self.fhs.append(file(s, 'w'))
104                    except IOError, ioe:
105                         raise error.general("creating log file '" + s + \
106                                             "': " + str(ioe))
107
108    def __del__(self):
109        for f in range(2, len(self.fhs)):
110            self.fhs[f].close()
111
112    def __str__(self):
113        t = ''
114        for tl in self.tail:
115            t += tl + os.linesep
116        return t[:-len(os.linesep)]
117
118    def _tail(self, text):
119        if type(text) is not list:
120            text = text.splitlines()
121        self.tail += text
122        if len(self.tail) > self.tail_size:
123            self.tail = self.tail[-self.tail_size:]
124
125    def has_stdout(self):
126        return self.fhs[0] is not None
127
128    def has_stderr(self):
129        return self.fhs[1] is not None
130
131    def output(self, text):
132        """Output the text message to all the logs."""
133        # Reformat the text to have local line types.
134        text = text.replace(chr(13), '').splitlines()
135        self._tail(text)
136        out = ''
137        for l in text:
138            out += l + os.linesep
139        for f in range(0, len(self.fhs)):
140            if self.fhs[f] is not None:
141                self.fhs[f].write(out)
142        self.flush()
143
144    def flush(self):
145        """Flush the output."""
146        for f in range(0, len(self.fhs)):
147            if self.fhs[f] is not None:
148                self.fhs[f].flush()
149
150if __name__ == "__main__":
151    l = log(['stdout', 'log.txt'], tail_size = 20)
152    for i in range(0, 10):
153        l.output('log: hello world: %d\n' % (i))
154    l.output('log: hello world CRLF\r\n')
155    l.output('log: hello world NONE')
156    l.flush()
157    print '=-' * 40
158    print 'tail: %d' % (len(l.tail))
159    print l
160    print '=-' * 40
161    for i in range(0, 10):
162        l.output('log: hello world 2: %d\n' % (i))
163    l.flush()
164    print '=-' * 40
165    print 'tail: %d' % (len(l.tail))
166    print l
167    print '=-' * 40
168    for i in [0, 1]:
169        quiet = False
170        tracing = False
171        print '- quiet:%s - trace:%s %s' % (str(quiet), str(tracing), '-' * 30)
172        trace('trace with quiet and trace off')
173        notice('notice with quiet and trace off')
174        quiet = True
175        tracing = False
176        print '- quiet:%s - trace:%s %s' % (str(quiet), str(tracing), '-' * 30)
177        trace('trace with quiet on and trace off')
178        notice('notice with quiet on and trace off')
179        quiet = False
180        tracing = True
181        print '- quiet:%s - trace:%s %s' % (str(quiet), str(tracing), '-' * 30)
182        trace('trace with quiet off and trace on')
183        notice('notice with quiet off and trace on')
184        quiet = True
185        tracing = True
186        print '- quiet:%s - trace:%s %s' % (str(quiet), str(tracing), '-' * 30)
187        trace('trace with quiet on and trace on')
188        notice('notice with quiet on and trace on')
189        default = l
190    print '=-' * 40
191    print 'tail: %d' % (len(l.tail))
192    print l
193    print '=-' * 40
194    del l
Note: See TracBrowser for help on using the repository browser.