Skip to content

Commit

Permalink
Fixed an issue with a "wrong" timestamp in device monitor output usin…
Browse files Browse the repository at this point in the history
…g `"time" filter` // Resolve #3712
  • Loading branch information
ivankravets committed Oct 30, 2020
1 parent 0bbe7f8 commit 96b1a1c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
1 change: 1 addition & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ PlatformIO Core 5
- Fixed an "AssertionError: ensure_dir_exists" when checking library updates from simultaneous subprocesses (`issue #3677 <https://github.com/platformio/platformio-core/issues/3677>`_)
- Fixed an issue when `pio package publish <https://docs.platformio.org/page/core/userguide/package/cmd_publish.html>`__ command removes original archive after submitting to the registry (`issue #3716 <https://github.com/platformio/platformio-core/issues/3716>`_)
- Fixed an issue when multiple `pio lib install <https://docs.platformio.org/page/core/userguide/lib/cmd_install.html>`__ command with the same local library results in duplicates in ``lib_deps`` (`issue #3715 <https://github.com/platformio/platformio-core/issues/3715>`_)
- Fixed an issue with a "wrong" timestamp in device monitor output using `"time" filter <https://docs.platformio.org/page/core/userguide/device/cmd_monitor.html#filters>`__ (`issue #3712 <https://github.com/platformio/platformio-core/issues/3712>`_)

5.0.1 (2020-09-10)
~~~~~~~~~~~~~~~~~~
Expand Down
13 changes: 8 additions & 5 deletions platformio/commands/device/filters/time.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@ class Timestamp(DeviceMonitorFilter):

def __init__(self, *args, **kwargs):
super(Timestamp, self).__init__(*args, **kwargs)
self._first_text_received = False
self._line_started = False

def rx(self, text):
if self._first_text_received and "\n" not in text:
if self._line_started and "\n" not in text:
return text
timestamp = datetime.now().strftime("%H:%M:%S.%f")[:-3]
if not self._first_text_received:
self._first_text_received = True
return "%s > %s" % (timestamp, text)
if not self._line_started:
self._line_started = True
text = "%s > %s" % (timestamp, text)
if text.endswith("\n"):
self._line_started = False
return text[:-1].replace("\n", "\n%s > " % timestamp) + "\n"
return text.replace("\n", "\n%s > " % timestamp)

4 comments on commit 96b1a1c

@hoegge
Copy link

@hoegge hoegge commented on 96b1a1c Oct 30, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks better and solves the issue, but can text include more than one line, e.g.:

´this_is_a_line_\n_and_this_is_the_next´

in which case there would be no timestamp in front of "_and_this_is_the_next", but maybe the serial data always is separated by \n ?

Thanks for looking into this.

@ivankravets
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean? See a test code:

#include <Arduino.h>

void setup() {
	Serial.begin(9600);
}

void loop() {
	Serial.println("Classic line. Wait 2 secs now");
	delay(2000);
	Serial.print("Part of line, wait 3 secs, no NL...");
	delay(3000);
	Serial.println("Finished line! Next line is with multiple NLs");
	Serial.println("1\n2\n3\nWait 5 secs");
	delay(5000);
}

and output

Screen Shot 2020-10-30 at 17 45 53

@hoegge
Copy link

@hoegge hoegge commented on 96b1a1c Oct 31, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you are perfectly right. I misread the code - sorry for that and the trouble - and thanks again for fixing this. Will help a lot debugging code with correct timestamps. Thanks.
PS: And thanks for suggesting me to fix it myself - I would have tried - you were just too fast :-)

@ivankravets
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy coding with PlatformIO! ;)

Please sign in to comment.