hgext/blackbox.py
author Pierre-Yves David <pierre-yves.david@octobus.net>
Wed, 22 Feb 2023 03:34:48 +0100
changeset 50150 b09a0afcb975
parent 49853 e63ab79b2fa1
child 50765 7f8f6fe13fa9
child 50941 4323af38e3f2
permissions -rw-r--r--
keyword: wrap dirstate mutation in `changing_files` context This is the way.
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.
42615
56132ebd14c6 blackbox: disable extremely verbose logging (issue6110)
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 41532
diff changeset
    12
The events that get logged can be configured via the blackbox.track and
56132ebd14c6 blackbox: disable extremely verbose logging (issue6110)
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 41532
diff changeset
    13
blackbox.ignore config keys.
28246
b862e793ec10 blackbox: log dirty state
timeless <timeless@mozdev.org>
parents: 28245
diff changeset
    14
19162
27013ace80eb blackbox: fix literal block syntax
Takumi IINO <trot.thunder@gmail.com>
parents: 19066
diff changeset
    15
Examples::
18669
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
    16
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
    17
  [blackbox]
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
    18
  track = *
42615
56132ebd14c6 blackbox: disable extremely verbose logging (issue6110)
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 41532
diff changeset
    19
  ignore = pythonhook
28303
ce24de063aa5 blackbox: rewrite dirty documentation noting it is expensive
timeless <timeless@mozdev.org>
parents: 28248
diff changeset
    20
  # dirty is *EXPENSIVE* (slow);
ce24de063aa5 blackbox: rewrite dirty documentation noting it is expensive
timeless <timeless@mozdev.org>
parents: 28248
diff changeset
    21
  # 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
    22
  dirty = True
28305
f5ae291dfedf blackbox: optionally log event source
timeless <timeless@mozdev.org>
parents: 28304
diff changeset
    23
  # record the source of log messages
f5ae291dfedf blackbox: optionally log event source
timeless <timeless@mozdev.org>
parents: 28304
diff changeset
    24
  logsource = True
18669
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
    25
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
    26
  [blackbox]
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
    27
  track = command, commandfinish, commandexception, exthook, pythonhook
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
    28
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
    29
  [blackbox]
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
    30
  track = incoming
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
    31
19066
2cad301a7f06 blackbox: automatically rotate log files
Bryan O'Sullivan <bryano@fb.com>
parents: 19052
diff changeset
    32
  [blackbox]
2cad301a7f06 blackbox: automatically rotate log files
Bryan O'Sullivan <bryano@fb.com>
parents: 19052
diff changeset
    33
  # limit the size of a log file
2cad301a7f06 blackbox: automatically rotate log files
Bryan O'Sullivan <bryano@fb.com>
parents: 19052
diff changeset
    34
  maxsize = 1.5 MB
2cad301a7f06 blackbox: automatically rotate log files
Bryan O'Sullivan <bryano@fb.com>
parents: 19052
diff changeset
    35
  # 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
    36
  maxfiles = 3
2cad301a7f06 blackbox: automatically rotate log files
Bryan O'Sullivan <bryano@fb.com>
parents: 19052
diff changeset
    37
40439
25f1c7bd649d blackbox: add configitem for format of log timestamps
Matt DeVore <matvore@google.com>
parents: 40295
diff changeset
    38
  [blackbox]
48572
fe4922564661 blackbox: correct date format doc
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 46600
diff changeset
    39
  # Include microseconds in log entries with %f (see Python function
40439
25f1c7bd649d blackbox: add configitem for format of log timestamps
Matt DeVore <matvore@google.com>
parents: 40295
diff changeset
    40
  # datetime.datetime.strftime)
46600
36f3a64846c8 blackbox: Remove misleading quotes in config example
Simon Sapin <simon.sapin@octobus.net>
parents: 45942
diff changeset
    41
  date-format = %Y-%m-%d @ %H:%M:%S.%f
40439
25f1c7bd649d blackbox: add configitem for format of log timestamps
Matt DeVore <matvore@google.com>
parents: 40295
diff changeset
    42
18669
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
    43
