Skip to content

Commit

Permalink
mlearning/tflite-micro: add a config option to redirect micro log to …
Browse files Browse the repository at this point in the history
…syslog

new config option TFLITEMICRO_SYSLOG to redirect micro log to syslog

Signed-off-by: chao an <anchao@lixiang.com>
  • Loading branch information
anchao committed Dec 10, 2024
1 parent 37acd5e commit 77b4931
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 0 deletions.
4 changes: 4 additions & 0 deletions mlearning/tflite-micro/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
24 changes: 24 additions & 0 deletions mlearning/tflite-micro/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
5 changes: 5 additions & 0 deletions mlearning/tflite-micro/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
66 changes: 66 additions & 0 deletions mlearning/tflite-micro/tflm_syslog.cc
Original file line number Diff line number Diff line change
@@ -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 <syslog.h>
#include <stdio.h>

#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)

0 comments on commit 77b4931

Please sign in to comment.