From 77b493111ff8779c01500239569b4613b5a39619 Mon Sep 17 00:00:00 2001 From: chao an Date: Mon, 9 Dec 2024 13:25:11 +0800 Subject: [PATCH] mlearning/tflite-micro: add a config option to redirect micro log to syslog new config option TFLITEMICRO_SYSLOG to redirect micro log to syslog Signed-off-by: chao an --- mlearning/tflite-micro/CMakeLists.txt | 4 ++ mlearning/tflite-micro/Kconfig | 24 ++++++++++ mlearning/tflite-micro/Makefile | 5 ++ mlearning/tflite-micro/tflm_syslog.cc | 66 +++++++++++++++++++++++++++ 4 files changed, 99 insertions(+) create mode 100644 mlearning/tflite-micro/tflm_syslog.cc diff --git a/mlearning/tflite-micro/CMakeLists.txt b/mlearning/tflite-micro/CMakeLists.txt index abc9b9fa5f6..eb5221ec4ef 100644 --- a/mlearning/tflite-micro/CMakeLists.txt +++ b/mlearning/tflite-micro/CMakeLists.txt @@ -101,6 +101,10 @@ if(CONFIG_TFLITEMICRO) # Remove test file list(FILTER TFLITE_MICRO_SRCS EXCLUDE REGEX ".*test.cc") + if(CONFIG_TFLITEMICRO_SYSLOG) + list(FILTER TFLITE_MICRO_SRCS EXCLUDE REGEX "micro_log.cc") + list(APPEND TFLITE_MICRO_SRCS ${CMAKE_CURRENT_LIST_DIR}/tflm_syslog.cc) + endif() if(CONFIG_MLEARNING_CMSIS_NN) list(APPEND COMMON_FLAGS -DCMSIS_NN) diff --git a/mlearning/tflite-micro/Kconfig b/mlearning/tflite-micro/Kconfig index 06193fff16e..359ea581934 100644 --- a/mlearning/tflite-micro/Kconfig +++ b/mlearning/tflite-micro/Kconfig @@ -32,6 +32,30 @@ config TFLITEMICRO_TOOL bool "tflite-micro cmdline tool" default n +config TFLITEMICRO_SYSLOG + bool "tflite-micro syslog backend" + default n + +if TFLITEMICRO_SYSLOG + +config TFLITEMICRO_SYSLOG_LEVEL + int "tflite-micro syslog level" + default 6 + ---help--- + Syslog level mapping of tflm, This log level mapping is consistent + with the nuttx syslog level, please refer to syslog.h: + + define LOG_EMERG 0 /* System is unusable */ + define LOG_ALERT 1 /* Action must be taken immediately */ + define LOG_CRIT 2 /* Critical conditions */ + define LOG_ERR 3 /* Error conditions */ + define LOG_WARNING 4 /* Warning conditions */ + define LOG_NOTICE 5 /* Normal, but significant, condition */ + define LOG_INFO 6 /* Informational message */ + define LOG_DEBUG 7 /* Debug-level message */ + +endif # TFLITEMICRO_SYSLOG + if TFLITEMICRO_TOOL config TFLITEMICRO_TOOL_PRIORITY int "tflite-micro tool priority" diff --git a/mlearning/tflite-micro/Makefile b/mlearning/tflite-micro/Makefile index b5dbd7f814a..dd0c1c6ad71 100644 --- a/mlearning/tflite-micro/Makefile +++ b/mlearning/tflite-micro/Makefile @@ -78,6 +78,11 @@ CXXSRCS += $(wildcard $(TFLM_DIR)/tensorflow/lite/micro/tflite_bridge/*.cc) CXXSRCS += $(wildcard $(TFLM_DIR)/tensorflow/lite/schema/*.cc) CXXSRCS := $(filter-out %test.cc, $(CXXSRCS)) +ifneq ($(CONFIG_TFLITEMICRO_SYSLOG),) + CXXSRCS := $(filter-out %micro_log.cc, $(CXXSRCS)) + CXXSRCS += $(wildcard $(CURDIR)/tflm_syslog.cc) +endif + # cmsis ifneq ($(CONFIG_MLEARNING_CMSIS_NN),) COMMON_FLAGS += -DCMSIS_NN diff --git a/mlearning/tflite-micro/tflm_syslog.cc b/mlearning/tflite-micro/tflm_syslog.cc new file mode 100644 index 00000000000..f306a9153bf --- /dev/null +++ b/mlearning/tflite-micro/tflm_syslog.cc @@ -0,0 +1,66 @@ +/**************************************************************************** + * apps/mlearning/tflite-micro/tflm_syslog.cc + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +#include "tensorflow/lite/micro/micro_log.h" + +#include +#include + +#if !defined(TF_LITE_STRIP_ERROR_STRINGS) +#include "tensorflow/lite/micro/debug_log.h" +#endif + +#if !defined(TF_LITE_STRIP_ERROR_STRINGS) + +void VMicroPrintf(FAR const char *format, va_list ap) +{ + vsyslog(CONFIG_TFLITEMICRO_SYSLOG_LEVEL, format, ap); +} + +void MicroPrintf(FAR const char *format, ...) +{ + va_list ap; + + /* Let vsyslog do the work */ + + va_start(ap, format); + vsyslog(CONFIG_TFLITEMICRO_SYSLOG_LEVEL, format, ap); + va_end(ap); +} + +int MicroSnprintf(FAR char *buffer, size_t buf_size, FAR const char *format, ...) +{ + va_list ap; + int ret; + + va_start(ap, format); + ret = vsnprintf(buffer, buf_size, format, ap); + va_end(ap); + + return ret; +} + +int MicroVsnprintf(FAR char *buffer, size_t buf_size, + FAR const char *format, va_list vlist) +{ + return vsnprintf(buffer, buf_size, format, vlist); +} + +#endif // !defined(TF_LITE_STRIP_ERROR_STRINGS)