Skip to content

Commit b22b45d

Browse files
authored
Merge pull request #52 from espressif/fix/improve_log
Fix/improve log
2 parents aca5529 + 98cb181 commit b22b45d

File tree

3 files changed

+23
-24
lines changed

3 files changed

+23
-24
lines changed

pytest-embedded-serial/pytest_embedded_serial/serial.py

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -60,23 +60,9 @@ def _start(self):
6060
pass
6161

6262
def _forward_io(self, pexpect_proc: PexpectProcess) -> None:
63-
rest = b''
6463
while self.proc.is_open:
6564
try:
66-
s = self.proc.read_until()
67-
s = rest + s
68-
69-
res = s.rsplit(b'\n', maxsplit=1)
70-
if len(res) == 1:
71-
rest = s
72-
else:
73-
s = res[0]
74-
rest = res[1]
75-
if s:
76-
pexpect_proc.write(s + b'\n')
65+
s = self.proc.read_all()
66+
pexpect_proc.write(s)
7767
except: # noqa daemon thread may run at any case
78-
try:
79-
pexpect_proc.write(rest) # try to write the rest of line
80-
except: # noqa daemon thread may run at any case
81-
pass
8268
break

pytest-embedded/pytest_embedded/log.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ def __init__(
4949
self._with_timestamp = with_timestamp
5050
self._write_lock = threading.Lock()
5151

52+
self._added_prefix = False
53+
5254
def send(self, s: AnyStr) -> int:
5355
"""
5456
Write to the pexpect process and log.
@@ -68,13 +70,24 @@ def send(self, s: AnyStr) -> int:
6870
# for pytest logging
6971
_temp = sys.stdout
7072
sys.stdout = self.STDOUT # ensure the following print uses system sys.stdout
71-
for line in to_str(s).replace('\r', '\n').split('\n'):
72-
if line.strip():
73-
if self.source:
74-
line = f'[{self.source}] {line}'
75-
if self._with_timestamp:
76-
line = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') + ' ' + line
77-
print(line)
73+
74+
_s = to_str(s)
75+
prefix = ''
76+
if self.source:
77+
prefix = f'[{self.source}] ' + prefix
78+
if self._with_timestamp:
79+
prefix = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') + ' ' + prefix
80+
81+
if not self._added_prefix:
82+
_s = prefix + _s
83+
self._added_prefix = True
84+
_s = _s.replace('\n', '\n' + prefix)
85+
if _s.endswith(prefix):
86+
_s = _s.rsplit(prefix, maxsplit=1)[0]
87+
self._added_prefix = False
88+
89+
sys.stdout.write(_s)
90+
sys.stdout.flush()
7891
sys.stdout = _temp
7992

8093
# write the bytes into the pexpect process

pytest-embedded/pytest_embedded/plugin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ def _close_or_terminate(obj):
319319
current_kwargs[k] = getter(v)
320320
else:
321321
current_kwargs[k] = v
322-
if func.__name__ == '_pexpect_logfile':
322+
if func.__name__ in ['_pexpect_logfile', 'pexpect_proc']:
323323
current_kwargs['count'] = i
324324
current_kwargs['total'] = _COUNT
325325
res.append(func(*args, **current_kwargs))

0 commit comments

Comments
 (0)