source: rtems-tools/specbuilder/specbuilder/log.py @ 7231f49

4.104.115
Last change on this file since 7231f49 was 7231f49, checked in by Chris Johns <chrisj@…>, on 08/09/10 at 01:29:43

2010-08-09 Chris Johns <chrisj@…>

  • specbuilder, specbuilder/sb-build, specbuilder/sb-crossgcc, specbuilder/sb-setup, specbuilder/sb-specdump, specbuilder/sb-status, specbuilder/specbuilder/.cvsignore, specbuilder/specbuilder/build.py, specbuilder/specbuilder/config.sub, specbuilder/specbuilder/crossgcc.py, specbuilder/specbuilder/darwin.py, specbuilder/specbuilder/defaults.py, specbuilder/specbuilder/error.py, specbuilder/specbuilder/execute.py, specbuilder/specbuilder/log.py, specbuilder/specbuilder/setup.py, specbuilder/specbuilder/spec.py, specbuilder/specbuilder/status.py: New.
  • Property mode set to 100755
File size: 3.1 KB
Line 
1#
2# $Id$
3#
4# RTEMS Tools Project (http://www.rtems.org/)
5# Copyright 2010 Chris Johns (chrisj@rtems.org)
6# All rights reserved.
7#
8# This file is part of the RTEMS Tools package in 'rtems-testing'.
9#
10# RTEMS Tools is free software: you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation, either version 3 of the License, or
13# (at your option) any later version.
14#
15# RTEMS Tools is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with RTEMS Tools.  If not, see <http://www.gnu.org/licenses/>.
22#
23
24#
25# Log output to stdout and/or a file.
26#
27
28import os
29import sys
30
31import error
32
33#
34# A global log.
35#
36default = None
37
38def set_default_once(log):
39    if default is None:
40        default = log
41
42def output(text = os.linesep, log = None):
43    """Output the text to a log if provided else send it to stdout."""
44    if text is None:
45        text = os.linesep
46    if type(text) is list:
47        _text = ''
48        for l in text:
49            _text += l + os.linesep
50        text = _text
51    if log:
52        log.output(text)
53    elif default is not None:
54        default.output(text)
55    else:
56        for l in text.replace(chr(13), '').splitlines():
57            print l
58
59def flush(log = None):
60    if log:
61        log.flush()
62    elif default is not None:
63        default.flush()
64
65class log:
66    """Log output to stdout or a file."""
67    def __init__(self, streams = None):
68        self.fhs = [None, None]
69        if streams:
70            for s in streams:
71                if s == 'stdout':
72                    self.fhs[0] = sys.stdout
73                elif s == 'stderr':
74                    self.fhs[1] = sys.stderr
75                else:
76                    try:
77                        self.fhs.append(file(s, 'w'))
78                    except IOError, ioe:
79                         raise error.general("creating log file '" + s + \
80                                             "': " + str(ioe))
81
82    def __del__(self):
83        for f in range(2, len(self.fhs)):
84            self.fhs[f].close()
85
86    def has_stdout(self):
87        return self.fhs[0] is not None
88
89    def has_stderr(self):
90        return self.fhs[1] is not None
91
92    def output(self, text):
93        """Output the text message to all the logs."""
94        # Reformat the text to have local line types.
95        out = ''
96        for l in text.replace(chr(13), '').splitlines():
97            out += l + os.linesep
98        for f in range(0, len(self.fhs)):
99            if self.fhs[f] is not None:
100                self.fhs[f].write(out)
101
102    def flush(self):
103        """Flush the output."""
104        for f in range(0, len(self.fhs)):
105            if self.fhs[f] is not None:
106                self.fhs[f].flush()
107       
108if __name__ == "__main__":
109    l = log(['stdout', 'log.txt'])
110    for i in range(0, 10):
111        l.output('hello world: %d\n' % (i))
112    l.output('hello world CRLF\r\n')
113    l.output('hello world NONE')
114    l.flush()
115    del l
Note: See TracBrowser for help on using the repository browser.