hgext/blackbox.py
author Yuya Nishihara <yuya@tcha.org>
Sun, 11 Nov 2018 17:25:34 +0900
changeset 40647 6d9a87fb8c89
parent 40646 179c02baaa8c
child 40648 85372dc0cca3
permissions -rw-r--r--
blackbox: initialize inlog flag properly And ditch the "bb" prefix as it's no longer a ui extension class.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
18676
1506eb487ddd blackbox: fix copyright
Bryan O'Sullivan <bryano@fb.com>
parents: 18675
diff changeset
     1
# blackbox.py - log repository events to a file for post-mortem debugging
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
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
     9
"""log repository events to a blackbox for debugging
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
    10
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
    11
Logs event information to .hg/blackbox.log to help debug and diagnose problems.
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
    12
The events that get logged can be configured via the blackbox.track config key.
28246
b862e793ec10 blackbox: log dirty state
timeless <timeless@mozdev.org>
parents: 28245
diff changeset
    13
19162
27013ace80eb blackbox: fix literal block syntax
Takumi IINO <trot.thunder@gmail.com>
parents: 19066
diff changeset
    14
Examples::
18669
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
    15
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
    16
  [blackbox]
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
    17
  track = *
28303
ce24de063aa5 blackbox: rewrite dirty documentation noting it is expensive
timeless <timeless@mozdev.org>
parents: 28248
diff changeset
    18
  # dirty is *EXPENSIVE* (slow);
ce24de063aa5 blackbox: rewrite dirty documentation noting it is expensive
timeless <timeless@mozdev.org>
parents: 28248
diff changeset
    19
  # each log entry indicates `+` if the repository is dirty, like :hg:`id`.
28246
b862e793ec10 blackbox: log dirty state
timeless <timeless@mozdev.org>
parents: 28245
diff changeset
    20
  dirty = True
28305
f5ae291dfedf blackbox: optionally log event source
timeless <timeless@mozdev.org>
parents: 28304
diff changeset
    21
  # record the source of log messages
f5ae291dfedf blackbox: optionally log event source
timeless <timeless@mozdev.org>
parents: 28304
diff changeset
    22
  logsource = True
18669
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
    23
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
    24
  [blackbox]
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
    25
  track = command, commandfinish, commandexception, exthook, pythonhook
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
    26
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
    27
  [blackbox]
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
    28
  track = incoming
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
    29
19066
2cad301a7f06 blackbox: automatically rotate log files
Bryan O'Sullivan <bryano@fb.com>
parents: 19052
diff changeset
    30
  [blackbox]
2cad301a7f06 blackbox: automatically rotate log files
Bryan O'Sullivan <bryano@fb.com>
parents: 19052
diff changeset
    31
  # limit the size of a log file
2cad301a7f06 blackbox: automatically rotate log files
Bryan O'Sullivan <bryano@fb.com>
parents: 19052
diff changeset
    32
  maxsize = 1.5 MB
2cad301a7f06 blackbox: automatically rotate log files
Bryan O'Sullivan <bryano@fb.com>
parents: 19052
diff changeset
    33
  # rotate up to N log files when the current one gets too big
2cad301a7f06 blackbox: automatically rotate log files
Bryan O'Sullivan <bryano@fb.com>
parents: 19052
diff changeset
    34
  maxfiles = 3
2cad301a7f06 blackbox: automatically rotate log files
Bryan O'Sullivan <bryano@fb.com>
parents: 19052
diff changeset
    35
40439
25f1c7bd649d blackbox: add configitem for format of log timestamps
Matt DeVore <matvore@google.com>
parents: 40295
diff changeset
    36
  [blackbox]
25f1c7bd649d blackbox: add configitem for format of log timestamps
Matt DeVore <matvore@google.com>
parents: 40295
diff changeset
    37
  # Include nanoseconds in log entries with %f (see Python function
25f1c7bd649d blackbox: add configitem for format of log timestamps
Matt DeVore <matvore@google.com>
parents: 40295
diff changeset
    38
  # datetime.datetime.strftime)
25f1c7bd649d blackbox: add configitem for format of log timestamps
Matt DeVore <matvore@google.com>
parents: 40295
diff changeset
    39
  date-format = '%Y-%m-%d @ %H:%M:%S.%f'
25f1c7bd649d blackbox: add configitem for format of log timestamps
Matt DeVore <matvore@google.com>
parents: 40295
diff changeset
    40
18669
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
    41
"""
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
    42
