forked from tvwerkhoven/metacheck
-
Notifications
You must be signed in to change notification settings - Fork 0
/
liblog.py
125 lines (108 loc) · 3.37 KB
/
liblog.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
#!/usr/bin/env python
# encoding: utf-8
"""
This is liblog from astooki.liblog, providing logging functions
This module provides logging functions to log data using prefixes, loglevels
and permanent logfiles. This is probably only useful in more elaborate scripts
and quite meaningful on its own.
"""
## @file liblog.py
# @author Tim van Werkhoven (t.i.m.vanwerkhoven@xs4all.nl)
#
# Created by Tim van Werkhoven.
# Copyright (c) 2008--2011 Tim van Werkhoven (t.i.m.vanwerkhoven@xs4all.nl)
#
# This file is licensed under the Creative Commons Attribution-Share Alike
# license versions 3.0 or higher, see
# http://creativecommons.org/licenses/by-sa/3.0/
#=============================================================================
# Import libraries here
#=============================================================================
import sys
import time
#=============================================================================
# Defines
#=============================================================================
# Various levels of verbosity (make sure these increment nicely)
# EMERG = 0
# ALERT = 1
# CRIT = 2
ERR = 3
WARNING = 4
NOTICE = 5
INFO = 6
DEBUG = 7
## @brief strings describing the different loglevel
LVLDESC=['[EMERG]', \
'[ALERT]', \
'[CRIT ]', \
'[ERROR]', \
'[warn ]', \
'[notic]', \
'[info ]', \
'[debug]']
## @brief Exit code for messages with the ERR level
EXIT = -1
## @brief Stores logfile when set
LOGFILE = None
LOGFD = 0
LOGLASTDAY = 0
## @brief Verbosity level to use
VERBOSITY = 4
## @brief Reset color codes
RESETCL = "\033[0m"
## @brief Debug color, blue text
DEBUGCL = "\033[34m"
## @brief Warning color, yellow text on black bg
WARNCL = "\033[33;40m"
## @brief Error color, white text on red bg
ERRORCL = "\033[37;41m"
#=============================================================================
# Routines
#=============================================================================
## @brief (Re-)initialize logging to disk at 'logfile'
def initLogFile(logfile):
#import libfile
# Don't save old file, simply append to existing file
#libfile.saveOldFile(logfile)
global LOGFD
if (not LOGFD):
LOGFD = open(logfile, "a+")
else:
LOGFD.close()
LOGFD = open(logfile, "a+")
## @brief Print log message with a certain verbosity.
#
# Print a log message. If LOGFD is set, it is also written to the file that
# file descriptor is poiting to. Status levels are prepended to the output.
#
# @param verb The status level of the message
# @param msg The message to print
# @param err Exit status to use for verb == ERR
def prNot(verb, msg, err=EXIT):
# First save to file if LOGFD is set...
if (verb < DEBUG and LOGFD):
tm = time.localtime()
global LOGLASTDAY
if (LOGLASTDAY != tm[2]):
print >> LOGFD, "-"*20, time.asctime(tm), "-"*20
LOGLASTDAY = tm[2]
print >> LOGFD, time.strftime("%H:%M:%S", tm), LVLDESC[verb], msg
LOGFD.flush()
# Then print to screen
if (VERBOSITY >= verb):
if (verb >= INFO):
sys.stdout.write(DEBUGCL)
print LVLDESC[verb], RESETCL, msg
elif (verb == WARNING):
sys.stdout.write(WARNCL)
print LVLDESC[verb], RESETCL, msg
elif (verb == ERR):
sys.stdout.write(ERRORCL)
print LVLDESC[verb], RESETCL, msg
# If we have an error, close the FD! otherwise the last message(s) will
# be lost.
if LOGFD: LOGFD.close()
sys.exit(err)
else:
print LVLDESC[verb], msg