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

4.104.114.95
Last change on this file since 6168929 was 6168929, checked in by Chris Johns <chrisj@…>, on 02/17/15 at 23:12:03

Fixes to get a better update on MSYS2.

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