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 import os
34 import sys
35 import traceback
36 import time
37 import fcntl
38 import atexit
39 from spacewalk.common.fileutils import getUidGid
40 from spacewalk.common.rhnLib import isSUSE
41
42 LOG = None
43
44
45
46
48 if time.daylight:
49
50 tz_offset = time.altzone
51 else:
52
53 tz_offset = time.timezone
54
55
56 if tz_offset < 0:
57 sign = '+'
58 else:
59 sign = '-'
60 hours, secs = divmod(abs(tz_offset), 3600)
61 mins = secs / 60
62
63 tz_offset_string = " %s%02d:%02d" % (sign, hours, mins)
64 t = time.strftime("%Y/%m/%d %H:%M:%S", time.localtime(time.time()))
65 return t + tz_offset_string
66
67
68
69
71 s = fcntl.fcntl(fd, fcntl.F_GETFD)
72 fcntl.fcntl(fd, fcntl.F_SETFD, s | fcntl.FD_CLOEXEC)
73
74
75
76
77
78
79 -def initLOG(log_file="stderr", level=0):
80 global LOG
81
82
83 if LOG is not None:
84
85 if log_file is None or LOG.file == log_file:
86
87 LOG.level = level
88 return
89
90 LOG = None
91 elif log_file is None:
92 log_file = "/dev/null"
93
94
95 log_path = os.path.dirname(log_file)
96 if log_file not in ('stderr', 'stdout') \
97 and log_path and not os.path.exists(os.path.dirname(log_file)):
98 log_stderr("WARNING: log path not found; attempting to create %s" %
99 log_path, sys.exc_info()[:2])
100
101
102 if isSUSE():
103 apache_uid, apache_gid = getUidGid('wwwrun', 'www')
104 else:
105 apache_uid, apache_gid = getUidGid('apache', 'apache')
106
107 try:
108 os.makedirs(log_path)
109 if os.getuid() == 0:
110 os.chown(log_path, apache_uid, 0)
111 else:
112 os.chown(log_path, apache_uid, apache_gid)
113 except:
114 log_stderr("ERROR: unable to create log file path %s" % log_path,
115 sys.exc_info()[:2])
116 return
117
118
119
120 LOG = rhnLog(log_file, level)
121 return
122
123
124
125
130
131
132
133
135 pid = os.getpid()
136 for arg in args:
137 sys.stderr.write("Spacewalk %s %s: %s\n" % (
138 pid, log_time(), arg))
139 sys.stderr.flush()
140
141
142
143
151
152
153
154
158
159
160
161
162
166
167
168
169
171
173 self.level = level
174 self.log_info = "0.0.0.0: "
175 self.file = log_file
176 self.pid = os.getpid()
177 self.real = 0
178 if self.file in ["stderr", "stdout"]:
179 self.fd = getattr(sys, self.file)
180 self.log_info = ""
181 return
182
183 newfileYN = 0
184 if not os.path.exists(self.file):
185 newfileYN = 1
186
187
188 try:
189
190 self.fd = open(self.file, "a", 1)
191 set_close_on_exec(self.fd)
192 if newfileYN:
193 if isSUSE():
194 apache_uid, apache_gid = getUidGid('wwwrun', 'www')
195 else:
196 apache_uid, apache_gid = getUidGid('apache', 'apache')
197 if os.getuid() == 0:
198 os.chown(self.file, apache_uid, 0)
199 else:
200 os.chown(self.file, apache_uid, apache_gid)
201 os.chmod(self.file, int('0660', 8))
202 except:
203 log_stderr("ERROR LOG FILE: Couldn't open log file %s" % self.file,
204 sys.exc_info()[:2])
205 self.file = "stderr"
206 self.fd = sys.stderr
207 else:
208 self.real = 1
209
210
212 tbStack = traceback.extract_stack()
213 callid = len(tbStack) - 3
214 module = ''
215 try:
216 module = tbStack[callid][0]
217 arr = module.split('/')
218 if len(arr) > 1:
219 lastDir = arr[-2] + "/"
220 else:
221 lastDir = ""
222 filename = arr[-1]
223 filename = filename[:filename.rindex('.')]
224 module = lastDir + filename
225 del lastDir
226 except:
227 module = ''
228
229 msg = "%s%s.%s" % (self.log_info, module, tbStack[callid][2])
230 if args:
231 msg = "%s%s" % (msg, repr(args))
232 self.writeMessage(msg)
233
234
236 if self.real:
237 msg = "%s %d %s" % (log_time(), self.pid, msg)
238 else:
239 msg = "%s %s" % (log_time(), msg)
240 self.writeToLog(msg)
241
242
244
245
246 self.fd.write("%s\n" % msg)
247
248
250 remoteAddr = '0.0.0.0'
251 if req:
252 if "X-Forwarded-For" in req.headers_in:
253 remoteAddr = req.headers_in["X-Forwarded-For"]
254 else:
255 remoteAddr = req.connection.remote_ip
256 self.log_info = "%s: " % (remoteAddr, )
257
258
260 if self.real:
261 self.fd.close()
262 self.level = self.log_info = None
263 self.pid = self.file = self.real = self.fd = None
264
265
271
272 atexit.register(_exit)
273
274
275
276 if __name__ == "__main__":
277 print("You can not run this module by itself")
278 sys.exit(-1)
279
280