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

4.104.114.95
Last change on this file since ba8a935 was ba8a935, checked in by Chris Johns <chrisj@…>, on 10/31/12 at 03:48:27

Change name to Source Builder from Tool Builder.

  • Property mode set to 100755
File size: 3.2 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
34def set_default_once(log):
35    if default is None:
36        default = log
37
38def output(text = os.linesep, log = None):
39    """Output the text to a log if provided else send it to stdout."""
40    if text is None:
41        text = os.linesep
42    if type(text) is list:
43        _text = ''
44        for l in text:
45            _text += l + os.linesep
46        text = _text
47    if log:
48        log.output(text)
49    elif default is not None:
50        default.output(text)
51    else:
52        for l in text.replace(chr(13), '').splitlines():
53            print l
54
55def flush(log = None):
56    if log:
57        log.flush()
58    elif default is not None:
59        default.flush()
60
61class log:
62    """Log output to stdout or a file."""
63    def __init__(self, streams = None):
64        self.fhs = [None, None]
65        if streams:
66            for s in streams:
67                if s == 'stdout':
68                    self.fhs[0] = sys.stdout
69                elif s == 'stderr':
70                    self.fhs[1] = sys.stderr
71                else:
72                    try:
73                        self.fhs.append(file(s, 'w'))
74                    except IOError, ioe:
75                         raise error.general("creating log file '" + s + \
76                                             "': " + str(ioe))
77
78    def __del__(self):
79        for f in range(2, len(self.fhs)):
80            self.fhs[f].close()
81
82    def has_stdout(self):
83        return self.fhs[0] is not None
84
85    def has_stderr(self):
86        return self.fhs[1] is not None
87
88    def output(self, text):
89        """Output the text message to all the logs."""
90        # Reformat the text to have local line types.
91        out = ''
92        for l in text.replace(chr(13), '').splitlines():
93            out += l + os.linesep
94        for f in range(0, len(self.fhs)):
95            if self.fhs[f] is not None:
96                self.fhs[f].write(out)
97
98    def flush(self):
99        """Flush the output."""
100        for f in range(0, len(self.fhs)):
101            if self.fhs[f] is not None:
102                self.fhs[f].flush()
103
104if __name__ == "__main__":
105    l = log(['stdout', 'log.txt'])
106    for i in range(0, 10):
107        l.output('hello world: %d\n' % (i))
108    l.output('hello world CRLF\r\n')
109    l.output('hello world NONE')
110    l.flush()
111    del l
Note: See TracBrowser for help on using the repository browser.