28090
8113c88b8e6d blackbox: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28027
diff changeset
    43
from __future__ import absolute_import
8113c88b8e6d blackbox: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28027
diff changeset
    44
8113c88b8e6d blackbox: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28027
diff changeset
    45
import errno
8113c88b8e6d blackbox: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28027
diff changeset
    46
import re
8113c88b8e6d blackbox: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28027
diff changeset
    47
18669
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
    48
from mercurial.i18n import _
28245
caa2a0c6fbb7 blackbox: log working directory version
timeless <timeless@mozdev.org>
parents: 28244
diff changeset
    49
from mercurial.node import hex
caa2a0c6fbb7 blackbox: log working directory version
timeless <timeless@mozdev.org>
parents: 28244
diff changeset
    50
28090
8113c88b8e6d blackbox: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28027
diff changeset
    51
from mercurial import (
35667
de598e84c244 py3: cast error message to localstr in blackbox.py
Gregory Szorc <gregory.szorc@gmail.com>
parents: 34972
diff changeset
    52
    encoding,
38783
e7aa113b14f7 global: use pycompat.xrange()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37120
diff changeset
    53
    pycompat,
32337
46ba2cdda476 registrar: move cmdutil.command to registrar module (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32155
diff changeset
    54
    registrar,
28248
851c41a21869 blackbox: properly replace ui class
timeless <timeless@mozdev.org>
parents: 28247
diff changeset
    55
    ui as uimod,
28090
8113c88b8e6d blackbox: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28027
diff changeset
    56
    util,
8113c88b8e6d blackbox: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28027
diff changeset
    57
)
37120
a8a902d7176e procutil: bulk-replace function calls to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36607
diff changeset
    58
from mercurial.utils import (
a8a902d7176e procutil: bulk-replace function calls to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36607
diff changeset
    59
    dateutil,
a8a902d7176e procutil: bulk-replace function calls to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36607
diff changeset
    60
    procutil,
a8a902d7176e procutil: bulk-replace function calls to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36607
diff changeset
    61
)
18669
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
    62
29841
d5883fd055c6 extensions: change magic "shipped with hg" string
Augie Fackler <augie@google.com>
parents: 28552
diff changeset
    63
# Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for
25186
80c5b2666a96 extensions: document that `testedwith = 'internal'` is special
Augie Fackler <augie@google.com>
parents: 23877
diff changeset
    64
# extensions which SHIP WITH MERCURIAL. Non-mainline extensions should
80c5b2666a96 extensions: document that `testedwith = 'internal'` is special
Augie Fackler <augie@google.com>
parents: 23877
diff changeset
    65
# be specifying the version(s) of Mercurial they are tested with, or
80c5b2666a96 extensions: document that `testedwith = 'internal'` is special
Augie Fackler <augie@google.com>
parents: 23877
diff changeset
    66
# leave the attribute unspecified.
29841
d5883fd055c6 extensions: change magic "shipped with hg" string
Augie Fackler <augie@google.com>
parents: 28552
diff changeset
    67
testedwith = 'ships-with-hg-core'
33134
7dc090faa8a4 blackbox: minor code reordering
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 32412
diff changeset
    68
7dc090faa8a4 blackbox: minor code reordering
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 32412
diff changeset
    69
cmdtable = {}
7dc090faa8a4 blackbox: minor code reordering
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 32412
diff changeset
    70
command = registrar.command(cmdtable)
7dc090faa8a4 blackbox: minor code reordering
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 32412
diff changeset
    71
33135
0a638f37f2d2 configitems: register 'blackbox.maxsize' as an example of 'configbytes'
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33134
diff changeset
    72
configtable = {}
0a638f37f2d2 configitems: register 'blackbox.maxsize' as an example of 'configbytes'
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33134
diff changeset
    73
configitem = registrar.configitem(configtable)
0a638f37f2d2 configitems: register 'blackbox.maxsize' as an example of 'configbytes'
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33134
diff changeset
    74
33189
1df74b71396d configitems: register the 'blackbox.dirty' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33136
diff changeset
    75
configitem('blackbox', 'dirty',
1df74b71396d configitems: register the 'blackbox.dirty' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33136
diff changeset
    76
    default=False,
1df74b71396d configitems: register the 'blackbox.dirty' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33136
diff changeset
    77
)
33135
0a638f37f2d2 configitems: register 'blackbox.maxsize' as an example of 'configbytes'
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33134
diff changeset
    78
configitem('blackbox', 'maxsize',
33136
59c135bb31bc blackbox: use a human readable version of the default
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33135
diff changeset
    79
    default='1 MB',
33135
0a638f37f2d2 configitems: register 'blackbox.maxsize' as an example of 'configbytes'
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33134
diff changeset
    80
)
33190
0ef40bb20264 configitems: register the 'blackbox.logsource' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33189
diff changeset
    81
configitem('blackbox', 'logsource',
0ef40bb20264 configitems: register the 'blackbox.logsource' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33189
diff changeset
    82
    default=False,
0ef40bb20264 configitems: register the 'blackbox.logsource' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33189
diff changeset
    83
)
34745
0b46440b1b45 configitems: register the 'blackbox.maxfiles' config
Boris Feld <boris.feld@octobus.net>
parents: 34583
diff changeset
    84
configitem('blackbox', 'maxfiles',
0b46440b1b45 configitems: register the 'blackbox.maxfiles' config
Boris Feld <boris.feld@octobus.net>
parents: 34583
diff changeset
    85
    default=7,
0b46440b1b45 configitems: register the 'blackbox.maxfiles' config
Boris Feld <boris.feld@octobus.net>
parents: 34583
diff changeset
    86
)
34517
49b72b6f6d66 configitems: register the 'blackbox.track' config
Boris Feld <boris.feld@octobus.net>
parents: 34300
diff changeset
    87
configitem('blackbox', 'track',
34583
19b2c062654c configitems: fix registration for 'blackbox.track' config
Boris Feld <boris.feld@octobus.net>
parents: 34517
diff changeset
    88
    default=lambda: ['*'],
34517
49b72b6f6d66 configitems: register the 'blackbox.track' config
Boris Feld <boris.feld@octobus.net>
parents: 34300
diff changeset
    89
)
40439
25f1c7bd649d blackbox: add configitem for format of log timestamps
Matt DeVore <matvore@google.com>
parents: 40295
diff changeset
    90
configitem('blackbox', 'date-format',
25f1c7bd649d blackbox: add configitem for format of log timestamps
Matt DeVore <matvore@google.com>
parents: 40295
diff changeset
    91
    default='%Y/%m/%d %H:%M:%S',
25f1c7bd649d blackbox: add configitem for format of log timestamps
Matt DeVore <matvore@google.com>
parents: 40295
diff changeset
    92
)
33135
0a638f37f2d2 configitems: register 'blackbox.maxsize' as an example of 'configbytes'
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33134
diff changeset
    93
40643
56694b4d41b0 blackbox: rename variables to prepare extracting core logic from ui wrapper
Yuya Nishihara <yuya@tcha.org>
parents: 40634
diff changeset
    94
_lastlogger = None
18669
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
    95
34300
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
    96
def _openlogfile(ui, vfs):
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
    97
    def rotate(oldpath, newpath):
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
    98
        try:
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
    99
            vfs.unlink(newpath)
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
   100
        except OSError as err:
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
   101
            if err.errno != errno.ENOENT:
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
   102
                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
   103
                         (newpath, err.strerror))
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
   104
        try:
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
   105
            if newpath:
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
   106
                vfs.rename(oldpath, newpath)
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
   107
        except OSError as err:
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
   108
            if err.errno != errno.ENOENT:
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
   109
                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
   110
                         (newpath, oldpath, err.strerror))
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
   111
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
   112
    maxsize = ui.configbytes('blackbox', 'maxsize')
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
   113
    name = 'blackbox.log'
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
   114
    if maxsize > 0:
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
   115
        try:
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
   116
            st = vfs.stat(name)
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
   117
        except OSError:
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
   118
            pass
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
   119
        else:
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
   120
            if st.st_size >= maxsize:
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
   121
                path = vfs.join(name)
