Issues (229)

client/pygameui/demo/log.py (3 issues)

1
#
2
#  Copyright 2001 - 2016 Ludek Smid [http://www.ospace.net/]
3
#
4
#  This file is part of Pygame.UI.
5
#
6
#  Pygame.UI is free software; you can redistribute it and/or modify
7
#  it under the terms of the Lesser GNU General Public License as published by
8
#  the Free Software Foundation; either version 2.1 of the License, or
9
#  (at your option) any later version.
10
#
11
#  Pygame.UI is distributed in the hope that it will be useful,
12
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
#  Lesser GNU General Public License for more details.
15
#
16
#  You should have received a copy of the Lesser GNU General Public License
17
#  along with Pygame.UI; if not, write to the Free Software
18
#  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
#
20
21
# Logger facility
22
23
import time
24
import string
25
import sys
26
import traceback, inspect, os.path
27
28
__startTime = time.time()
29
30
errorLog = None
31
msgLog = None
32
33
LEVEL_DEBUG = 40
34
LEVEL_MESSAGE = 30
35
LEVEL_WARNING = 20
36
LEVEL_ERROR = 10
37
LEVEL_FATAL = 0
38
39
level = LEVEL_DEBUG
40
41
__srcfile = os.path.splitext(__file__)
42
if __srcfile[1] in [".pyc", ".pyo"]:
43
    __srcfile = __srcfile[0] + ".py"
44
else:
45
    __srcfile = __file__
46
47
def setErrorLog(filename):
48
    global errorLog
49
    errorLog = open(filename, 'w')
50
51
def setMessageLog(filename):
52
    global msgLog
53
    msgLog = open(filename, 'w')
54
55
def setLevel(aLevel):
56
    global level
57
    level = aLevel
58
59
def __getTime():
60
    diff = int(time.time() - __startTime);
61
    secs = diff % 60;
62
    mins = diff % 3600 / 60;
63
    hours = diff / 3600;
64
    # TODO enable me
65
    # return time.strftime('%Y-%m-%d %H:%M:%S')
66
    return time.strftime('%H:%M:%S')
67
    #@return '%02d:%02d:%02d [%s]' % (hours, mins, secs, time.strftime('%Y%m%d%H%M%S'))
68
    #@return '%02d:%02d:%02d' % (hours, mins, secs)
69
70 View Code Duplication
def __getCaller():
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
71
    return "--"
72
    f = inspect.stack()[2]
73
    # be smart and try to get oid of object
74
    frame = f[0]
75
    name = frame.f_globals['__name__']
76
    #if len(name) > 15:
77
    #    name = '..%s' % name[-13:]
78
    if frame.f_locals.has_key('obj') and hasattr(frame.f_locals['obj'], 'oid'):
79
        return '%s %d [oid=%s]' % (
80
            name,
81
            frame.f_lineno,
82
            frame.f_locals['obj'].oid
83
        )
84
    return '%s %d' % (name, frame.f_lineno)
85
86
def debug(*args):
87
    if level < LEVEL_DEBUG:
88
        return
89
    print __getTime(), 'DBG', __getCaller(),
90
    for item in args:
91
        print str(item),
92
    print
93
    if msgLog:
94
        print >> msgLog, __getTime(), 'DBG', __getCaller(),
95
        for item in args:
96
            print >> msgLog, str(item),
97
        print >> msgLog
98
        msgLog.flush()
99
100
def message(*args):
101
    if level < LEVEL_MESSAGE:
102
        return
103
    print __getTime(), 'MSG', __getCaller(),
104
    for item in args:
105
        print str(item),
106
    print
107
    if msgLog:
108
        print >> msgLog, __getTime(), 'MSG', __getCaller(),
109
        for item in args:
110
            print >> msgLog, str(item),
111
        print >> msgLog
112
        msgLog.flush()
113
114 View Code Duplication
def warning(*args):
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
115
    if level < LEVEL_WARNING:
116
        return
117
    # TODO lock!
118
    print __getTime(), 'WAR', __getCaller(),
119
    for item in args:
120
        print str(item),
121
    print
122
    if sys.exc_info() != (None, None, None):
123
        print 79 * '-'
124
        traceback.print_exc(file=sys.stdout)
125
        print 79 * '-'
126
    if errorLog:
127
        print >> errorLog, __getTime(), 'WAR', __getCaller(),
128
        for item in args:
129
            print >> errorLog, str(item),
130
        print >> errorLog
131
        if sys.exc_info() != (None, None, None):
132
            print >> errorLog, 79 * '-'
133
            traceback.print_exc(file=errorLog)
134
            print >> errorLog, 79 * '-'
135
        errorLog.flush()
136
    if msgLog:
137
        print >> msgLog, __getTime(), 'WAR', __getCaller(),
138
        for item in args:
139
            print >> msgLog, str(item),
140
        print >> msgLog
141
        if sys.exc_info() != (None, None, None):
142
            print >> msgLog, 79 * '-'
143
            traceback.print_exc(file=msgLog)
144
            print >>msgLog, 79 * '-'
145
        msgLog.flush()
146
147 View Code Duplication
def error(*args):
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
148
    print __getTime(), 'ERR', __getCaller(),
149
    for item in args:
150
        print str(item),
151
    print
152
    if sys.exc_info() != (None, None, None):
153
        print 79 * '-'
154
        traceback.print_exc(file=sys.stdout)
155
        print 79 * '-'
156
    if errorLog:
157
        print >> errorLog, __getTime(), 'ERR', __getCaller(),
158
        for item in args:
159
            print >> errorLog, str(item),
160
        print >> errorLog
161
        if sys.exc_info() != (None, None, None):
162
            print >> errorLog, 79 * '-'
163
            traceback.print_exc(file=errorLog)
164
            print >> errorLog, 79 * '-'
165
        errorLog.flush()
166
    if msgLog:
167
        print >> msgLog, __getTime(), 'ERR', __getCaller(),
168
        for item in args:
169
            print >> msgLog, str(item),
170
        print >> msgLog
171
        if sys.exc_info() != (None, None, None):
172
            print >> msgLog, 79 * '-'
173
            traceback.print_exc(file=msgLog)
174
            print >>msgLog, 79 * '-'
175
        msgLog.flush()
176
    sys.exit(1)
177