/usr/share/pyshared/wicd/logfile.py is in python-wicd 1.7.2.4-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 179 180 181 182 183 184 185 186 | #!/usr/bin/env python
#
# Copyright (C) 1999-2006 Keith Dart <keith@kdart.com>
# Copyright (C) 2008-2009 Dan O'Reilly <oreilldf@gmail.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
"""
Managing logfile rotation. A ManagedLog object is a file-like object that
rotates itself when a maximum size is reached.
"""
import sys
import os
import time
class SizeError(IOError):
pass
class LogFile(file):
"""LogFile(name, [mode="w"], [maxsize=360000])
Opens a new file object. After writing <maxsize> bytes a SizeError
will be raised.
"""
def __init__(self, name, mode="a", maxsize=360000):
super(LogFile, self).__init__(name, mode)
self.maxsize = maxsize
self.eol = True
try:
self.written = os.fstat(self.fileno())[6]
except OSError:
self.written = 0
def write(self, data):
self.written += len(data)
data = data.decode('utf-8').encode('utf-8')
if len(data) <= 0: return
if self.eol:
super(LogFile, self).write(self.get_time() + ' :: ')
self.eol = False
if data[-1] == '\n':
self.eol = True
data = data[:-1]
super(LogFile, self).write(data.replace(
'\n', '\n' + self.get_time() + ' :: '))
if self.eol:
super(LogFile, self).write('\n')
self.flush()
if self.written > self.maxsize:
raise SizeError
def get_time(self):
""" Return a string with the current time nicely formatted.
The format of the returned string is yyyy/mm/dd HH:MM:SS
"""
x = time.localtime()
return ''.join([
str(x[0]).rjust(4, '0'), '/', str(x[1]).rjust(2, '0'), '/',
str(x[2]).rjust(2, '0'), ' ', str(x[3]).rjust(2, '0'), ':',
str(x[4]).rjust(2, '0'), ':', str(x[5]).rjust(2, '0')])
def rotate(self):
return rotate(self)
def note(self, text):
"""Writes a specially formated note text to the file.
The note starts with the string '\\n#*=' so you can easily filter them.
"""
self.write("\n#*===== %s =====\n" % (text,))
class ManagedLog(object):
"""ManagedLog(name, [maxsize=360000], [maxsave=9])
A ManagedLog instance is a persistent log object. Write data with the
write() method. The log size and rotation is handled automatically.
"""
def __init__(self, name, maxsize=360000, maxsave=3):
if not os.path.exists(os.path.dirname(name)):
os.makedirs(os.path.dirname(name))
self._lf = LogFile(name, "a", maxsize)
self.maxsave = maxsave
def __repr__(self):
return "%s(%r, %r, %r)" % (self.__class__.__name__, self._lf.name,
self._lf.maxsize, self.maxsave)
def write(self, data):
try:
self._lf.write(data)
except SizeError:
self._lf = rotate(self._lf, self.maxsave)
def note(self, data):
try:
self._lf.note(data)
except SizeError:
self._lf = rotate(self._lf, self.maxsave)
def written(self):
return self._lf.written
def rotate(self):
self._lf = rotate(self._lf, self.maxsave)
# auto-delegate remaining methods (but you should not read or seek an open
# log file).
def __getattr__(self, name):
return getattr(self._lf, name)
# useful for logged stdout for daemon processes
class ManagedStdio(ManagedLog):
def write(self, data):
try:
self._lf.write(data)
except SizeError:
sys.stdout.flush()
sys.stderr.flush()
self._lf = rotate(self._lf, self.maxsave)
fd = self._lf.fileno()
os.dup2(fd, 1)
os.dup2(fd, 2)
sys.stdout = sys.stderr = self
def rotate(fileobj, maxsave=9):
name = fileobj.name
mode = fileobj.mode
maxsize = fileobj.maxsize
fileobj.close()
shiftlogs(name, maxsave)
return LogFile(name, mode, maxsize)
# assumes basename logfile is closed.
def shiftlogs(basename, maxsave):
topname = "%s.%d" % (basename, maxsave)
if os.path.isfile(topname):
os.unlink(topname)
for i in range(maxsave, 0, -1):
oldname = "%s.%d" % (basename, i)
newname = "%s.%d" % (basename, i+1)
try:
os.rename(oldname, newname)
except OSError:
pass
try:
os.rename(basename, "%s.1" % (basename))
except OSError:
pass
def open(name, maxsize=360000, maxsave=9):
return ManagedLog(name, maxsize, maxsave)
def writelog(logobj, data):
try:
logobj.write(data)
except SizeError:
return rotate(logobj)
else:
return logobj
|