Show More
@@ -1,48 +1,58 b'' | |||
|
1 | 1 | # Support code for event tracing in Mercurial. Lives in demandimport |
|
2 | 2 | # so it can also be used in demandimport. |
|
3 | 3 | # |
|
4 | 4 | # Copyright 2018 Google LLC. |
|
5 | 5 | # |
|
6 | 6 | # This software may be used and distributed according to the terms of the |
|
7 | 7 | # GNU General Public License version 2 or any later version. |
|
8 | 8 | from __future__ import absolute_import |
|
9 | 9 | |
|
10 | 10 | import contextlib |
|
11 | 11 | import os |
|
12 | 12 | |
|
13 | 13 | _pipe = None |
|
14 | 14 | _checked = False |
|
15 | 15 | |
|
16 | 16 | def _isactive(): |
|
17 | 17 | global _pipe, _session, _checked |
|
18 | 18 | if _pipe is None: |
|
19 | 19 | if _checked: |
|
20 | 20 | return False |
|
21 | 21 | _checked = True |
|
22 | 22 | if 'HGCATAPULTSERVERPIPE' not in os.environ: |
|
23 | 23 | return False |
|
24 | 24 | _pipe = open(os.environ['HGCATAPULTSERVERPIPE'], 'w', 1) |
|
25 | 25 | _session = os.environ.get('HGCATAPULTSESSION', 'none') |
|
26 | 26 | return True |
|
27 | 27 | |
|
28 | 28 | @contextlib.contextmanager |
|
29 | 29 | def log(whencefmt, *whenceargs): |
|
30 | 30 | if not _isactive(): |
|
31 | 31 | yield |
|
32 | 32 | return |
|
33 | 33 | whence = whencefmt % whenceargs |
|
34 | 34 | try: |
|
35 | 35 | # Both writes to the pipe are wrapped in try/except to ignore |
|
36 | 36 | # errors, as we can see mysterious errors in here if the pager |
|
37 | 37 | # is active. Presumably other conditions could trigger |
|
38 | 38 | # problems too. |
|
39 | 39 | try: |
|
40 | 40 | _pipe.write('START %s %s\n' % (_session, whence)) |
|
41 | 41 | except IOError: |
|
42 | 42 | pass |
|
43 | 43 | yield |
|
44 | 44 | finally: |
|
45 | 45 | try: |
|
46 | 46 | _pipe.write('END %s %s\n' % (_session, whence)) |
|
47 | 47 | except IOError: |
|
48 | 48 | pass |
|
49 | ||
|
50 | def counter(label, amount, *labelargs): | |
|
51 | if not _isactive(): | |
|
52 | return | |
|
53 | l = label % labelargs | |
|
54 | # See above in log() for why this is in a try/except. | |
|
55 | try: | |
|
56 | _pipe.write('COUNTER %s %d %s\n' % (_session, amount, l)) | |
|
57 | except IOError: | |
|
58 | pass |
General Comments 0
You need to be logged in to leave comments.
Login now