34745
0b46440b1b45 configitems: register the 'blackbox.maxfiles' config
Boris Feld <boris.feld@octobus.net>
parents: 34583
diff changeset
   122
                maxfiles = ui.configint('blackbox', 'maxfiles')
38783
e7aa113b14f7 global: use pycompat.xrange()
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37120
diff changeset
   123
                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
   124
                    rotate(oldpath='%s.%d' % (path, i - 1),
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
   125
                           newpath='%s.%d' % (path, i))
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
   126
                rotate(oldpath=path,
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
   127
                       newpath=maxfiles > 0 and path + '.1')
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
   128
    return vfs(name, 'a')
e6723c939344 blackbox: move _openlogfile to a separate method
Jun Wu <quark@fb.com>
parents: 34299
diff changeset
   129
40645
fff3e213ace9 blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents: 40644
diff changeset
   130
class blackboxlogger(object):
fff3e213ace9 blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents: 40644
diff changeset
   131
    def __init__(self, ui):
40646
179c02baaa8c blackbox: initialize repo attribute properly
Yuya Nishihara <yuya@tcha.org>
parents: 40645
diff changeset
   132
        self._repo = None
40647
6d9a87fb8c89 blackbox: initialize inlog flag properly
Yuya Nishihara <yuya@tcha.org>
parents: 40646
diff changeset
   133
        self._inlog = False
