cpu/esp32: log_module implementation

The implementation of `log_module` for ESP32 was changed from functions to a macro-based implementation to be able to use the bunch of macros for colored and tagget log output generation.
This commit is contained in:
Gunar Schorcht 2019-08-20 16:23:42 +02:00
parent 327b9fd32e
commit 9a4cdc7e93
2 changed files with 28 additions and 114 deletions

View File

@ -23,22 +23,35 @@
extern "C" {
#endif
/**
* @brief log_write overridden function
*
* @param[in] level (unused)
* @param[in] format String that the function will print
*/
void log_write(unsigned level, const char *format, ...);
#include <stdio.h>
#include "esp_common_log.h"
/**
* @brief log_write overridden function, tagged version
*
* @param[in] level Level of the message
* @param[in] tag Additional information like function or module
* @param[in] format String that the function will print
*/
void log_write_tagged(unsigned level, const char *tag, const char *format, ...);
#ifdef MODULE_LOG_PRINTFNOFORMAT
static inline void log_write(unsigned level, const char *format, ...) {
(void)level;
puts(format);
}
#else /* MODULE_LOG_PRINTFNOFORMAT */
#define log_write(level, ...) \
do { \
if (level == LOG_ERROR) { \
LOG_TAG(LOG_ERROR, E, __func__, ##__VA_ARGS__); \
} \
else if (level == LOG_WARNING) { \
LOG_TAG(LOG_WARNING, W, __func__, ##__VA_ARGS__); \
} \
else if (level == LOG_INFO) { \
LOG_TAG(LOG_INFO, D, __func__, ##__VA_ARGS__); \
} \
else if (level == LOG_DEBUG) { \
LOG_TAG(LOG_DEBUG, E, __func__, ##__VA_ARGS__); \
} \
} while (0U);
#endif /* MODULE_LOG_PRINTFNOFORMAT */
#ifdef __cplusplus
}

View File

@ -1,99 +0,0 @@
/*
* Copyright (C) 2018 Gunar Schorcht
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup cpu_esp32
* @{
*
* @file
* @brief Log module to realize consistent log messages
*
* @author Gunar Schorcht <gunar@schorcht.net>
*/
#include <stdio.h>
#include <string.h>
#include "rom/ets_sys.h"
#include "cpu_conf.h"
#include "log.h"
#include "syscalls.h"
char _printf_buf[PRINTF_BUFSIZ];
bool _new_line = true;
void log_write(unsigned level, const char *format, ...)
{
if (level == LOG_NONE) {
return;
}
if (_new_line) {
/* if we are in new line, we print the prefix */
char lc = 'U';
switch (level) {
case LOG_ERROR : lc = 'E'; break;
case LOG_WARNING: lc = 'W'; break;
case LOG_INFO : lc = 'I'; break;
case LOG_DEBUG : lc = 'D'; break;
case LOG_ALL : lc = 'V'; break;
}
ets_printf("%c (%u) ", lc, system_get_time_ms());
}
va_list arglist;
va_start(arglist, format);
int ret = vsnprintf(_printf_buf, PRINTF_BUFSIZ, format, arglist);
if (ret > 0) {
ets_printf (_printf_buf);
}
va_end(arglist);
_new_line = (strrchr(format, '\n') != NULL);
}
void log_write_tagged(unsigned level, const char *tag, const char *format, ...)
{
if (level == LOG_NONE) {
return;
}
if (_new_line) {
/* if we are in new line, we print the prefix */
char lc = 'U';
switch (level) {
case LOG_ERROR : lc = 'E'; break;
case LOG_WARNING: lc = 'W'; break;
case LOG_INFO : lc = 'I'; break;
case LOG_DEBUG : lc = 'D'; break;
case LOG_ALL : lc = 'V'; break;
}
#if LOG_TAG_IN_BRACKETS
ets_printf("%c (%u) [%10s]: ", lc, system_get_time_ms(), tag);
#else
ets_printf("%c (%u) %10s: ", lc, system_get_time_ms(), tag);
#endif
}
va_list arglist;
va_start(arglist, format);
int ret = vsnprintf(_printf_buf, PRINTF_BUFSIZ, format, arglist);
if (ret > 0) {
ets_printf (_printf_buf);
}
va_end(arglist);
_new_line = (strrchr(format, '\n') != NULL);
}