-
Notifications
You must be signed in to change notification settings - Fork 0
/
pcsd_log.py
179 lines (161 loc) · 5.1 KB
/
pcsd_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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# This file is part of Fork of crcnetd - CRCnet Configuration System Daemon
#
# Copyright (c) 2012 sun-exploit <[email protected]>
#
# Fork of crcnetd is free software: you may copy, redistribute
# 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 file 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, see <http://www.gnu.org/licenses/>.
#
# This file incorporates work covered by the following copyright and
# permission notice:
#
# Copyright (C) 2006 The University of Waikato
#
# This file is part of crcnetd - CRCnet Configuration System Daemon
#
# Provides error and information logging routines to the rest of the
# application.
#
# Author: Matt Brown <[email protected]>
# Version: $Id$
#
# crcnetd is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License version 2 as published by the Free
# Software Foundation.
#
# crcnetd 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
# crcnetd; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
import sys
import os
import os.path
import signal
import syslog
import traceback
import time
from pcsd_common import *
pcs_utils_type = PCSD_CORE
_isDaemon = 0
_verbose = 0
_tracebackLog = None
def setVerbose():
global _verbose
_verbose = 1
def isVerbose():
if _verbose:
return True
else:
return False
def setDaemonStatus(val, name=None):
global _isDaemon
_isDaemon = val
if _isDaemon:
if name is None: name = os.path.basename(sys.argv[0])
syslog.openlog(name, syslog.LOG_PID, syslog.LOG_DAEMON)
def setTracebackLog(log=None):
global _tracebackLog
_tracebackLog = log
def prepareTraceback(exc_info):
if exc_info is None:
return ""
(type, value, tb) = exc_info
return traceback.format_exception(type, value, tb)
def log_tb(exc_info, msg=""):
global _tracebackLog
tb = prepareTraceback(exc_info)
if len(tb) <= 0 and msg == "":
return
if _tracebackLog is None:
return
try:
fd = open(_tracebackLog, "a")
fd.write("%s\n" % time.ctime())
if msg!="": fd.write("%s\n" % msg)
for line in tb: fd.write(line)
fd.write("")
fd.close()
except:
(etype, value, etb) = sys.exc_info()
log_error("Failed to write traceback logfile: %s" % value)
def log_debug(msg, exc_info=None):
"""Logs a debugging message"""
global _isDaemon
if not _isDaemon:
sys.stderr.write("D: %s\n" % msg)
log_tb(exc_info, msg)
def log_info(msg, exc_info=None):
"""Logs an information message"""
global _isDaemon
if _isDaemon:
syslog.syslog(syslog.LOG_INFO, msg)
else:
sys.stdout.write("I: %s\n" % msg)
log_tb(exc_info)
def log_notice(msg, exc_info=None):
"""Logs an notice message"""
global _isDaemon
if _isDaemon:
syslog.syslog(syslog.LOG_NOTICE, msg)
else:
sys.stdout.write("N: %s\n" % msg)
log_tb(exc_info)
def log_warn(msg, exc_info=None):
"""Logs a warning message"""
global _isDaemon
if _isDaemon:
syslog.syslog(syslog.LOG_WARNING, msg)
else:
sys.stderr.write("W: %s\n" % msg)
log_tb(exc_info)
def log_error(msg, exc_info=None):
"""Logs an error message"""
global _isDaemon
tb = prepareTraceback(exc_info)
if _isDaemon:
syslog.syslog(syslog.LOG_ERR, msg)
else:
sys.stderr.write("E: %s\n" % msg)
log_tb(exc_info)
def log_fatal(msg, exc_info=None):
"""Logs a fatal error message and exits"""
global _isDaemon
tb = prepareTraceback(exc_info)
if _isDaemon:
for line in tb: syslog.syslog(syslog.LOG_CRIT, line.strip())
syslog.syslog(syslog.LOG_CRIT, msg)
else:
sys.stderr.write("F: %s\n" % msg)
log_tb(exc_info)
# Tell ourselves to shutdown!
os.kill(os.getpid(), signal.SIGTERM)
def log_command(command):
with os.popen(command) as fh:
output = fh.readlines()
rv = fh.close()
str = "".join(output).strip()
if rv != None and len(str)>0:
log_debug("Command (%s) output:\n %s" % (command, str))
return rv
def log_custom(file, msg):
try:
fd = open(file, "a")
fd.write("%s " % time.ctime())
fd.write("%s\n" % msg)
fd.close()
except:
(etype, value, etb) = sys.exc_info()
log_error("Failed to write to custom logfile(%s): %s" % (file,msg))