40645
fff3e213ace9 blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents: 40644
diff changeset
   134
        self.track = ui.configlist('blackbox', 'track')
40644
a9393d7600f3 blackbox: extract logger class from ui wrapper
Yuya Nishihara <yuya@tcha.org>
parents: 40643
diff changeset
   135
40645
fff3e213ace9 blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents: 40644
diff changeset
   136
    @property
fff3e213ace9 blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents: 40644
diff changeset
   137
    def _bbvfs(self):
fff3e213ace9 blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents: 40644
diff changeset
   138
        vfs = None
40646
179c02baaa8c blackbox: initialize repo attribute properly
Yuya Nishihara <yuya@tcha.org>
parents: 40645
diff changeset
   139
        if self._repo:
179c02baaa8c blackbox: initialize repo attribute properly
Yuya Nishihara <yuya@tcha.org>
parents: 40645
diff changeset
   140
            vfs = self._repo.vfs
40645
fff3e213ace9 blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents: 40644
diff changeset
   141
            if not vfs.isdir('.'):
fff3e213ace9 blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents: 40644
diff changeset
   142
                vfs = None
fff3e213ace9 blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents: 40644
diff changeset
   143
        return vfs
34110
029b33adbd17 blackbox: remove _bbvfs state
Jun Wu <quark@fb.com>
parents: 34109
diff changeset
   144
40645
fff3e213ace9 blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents: 40644
diff changeset
   145
    def log(self, ui, event, msg, opts):
fff3e213ace9 blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents: 40644
diff changeset
   146
        global _lastlogger
fff3e213ace9 blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents: 40644
diff changeset
   147
        if not '*' in self.track and not event in self.track:
fff3e213ace9 blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents: 40644
diff changeset
   148
            return
18669
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
   149
40645
fff3e213ace9 blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents: 40644
diff changeset
   150
        if self._bbvfs:
fff3e213ace9 blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents: 40644
diff changeset
   151
            _lastlogger = self
fff3e213ace9 blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents: 40644
diff changeset
   152
        elif _lastlogger and _lastlogger._bbvfs:
fff3e213ace9 blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents: 40644
diff changeset
   153
            # certain logger instances exist outside the context of
