Skip to content

Commit

Permalink
[rqd] Fix non ASCII chars (#1335)
Browse files Browse the repository at this point in the history
- Ensure consistent handling of non-ASCII characters in logs
- Always encode lines to ASCII with an 'ignore' option to discard non-ASCII characters.
- Removed unused file_descriptor block and ensured consistent encoding logic.

This change prevents UnicodeEncodeError and ensures consistent log outputs.
  • Loading branch information
ramonfigueiredo authored Jul 10, 2024
1 parent c345dff commit c56d8cb
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions rqd/rqd/rqcore.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,17 +318,13 @@ def runLinux(self):
else:
tempCommand += [self._createCommandFile(runFrame.command)]

if rqd.rqconstants.RQD_PREPEND_TIMESTAMP:
file_descriptor = subprocess.PIPE
else:
file_descriptor = self.rqlog
# pylint: disable=subprocess-popen-preexec-fn
frameInfo.forkedCommand = subprocess.Popen(tempCommand,
env=self.frameEnv,
cwd=self.rqCore.machine.getTempPath(),
stdin=subprocess.PIPE,
stdout=file_descriptor,
stderr=file_descriptor,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
close_fds=True,
preexec_fn=os.setsid)
finally:
Expand All @@ -343,6 +339,16 @@ def runLinux(self):

if rqd.rqconstants.RQD_PREPEND_TIMESTAMP:
pipe_to_file(frameInfo.forkedCommand.stdout, frameInfo.forkedCommand.stderr, self.rqlog)
else:
with open(self.rqlog, 'a') as f:
# Convert to ASCII while discarding characters that can not be encoded
for line in frameInfo.forkedCommand.stdout:
line = line.encode('ascii', 'ignore')
f.write(line.decode('ascii') + '\n')
for line in frameInfo.forkedCommand.stderr:
line = line.encode('ascii', 'ignore')
f.write(line.decode('ascii') + '\n')

returncode = frameInfo.forkedCommand.wait()

# Find exitStatus and exitSignal
Expand Down Expand Up @@ -1222,6 +1228,8 @@ def print_and_flush_ln(fd, last_timestamp):

remainder = lines[-1]
for line in lines[0:-1]:
# Convert to ASCII while discarding characters that can not be encoded
line = line.encode('ascii', 'ignore')
print("[%s] %s" % (curr_line_timestamp, line), file=outfile)
outfile.flush()
os.fsync(outfile)
Expand Down

0 comments on commit c56d8cb

Please sign in to comment.