feat: add console save log

This commit is contained in:
Chen Wu
2018-11-25 21:05:59 +08:00
parent 70d708cdc7
commit 2a1b95beb7
3 changed files with 51 additions and 4 deletions

View File

@ -244,4 +244,12 @@ config ESPTOOLPY_ENABLE_TIME
help
Enable this option, time string will be added at the head of serial input data line.
config ESPTOOLPY_ENABLE_SAVELOG
bool "Enable console log save"
default n
help
Enable this option, all console output will auto-save to current 'log' directory which same level as the 'build` directory;
if 'log' directory is not exist, will create it.
All console output will be saved as a log filename, named PROJECT_NAME + timestamp.log, which PROJECT_NAME defined in your demo Makefile.
endmenu

View File

@ -13,6 +13,12 @@ else
ENABLE_TIME := n
endif
ifdef CONFIG_ESPTOOLPY_ENABLE_SAVELOG
ENABLE_SAVELOG := y
else
ENABLE_SAVELOG := n
endif
CONFIG_ESPTOOLPY_COMPRESSED ?=
PYTHON ?= $(call dequote,$(CONFIG_PYTHON))
@ -99,7 +105,7 @@ endif
simple_monitor: $(call prereq_if_explicit,%flash)
$(MONITOR_PYTHON) -m serial.tools.miniterm --rts 0 --dtr 0 --raw $(ESPPORT) $(MONITORBAUD)
MONITOR_OPTS := --baud $(MONITORBAUD) --port $(ESPPORT) --toolchain-prefix $(CONFIG_TOOLPREFIX) --make "$(MAKE)" --enable-time $(ENABLE_TIME)
MONITOR_OPTS := --baud $(MONITORBAUD) --port $(ESPPORT) --toolchain-prefix $(CONFIG_TOOLPREFIX) --make "$(MAKE)" --enable-time $(ENABLE_TIME) --enable-savelog $(ENABLE_SAVELOG)
monitor: $(call prereq_if_explicit,%flash)
$(summary) MONITOR

View File

@ -38,6 +38,7 @@ try:
except ImportError:
import Queue as queue
import time
import datetime
import sys
import serial
import serial.tools.miniterm as miniterm
@ -74,6 +75,20 @@ def red_print(message):
__version__ = "1.0"
def mkdir(path):
path=path.strip()
path=path.rstrip("\\")
isExists=os.path.exists(path)
if not isExists:
os.makedirs(path)
def esp_openlog(project_name):
logdir = os.getcwd() + '/esplog'
mkdir(logdir)
filename = project_name + datetime.datetime.now().strftime('%Y%m%d%H%M%S.log')
return open(logdir + '/' + filename, 'w+')
# Tags for tuples in queues
TAG_KEY = 0
TAG_SERIAL = 1
@ -231,7 +246,7 @@ class Monitor(object):
Main difference is that all event processing happens in the main thread, not the worker threads.
"""
def __init__(self, serial_instance, elf_file, make="make", toolchain_prefix=DEFAULT_TOOLCHAIN_PREFIX, eol="CRLF", enable_time='n'):
def __init__(self, serial_instance, elf_file, make="make", toolchain_prefix=DEFAULT_TOOLCHAIN_PREFIX, eol="CRLF", enable_time='n', enable_savelog='n'):
super(Monitor, self).__init__()
self.event_queue = queue.Queue()
self.console = miniterm.Console()
@ -259,7 +274,10 @@ class Monitor(object):
self.menu_key = CTRL_T
self.exit_key = CTRL_RBRACKET
self.enable_time = enable_time
self.enable_savelog = enable_savelog
self.next_line = True
if self.enable_savelog == 'y':
self.log_file = esp_openlog(os.path.splitext(os.path.basename(self.elf_file))[0])
self.translate_eol = {
"CRLF": lambda c: c.replace(b"\n", b"\r\n"),
@ -288,6 +306,8 @@ class Monitor(object):
try:
self.console_reader.stop()
self.serial_reader.stop()
if self.enable_savelog == 'y':
self.log_file.close()
except:
pass
sys.stderr.write(ANSI_NORMAL + "\n")
@ -319,7 +339,14 @@ class Monitor(object):
if self.enable_time == 'y' and self.next_line == True:
self.console.write_bytes(get_time_stamp() + ": ")
self.next_line = False
if self.enable_savelog == 'y':
self.log_file.write(get_time_stamp() + ": ")
self.console.write_bytes(b)
if self.enable_savelog == 'y':
self.log_file.write(b)
if b == b'\n': # end of line
self.handle_serial_input_line(self._read_line.strip())
self._read_line = b""
@ -489,7 +516,13 @@ def main():
parser.add_argument(
'--enable-time',
help='Serial port device',
help='enable console timestamp',
default=False
)
parser.add_argument(
'--enable-savelog',
help='enable console log',
default=False
)
@ -518,7 +551,7 @@ def main():
except KeyError:
pass # not running a make jobserver
monitor = Monitor(serial_instance, args.elf_file.name, args.make, args.toolchain_prefix, args.eol, args.enable_time)
monitor = Monitor(serial_instance, args.elf_file.name, args.make, args.toolchain_prefix, args.eol, args.enable_time, args.enable_savelog)
yellow_print('--- idf_monitor on {p.name} {p.baudrate} ---'.format(
p=serial_instance))