mirror of
https://gerrit.googlesource.com/git-repo
synced 2024-12-21 07:16:21 +00:00
repo_trace: adjust formatting, update man page.
No behavior change in this CL. Change-Id: Iab1eb01864ea8a5aec3a683200764d20786b42de Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/351474 Tested-by: LaMont Jones <lamontjones@google.com> Reviewed-by: Xin Li <delphij@google.com>
This commit is contained in:
parent
b240d28bc0
commit
afd767103e
@ -25,7 +25,7 @@ control color usage: auto, always, never
|
|||||||
\fB\-\-trace\fR
|
\fB\-\-trace\fR
|
||||||
trace git command execution (REPO_TRACE=1)
|
trace git command execution (REPO_TRACE=1)
|
||||||
.TP
|
.TP
|
||||||
\fB\-\-trace-to-stderr\fR
|
\fB\-\-trace\-to\-stderr\fR
|
||||||
trace outputs go to stderr in addition to
|
trace outputs go to stderr in addition to
|
||||||
\&.repo/TRACE_FILE
|
\&.repo/TRACE_FILE
|
||||||
.TP
|
.TP
|
||||||
|
@ -66,7 +66,7 @@ class Trace(ContextDecorator):
|
|||||||
|
|
||||||
def _time(self):
|
def _time(self):
|
||||||
"""Generate nanoseconds of time in a py3.6 safe way"""
|
"""Generate nanoseconds of time in a py3.6 safe way"""
|
||||||
return int(time.time()*1e+9)
|
return int(time.time() * 1e+9)
|
||||||
|
|
||||||
def __init__(self, fmt, *args, first_trace=False):
|
def __init__(self, fmt, *args, first_trace=False):
|
||||||
if not IsTrace():
|
if not IsTrace():
|
||||||
@ -78,14 +78,13 @@ class Trace(ContextDecorator):
|
|||||||
|
|
||||||
if first_trace:
|
if first_trace:
|
||||||
_ClearOldTraces()
|
_ClearOldTraces()
|
||||||
self._trace_msg = '%s %s' % (_NEW_COMMAND_SEP, self._trace_msg)
|
self._trace_msg = f'{_NEW_COMMAND_SEP} {self._trace_msg}'
|
||||||
|
|
||||||
|
|
||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
if not IsTrace():
|
if not IsTrace():
|
||||||
return self
|
return self
|
||||||
|
|
||||||
print_msg = f'PID: {os.getpid()} START: {self._time()} :' + self._trace_msg + '\n'
|
print_msg = f'PID: {os.getpid()} START: {self._time()} :{self._trace_msg}\n'
|
||||||
|
|
||||||
with open(_TRACE_FILE, 'a') as f:
|
with open(_TRACE_FILE, 'a') as f:
|
||||||
print(print_msg, file=f)
|
print(print_msg, file=f)
|
||||||
@ -99,7 +98,7 @@ class Trace(ContextDecorator):
|
|||||||
if not IsTrace():
|
if not IsTrace():
|
||||||
return False
|
return False
|
||||||
|
|
||||||
print_msg = f'PID: {os.getpid()} END: {self._time()} :' + self._trace_msg + '\n'
|
print_msg = f'PID: {os.getpid()} END: {self._time()} :{self._trace_msg}\n'
|
||||||
|
|
||||||
with open(_TRACE_FILE, 'a') as f:
|
with open(_TRACE_FILE, 'a') as f:
|
||||||
print(print_msg, file=f)
|
print(print_msg, file=f)
|
||||||
@ -115,9 +114,10 @@ def _GetTraceFile():
|
|||||||
# TODO: refactor to pass repodir to Trace.
|
# TODO: refactor to pass repodir to Trace.
|
||||||
repo_dir = os.path.dirname(os.path.dirname(__file__))
|
repo_dir = os.path.dirname(os.path.dirname(__file__))
|
||||||
trace_file = os.path.join(repo_dir, _TRACE_FILE_NAME)
|
trace_file = os.path.join(repo_dir, _TRACE_FILE_NAME)
|
||||||
print('Trace outputs in %s' % trace_file, file=sys.stderr)
|
print(f'Trace outputs in {trace_file}', file=sys.stderr)
|
||||||
return trace_file
|
return trace_file
|
||||||
|
|
||||||
|
|
||||||
def _ClearOldTraces():
|
def _ClearOldTraces():
|
||||||
"""Clear the oldest commands if trace file is too big.
|
"""Clear the oldest commands if trace file is too big.
|
||||||
|
|
||||||
@ -126,13 +126,13 @@ def _ClearOldTraces():
|
|||||||
will not work precisely.
|
will not work precisely.
|
||||||
"""
|
"""
|
||||||
if os.path.isfile(_TRACE_FILE):
|
if os.path.isfile(_TRACE_FILE):
|
||||||
while os.path.getsize(_TRACE_FILE)/(1024*1024) > _MAX_SIZE:
|
while os.path.getsize(_TRACE_FILE) / (1024 * 1024) > _MAX_SIZE:
|
||||||
temp_file = _TRACE_FILE + '.tmp'
|
temp_file = _TRACE_FILE + '.tmp'
|
||||||
with open(_TRACE_FILE, 'r', errors='ignore') as fin:
|
with open(_TRACE_FILE, 'r', errors='ignore') as fin:
|
||||||
with open(temp_file, 'w') as tf:
|
with open(temp_file, 'w') as tf:
|
||||||
trace_lines = fin.readlines()
|
trace_lines = fin.readlines()
|
||||||
for i , l in enumerate(trace_lines):
|
for i, l in enumerate(trace_lines):
|
||||||
if 'END:' in l and _NEW_COMMAND_SEP in l:
|
if 'END:' in l and _NEW_COMMAND_SEP in l:
|
||||||
tf.writelines(trace_lines[i+1:])
|
tf.writelines(trace_lines[i + 1:])
|
||||||
break
|
break
|
||||||
platform_utils.rename(temp_file, _TRACE_FILE)
|
platform_utils.rename(temp_file, _TRACE_FILE)
|
||||||
|
Loading…
Reference in New Issue
Block a user