-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapplog.py
126 lines (113 loc) · 3.11 KB
/
applog.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
#!/usr/bin/env python
#
#############################################################################
#
# applog - Application logging
# Copyright (C) 2015, testcams.com
#
# This module is licensed under GPL v3: http://www.gnu.org/licenses/gpl-3.0.html
#
#############################################################################
#
from __future__ import print_function
from __future__ import division
import sys
#
# logging flags
#
# logging level flags
APPLOGF_LEVEL_ERROR = (0x00000001<<0)
APPLOGF_LEVEL_INFORMATIONAL = (0x00000001<<1)
APPLOGF_LEVEL_WARNING = (0x00000001<<2)
APPLOGF_LEVEL_VERBOSE = (0x00000001<<3)
APPLOGF_LEVEL_DEBUG = (0x00000001<<4)
APPLOGF_LEVEL_MASK = 0x000000FF
APPLOGF_DONT_WRITE_TO_CONSOLE = (0x00000001<<8)
#
# global variables
#
gFileSessionLog = None
gFileLifetimeLog = None
gLoggingFlags = 0
#
# initialize this module, which includes opening log file(s) for writing
#
def applog_init(loggingFlags=APPLOGF_LEVEL_INFORMATIONAL | APPLOGF_LEVEL_ERROR, sessionLogFilename=None, lifetimeLogFilename=None):
global gFileSessionLog, gFileLifetimeLog, gLoggingFlags
gLoggingFlags = loggingFlags
if sessionLogFilename:
try:
gFileSessionLog = open(sessionLogFilename, "w")
except IOError as e:
print("Unable to open/create logfile \"{:s}\". {:s}".format(sessionLogFilename, str(e)))
return e.errno
if lifetimeLogFilename:
try:
gFileLifetimeLog = open(lifetimeLogFilename, "a")
except IOError as e:
print("Unable to open/create logfile \"{:s}\". {:s}".format(lifetimeLogFilename, str(e)))
return e.errno
return 0
#
# set new logging flags
#
def applog_set_loggingFlags(newLoggingFlags):
global gLoggingFlags
gLoggingFlags = newLoggingFlags
#
# shutdown this module
#
def applog_shutdown():
try:
if gFileSessionLog:
gFileSessionLog.close()
if gFileLifetimeLog:
gFileSessionLog.close()
except IOError as e:
print("Unable to close logfile. {:s}".format(str(e)))
#
# Log message with specified level
#
def applog(str, flags=APPLOGF_LEVEL_INFORMATIONAL):
if gLoggingFlags & (flags & APPLOGF_LEVEL_MASK):
if not (flags & APPLOGF_DONT_WRITE_TO_CONSOLE):
if flags & APPLOGF_LEVEL_ERROR:
print(str, file=sys.stderr)
else:
print(str)
# redirect output to log file(s)
if gFileSessionLog:
print(str, file=gFileSessionLog)
if gFileLifetimeLog:
print(str, file=gFileLifetimeLog)
#
# Logging wrapper functions for each level
#
def applog_i(s):
applog(s, APPLOGF_LEVEL_INFORMATIONAL)
def applog_v(s):
applog(s, APPLOGF_LEVEL_VERBOSE)
def applog_w(s):
applog(s, APPLOGF_LEVEL_WARNING)
def applog_e(s):
applog(s, APPLOGF_LEVEL_ERROR)
def applog_d(s):
applog(s, APPLOGF_LEVEL_DEBUG)
#
# Logging check-enabled functions, used to avoid
# performance penalty of generating log message
# if logging level is not enabled
#
def isDebugLog():
return (gLoggingFlags & APPLOGF_LEVEL_DEBUG)
def isVerboseLog():
return (gLoggingFlags & APPLOGF_LEVEL_VERBOSE)
#
# console writing function wrappers
#
def consoleWriteLine(msg):
sys.stdout.write(msg)
sys.stdout.flush()
def consoleClearLine():
sys.stdout.write("\r" + " "*78 + "\r")
sys.stdout.flush()