fff3e213ace9 blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents: 40644
diff changeset
   154
            # a repo, so just default to the last blackbox logger that
fff3e213ace9 blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents: 40644
diff changeset
   155
            # was seen.
fff3e213ace9 blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents: 40644
diff changeset
   156
            pass
fff3e213ace9 blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents: 40644
diff changeset
   157
        else:
fff3e213ace9 blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents: 40644
diff changeset
   158
            return
fff3e213ace9 blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents: 40644
diff changeset
   159
        _lastlogger._log(ui, event, msg, opts)
34299
b1d4ac068961 blackbox: do not prevent 'chg init' from working
Jun Wu <quark@fb.com>
parents: 34276
diff changeset
   160
40645
fff3e213ace9 blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents: 40644
diff changeset
   161
    def _log(self, ui, event, msg, opts):
40647
6d9a87fb8c89 blackbox: initialize inlog flag properly
Yuya Nishihara <yuya@tcha.org>
parents: 40646
diff changeset
   162
        if self._inlog:
40645
fff3e213ace9 blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents: 40644
diff changeset
   163
            # recursion and failure guard
fff3e213ace9 blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents: 40644
diff changeset
   164
            return
40647
6d9a87fb8c89 blackbox: initialize inlog flag properly
Yuya Nishihara <yuya@tcha.org>
parents: 40646
diff changeset
   165
        self._inlog = True
40645
fff3e213ace9 blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents: 40644
diff changeset
   166
        default = ui.configdate('devel', 'default-date')
fff3e213ace9 blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents: 40644
diff changeset
   167
        date = dateutil.datestr(default, ui.config('blackbox', 'date-format'))
fff3e213ace9 blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents: 40644
diff changeset
   168
        user = procutil.getuser()
fff3e213ace9 blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents: 40644
diff changeset
   169
        pid = '%d' % procutil.getpid()
fff3e213ace9 blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents: 40644
diff changeset
   170
        formattedmsg = msg[0] % msg[1:]
fff3e213ace9 blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents: 40644
diff changeset
   171
        rev = '(unknown)'
fff3e213ace9 blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents: 40644
diff changeset
   172
        changed = ''
40646
179c02baaa8c blackbox: initialize repo attribute properly
Yuya Nishihara <yuya@tcha.org>
parents: 40645
diff changeset
   173
        ctx = self._repo[None]
40645
fff3e213ace9 blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents: 40644
diff changeset
   174
        parents = ctx.parents()
fff3e213ace9 blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents: 40644
diff changeset
   175
        rev = ('+'.join([hex(p.node()) for p in parents]))
fff3e213ace9 blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents: 40644
diff changeset
   176
        if (ui.configbool('blackbox', 'dirty') and
fff3e213ace9 blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents: 40644
diff changeset
   177
            ctx.dirty(missing=True, merge=False, branch=False)):
fff3e213ace9 blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents: 40644
diff changeset
   178
            changed = '+'
fff3e213ace9 blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents: 40644
diff changeset
   179
        if ui.configbool('blackbox', 'logsource'):
fff3e213ace9 blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents: 40644
diff changeset
   180
            src = ' [%s]' % event
fff3e213ace9 blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents: 40644
diff changeset
   181
        else:
fff3e213ace9 blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents: 40644
diff changeset
   182
            src = ''
fff3e213ace9 blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents: 40644
diff changeset
   183
        try:
fff3e213ace9 blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents: 40644
diff changeset
   184
            fmt = '%s %s @%s%s (%s)%s> %s'
fff3e213ace9 blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents: 40644
diff changeset
   185
            args = (date, user, rev, changed, pid, src, formattedmsg)
fff3e213ace9 blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents: 40644
diff changeset
   186
            with _openlogfile(ui, self._bbvfs) as fp:
fff3e213ace9 blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents: 40644
diff changeset
   187
                fp.write(fmt % args)
