mercurial/loggingutil.py
author Yuya Nishihara <yuya@tcha.org>
Sun, 18 Nov 2018 18:35:31 +0900
changeset 40800 698477777883
parent 40799 03127e580980
child 40821 96be0ecad648
permissions -rw-r--r--
loggingutil: document openlogfile() This function will be used later for command-server logging.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
40799
03127e580980 loggingutil: extract openlogfile() and proxylogger to new module
Yuya Nishihara <yuya@tcha.org>
parents: 40798
diff changeset
     1
# loggingutil.py - utility for logging events
18669
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
     2
#
18676
1506eb487ddd blackbox: fix copyright
Bryan O'Sullivan <bryano@fb.com>
parents: 18675
diff changeset
     3
# Copyright 2010 Nicolas Dumazet
18669
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
     4
# Copyright 2013 Facebook, Inc.
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
     5
#
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
     6
# This software may be used and distributed according to the terms of the
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
     7
# GNU General Public License version 2 or any later version.
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
     8
28090
8113c88b8e6d blackbox: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28027
diff changeset
     9
from __future__ import absolute_import
8113c88b8e6d blackbox: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28027
diff changeset
    10
8113c88b8e6d blackbox: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28027
diff changeset
    11
import errno
8113c88b8e6d blackbox: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28027
diff changeset
    12
40799
03127e580980 loggingutil: extract openlogfile() and proxylogger to new module
Yuya Nishihara <yuya@tcha.org>
parents: 40798
diff changeset
    13
from . import (
38783
e7aa113b14f7 global: use pycompat.xrange()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37120
diff changeset
    14
    pycompat,
37120
a8a902d7176e procutil: bulk-replace function calls to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36607
diff changeset
    15
)
18669
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
    16
40799
03127e580980 loggingutil: extract openlogfile() and proxylogger to new module
Yuya Nishihara <yuya@tcha.org>
parents: 40798
diff changeset
    17
def openlogfile(ui, vfs, name, maxfiles=0, maxsize=0):
40800
698477777883 loggingutil: document openlogfile()
Yuya Nishihara <yuya@tcha.org>
parents: 40799
diff changeset
    18
    """Open log file in append mode, with optional rotation
698477777883 loggingutil: document openlogfile()
Yuya Nishihara <yuya@tcha.org>
parents: 40799
diff changeset
    19
698477777883 loggingutil: document openlogfile()
Yuya Nishihara <yuya@tcha.org>
parents: 40799
diff changeset
    20
    If maxsize > 0, the log file will be rotated up to maxfiles.
698477777883 loggingutil: document openlogfile()
Yuya Nishihara <yuya@tcha.org>
parents: 40799
diff changeset
    21
    """
34300
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
    22
    def rotate(oldpath, newpath):
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
    23
        try:
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
    24
            vfs.unlink(newpath)
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
    25
        except OSError as err:
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
    26
            if err.errno != errno.ENOENT:
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
    27
                ui.debug("warning: cannot remove '%s': %s\n" %
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
    28
                         (newpath, err.strerror))
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
    29
        try:
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
    30
            if newpath:
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
    31
                vfs.rename(oldpath, newpath)
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
    32
        except OSError as err:
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
    33
            if err.errno != errno.ENOENT:
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
    34
                ui.debug("warning: cannot rename '%s' to '%s': %s\n" %
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
    35
                         (newpath, oldpath, err.strerror))
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
    36
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
    37
    if maxsize > 0:
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
    38
        try:
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
    39
            st = vfs.stat(name)
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
    40
        except OSError:
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
    41
            pass
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
    42
        else:
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
    43
            if st.st_size >= maxsize:
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
    44
                path = vfs.join(name)
38783
e7aa113b14f7 global: use pycompat.xrange()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37120
diff changeset
    45
                for i in pycompat.xrange(maxfiles - 1, 1, -1):
34300
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
    46
                    rotate(oldpath='%s.%d' % (path, i - 1),
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
    47
                           newpath='%s.%d' % (path, i))
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
    48
                rotate(oldpath=path,
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
    49
                       newpath=maxfiles > 0 and path + '.1')
40797
ea2688c84e4b blackbox: just try writing to repo.vfs and update lastlogger on success
Yuya Nishihara <yuya@tcha.org>
parents: 40764
diff changeset
    50
    return vfs(name, 'a', makeparentdirs=False)
34300
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
    51
40762
37d6ee46a965 blackbox: extract global last logger to proxylogger class
Yuya Nishihara <yuya@tcha.org>
parents: 40760
diff changeset
    52
class proxylogger(object):
37d6ee46a965 blackbox: extract global last logger to proxylogger class
Yuya Nishihara <yuya@tcha.org>
parents: 40760
diff changeset
    53
    """Forward log events to another logger to be set later"""
37d6ee46a965 blackbox: extract global last logger to proxylogger class
Yuya Nishihara <yuya@tcha.org>
parents: 40760
diff changeset
    54
37d6ee46a965 blackbox: extract global last logger to proxylogger class
Yuya Nishihara <yuya@tcha.org>
parents: 40760
diff changeset
    55
    def __init__(self):
37d6ee46a965 blackbox: extract global last logger to proxylogger class
Yuya Nishihara <yuya@tcha.org>
parents: 40760
diff changeset
    56
        self.logger = None
37d6ee46a965 blackbox: extract global last logger to proxylogger class
Yuya Nishihara <yuya@tcha.org>
parents: 40760
diff changeset
    57
37d6ee46a965 blackbox: extract global last logger to proxylogger class
Yuya Nishihara <yuya@tcha.org>
parents: 40760
diff changeset
    58
    def tracked(self, event):
37d6ee46a965 blackbox: extract global last logger to proxylogger class
Yuya Nishihara <yuya@tcha.org>
parents: 40760
diff changeset
    59
        return self.logger is not None and self.logger.tracked(event)
37d6ee46a965 blackbox: extract global last logger to proxylogger class
Yuya Nishihara <yuya@tcha.org>
parents: 40760
diff changeset
    60
37d6ee46a965 blackbox: extract global last logger to proxylogger class
Yuya Nishihara <yuya@tcha.org>
parents: 40760
diff changeset
    61
    def log(self, ui, event, msg, opts):
37d6ee46a965 blackbox: extract global last logger to proxylogger class
Yuya Nishihara <yuya@tcha.org>
parents: 40760
diff changeset
    62
        assert self.logger is not None
37d6ee46a965 blackbox: extract global last logger to proxylogger class
Yuya Nishihara <yuya@tcha.org>
parents: 40760
diff changeset
    63
        self.logger.log(ui, event, msg, opts)