Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log2File - override default log file name #5076

Closed
FarbakyPal opened this issue Jan 30, 2025 · 4 comments
Closed

Log2File - override default log file name #5076

FarbakyPal opened this issue Jan 30, 2025 · 4 comments

Comments

@FarbakyPal
Copy link

I would like to have an option for the logfile, to have fixed, user specified name.
Everytime the serial port is opened, it creates a new log file with the specific name, or if the file allready exsist, it deletes the original content and starts from the empty log file.

@FarbakyPal
Copy link
Author

Thank your for your response!
In the end I managed to make my own filter work.
The demo filter returned "Warning! Skipping unknown filters"... error for a long time.
For me it wasn't intuitive to use the variable NAME in the .ini "monitor_filter = ", so I used the .py file name.
In hindsight it is reasonable.
If I can make a suggestion for the documentation, in the custon filter section, next to the filter_demo.py code, I would also include a snippet like "monitor_filter = demo" to make it more intuitive for less experienced users.

@ivankravets
Copy link
Member

Could you provide the source code of your monitor here? So, the others can use it.

@FarbakyPal
Copy link
Author

Sure, maybe it proves usefull to others. Here is the .py code:

import io
import os
from datetime import datetime

from platformio.public import DeviceMonitorFilterBase



class filter_own(DeviceMonitorFilterBase):
    NAME = "filter_own"

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self._bufferOut = ""
        self._bufferIn = ""
        if self.options.get("eol") == "CR":
            self._eol = "\r"
        elif self.options.get("eol") == "LF":
            self._eol = "\n"
        else:
            self._eol = "\r\n"
        

    def __del__(self):
        if self._log_fp:
            self._log_fp.close()

    def rx(self, text):
        self._bufferIn += text
        if self._bufferIn.endswith(self._eol):
            timestamp = datetime.now().strftime("%H:%M:%S.%f")[:-3]
            text = "Recieved at %s -- %s" % (timestamp, self._bufferIn)
            self._bufferIn = ""
            self._log_fp.write(text)
            self._log_fp.flush()
            return text
        return ""

    def tx(self, text):
        self._bufferOut += text
        if self._bufferOut.endswith(self._eol):
            timestamp = datetime.now().strftime("%H:%M:%S.%f")[:-3]
            logText = "Sent     at %s -- %s" % (timestamp, self._bufferOut)
            text=self._bufferOut
            print(logText)
            self._bufferOut = ""
            self._log_fp.write(logText)
            self._log_fp.flush()
            return text
        return ""

    def __call__(self):
        if not os.path.isdir("logs"):
            os.makedirs("logs")
        log_file_name = os.path.join(
            "logs", "serial-monitor.log"
        )
        print("--- Logging an output to %s" % os.path.abspath(log_file_name))
        self._log_fp = io.open(log_file_name, "w", encoding="utf-8")
        self._log_fp.write("--- Log file date: %s ---\r\n" % datetime.now().strftime("20%y. %m. %d. -- %H : %M"))
        self._log_fp.flush()
        return self

In The paltformio.ini file you need to add:

monitor_filters =  filter_own

This filter saves the serial communication, both the data received and sent by the computer into a log file called "serial-monitor.log". It also adds a timestamp to every message for extra information.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants