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