source: rtems-source-builder/source-builder/sb/log.py @ 74da24c

4.104.114.95
Last change on this file since 74da24c was 74da24c, checked in by Chris Johns <chrisj@…>, on 05/08/14 at 02:58:14

sb: Generate an error report on an error.

Generate an error report users can send to the mailing list with
error details.

  • Property mode set to 100755
File size: 5.9 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
89def tail(log = None):
90    if log is not None:
91        return log.tail
92    if default is not None:
93        return default.tail
94    return 'No log output'
95
96class log:
97    """Log output to stdout or a file."""
98    def __init__(self, streams = None, tail_size = 200):
99        self.tail = []
100        self.tail_size = tail_size
101        self.fhs = [None, None]
102        if streams:
103            for s in streams:
104                if s == 'stdout':
105                    self.fhs[0] = sys.stdout
106                elif s == 'stderr':
107                    self.fhs[1] = sys.stderr
108                else:
109                    try:
110                        self.fhs.append(file(s, 'w'))
111                    except IOError, ioe:
112                         raise error.general("creating log file '" + s + \
113                                             "': " + str(ioe))
114
115    def __del__(self):
116        for f in range(2, len(self.fhs)):
117            self.fhs[f].close()
118
119    def __str__(self):
120        t = ''
121        for tl in self.tail:
122            t += tl + os.linesep
123        return t[:-len(os.linesep)]
124
125    def _tail(self, text):
126        if type(text) is not list:
127            text = text.splitlines()
128        self.tail += text
129        if len(self.tail) > self.tail_size:
130            self.tail = self.tail[-self.tail_size:]
131
132    def has_stdout(self):
133        return self.fhs[0] is not None
134
135    def has_stderr(self):
136        return self.fhs[1] is not None
137
138    def output(self, text):
139        """Output the text message to all the logs."""
140        # Reformat the text to have local line types.
141        text = text.replace(chr(13), '').splitlines()
142        self._tail(text)
143        out = ''
144        for l in text:
145            out += l + os.linesep
146        for f in range(0, len(self.fhs)):
147            if self.fhs[f] is not None:
148                self.fhs[f].write(out)
149        self.flush()
150
151    def flush(self):
152        """Flush the output."""
153        for f in range(0, len(self.fhs)):
154            if self.fhs[f] is not None:
155                self.fhs[f].flush()
156
157if __name__ == "__main__":
158    l = log(['stdout', 'log.txt'], tail_size = 20)
159    for i in range(0, 10):
160        l.output('log: hello world: %d\n' % (i))
161    l.output('log: hello world CRLF\r\n')
162    l.output('log: hello world NONE')
163    l.flush()
164    print '=-' * 40
165    print 'tail: %d' % (len(l.tail))
166    print l
167    print '=-' * 40
168    for i in range(0, 10):
169        l.output('log: hello world 2: %d\n' % (i))
170    l.flush()
171    print '=-' * 40
172    print 'tail: %d' % (len(l.tail))
173    print l
174    print '=-' * 40
175    for i in [0, 1]:
176        quiet = False
177        tracing = False
178        print '- quiet:%s - trace:%s %s' % (str(quiet), str(tracing), '-' * 30)
179        trace('trace with quiet and trace off')
180        notice('notice with quiet and trace off')
181        quiet = True
182        tracing = False
183        print '- quiet:%s - trace:%s %s' % (str(quiet), str(tracing), '-' * 30)
184        trace('trace with quiet on and trace off')
185        notice('notice with quiet on and trace off')
186        quiet = False
187        tracing = True
188        print '- quiet:%s - trace:%s %s' % (str(quiet), str(tracing), '-' * 30)
189        trace('trace with quiet off and trace on')
190        notice('notice with quiet off and trace on')
191        quiet = True
192        tracing = True
193        print '- quiet:%s - trace:%s %s' % (str(quiet), str(tracing), '-' * 30)
194        trace('trace with quiet on and trace on')
195        notice('notice with quiet on and trace on')
196        default = l
197    print '=-' * 40
198    print 'tail: %d' % (len(l.tail))
199    print l
200    print '=-' * 40
201    del l
Note: See TracBrowser for help on using the repository browser.