summaryrefslogtreecommitdiffstats
path: root/source-builder/sb/log.py
blob: 301bd96697bde41557af12e82aa73b3fc229e111 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#
# RTEMS Tools Project (http://www.rtems.org/)
# Copyright 2010-2012 Chris Johns (chrisj@rtems.org)
# All rights reserved.
#
# This file is part of the RTEMS Tools package in 'rtems-testing'.
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

#
# Log output to stdout and/or a file.
#

from __future__ import print_function

import os
import sys

import error

#
# A global log.
#
default = None

#
# Global parameters.
#
tracing = False
quiet = False

def set_default_once(log):
    global default
    if default is None:
        default = log

def _output(text = os.linesep, log = None):
    """Output the text to a log if provided else send it to stdout."""
    if text is None:
        text = os.linesep
    if type(text) is list:
        _text = ''
        for l in text:
            _text += l + os.linesep
        text = _text
    if log:
        log.output(text)
    elif default is not None:
        default.output(text)
    else:
        for l in text.replace(chr(13), '').splitlines():
            print(l)
        sys.stdout.flush()

def stdout_raw(text = os.linesep):
    print(text, end = '')
    sys.stdout.flush()

def stderr(text = os.linesep, log = None):
    for l in text.replace(chr(13), '').splitlines():
        print(l, file = sys.stderr)
        sys.stderr.flush()

def output(text = os.linesep, log = None):
    if not quiet:
        _output(text, log)

def notice(text = os.linesep, log = None):
    if not quiet and default is not None and not default.has_stdout():
        for l in text.replace(chr(13), '').splitlines():
            print(l)
        sys.stdout.flush()
    _output(text, log)

def trace(text = os.linesep, log = None):
    if tracing:
        _output(text, log)

def warning(text = os.linesep, log = None):
    for l in text.replace(chr(13), '').splitlines():
        notice('warning: %s' % (l), log)

def flush(log = None):
    if log:
        log.flush()
    elif default is not None:
        default.flush()

def tail(log = None):
    if log is not None:
        return log.tail
    if default is not None:
        return default.tail
    return 'No log output'

class log:
    """Log output to stdout or a file."""
    def __init__(self, streams = None, tail_size = 200):
        self.tail = []
        self.tail_size = tail_size
        self.fhs = [None, None]
        if streams:
            for s in streams:
                if s == 'stdout':
                    self.fhs[0] = sys.stdout
                elif s == 'stderr':
                    self.fhs[1] = sys.stderr
                else:
                    try:
                        self.fhs.append(open(s, 'w'))
                    except IOError as ioe:
                         raise error.general("creating log file '" + s + \
                                             "': " + str(ioe))

    def __del__(self):
        for f in range(2, len(self.fhs)):
            self.fhs[f].close()

    def __str__(self):
        t = ''
        for tl in self.tail:
            t += tl + os.linesep
        return t[:-len(os.linesep)]

    def _tail(self, text):
        if type(text) is not list:
            text = text.splitlines()
        self.tail += text
        if len(self.tail) > self.tail_size:
            self.tail = self.tail[-self.tail_size:]

    def has_stdout(self):
        return self.fhs[0] is not None

    def has_stderr(self):
        return self.fhs[1] is not None

    def output(self, text):
        """Output the text message to all the logs."""
        # Reformat the text to have local line types.
        text = text.replace(chr(13), '').splitlines()
        self._tail(text)
        out = ''
        for l in text:
            out += l + os.linesep
        for f in range(0, len(self.fhs)):
            if self.fhs[f] is not None:
                self.fhs[f].write(out)
        self.flush()

    def flush(self):
        """Flush the output."""
        for f in range(0, len(self.fhs)):
            if self.fhs[f] is not None:
                self.fhs[f].flush()

if __name__ == "__main__":
    l = log(['stdout', 'log.txt'], tail_size = 20)
    for i in range(0, 10):
        l.output('log: hello world: %d\n' % (i))
    l.output('log: hello world CRLF\r\n')
    l.output('log: hello world NONE')
    l.flush()
    print('=-' * 40)
    print('tail: %d' % (len(l.tail)))
    print(l)
    print('=-' * 40)
    for i in range(0, 10):
        l.output('log: hello world 2: %d\n' % (i))
    l.flush()
    print('=-' * 40)
    print('tail: %d' % (len(l.tail)))
    print(l)
    print('=-' * 40)
    for i in [0, 1]:
        quiet = False
        tracing = False
        print('- quiet:%s - trace:%s %s' % (str(quiet), str(tracing), '-' * 30))
        trace('trace with quiet and trace off')
        notice('notice with quiet and trace off')
        quiet = True
        tracing = False
        print('- quiet:%s - trace:%s %s' % (str(quiet), str(tracing), '-' * 30))
        trace('trace with quiet on and trace off')
        notice('notice with quiet on and trace off')
        quiet = False
        tracing = True
        print('- quiet:%s - trace:%s %s' % (str(quiet), str(tracing), '-' * 30))
        trace('trace with quiet off and trace on')
        notice('notice with quiet off and trace on')
        quiet = True
        tracing = True
        print('- quiet:%s - trace:%s %s' % (str(quiet), str(tracing), '-' * 30))
        trace('trace with quiet on and trace on')
        notice('notice with quiet on and trace on')
        default = l
    print('=-' * 40)
    print('tail: %d' % (len(l.tail)))
    print(l)
    print('=-' * 40)
    del l