Skip to content

Commit

Permalink
Piped output logs timestamping refined
Browse files Browse the repository at this point in the history
  • Loading branch information
luav committed Aug 28, 2018
1 parent bf760a1 commit db1e1a8
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions mpepool.py
Original file line number Diff line number Diff line change
Expand Up @@ -1121,25 +1121,22 @@ def complete(self, graceful=None):
# Fetch piped data if any, required to be done before the proc.wait to avoid deadlocks:
# https://docs.python.org/3/library/subprocess.html#subprocess.Popen.waithttps://docs.python.org/3/library/subprocess.html#subprocess.Popen.wait
self.fetchPipedData(0)
timestamp = None
# Persis the piped output if required
for pout, plog in ((self.pipedout, self.poutlog), (self.pipederr, self.perrlog)):
if not pout or plog is None: # Omit production of the empty logs
continue
# Ensure existence of the parent directory for the filename
if isinstance(plog, str):
customfile = isinstance(plog, str)
if customfile:
basedir = os.path.split(plog)[0]
if basedir and not os.path.exists(basedir):
os.makedirs(basedir)
# Append to the file
flog = None
# First, add a timestamp even if the log body is empty to be aware about the logging fact
timestamp = None
try:
flog = plog if not isinstance(plog, str) else open(plog, 'a')
# Add a timestamp if the file is not empty to distinguish logs
if isinstance(plog, str) and os.fstat(flog.fileno()).st_size:
if timestamp is None:
timestamp = time.gmtime()
flog = plog if not customfile else open(plog, 'a')
except IOError as err:
print('ERROR on opening the piped log "{}" for "{}": {}. Default output channel is used.'
.format(plog, self.name, err), file=sys.stdout)
Expand All @@ -1148,10 +1145,18 @@ def complete(self, graceful=None):
if plog is self.perrlog:
flog = sys.stderr
try:
if timestamp is not None:
# Add a timestamp if the file is not empty to distinguish logs
## not customfile or
if customfile and os.fstat(flog.fileno()).st_size: # Add timestamp only to the non-empty file
if timestamp is None:
timestamp = time.gmtime()
print(timeheader(timestamp), file=flog) # Note: prints also newline unlike flog.write()
# Append the log body itself if any
flog.write(pout) # Write the piped output
# Note: the file is automatically closed by the object destructor,
# moreover some system files like devnull should not be closed by the user
# if customfile:
# flog.close()
except IOError as err:
print('ERROR on logging piped data "{}" for "{}": {}'
.format(plog, self.name, err), file=sys.stdout)
Expand Down Expand Up @@ -2234,11 +2239,11 @@ def __start(self, job, concur=True):
try:
fout = None
if joutp is job.stdout:
fout = open(joutp, 'a')
fout = open(joutp, 'a') # Note: the file is closed by the ExecPool on the job worker completion
job._stdout = fout #pylint: disable=W0212
outcapt = 'stdout'
elif joutp is job.stderr:
fout = open(joutp, 'a')
fout = open(joutp, 'a') # Note: the file is closed by the ExecPool on the job worker completion
job._stderr = fout #pylint: disable=W0212
outcapt = 'stderr'
else:
Expand Down

0 comments on commit db1e1a8

Please sign in to comment.