forked from fofix/fofix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Log.py
134 lines (119 loc) · 4.57 KB
/
Log.py
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
#####################################################################
# -*- coding: iso-8859-1 -*- #
# #
# Frets on Fire #
# Copyright (C) 2006 Sami Kyöstilä #
# 2009 John Stumpo #
# #
# This program is free software; you can redistribute it and/or #
# modify it under the terms of the GNU General Public License #
# as published by the Free Software Foundation; either version 2 #
# of the License, or (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program; if not, write to the Free Software #
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, #
# MA 02110-1301, USA. #
#####################################################################
'''
Functions for writing to the logfile.
'''
import sys
import os
import Resource
import Version
import traceback
import time
import warnings
# Whether to output log entries to stdout in addition to the logfile.
quiet = True
# File object representing the logfile.
if os.name == "posix": # evilynux - logfile in ~/.fofix/ for GNU/Linux and MacOS X
# evilynux - Under MacOS X, put the logs in ~/Library/Logs
if os.uname()[0] == "Darwin":
logFile = open(os.path.join(Resource.getWritableResourcePath(),
"..", "..", "Logs",
Version.PROGRAM_UNIXSTYLE_NAME + ".log"), "w")
else: # GNU/Linux et al.
logFile = open(os.path.join(Resource.getWritableResourcePath(), Version.PROGRAM_UNIXSTYLE_NAME + ".log"), "w")
elif os.name == "nt":
logFile = open(os.path.join(Resource.getWritableResourcePath(), Version.PROGRAM_UNIXSTYLE_NAME + ".log"), "w")
else:
logFile = open(Version.PROGRAM_UNIXSTYLE_NAME + ".log", "w") #MFH - local logfile!
# Character encoding to use for logging.
encoding = "iso-8859-1"
if "-v" in sys.argv or "--verbose" in sys.argv:
quiet = False
# Labels for different priorities, as output to the logfile.
labels = {
"warn": "(W)",
"debug": "(D)",
"notice": "(N)",
"error": "(E)",
}
# Labels for different priorities, as output to stdout.
if os.name == "posix":
displaylabels = {
"warn": "\033[1;33m(W)\033[0m",
"debug": "\033[1;34m(D)\033[0m",
"notice": "\033[1;32m(N)\033[0m",
"error": "\033[1;31m(E)\033[0m",
}
else:
displaylabels = labels
def _log(cls, msg):
'''
Generic logging function.
@param cls: Priority class for the message
@param msg: Log message text
'''
if not isinstance(msg, unicode):
msg = unicode(msg, encoding).encode(encoding, "ignore")
timeprefix = "[%12.6f] " % (time.time() - _initTime)
if not quiet:
print timeprefix + displaylabels[cls] + " " + msg
print >>logFile, timeprefix + labels[cls] + " " + msg
logFile.flush() #stump: truncated logfiles be gone!
def error(msg):
'''
Log a major error.
If this is called while handling an exception, the traceback will
be automatically included in the log.
@param msg: Error message text
'''
if sys.exc_info() == (None, None, None):
#warnings.warn("Log.error() called without an active exception", UserWarning, 2) #stump: should we enforce this?
_log("error", msg)
else:
_log("error", msg + "\n" + traceback.format_exc())
def warn(msg):
'''
Log a warning.
@param msg: Warning message text
'''
_log("warn", msg)
def notice(msg):
'''
Log a notice.
@param msg: Notice message text
'''
_log("notice", msg)
def debug(msg):
'''
Log a debug message.
@param msg: Debug message text
'''
_log("debug", msg)
def _showwarning(*args, **kw):
'''A hook to catch Python warnings.'''
warn("A Python warning was issued:\n" + warnings.formatwarning(*args, **kw))
_old_showwarning(*args, **kw)
_old_showwarning = warnings.showwarning
warnings.showwarning = _showwarning
_initTime = time.time()
debug("Logging initialized: " + time.asctime())