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