File: Log.py

package info (click to toggle)
fofix-dfsg 3.121-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 27,016 kB
  • ctags: 4,131
  • sloc: python: 44,002; makefile: 99; perl: 42; sh: 39
file content (121 lines) | stat: -rwxr-xr-x 4,488 bytes parent folder | download | duplicates (4)
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
#####################################################################
# -*- 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.                                              #
#####################################################################

##@package Log
# Functions for various types of logging that FoFiX needs to do.

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.appName() + ".log"), "w")
  else: # GNU/Linux et al.
    logFile = open(os.path.join(Resource.getWritableResourcePath(), Version.appName() + ".log"), "w")
else:
  logFile = open(Version.appName() + ".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

## Generic logging function.
# @param cls    Priority class for the message
# @param msg    Log message text
def _log(cls, msg):
  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!

## 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
def error(msg):
  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())

## Log a warning.
# @param msg    Warning message text
def warn(msg):
  _log("warn", msg)

## Log a notice.
# @param msg    Notice message text
def notice(msg):
  _log("notice", msg)

## Log a debug message.
# @param msg    Debug message text
def debug(msg):
  _log("debug", msg)

## A hook to catch Python warnings.
def _showwarning(*args, **kw):
  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())