##// END OF EJS Templates
demandimport: explicitly declare `_session` at the module level...
marmoute -
r42939:978c9a0c stable
parent child Browse files
Show More
@@ -1,58 +1,59 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 _session = 'none'
15 16
16 17 def _isactive():
17 18 global _pipe, _session, _checked
18 19 if _pipe is None:
19 20 if _checked:
20 21 return False
21 22 _checked = True
22 23 if 'HGCATAPULTSERVERPIPE' not in os.environ:
23 24 return False
24 25 _pipe = open(os.environ['HGCATAPULTSERVERPIPE'], 'w', 1)
25 26 _session = os.environ.get('HGCATAPULTSESSION', 'none')
26 27 return True
27 28
28 29 @contextlib.contextmanager
29 30 def log(whencefmt, *whenceargs):
30 31 if not _isactive():
31 32 yield
32 33 return
33 34 whence = whencefmt % whenceargs
34 35 try:
35 36 # Both writes to the pipe are wrapped in try/except to ignore
36 37 # errors, as we can see mysterious errors in here if the pager
37 38 # is active. Presumably other conditions could trigger
38 39 # problems too.
39 40 try:
40 41 _pipe.write('START %s %s\n' % (_session, whence))
41 42 except IOError:
42 43 pass
43 44 yield
44 45 finally:
45 46 try:
46 47 _pipe.write('END %s %s\n' % (_session, whence))
47 48 except IOError:
48 49 pass
49 50
50 51 def counter(label, amount, *labelargs):
51 52 if not _isactive():
52 53 return
53 54 l = label % labelargs
54 55 # See above in log() for why this is in a try/except.
55 56 try:
56 57 _pipe.write('COUNTER %s %d %s\n' % (_session, amount, l))
57 58 except IOError:
58 59 pass
General Comments 0
You need to be logged in to leave comments. Login now