fff3e213ace9 blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents: 40644
diff changeset
   188
        except (IOError, OSError) as err:
fff3e213ace9 blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents: 40644
diff changeset
   189
            ui.debug('warning: cannot write to blackbox.log: %s\n' %
fff3e213ace9 blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents: 40644
diff changeset
   190
                     encoding.strtolocal(err.strerror))
40647
6d9a87fb8c89 blackbox: initialize inlog flag properly
Yuya Nishihara <yuya@tcha.org>
parents: 40646
diff changeset
   191
            # do not restore _inlog intentionally to avoid failed
40645
fff3e213ace9 blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents: 40644
diff changeset
   192
            # logging again
fff3e213ace9 blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents: 40644
diff changeset
   193
        else:
40647
6d9a87fb8c89 blackbox: initialize inlog flag properly
Yuya Nishihara <yuya@tcha.org>
parents: 40646
diff changeset
   194
            self._inlog = False
18669
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
   195
40645
fff3e213ace9 blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents: 40644
diff changeset
   196
    def setrepo(self, repo):
40646
179c02baaa8c blackbox: initialize repo attribute properly
Yuya Nishihara <yuya@tcha.org>
parents: 40645
diff changeset
   197
        self._repo = repo
18669
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
   198
40644
a9393d7600f3 blackbox: extract logger class from ui wrapper
Yuya Nishihara <yuya@tcha.org>
parents: 40643
diff changeset
   199
def wrapui(ui):
a9393d7600f3 blackbox: extract logger class from ui wrapper
Yuya Nishihara <yuya@tcha.org>
parents: 40643
diff changeset
   200
    class blackboxui(ui.__class__):
a9393d7600f3 blackbox: extract logger class from ui wrapper
Yuya Nishihara <yuya@tcha.org>
parents: 40643
diff changeset
   201
        def __init__(self, src=None):
a9393d7600f3 blackbox: extract logger class from ui wrapper
Yuya Nishihara <yuya@tcha.org>
parents: 40643
diff changeset
   202
            super(blackboxui, self).__init__(src)
a9393d7600f3 blackbox: extract logger class from ui wrapper
Yuya Nishihara <yuya@tcha.org>
parents: 40643
diff changeset
   203
            if src and r'_bblogger' in src.__dict__:
a9393d7600f3 blackbox: extract logger class from ui wrapper
Yuya Nishihara <yuya@tcha.org>
parents: 40643
diff changeset
   204
                self._bblogger = src._bblogger
a9393d7600f3 blackbox: extract logger class from ui wrapper
Yuya Nishihara <yuya@tcha.org>
parents: 40643
diff changeset
   205
a9393d7600f3 blackbox: extract logger class from ui wrapper
Yuya Nishihara <yuya@tcha.org>
parents: 40643
diff changeset
   206
        # trick to initialize logger after configuration is loaded, which
a9393d7600f3 blackbox: extract logger class from ui wrapper
Yuya Nishihara <yuya@tcha.org>
parents: 40643
diff changeset
   207
        # can be replaced later with blackboxlogger(ui) in uisetup(), where
a9393d7600f3 blackbox: extract logger class from ui wrapper
Yuya Nishihara <yuya@tcha.org>
parents: 40643
diff changeset
   208
        # both user and repo configurations should be available.
a9393d7600f3 blackbox: extract logger class from ui wrapper
Yuya Nishihara <yuya@tcha.org>
parents: 40643
diff changeset
   209
        @util.propertycache
a9393d7600f3 blackbox: extract logger class from ui wrapper
Yuya Nishihara <yuya@tcha.org>
parents: 40643
diff changeset
   210
        def _bblogger(self):
a9393d7600f3 blackbox: extract logger class from ui wrapper
Yuya Nishihara <yuya@tcha.org>
parents: 40643
diff changeset
   211
            return blackboxlogger(self)
a9393d7600f3 blackbox: extract logger class from ui wrapper
Yuya Nishihara <yuya@tcha.org>
parents: 40643
diff changeset
   212