"""
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
    44
28090
8113c88b8e6d blackbox: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28027
diff changeset
    45
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,
40799
03127e580980 loggingutil: extract openlogfile() and proxylogger to new module
Yuya Nishihara <yuya@tcha.org>
parents: 40798
diff changeset
    53
    loggingutil,
32337
46ba2cdda476 registrar: move cmdutil.command to registrar module (API)
Yuya Nishihara <yuya@tcha.org>
parents: 32155
diff changeset
    54
    registrar,
28090
8113c88b8e6d blackbox: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28027
diff changeset
    55
)
37120
a8a902d7176e procutil: bulk-replace function calls to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36607
diff changeset
    56
from mercurial.utils import (
a8a902d7176e procutil: bulk-replace function calls to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36607
diff changeset
    57
    dateutil,
a8a902d7176e procutil: bulk-replace function calls to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36607
diff changeset
    58
    procutil,
a8a902d7176e procutil: bulk-replace function calls to point to new module
Yuya Nishihara <yuya@tcha.org>
parents: 36607
diff changeset
    59
)
18669
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
    60
29841
d5883fd055c6 extensions: change magic "shipped with hg" string
Augie Fackler <augie@google.com>
parents: 28552
diff changeset
    61
# 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
    62
# 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
    63
# 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
    64
# leave the attribute unspecified.
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    65
testedwith = b'ships-with-hg-core'
33134
7dc090faa8a4 blackbox: minor code reordering
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 32412
diff changeset
    66
7dc090faa8a4 blackbox: minor code reordering
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 32412
diff changeset
    67
cmdtable = {}
7dc090faa8a4 blackbox: minor code reordering
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 32412
diff changeset
    68
command = registrar.command(cmdtable)
7dc090faa8a4 blackbox: minor code reordering
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 32412
diff changeset
    69
33135
0a638f37f2d2 configitems: register 'blackbox.maxsize' as an example of 'configbytes'
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33134
diff changeset
    70
configtable = {}
0a638f37f2d2 configitems: register 'blackbox.maxsize' as an example of 'configbytes'
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33134
diff changeset
    71
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
    72
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42615
diff changeset
    73
configitem(
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43506
diff changeset
    74
    b'blackbox',
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43506
diff changeset
    75
    b'dirty',
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43506
diff changeset
    76
    default=False,
33189
1df74b71396d configitems: register the 'blackbox.dirty' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33136
diff changeset
    77
)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42615
diff changeset
    78
configitem(
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43506
diff changeset
    79
    b'blackbox',
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43506
diff changeset
    80
    b'maxsize',
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43506
diff changeset
    81
    default=b'1 MB',
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42615
diff changeset
    82
)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42615
diff changeset
    83
configitem(
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43506
diff changeset
    84
    b'blackbox',
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43506
diff changeset
    85
    b'logsource',
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43506
diff changeset
    86
    default=False,
33135
0a638f37f2d2 configitems: register 'blackbox.maxsize' as an example of 'configbytes'
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33134
diff changeset
    87
)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42615
diff changeset
    88
configitem(
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43506
diff changeset
    89
    b'blackbox',
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43506
diff changeset
    90
    b'maxfiles',
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43506
diff changeset
    91
    default=7,
33190
0ef40bb20264 configitems: register the 'blackbox.logsource' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33189
diff changeset
    92
)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42615
diff changeset
    93
configitem(
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43506
diff changeset
    94
    b'blackbox',
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43506
diff changeset
    95
    b'track',
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43506
diff changeset
    96
    default=lambda: [b'*'],
34745
0b46440b1b45 configitems: register the 'blackbox.maxfiles' config
Boris Feld <boris.feld@octobus.net>
parents: 34583
diff changeset
    97
)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42615
diff changeset
    98
configitem(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    99
    b'blackbox',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   100
    b'ignore',
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   101
    default=lambda: [b'chgserver', b'cmdserver', b'extension'],
42615
56132ebd14c6 blackbox: disable extremely verbose logging (issue6110)
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 41532
diff changeset
   102
)
48573
011f5218ff2d blackbox: add milliseconds to blackbox logs by default
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 48572
diff changeset
   103
configitem(b'blackbox', b'date-format', default=b'')
33135
0a638f37f2d2 configitems: register 'blackbox.maxsize' as an example of 'configbytes'
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 33134
diff changeset
   104
40799
03127e580980 loggingutil: extract openlogfile() and proxylogger to new module
Yuya Nishihara <yuya@tcha.org>
parents: 40798
diff changeset
   105
_lastlogger = loggingutil.proxylogger()
40762
37d6ee46a965 blackbox: extract global last logger to proxylogger class
Yuya Nishihara <yuya@tcha.org>
parents: 40760
diff changeset
   106
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42615
diff changeset
   107
48946
642e31cb55f0 py3: use class X: instead of class X(object):
Gregory Szorc <gregory.szorc@gmail.com>
parents: 48875
diff changeset
   108
class blackboxlogger:
40764
567e164f89b8 blackbox: initialize logger with repo instance
Yuya Nishihara <yuya@tcha.org>
parents: 40763
diff changeset
   109
    def __init__(self, ui, repo):
567e164f89b8 blackbox: initialize logger with repo instance
Yuya Nishihara <yuya@tcha.org>
parents: 40763
diff changeset
   110
        self._repo = repo
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   111
        self._trackedevents = set(ui.configlist(b'blackbox', b'track'))
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   112
        self._ignoredevents = set(ui.configlist(b'blackbox', b'ignore'))
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   113
        self._maxfiles = ui.configint(b'blackbox', b'maxfiles')
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   114
        self._maxsize = ui.configbytes(b'blackbox', b'maxsize')
40993
49d48489a16b blackbox: resurrect recursion guard
Yuya Nishihara <yuya@tcha.org>
parents: 40799
diff changeset
   115
        self._inlog = False
40644
a9393d7600f3 blackbox: extract logger class from ui wrapper
Yuya Nishihara <yuya@tcha.org>
parents: 40643
diff changeset
   116
40648
85372dc0cca3 blackbox: extract function to test if log event is tracked
Yuya Nishihara <yuya@tcha.org>
parents: 40647
diff changeset
   117
    def tracked(self, event):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42615
diff changeset
   118
        return (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42615
diff changeset
   119
            b'*' in self._trackedevents and event not in self._ignoredevents
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42615
diff changeset
   120
        ) or event in self._trackedevents
40648
85372dc0cca3 blackbox: extract function to test if log event is tracked
Yuya Nishihara <yuya@tcha.org>
parents: 40647
diff changeset
   121
40645
fff3e213ace9 blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents: 40644
diff changeset
   122
    def log(self, ui, event, msg, opts):
40993
49d48489a16b blackbox: resurrect recursion guard
Yuya Nishihara <yuya@tcha.org>
parents: 40799
diff changeset
   123
        # self._log() -> ctx.dirty() may create new subrepo instance, which
49d48489a16b blackbox: resurrect recursion guard
Yuya Nishihara <yuya@tcha.org>
parents: 40799
diff changeset
   124
        # ui is derived from baseui. So the recursion guard in ui.log()
49d48489a16b blackbox: resurrect recursion guard
Yuya Nishihara <yuya@tcha.org>
parents: 40799
diff changeset
   125
        # doesn't work as it's local to the ui instance.
49d48489a16b blackbox: resurrect recursion guard
Yuya Nishihara <yuya@tcha.org>
parents: 40799
diff changeset
   126
        if self._inlog:
49d48489a16b blackbox: resurrect recursion guard
Yuya Nishihara <yuya@tcha.org>
parents: 40799
diff changeset
   127
            return
49d48489a16b blackbox: resurrect recursion guard
Yuya Nishihara <yuya@tcha.org>
parents: 40799
diff changeset
   128
        self._inlog = True
49d48489a16b blackbox: resurrect recursion guard
Yuya Nishihara <yuya@tcha.org>
parents: 40799
diff changeset
   129
        try:
49d48489a16b blackbox: resurrect recursion guard
Yuya Nishihara <yuya@tcha.org>
parents: 40799
diff changeset
   130
            self._log(ui, event, msg, opts)
49d48489a16b blackbox: resurrect recursion guard
Yuya Nishihara <yuya@tcha.org>
parents: 40799
diff changeset
   131
        finally:
49d48489a16b blackbox: resurrect recursion guard
Yuya Nishihara <yuya@tcha.org>
parents: 40799
diff changeset
   132
            self._inlog = False
49d48489a16b blackbox: resurrect recursion guard
Yuya Nishihara <yuya@tcha.org>
parents: 40799
diff changeset
   133
49d48489a16b blackbox: resurrect recursion guard
Yuya Nishihara <yuya@tcha.org>
parents: 40799
diff changeset
   134
    def _log(self, ui, event, msg, opts):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   135
        default = ui.configdate(b'devel', b'default-date')
48573
011f5218ff2d blackbox: add milliseconds to blackbox logs by default
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 48572
diff changeset
   136
        dateformat = ui.config(b'blackbox', b'date-format')
011f5218ff2d blackbox: add milliseconds to blackbox logs by default
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 48572
diff changeset
   137
        if dateformat:
011f5218ff2d blackbox: add milliseconds to blackbox logs by default
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 48572
diff changeset
   138
            date = dateutil.datestr(default, dateformat)
011f5218ff2d blackbox: add milliseconds to blackbox logs by default
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 48572
diff changeset
   139
        else:
011f5218ff2d blackbox: add milliseconds to blackbox logs by default
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 48572
diff changeset
   140
            # We want to display milliseconds (more precision seems
011f5218ff2d blackbox: add milliseconds to blackbox logs by default
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 48572
diff changeset
   141
            # unnecessary).  Since %.3f is not supported, use %f and truncate
011f5218ff2d blackbox: add milliseconds to blackbox logs by default
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 48572
diff changeset
   142
            # microseconds.
48574
abbecb5cd6f3 blackbox: change year in logs to ISO 8601 format
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 48573
diff changeset
   143
            date = dateutil.datestr(default, b'%Y-%m-%d %H:%M:%S.%f')[:-3]
40645
fff3e213ace9 blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents: 40644
diff changeset
   144
        user = procutil.getuser()
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   145
        pid = b'%d' % procutil.getpid()
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   146
        changed = b''
40646
179c02baaa8c blackbox: initialize repo attribute properly
Yuya Nishihara <yuya@tcha.org>
parents: 40645
diff changeset
   147
        ctx = self._repo[None]
40645
fff3e213ace9 blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents: 40644
diff changeset
   148
        parents = ctx.parents()
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   149
        rev = b'+'.join([hex(p.node()) for p in parents])
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   150
        if ui.configbool(b'blackbox', b'dirty') and ctx.dirty(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42615
diff changeset
   151
            missing=True, merge=False, branch=False
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42615
diff changeset
   152
        ):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   153
            changed = b'+'
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   154
        if ui.configbool(b'blackbox', b'logsource'):
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   155
            src = b' [%s]' % event
40645
fff3e213ace9 blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents: 40644
diff changeset
   156
        else:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   157
            src = b''
40645
fff3e213ace9 blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents: 40644
diff changeset
   158
        try:
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   159
            fmt = b'%s %s @%s%s (%s)%s> %s'
40760
ffd574c144d2 ui: pass in formatted message to logger.log()
Yuya Nishihara <yuya@tcha.org>
parents: 40759
diff changeset
   160
            args = (date, user, rev, changed, pid, src, msg)
40799
03127e580980 loggingutil: extract openlogfile() and proxylogger to new module
Yuya Nishihara <yuya@tcha.org>
parents: 40798
diff changeset
   161
            with loggingutil.openlogfile(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42615
diff changeset
   162
                ui,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42615
diff changeset
   163
                self._repo.vfs,
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   164
                name=b'blackbox.log',
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42615
diff changeset
   165
                maxfiles=self._maxfiles,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42615
diff changeset
   166
                maxsize=self._maxsize,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42615
diff changeset
   167
            ) as fp:
40645
fff3e213ace9 blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents: 40644
diff changeset
   168
                fp.write(fmt % args)
fff3e213ace9 blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents: 40644
diff changeset
   169
        except (IOError, OSError) as err:
40758
eb5948f29c60 blackbox: change the way of deactivating the logger on write error
Yuya Nishihara <yuya@tcha.org>
parents: 40730
diff changeset
   170
            # deactivate this to avoid failed logging again
40763
3ede5d1724bb blackbox: do not nullify repo to deactivate the logger on failure
Yuya Nishihara <yuya@tcha.org>
parents: 40762
diff changeset
   171
            self._trackedevents.clear()
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42615
diff changeset
   172
            ui.debug(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   173
                b'warning: cannot write to blackbox.log: %s\n'
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42615
diff changeset
   174
                % encoding.strtolocal(err.strerror)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42615
diff changeset
   175
            )
40797
ea2688c84e4b blackbox: just try writing to repo.vfs and update lastlogger on success
Yuya Nishihara <yuya@tcha.org>
parents: 40764
diff changeset
   176
            return
ea2688c84e4b blackbox: just try writing to repo.vfs and update lastlogger on success
Yuya Nishihara <yuya@tcha.org>
parents: 40764
diff changeset
   177
        _lastlogger.logger = self
18669
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
   178
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42615
diff changeset
   179
40730
55b053af7196 ui: manage logger instances and event filtering by core ui
Yuya Nishihara <yuya@tcha.org>
parents: 40648
diff changeset
   180
def uipopulate(ui):
40762
37d6ee46a965 blackbox: extract global last logger to proxylogger class
Yuya Nishihara <yuya@tcha.org>
parents: 40760
diff changeset
   181
    ui.setlogger(b'blackbox', _lastlogger)
40730
55b053af7196 ui: manage logger instances and event filtering by core ui
Yuya Nishihara <yuya@tcha.org>
parents: 40648
diff changeset
   182
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42615
diff changeset
   183
18669
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
   184
def reposetup(ui, repo):
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
   185
    # 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
   186
    # 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
   187
    # the blackbox setup for it.
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
   188
    if not repo.local():
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
   189
        return
18242716a014 blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff changeset
   190
40730
55b053af7196 ui: manage logger instances and event filtering by core ui
Yuya Nishihara <yuya@tcha.org>
parents: 40648
diff changeset
   191
    # Since blackbox.log is stored in the repo directory, the logger should be
55b053af7196 ui: manage logger instances and event filtering by core ui
Yuya Nishihara <yuya@tcha.org>
parents: 40648
diff changeset
   192
    # instantiated per repository.
40764
567e164f89b8 blackbox: initialize logger with repo instance
Yuya Nishihara <yuya@tcha.org>
parents: 40763
diff changeset
   193
    logger = blackboxlogger(ui, repo)
40730
55b053af7196 ui: manage logger instances and event filtering by core ui
Yuya Nishihara <yuya@tcha.org>
parents: 40648
diff changeset
   194
    ui.setlogger(b'blackbox', logger)
34276
b90bd9a98c8b blackbox: set lastui even if ui.log is not called (issue5518)
Jun Wu <quark@fb.com>
parents: 34275
diff changeset
   195
40762
37d6ee46a965 blackbox: extract global last logger to proxylogger class
Yuya Nishihara <yuya@tcha.org>
parents: 40760
diff changeset
   196
    # Set _lastlogger even if ui.log is not called. This gives blackbox a
37d6ee46a965 blackbox: extract global last logger to proxylogger class
Yuya Nishihara <yuya@tcha.org>
parents: 40760
diff changeset
   197
    # fallback place to log
37d6ee46a965 blackbox: extract global last logger to proxylogger class
Yuya Nishihara <yuya@tcha.org>
parents: 40760
diff changeset
   198
    if _lastlogger.logger is None:
37d6ee46a965 blackbox: extract global last logger to proxylogger class
Yuya Nishihara <yuya@tcha.org>
parents: 40760
diff changeset
   199
        _lastlogger.logger = logger
34276
b90bd9a98c8b blackbox: set lastui even if ui.log is not called (issue5518)
Jun Wu <quark@fb.com>
parents: 34275
diff changeset
   200
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   201
    repo._wlockfreeprefix.add(b'blackbox.log')
18673
f27598902007 blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents: 18669
diff changeset
   202
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42615
diff changeset
   203
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42615
diff changeset
   204
@command(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   205
    b'blackbox',
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43506
diff changeset
   206
    [
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43506
diff changeset
   207
        (b'l', b'limit', 10, _(b'the number of events to show')),
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43506
diff changeset
   208
    ],
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   209
    _(b'hg blackbox [OPTION]...'),
40295
fa88170c10bb help: adding a proper declaration for shortlist/basic commands (API)
Rodrigo Damazio <rdamazio@google.com>
parents: 40293
diff changeset
   210
    helpcategory=command.CATEGORY_MAINTENANCE,
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42615
diff changeset
   211
    helpbasic=True,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42615
diff changeset
   212
)
18673
f27598902007 blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents: 18669
diff changeset
   213
def blackbox(ui, repo, *revs, **opts):
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43506
diff changeset
   214
    """view the recent repository events"""
18673
f27598902007 blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents: 18669
diff changeset
   215
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   216
    if not repo.vfs.exists(b'blackbox.log'):
18673
f27598902007 blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents: 18669
diff changeset
   217
        return
f27598902007 blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents: 18669
diff changeset
   218
43506
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43077
diff changeset
   219
    limit = opts.get('limit')
49853
e63ab79b2fa1 typing: add some assertions that a variable isn't None
Matt Harbison <matt_harbison@yahoo.com>
parents: 48946
diff changeset
   220
    assert limit is not None  # help pytype
e63ab79b2fa1 typing: add some assertions that a variable isn't None
Matt Harbison <matt_harbison@yahoo.com>
parents: 48946
diff changeset
   221
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   222
    fp = repo.vfs(b'blackbox.log', b'r')
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   223
    lines = fp.read().split(b'\n')
18673
f27598902007 blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents: 18669
diff changeset
   224
f27598902007 blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents: 18669
diff changeset
   225
    count = 0
f27598902007 blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents: 18669
diff changeset
   226
    output = []
f27598902007 blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents: 18669
diff changeset
   227
    for line in reversed(lines):
f27598902007 blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents: 18669
diff changeset
   228
        if count >= limit:
f27598902007 blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents: 18669
diff changeset
   229
            break
f27598902007 blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents: 18669
diff changeset
   230
48574
abbecb5cd6f3 blackbox: change year in logs to ISO 8601 format
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 48573
diff changeset
   231
        # count the commands by matching lines like:
abbecb5cd6f3 blackbox: change year in logs to ISO 8601 format
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 48573
diff changeset
   232
        # 2013/01/23 19:13:36 root>
abbecb5cd6f3 blackbox: change year in logs to ISO 8601 format
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 48573
diff changeset
   233
        # 2013/01/23 19:13:36 root (1234)>
abbecb5cd6f3 blackbox: change year in logs to ISO 8601 format
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 48573
diff changeset
   234
        # 2013/01/23 19:13:36 root @0000000000000000000000000000000000000000 (1234)>
abbecb5cd6f3 blackbox: change year in logs to ISO 8601 format
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 48573
diff changeset
   235
        # 2013-01-23 19:13:36.000 root @0000000000000000000000000000000000000000 (1234)>
48573
011f5218ff2d blackbox: add milliseconds to blackbox logs by default
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 48572
diff changeset
   236
        if re.match(
48574
abbecb5cd6f3 blackbox: change year in logs to ISO 8601 format
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 48573
diff changeset
   237
            br'^\d{4}[-/]\d{2}[-/]\d{2} \d{2}:\d{2}:\d{2}(.\d*)? .*> .*', line
48573
011f5218ff2d blackbox: add milliseconds to blackbox logs by default
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 48572
diff changeset
   238
        ):
18673
f27598902007 blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents: 18669
diff changeset
   239
            count += 1
f27598902007 blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents: 18669
diff changeset
   240
        output.append(line)
f27598902007 blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents: 18669
diff changeset
   241
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   242
    ui.status(b'\n'.join(reversed(output)))