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

4.104.114.95
Last change on this file since c49e500 was c49e500, checked in by Chris Johns <chrisj@…>, on 07/29/14 at 00:04:55

sb: Add visual feedback for http type downloads.

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