a9393d7600f3 blackbox: extract logger class from ui wrapper
Yuya Nishihara <yuya@tcha.org>
parents: 40643
diff changeset
   213
        def debug(self, *msg, **opts):
a9393d7600f3 blackbox: extract logger class from ui wrapper
Yuya Nishihara <yuya@tcha.org>
parents: 40643
diff changeset
   214
            super(blackboxui, self).debug(*msg, **opts)
a9393d7600f3 blackbox: extract logger class from ui wrapper
Yuya Nishihara <yuya@tcha.org>
parents: 40643
diff changeset
   215
            if self.debugflag:
a9393d7600f3 blackbox: extract logger class from ui wrapper
Yuya Nishihara <yuya@tcha.org>
parents: 40643
diff changeset
   216
                self.log('debug', '%s', ''.join(msg))
a9393d7600f3 blackbox: extract logger class from ui wrapper
Yuya Nishihara <yuya@tcha.org>
parents: 40643
diff changeset
   217
a9393d7600f3 blackbox: extract logger class from ui wrapper
Yuya Nishihara <yuya@tcha.org>
parents: 40643
diff changeset
   218
        def log(self, event, *msg, **opts):
a9393d7600f3 blackbox: extract logger class from ui wrapper
Yuya Nishihara <yuya@tcha.org>
parents: 40643
diff changeset
   219
            super(blackboxui, self).log(event, *msg, **opts)
a9393d7600f3 blackbox: extract logger class from ui wrapper
Yuya Nishihara <yuya@tcha.org>
parents: 40643
diff changeset
   220
            self._bblogger.log(self, event, msg, opts)
a9393d7600f3 blackbox: extract logger class from ui wrapper
Yuya Nishihara <yuya@tcha.org>
parents: 40643
diff changeset
   221
18669
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
   222
    ui.__class__ = blackboxui
28248
851c41a21869 blackbox: properly replace ui class
timeless <timeless@mozdev.org>
parents: 28247
diff changeset
   223
    uimod.ui = blackboxui
18669
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
   224
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
   225
def uisetup(ui):
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
   226
    wrapui(ui)
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
   227
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
   228
def reposetup(ui, repo):
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
   229
    # During 'hg pull' a httppeer repo is created to represent the remote repo.
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
   230
    # It doesn't have a .hg directory to put a blackbox in, so we don't do
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
   231
    # the blackbox setup for it.
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
   232
    if not repo.local():
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
   233
        return
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
   234
40644
a9393d7600f3 blackbox: extract logger class from ui wrapper
Yuya Nishihara <yuya@tcha.org>
parents: 40643
diff changeset
   235
    logger = getattr(ui, '_bblogger', None)
a9393d7600f3 blackbox: extract logger class from ui wrapper
Yuya Nishihara <yuya@tcha.org>
parents: 40643
diff changeset
   236
    if logger:
40643
56694b4d41b0 blackbox: rename variables to prepare extracting core logic from ui wrapper
Yuya Nishihara <yuya@tcha.org>
parents: 40634
diff changeset
   237
        logger.setrepo(repo)
34276
b90bd9a98c8b blackbox: set lastui even if ui.log is not called (issue5518)
Jun Wu <quark@fb.com>
parents: 34275
diff changeset
   238
40643
56694b4d41b0 blackbox: rename variables to prepare extracting core logic from ui wrapper
Yuya Nishihara <yuya@tcha.org>
parents: 40634
diff changeset
   239
        # Set _lastlogger even if ui.log is not called. This gives blackbox a
34276
b90bd9a98c8b blackbox: set lastui even if ui.log is not called (issue5518)
Jun Wu <quark@fb.com>
parents: 34275
diff changeset
   240
        # fallback place to log.
40643
56694b4d41b0 blackbox: rename variables to prepare extracting core logic from ui wrapper
Yuya Nishihara <yuya@tcha.org>
parents: 40634
diff changeset
   241
        global _lastlogger
56694b4d41b0 blackbox: rename variables to prepare extracting core logic from ui wrapper
Yuya Nishihara <yuya@tcha.org>
parents: 40634
diff changeset
   242
        if _lastlogger is None:
