##// END OF EJS Templates
tracing: extract tracing-active logic to separate function...
Augie Fackler -
r42676:d0b8a3cf default
parent child Browse files
Show More
@@ -1,44 +1,48 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
15
16 @contextlib.contextmanager
16 def _isactive():
17 def log(whencefmt, *whenceargs):
18 global _pipe, _session, _checked
17 global _pipe, _session, _checked
19 if _pipe is None:
18 if _pipe is None:
20 if _checked:
19 if _checked:
21 yield
20 return False
22 return
23 _checked = True
21 _checked = True
24 if 'HGCATAPULTSERVERPIPE' not in os.environ:
22 if 'HGCATAPULTSERVERPIPE' not in os.environ:
25 yield
23 return False
26 return
27 _pipe = open(os.environ['HGCATAPULTSERVERPIPE'], 'w', 1)
24 _pipe = open(os.environ['HGCATAPULTSERVERPIPE'], 'w', 1)
28 _session = os.environ.get('HGCATAPULTSESSION', 'none')
25 _session = os.environ.get('HGCATAPULTSESSION', 'none')
26 return True
27
28 @contextlib.contextmanager
29 def log(whencefmt, *whenceargs):
30 if not _isactive():
31 yield
32 return
29 whence = whencefmt % whenceargs
33 whence = whencefmt % whenceargs
30 try:
34 try:
31 # Both writes to the pipe are wrapped in try/except to ignore
35 # Both writes to the pipe are wrapped in try/except to ignore
32 # errors, as we can see mysterious errors in here if the pager
36 # errors, as we can see mysterious errors in here if the pager
33 # is active. Presumably other conditions could trigger
37 # is active. Presumably other conditions could trigger
34 # problems too.
38 # problems too.
35 try:
39 try:
36 _pipe.write('START %s %s\n' % (_session, whence))
40 _pipe.write('START %s %s\n' % (_session, whence))
37 except IOError:
41 except IOError:
38 pass
42 pass
39 yield
43 yield
40 finally:
44 finally:
41 try:
45 try:
42 _pipe.write('END %s %s\n' % (_session, whence))
46 _pipe.write('END %s %s\n' % (_session, whence))
43 except IOError:
47 except IOError:
44 pass
48 pass
General Comments 0
You need to be logged in to leave comments. Login now