source: rtems-tools/rtemstoolkit/log.py @ 3618a62

Last change on this file since 3618a62 was 7c032b0, checked in by Chris Johns <chrisj@…>, on Oct 11, 2017 at 7:09:52 PM

rtemstoolkit: Add a capture hook to logging.

  • Property mode set to 100755
File size: 7.0 KB
Line 
1#
2# RTEMS Tools Project (http://www.rtems.org/)
3# Copyright 2010-2016 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# Redistribution and use in source and binary forms, with or without
9# modification, are permitted provided that the following conditions are met:
10#
11# 1. Redistributions of source code must retain the above copyright notice,
12# this list of conditions and the following disclaimer.
13#
14# 2. Redistributions in binary form must reproduce the above copyright notice,
15# this list of conditions and the following disclaimer in the documentation
16# and/or other materials provided with the distribution.
17#
18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28# POSSIBILITY OF SUCH DAMAGE.
29#
30
31#
32# Log output to stdout and/or a file.
33#
34
35from __future__ import print_function
36
37import os
38import sys
39import threading
40
41#
42# Support to handle use in a package and as a unit test.
43# If there is a better way to let us know.
44#
45try:
46    from . import error
47except (ValueError, SystemError):
48    import error
49
50#
51# A global log.
52#
53default = None
54
55#
56# A global capture handler.
57#
58capture = None
59
60#
61# Global parameters.
62#
63tracing = False
64quiet = False
65
66#
67# Global output lock to keep output together when working with threads
68#
69lock = threading.Lock()
70
71def set_default_once(log):
72    if default is None:
73        default = log
74
75def _output(text = os.linesep, log = None):
76    """Output the text to a log if provided else send it to stdout."""
77    if text is None:
78        text = os.linesep
79    if type(text) is list:
80        text = os.linesep.join(text) + os.linesep
81    if log:
82        log.output(text)
83    elif default is not None:
84        default.output(text)
85    else:
86        lock.acquire()
87        for l in text.replace(chr(13), '').splitlines():
88            print(l)
89        lock.release()
90    if capture is not None:
91        lock.acquire()
92        capture(text)
93        lock.release()
94
95def stderr(text = os.linesep, log = None):
96    lock.acquire()
97    for l in text.replace(chr(13), '').splitlines():
98        print(l, file = sys.stderr)
99    lock.release()
100
101def output(text = os.linesep, log = None):
102    if not quiet:
103        _output(text, log)
104
105def notice(text = os.linesep, log = None, stdout_only = False):
106    if not quiet and \
107            (default is not None and not default.has_stdout() or stdout_only):
108        lock.acquire()
109        for l in text.replace(chr(13), '').splitlines():
110            print(l)
111        lock.release()
112    if not stdout_only:
113        _output(text, log)
114
115def trace(text = os.linesep, log = None):
116    if tracing:
117        _output(text, log)
118
119def warning(text = os.linesep, log = None):
120    for l in text.replace(chr(13), '').splitlines():
121        _output('warning: %s' % (l), log)
122
123def flush(log = None):
124    if log:
125        log.flush()
126    elif default is not None:
127        default.flush()
128
129class log:
130    """Log output to stdout or a file."""
131    def __init__(self, streams = None, tail_size = 100):
132        self.lock = threading.Lock()
133        self.tail = []
134        self.tail_size = tail_size
135        self.fhs = [None, None]
136        if streams:
137            for s in streams:
138                if s == 'stdout':
139                    self.fhs[0] = sys.stdout
140                elif s == 'stderr':
141                    self.fhs[1] = sys.stderr
142                else:
143                    try:
144                        self.fhs.append(open(s, 'w'))
145                    except IOError as ioe:
146                         raise error.general("creating log file '" + s + \
147                                             "': " + str(ioe))
148
149    def __del__(self):
150        for f in range(2, len(self.fhs)):
151            self.fhs[f].close()
152
153    def __str__(self):
154        t = ''
155        for tl in self.tail:
156            t += tl + os.linesep
157        return t[:-len(os.linesep)]
158
159    def _tail(self, text):
160        if type(text) is not list:
161            text = text.splitlines()
162        self.tail += text
163        if len(self.tail) > self.tail_size:
164            self.tail = self.tail[-self.tail_size:]
165
166    def has_stdout(self):
167        return self.fhs[0] is not None
168
169    def has_stderr(self):
170        return self.fhs[1] is not None
171
172    def output(self, text):
173        """Output the text message to all the logs."""
174        # Reformat the text to have local line types.
175        text = text.replace(chr(13), '').splitlines()
176        self._tail(text)
177        out = os.linesep.join(text) + os.linesep
178        self.lock.acquire()
179        try:
180            for f in range(0, len(self.fhs)):
181                if self.fhs[f] is not None:
182                    self.fhs[f].write(out)
183            self.flush()
184        except:
185            raise
186        finally:
187            self.lock.release()
188
189    def flush(self):
190        """Flush the output."""
191        for f in range(0, len(self.fhs)):
192            if self.fhs[f] is not None:
193                self.fhs[f].flush()
194
195if __name__ == "__main__":
196    l = log(['stdout', 'log.txt'], tail_size = 20)
197    for i in range(0, 10):
198        l.output('log: hello world: %d\n' % (i))
199    l.output('log: hello world CRLF\r\n')
200    l.output('log: hello world NONE')
201    l.flush()
202    print('=-' * 40)
203    print('tail: %d' % (len(l.tail)))
204    print(l)
205    print('=-' * 40)
206    for i in range(0, 10):
207        l.output('log: hello world 2: %d\n' % (i))
208    l.flush()
209    print('=-' * 40)
210    print('tail: %d' % (len(l.tail)))
211    print(l)
212    print('=-' * 40)
213    for i in [0, 1]:
214        quiet = False
215        tracing = False
216        print('- quiet:%s - trace:%s %s' % (str(quiet), str(tracing), '-' * 30))
217        trace('trace with quiet and trace off')
218        notice('notice with quiet and trace off')
219        quiet = True
220        tracing = False
221        print('- quiet:%s - trace:%s %s' % (str(quiet), str(tracing), '-' * 30))
222        trace('trace with quiet on and trace off')
223        notice('notice with quiet on and trace off')
224        quiet = False
225        tracing = True
226        print('- quiet:%s - trace:%s %s' % (str(quiet), str(tracing), '-' * 30))
227        trace('trace with quiet off and trace on')
228        notice('notice with quiet off and trace on')
229        quiet = True
230        tracing = True
231        print('- quiet:%s - trace:%s %s' % (str(quiet), str(tracing), '-' * 30))
232        trace('trace with quiet on and trace on')
233        notice('notice with quiet on and trace on')
234        default = l
235    print('=-' * 40)
236    print('tail: %d' % (len(l.tail)))
237    print(l)
238    print('=-' * 40)
239    del l
Note: See TracBrowser for help on using the repository browser.