Mercurial > hg-stable
annotate hgext/blackbox.py @ 42682:ab1900323b1d
fsmonitor: refactor watchmanclient.client to accept ui and repo path
This will make my next patch simpler.
Differential Revision: https://phab.mercurial-scm.org/D6680
author | Augie Fackler <augie@google.com> |
---|---|
date | Tue, 23 Jul 2019 14:36:38 -0400 |
parents | 56132ebd14c6 |
children | 2372284d9457 |
rev | line source |
---|---|
18676 | 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 | 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:
41546
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:
41546
diff
changeset
|
13 blackbox.ignore config keys. |
28246 | 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:
41546
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 | 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 |
40504
25f1c7bd649d
blackbox: add configitem for format of log timestamps
Matt DeVore <matvore@google.com>
parents:
40295
diff
changeset
|
38 [blackbox] |
25f1c7bd649d
blackbox: add configitem for format of log timestamps
Matt DeVore <matvore@google.com>
parents:
40295
diff
changeset
|
39 # 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
|
40 # datetime.datetime.strftime) |
25f1c7bd649d
blackbox: add configitem for format of log timestamps
Matt DeVore <matvore@google.com>
parents:
40295
diff
changeset
|
41 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
|
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 from __future__ import absolute_import |
8113c88b8e6d
blackbox: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
28027
diff
changeset
|
46 |
8113c88b8e6d
blackbox: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
28027
diff
changeset
|
47 import re |
8113c88b8e6d
blackbox: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
28027
diff
changeset
|
48 |
18669
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
49 from mercurial.i18n import _ |
28245
caa2a0c6fbb7
blackbox: log working directory version
timeless <timeless@mozdev.org>
parents:
28244
diff
changeset
|
50 from mercurial.node import hex |
caa2a0c6fbb7
blackbox: log working directory version
timeless <timeless@mozdev.org>
parents:
28244
diff
changeset
|
51 |
28090
8113c88b8e6d
blackbox: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
28027
diff
changeset
|
52 from mercurial import ( |
35667
de598e84c244
py3: cast error message to localstr in blackbox.py
Gregory Szorc <gregory.szorc@gmail.com>
parents:
35027
diff
changeset
|
53 encoding, |
40828
03127e580980
loggingutil: extract openlogfile() and proxylogger to new module
Yuya Nishihara <yuya@tcha.org>
parents:
40827
diff
changeset
|
54 loggingutil, |
32376
46ba2cdda476
registrar: move cmdutil.command to registrar module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
32204
diff
changeset
|
55 registrar, |
28090
8113c88b8e6d
blackbox: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
28027
diff
changeset
|
56 ) |
37123
a8a902d7176e
procutil: bulk-replace function calls to point to new module
Yuya Nishihara <yuya@tcha.org>
parents:
36636
diff
changeset
|
57 from mercurial.utils import ( |
a8a902d7176e
procutil: bulk-replace function calls to point to new module
Yuya Nishihara <yuya@tcha.org>
parents:
36636
diff
changeset
|
58 dateutil, |
a8a902d7176e
procutil: bulk-replace function calls to point to new module
Yuya Nishihara <yuya@tcha.org>
parents:
36636
diff
changeset
|
59 procutil, |
a8a902d7176e
procutil: bulk-replace function calls to point to new module
Yuya Nishihara <yuya@tcha.org>
parents:
36636
diff
changeset
|
60 ) |
18669
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
61 |
29852
d5883fd055c6
extensions: change magic "shipped with hg" string
Augie Fackler <augie@google.com>
parents:
28552
diff
changeset
|
62 # 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
|
63 # 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
|
64 # 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
|
65 # leave the attribute unspecified. |
29852
d5883fd055c6
extensions: change magic "shipped with hg" string
Augie Fackler <augie@google.com>
parents:
28552
diff
changeset
|
66 testedwith = 'ships-with-hg-core' |
33141
7dc090faa8a4
blackbox: minor code reordering
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32450
diff
changeset
|
67 |
7dc090faa8a4
blackbox: minor code reordering
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32450
diff
changeset
|
68 cmdtable = {} |
7dc090faa8a4
blackbox: minor code reordering
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32450
diff
changeset
|
69 command = registrar.command(cmdtable) |
7dc090faa8a4
blackbox: minor code reordering
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32450
diff
changeset
|
70 |
33142
0a638f37f2d2
configitems: register 'blackbox.maxsize' as an example of 'configbytes'
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
33141
diff
changeset
|
71 configtable = {} |
0a638f37f2d2
configitems: register 'blackbox.maxsize' as an example of 'configbytes'
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
33141
diff
changeset
|
72 configitem = registrar.configitem(configtable) |
0a638f37f2d2
configitems: register 'blackbox.maxsize' as an example of 'configbytes'
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
33141
diff
changeset
|
73 |
33191
1df74b71396d
configitems: register the 'blackbox.dirty' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
33143
diff
changeset
|
74 configitem('blackbox', 'dirty', |
1df74b71396d
configitems: register the 'blackbox.dirty' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
33143
diff
changeset
|
75 default=False, |
1df74b71396d
configitems: register the 'blackbox.dirty' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
33143
diff
changeset
|
76 ) |
33142
0a638f37f2d2
configitems: register 'blackbox.maxsize' as an example of 'configbytes'
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
33141
diff
changeset
|
77 configitem('blackbox', 'maxsize', |
33143
59c135bb31bc
blackbox: use a human readable version of the default
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
33142
diff
changeset
|
78 default='1 MB', |
33142
0a638f37f2d2
configitems: register 'blackbox.maxsize' as an example of 'configbytes'
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
33141
diff
changeset
|
79 ) |
33192
0ef40bb20264
configitems: register the 'blackbox.logsource' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
33191
diff
changeset
|
80 configitem('blackbox', 'logsource', |
0ef40bb20264
configitems: register the 'blackbox.logsource' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
33191
diff
changeset
|
81 default=False, |
0ef40bb20264
configitems: register the 'blackbox.logsource' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
33191
diff
changeset
|
82 ) |
34745
0b46440b1b45
configitems: register the 'blackbox.maxfiles' config
Boris Feld <boris.feld@octobus.net>
parents:
34583
diff
changeset
|
83 configitem('blackbox', 'maxfiles', |
0b46440b1b45
configitems: register the 'blackbox.maxfiles' config
Boris Feld <boris.feld@octobus.net>
parents:
34583
diff
changeset
|
84 default=7, |
0b46440b1b45
configitems: register the 'blackbox.maxfiles' config
Boris Feld <boris.feld@octobus.net>
parents:
34583
diff
changeset
|
85 ) |
34517
49b72b6f6d66
configitems: register the 'blackbox.track' config
Boris Feld <boris.feld@octobus.net>
parents:
34307
diff
changeset
|
86 configitem('blackbox', 'track', |
34583
19b2c062654c
configitems: fix registration for 'blackbox.track' config
Boris Feld <boris.feld@octobus.net>
parents:
34517
diff
changeset
|
87 default=lambda: ['*'], |
34517
49b72b6f6d66
configitems: register the 'blackbox.track' config
Boris Feld <boris.feld@octobus.net>
parents:
34307
diff
changeset
|
88 ) |
42615
56132ebd14c6
blackbox: disable extremely verbose logging (issue6110)
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
41546
diff
changeset
|
89 configitem('blackbox', 'ignore', |
56132ebd14c6
blackbox: disable extremely verbose logging (issue6110)
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
41546
diff
changeset
|
90 default=lambda: ['chgserver', 'cmdserver', 'extension'], |
56132ebd14c6
blackbox: disable extremely verbose logging (issue6110)
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
41546
diff
changeset
|
91 ) |
40504
25f1c7bd649d
blackbox: add configitem for format of log timestamps
Matt DeVore <matvore@google.com>
parents:
40295
diff
changeset
|
92 configitem('blackbox', 'date-format', |
25f1c7bd649d
blackbox: add configitem for format of log timestamps
Matt DeVore <matvore@google.com>
parents:
40295
diff
changeset
|
93 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
|
94 ) |
33142
0a638f37f2d2
configitems: register 'blackbox.maxsize' as an example of 'configbytes'
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
33141
diff
changeset
|
95 |
40828
03127e580980
loggingutil: extract openlogfile() and proxylogger to new module
Yuya Nishihara <yuya@tcha.org>
parents:
40827
diff
changeset
|
96 _lastlogger = loggingutil.proxylogger() |
40796
37d6ee46a965
blackbox: extract global last logger to proxylogger class
Yuya Nishihara <yuya@tcha.org>
parents:
40794
diff
changeset
|
97 |
40688
fff3e213ace9
blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents:
40687
diff
changeset
|
98 class blackboxlogger(object): |
40798
567e164f89b8
blackbox: initialize logger with repo instance
Yuya Nishihara <yuya@tcha.org>
parents:
40797
diff
changeset
|
99 def __init__(self, ui, repo): |
567e164f89b8
blackbox: initialize logger with repo instance
Yuya Nishihara <yuya@tcha.org>
parents:
40797
diff
changeset
|
100 self._repo = repo |
40691
85372dc0cca3
blackbox: extract function to test if log event is tracked
Yuya Nishihara <yuya@tcha.org>
parents:
40690
diff
changeset
|
101 self._trackedevents = set(ui.configlist('blackbox', 'track')) |
42615
56132ebd14c6
blackbox: disable extremely verbose logging (issue6110)
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
41546
diff
changeset
|
102 self._ignoredevents = set(ui.configlist('blackbox', 'ignore')) |
40827
644adf9c20fb
blackbox: pass in options to _openlogfile() as arguments
Yuya Nishihara <yuya@tcha.org>
parents:
40826
diff
changeset
|
103 self._maxfiles = ui.configint('blackbox', 'maxfiles') |
644adf9c20fb
blackbox: pass in options to _openlogfile() as arguments
Yuya Nishihara <yuya@tcha.org>
parents:
40826
diff
changeset
|
104 self._maxsize = ui.configbytes('blackbox', 'maxsize') |
41003
49d48489a16b
blackbox: resurrect recursion guard
Yuya Nishihara <yuya@tcha.org>
parents:
40828
diff
changeset
|
105 self._inlog = False |
40687
a9393d7600f3
blackbox: extract logger class from ui wrapper
Yuya Nishihara <yuya@tcha.org>
parents:
40686
diff
changeset
|
106 |
40691
85372dc0cca3
blackbox: extract function to test if log event is tracked
Yuya Nishihara <yuya@tcha.org>
parents:
40690
diff
changeset
|
107 def tracked(self, event): |
42615
56132ebd14c6
blackbox: disable extremely verbose logging (issue6110)
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
41546
diff
changeset
|
108 return ((b'*' in self._trackedevents |
56132ebd14c6
blackbox: disable extremely verbose logging (issue6110)
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
41546
diff
changeset
|
109 and event not in self._ignoredevents) |
56132ebd14c6
blackbox: disable extremely verbose logging (issue6110)
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
41546
diff
changeset
|
110 or event in self._trackedevents) |
40691
85372dc0cca3
blackbox: extract function to test if log event is tracked
Yuya Nishihara <yuya@tcha.org>
parents:
40690
diff
changeset
|
111 |
40688
fff3e213ace9
blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents:
40687
diff
changeset
|
112 def log(self, ui, event, msg, opts): |
41003
49d48489a16b
blackbox: resurrect recursion guard
Yuya Nishihara <yuya@tcha.org>
parents:
40828
diff
changeset
|
113 # self._log() -> ctx.dirty() may create new subrepo instance, which |
49d48489a16b
blackbox: resurrect recursion guard
Yuya Nishihara <yuya@tcha.org>
parents:
40828
diff
changeset
|
114 # ui is derived from baseui. So the recursion guard in ui.log() |
49d48489a16b
blackbox: resurrect recursion guard
Yuya Nishihara <yuya@tcha.org>
parents:
40828
diff
changeset
|
115 # doesn't work as it's local to the ui instance. |
49d48489a16b
blackbox: resurrect recursion guard
Yuya Nishihara <yuya@tcha.org>
parents:
40828
diff
changeset
|
116 if self._inlog: |
49d48489a16b
blackbox: resurrect recursion guard
Yuya Nishihara <yuya@tcha.org>
parents:
40828
diff
changeset
|
117 return |
49d48489a16b
blackbox: resurrect recursion guard
Yuya Nishihara <yuya@tcha.org>
parents:
40828
diff
changeset
|
118 self._inlog = True |
49d48489a16b
blackbox: resurrect recursion guard
Yuya Nishihara <yuya@tcha.org>
parents:
40828
diff
changeset
|
119 try: |
49d48489a16b
blackbox: resurrect recursion guard
Yuya Nishihara <yuya@tcha.org>
parents:
40828
diff
changeset
|
120 self._log(ui, event, msg, opts) |
49d48489a16b
blackbox: resurrect recursion guard
Yuya Nishihara <yuya@tcha.org>
parents:
40828
diff
changeset
|
121 finally: |
49d48489a16b
blackbox: resurrect recursion guard
Yuya Nishihara <yuya@tcha.org>
parents:
40828
diff
changeset
|
122 self._inlog = False |
49d48489a16b
blackbox: resurrect recursion guard
Yuya Nishihara <yuya@tcha.org>
parents:
40828
diff
changeset
|
123 |
49d48489a16b
blackbox: resurrect recursion guard
Yuya Nishihara <yuya@tcha.org>
parents:
40828
diff
changeset
|
124 def _log(self, ui, event, msg, opts): |
40688
fff3e213ace9
blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents:
40687
diff
changeset
|
125 default = ui.configdate('devel', 'default-date') |
fff3e213ace9
blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents:
40687
diff
changeset
|
126 date = dateutil.datestr(default, ui.config('blackbox', 'date-format')) |
fff3e213ace9
blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents:
40687
diff
changeset
|
127 user = procutil.getuser() |
fff3e213ace9
blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents:
40687
diff
changeset
|
128 pid = '%d' % procutil.getpid() |
fff3e213ace9
blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents:
40687
diff
changeset
|
129 changed = '' |
40689
179c02baaa8c
blackbox: initialize repo attribute properly
Yuya Nishihara <yuya@tcha.org>
parents:
40688
diff
changeset
|
130 ctx = self._repo[None] |
40688
fff3e213ace9
blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents:
40687
diff
changeset
|
131 parents = ctx.parents() |
fff3e213ace9
blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents:
40687
diff
changeset
|
132 rev = ('+'.join([hex(p.node()) for p in parents])) |
fff3e213ace9
blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents:
40687
diff
changeset
|
133 if (ui.configbool('blackbox', 'dirty') and |
fff3e213ace9
blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents:
40687
diff
changeset
|
134 ctx.dirty(missing=True, merge=False, branch=False)): |
fff3e213ace9
blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents:
40687
diff
changeset
|
135 changed = '+' |
fff3e213ace9
blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents:
40687
diff
changeset
|
136 if ui.configbool('blackbox', 'logsource'): |
fff3e213ace9
blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents:
40687
diff
changeset
|
137 src = ' [%s]' % event |
fff3e213ace9
blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents:
40687
diff
changeset
|
138 else: |
fff3e213ace9
blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents:
40687
diff
changeset
|
139 src = '' |
fff3e213ace9
blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents:
40687
diff
changeset
|
140 try: |
fff3e213ace9
blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents:
40687
diff
changeset
|
141 fmt = '%s %s @%s%s (%s)%s> %s' |
40794
ffd574c144d2
ui: pass in formatted message to logger.log()
Yuya Nishihara <yuya@tcha.org>
parents:
40793
diff
changeset
|
142 args = (date, user, rev, changed, pid, src, msg) |
40828
03127e580980
loggingutil: extract openlogfile() and proxylogger to new module
Yuya Nishihara <yuya@tcha.org>
parents:
40827
diff
changeset
|
143 with loggingutil.openlogfile( |
03127e580980
loggingutil: extract openlogfile() and proxylogger to new module
Yuya Nishihara <yuya@tcha.org>
parents:
40827
diff
changeset
|
144 ui, self._repo.vfs, name='blackbox.log', |
03127e580980
loggingutil: extract openlogfile() and proxylogger to new module
Yuya Nishihara <yuya@tcha.org>
parents:
40827
diff
changeset
|
145 maxfiles=self._maxfiles, maxsize=self._maxsize) as fp: |
40688
fff3e213ace9
blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents:
40687
diff
changeset
|
146 fp.write(fmt % args) |
fff3e213ace9
blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents:
40687
diff
changeset
|
147 except (IOError, OSError) as err: |
40792
eb5948f29c60
blackbox: change the way of deactivating the logger on write error
Yuya Nishihara <yuya@tcha.org>
parents:
40764
diff
changeset
|
148 # deactivate this to avoid failed logging again |
40797
3ede5d1724bb
blackbox: do not nullify repo to deactivate the logger on failure
Yuya Nishihara <yuya@tcha.org>
parents:
40796
diff
changeset
|
149 self._trackedevents.clear() |
40688
fff3e213ace9
blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents:
40687
diff
changeset
|
150 ui.debug('warning: cannot write to blackbox.log: %s\n' % |
fff3e213ace9
blackbox: unindent "if True" block
Yuya Nishihara <yuya@tcha.org>
parents:
40687
diff
changeset
|
151 encoding.strtolocal(err.strerror)) |
40826
ea2688c84e4b
blackbox: just try writing to repo.vfs and update lastlogger on success
Yuya Nishihara <yuya@tcha.org>
parents:
40798
diff
changeset
|
152 return |
ea2688c84e4b
blackbox: just try writing to repo.vfs and update lastlogger on success
Yuya Nishihara <yuya@tcha.org>
parents:
40798
diff
changeset
|
153 _lastlogger.logger = self |
18669
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
154 |
40764
55b053af7196
ui: manage logger instances and event filtering by core ui
Yuya Nishihara <yuya@tcha.org>
parents:
40691
diff
changeset
|
155 def uipopulate(ui): |
40796
37d6ee46a965
blackbox: extract global last logger to proxylogger class
Yuya Nishihara <yuya@tcha.org>
parents:
40794
diff
changeset
|
156 ui.setlogger(b'blackbox', _lastlogger) |
40764
55b053af7196
ui: manage logger instances and event filtering by core ui
Yuya Nishihara <yuya@tcha.org>
parents:
40691
diff
changeset
|
157 |
18669
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
158 def reposetup(ui, repo): |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
159 # 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
|
160 # 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
|
161 # the blackbox setup for it. |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
162 if not repo.local(): |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
163 return |
18242716a014
blackbox: adds a blackbox extension
Durham Goode <durham@fb.com>
parents:
diff
changeset
|
164 |
40764
55b053af7196
ui: manage logger instances and event filtering by core ui
Yuya Nishihara <yuya@tcha.org>
parents:
40691
diff
changeset
|
165 # 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:
40691
diff
changeset
|
166 # instantiated per repository. |
40798
567e164f89b8
blackbox: initialize logger with repo instance
Yuya Nishihara <yuya@tcha.org>
parents:
40797
diff
changeset
|
167 logger = blackboxlogger(ui, repo) |
40764
55b053af7196
ui: manage logger instances and event filtering by core ui
Yuya Nishihara <yuya@tcha.org>
parents:
40691
diff
changeset
|
168 ui.setlogger(b'blackbox', logger) |
34283
b90bd9a98c8b
blackbox: set lastui even if ui.log is not called (issue5518)
Jun Wu <quark@fb.com>
parents:
34282
diff
changeset
|
169 |
40796
37d6ee46a965
blackbox: extract global last logger to proxylogger class
Yuya Nishihara <yuya@tcha.org>
parents:
40794
diff
changeset
|
170 # 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:
40794
diff
changeset
|
171 # fallback place to log |
37d6ee46a965
blackbox: extract global last logger to proxylogger class
Yuya Nishihara <yuya@tcha.org>
parents:
40794
diff
changeset
|
172 if _lastlogger.logger is None: |
37d6ee46a965
blackbox: extract global last logger to proxylogger class
Yuya Nishihara <yuya@tcha.org>
parents:
40794
diff
changeset
|
173 _lastlogger.logger = logger |
34283
b90bd9a98c8b
blackbox: set lastui even if ui.log is not called (issue5518)
Jun Wu <quark@fb.com>
parents:
34282
diff
changeset
|
174 |
33436
9bb4decd43b0
repovfs: add a ward to check if locks are properly taken
Boris Feld <boris.feld@octobus.net>
parents:
33361
diff
changeset
|
175 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
|
176 |
40295
fa88170c10bb
help: adding a proper declaration for shortlist/basic commands (API)
Rodrigo Damazio <rdamazio@google.com>
parents:
40293
diff
changeset
|
177 @command('blackbox', |
18673
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
178 [('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
|
179 ], |
40293
c303d65d2e34
help: assigning categories to existing commands
rdamazio@google.com
parents:
38823
diff
changeset
|
180 _('hg blackbox [OPTION]...'), |
40295
fa88170c10bb
help: adding a proper declaration for shortlist/basic commands (API)
Rodrigo Damazio <rdamazio@google.com>
parents:
40293
diff
changeset
|
181 helpcategory=command.CATEGORY_MAINTENANCE, |
fa88170c10bb
help: adding a proper declaration for shortlist/basic commands (API)
Rodrigo Damazio <rdamazio@google.com>
parents:
40293
diff
changeset
|
182 helpbasic=True) |
18673
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
183 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
|
184 '''view the recent repository events |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
185 ''' |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
186 |
28026
a6db1163befa
blackbox: refactor use of vfs as _bbvfs
timeless <timeless@mozdev.org>
parents:
28025
diff
changeset
|
187 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
|
188 return |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
189 |
35027
375577785f49
py3: handle keyword arguments in hgext/blackbox.py
Pulkit Goyal <7895pulkit@gmail.com>
parents:
34745
diff
changeset
|
190 limit = opts.get(r'limit') |
28244
c17d7b1c40be
blackbox: rename fp variable
timeless <timeless@mozdev.org>
parents:
28243
diff
changeset
|
191 fp = repo.vfs('blackbox.log', 'r') |
c17d7b1c40be
blackbox: rename fp variable
timeless <timeless@mozdev.org>
parents:
28243
diff
changeset
|
192 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
|
193 |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
194 count = 0 |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
195 output = [] |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
196 for line in reversed(lines): |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
197 if count >= limit: |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
198 break |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
199 |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
200 # count the commands by matching lines like: 2013/01/23 19:13:36 root> |
41546
bd3f03d8cc9f
global: use raw strings for regular expressions with escapes
Gregory Szorc <gregory.szorc@gmail.com>
parents:
41387
diff
changeset
|
201 if re.match(br'^\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2} .*> .*', line): |
18673
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
202 count += 1 |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
203 output.append(line) |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
204 |
f27598902007
blackbox: adds a 'blackbox' command for viewing recent logs
Durham Goode <durham@fb.com>
parents:
18669
diff
changeset
|
205 ui.status('\n'.join(reversed(output))) |