56694b4d41b0 blackbox: rename variables to prepare extracting core logic from ui wrapper
Yuya Nishihara <yuya@tcha.org>
parents: 40634
diff changeset
   243
            _lastlogger = logger
34276
b90bd9a98c8b blackbox: set lastui even if ui.log is not called (issue5518)
Jun Wu <quark@fb.com>
parents: 34275
diff changeset
   244
33436
9bb4decd43b0 repovfs: add a ward to check if locks are properly taken
Boris Feld <boris.feld@octobus.net>
parents: 33361
diff changeset
   245
    repo._wlockfreeprefix.add('blackbox.log')
18673
f27598902007 blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents: 18669
diff changeset
   246
40295
fa88170c10bb help: adding a proper declaration for shortlist/basic commands (API)
Rodrigo Damazio <rdamazio@google.com>
parents: 40293
diff changeset
   247
@command('blackbox',
18673
f27598902007 blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents: 18669
diff changeset
   248
    [('l', 'limit', 10, _('the number of events to show')),
f27598902007 blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents: 18669
diff changeset
   249
    ],
40293
c303d65d2e34 help: assigning categories to existing commands
rdamazio@google.com
parents: 38783
diff changeset
   250
    _('hg blackbox [OPTION]...'),
40295
fa88170c10bb help: adding a proper declaration for shortlist/basic commands (API)
Rodrigo Damazio <rdamazio@google.com>
parents: 40293
diff changeset
   251
    helpcategory=command.CATEGORY_MAINTENANCE,
fa88170c10bb help: adding a proper declaration for shortlist/basic commands (API)
Rodrigo Damazio <rdamazio@google.com>
parents: 40293
diff changeset
   252
    helpbasic=True)
18673
f27598902007 blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents: 18669
diff changeset
   253
def blackbox(ui, repo, *revs, **opts):
f27598902007 blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents: 18669
diff changeset
   254
    '''view the recent repository events
f27598902007 blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents: 18669
diff changeset
   255
    '''
f27598902007 blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents: 18669
diff changeset
   256
28026
a6db1163befa blackbox: refactor use of vfs as _bbvfs
timeless <timeless@mozdev.org>
parents: 28025
diff changeset
   257
    if not repo.vfs.exists('blackbox.log'):
18673
f27598902007 blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents: 18669
diff changeset
   258
        return
f27598902007 blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents: 18669
diff changeset
   259
34972
375577785f49 py3: handle keyword arguments in hgext/blackbox.py
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34745
diff changeset
   260
    limit = opts.get(r'limit')
28244
c17d7b1c40be blackbox: rename fp variable
timeless <timeless@mozdev.org>
parents: 28243
diff changeset
   261
    fp = repo.vfs('blackbox.log', 'r')
c17d7b1c40be blackbox: rename fp variable
timeless <timeless@mozdev.org>
parents: 28243
diff changeset
   262
    lines = fp.read().split('\n')
18673
f27598902007 blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents: 18669
diff changeset
   263
f27598902007 blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents: 18669
diff changeset
   264
    count = 0
f27598902007 blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents: 18669
diff changeset
   265
    output = []
f27598902007 blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents: 18669
diff changeset
   266
    for line in reversed(lines):
f27598902007 blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents: 18669
diff changeset
   267
        if count >= limit:
f27598902007 blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents: 18669
diff changeset
   268
            break
f27598902007 blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents: 18669
diff changeset
   269
f27598902007 blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents: 18669
diff changeset
   270
        # count the commands by matching lines like: 2013/01/23 19:13:36 root>
f27598902007 blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents: 18669
diff changeset
   271
        if re.match('^\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2} .*> .*', line):
f27598902007 blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents: 18669
diff changeset
   272
            count += 1
f27598902007 blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents: 18669
diff changeset
   273
        output.append(line)
f27598902007 blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents: 18669
diff changeset
   274
f27598902007 blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents: 18669
diff changeset
   275
    ui.status('\n'.join(reversed(output)))