def _open_log_file():
"""Open a Porcupine log file.
Usually this opens and overwrites log.txt. If another Porcupine
process has it currently opened, this opens log1.txt instead, then
log2.txt and so on.
"""
# create an iterator 'log.txt', 'log2.txt', 'log3.txt', ...
filenames = itertools.chain(
['log.txt'],
map('log{}.txt'.format, itertools.count(start=2)),
)
for filename in filenames:
path = os.path.join(dirs.cachedir, filename)
# unfortunately there's not a mode that would open in write but
# not truncate like 'w' or seek to end like 'a'
fileno = os.open(path, os.O_WRONLY | os.O_CREAT, 0o644)
if _lock(fileno):
# now we can delete the old content, can't use os.truncate
# here because it doesn't exist on windows
file = open(fileno, 'w')
file.truncate(0)
return file
else:
os.close(fileno)
# FileHandler doesn't take already opened files and StreamHandler
# doesn't close the file :(
评论列表
文章目录