hgdemandimport/tracing.py
branchstable
changeset 40404 956ec6f1320d
parent 39397 452790284a15
child 42476 d0b8a3cfd732
equal deleted inserted replaced
40131:535fc8a22365 40404:956ec6f1320d
       
     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 from __future__ import absolute_import
       
     9 
       
    10 import contextlib
       
    11 import os
       
    12 
       
    13 _pipe = None
       
    14 _checked = False
       
    15 
       
    16 @contextlib.contextmanager
       
    17 def log(whencefmt, *whenceargs):
       
    18     global _pipe, _session, _checked
       
    19     if _pipe is None:
       
    20         if _checked:
       
    21             yield
       
    22             return
       
    23         _checked = True
       
    24         if 'HGCATAPULTSERVERPIPE' not in os.environ:
       
    25             yield
       
    26             return
       
    27         _pipe = open(os.environ['HGCATAPULTSERVERPIPE'], 'w', 1)
       
    28         _session = os.environ.get('HGCATAPULTSESSION', 'none')
       
    29     whence = whencefmt % whenceargs
       
    30     try:
       
    31         # 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
       
    33         # is active. Presumably other conditions could trigger
       
    34         # problems too.
       
    35         try:
       
    36             _pipe.write('START %s %s\n' % (_session, whence))
       
    37         except IOError:
       
    38             pass
       
    39         yield
       
    40     finally:
       
    41         try:
       
    42             _pipe.write('END %s %s\n' % (_session, whence))
       
    43         except IOError:
       
    44             pass