Issues (229)

server/lib/ige/log.py (4 issues)

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