Merge branch 'feature/add_log_tag_level' into 'master'

Add tag level set and check

See merge request sdk/ESP8266_RTOS_SDK!422
This commit is contained in:
Dong Heng
2018-09-04 17:21:19 +08:00
5 changed files with 230 additions and 6 deletions

View File

@ -44,5 +44,10 @@ config LOG_COLORS
In order to view these, your terminal program must support ANSI color codes. In order to view these, your terminal program must support ANSI color codes.
config LOG_SET_LEVEL
bool "Enable log set level"
default n
help
Enable this option, user can set tag level.
endmenu endmenu

View File

@ -41,6 +41,30 @@ typedef enum {
typedef int (*putchar_like_t)(int ch); typedef int (*putchar_like_t)(int ch);
#ifdef CONFIG_LOG_SET_LEVEL
/**
* @brief Set log level for given tag
*
* If logging for given component has already been enabled, changes previous setting.
*
* Note that this function can not raise log level above the level set using
* CONFIG_LOG_DEFAULT_LEVEL setting in menuconfig.
*
* To raise log level above the default one for a given file, define
* LOG_LOCAL_LEVEL to one of the ESP_LOG_* values, before including
* esp_log.h in this file.
*
* @param tag Tag of the log entries to enable. Must be a non-NULL zero terminated string.
* Value "*" resets log level for all tags to the given value.
*
* @param level Selects log level to enable. Only logs at this and lower verbosity
* levels will be shown.
*/
void esp_log_level_set(const char* tag, esp_log_level_t level);
#else
#define esp_log_level_set(tag, level)
#endif /* CONFIG_LOG_SET_LEVEL */
/** /**
* @brief Set function used to output log entries * @brief Set function used to output log entries
* *

View File

@ -16,6 +16,9 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <time.h> #include <time.h>
#include <stdbool.h>
#include <string.h>
#include <sys/queue.h>
#include <sys/lock.h> #include <sys/lock.h>
#include "esp_libc.h" #include "esp_libc.h"
@ -55,9 +58,126 @@ static uint32_t IRAM_ATTR esp_log_early_timestamp()
} }
#ifndef BOOTLOADER_BUILD #ifndef BOOTLOADER_BUILD
#ifdef CONFIG_LOG_SET_LEVEL
#define GLOBAL_TAG "*"
typedef struct uncached_tag_entry_{
SLIST_ENTRY(uncached_tag_entry_) entries;
uint8_t level; // esp_log_level_t as uint8_t
char tag[0]; // beginning of a zero-terminated string
} uncached_tag_entry_t;
static esp_log_level_t s_global_tag_level = ESP_LOG_VERBOSE;
static SLIST_HEAD(log_tags_head , uncached_tag_entry_) s_log_uncached_tags = SLIST_HEAD_INITIALIZER(s_log_uncached_tags);
static uncached_tag_entry_t *s_uncached_tag_entry_prev;
#endif /* CONFIG_LOG_SET_LEVEL */
static _lock_t s_lock; static _lock_t s_lock;
static putchar_like_t s_putchar_func = &putchar; static putchar_like_t s_putchar_func = &putchar;
#ifdef CONFIG_LOG_SET_LEVEL
/**
* @brief get entry by inputting tag
*/
static bool esp_log_get_tag_entry(const char *tag, uncached_tag_entry_t **entry)
{
uncached_tag_entry_t *it = NULL;
SLIST_FOREACH(it, &s_log_uncached_tags, entries) {
if (!strcmp(it->tag, tag)) {
//one tag in the linked list match, update the level
*entry = it;
//quit with it != NULL
return true;
}
}
return false;
}
static void clear_log_level_list(void)
{
uncached_tag_entry_t *it = NULL;
SLIST_FOREACH(it, &s_log_uncached_tags, entries) {
SLIST_REMOVE(&s_log_uncached_tags, it, uncached_tag_entry_, entries);
free(it);
}
}
/**
* @brief get level by inputting tag
*/
static esp_log_level_t esp_log_get_level(const char *tag)
{
esp_log_level_t out_level;
uncached_tag_entry_t *entry;
_lock_acquire_recursive(&s_lock);
if (s_uncached_tag_entry_prev && !strcmp(s_uncached_tag_entry_prev->tag, tag)) {
out_level = (esp_log_level_t)s_uncached_tag_entry_prev->level;
goto exit;
}
if (esp_log_get_tag_entry(tag, &entry) == true) {
out_level = (esp_log_level_t)entry->level;
s_uncached_tag_entry_prev = entry;
goto exit;
} else
out_level = s_global_tag_level;
exit:
_lock_release_recursive(&s_lock);
return out_level;
}
/**
* @brief check if system should output data
*/
static inline bool should_output(esp_log_level_t user_level, esp_log_level_t system_level)
{
return user_level <= system_level;
}
/**
* @brief set log level for given tag
*/
void esp_log_level_set(const char *tag, esp_log_level_t level)
{
size_t bytes;
uncached_tag_entry_t *entry, *new_entry;
_lock_acquire_recursive(&s_lock);
if (!strcmp(tag, GLOBAL_TAG)) {
s_global_tag_level = level;
clear_log_level_list();
goto exit;
}
if (esp_log_get_tag_entry(tag, &entry) == true) {
entry->level = level;
goto exit;
}
bytes = strlen(tag) + 1;
new_entry = malloc(sizeof(uncached_tag_entry_t) + bytes);
if (!new_entry)
goto exit;
new_entry->level = level;
memcpy(new_entry->tag, tag, bytes);
SLIST_INSERT_HEAD(&s_log_uncached_tags, new_entry, entries);
exit:
_lock_release_recursive(&s_lock);
}
#endif /* CONFIG_LOG_SET_LEVEL */
static int esp_log_write_str(const char *s) static int esp_log_write_str(const char *s)
{ {
int ret; int ret;
@ -114,9 +234,15 @@ void esp_log_write(esp_log_level_t level, const char *tag, const char *fmt, ...
int ret; int ret;
va_list va; va_list va;
char *pbuf; char *pbuf;
char prefix = level >= ESP_LOG_MAX ? 'N' : s_log_prefix[level]; char prefix;
_lock_acquire_recursive(&s_lock);
#ifdef CONFIG_LOG_SET_LEVEL
if (!should_output(level, esp_log_get_level(tag)))
goto exit;
#endif
_lock_acquire(&s_lock);
#ifdef CONFIG_LOG_COLORS #ifdef CONFIG_LOG_COLORS
static char buf[16]; static char buf[16];
uint32_t color = level >= ESP_LOG_MAX ? 0 : s_log_color[level]; uint32_t color = level >= ESP_LOG_MAX ? 0 : s_log_color[level];
@ -128,7 +254,7 @@ void esp_log_write(esp_log_level_t level, const char *tag, const char *fmt, ...
goto exit; goto exit;
} }
#endif #endif
prefix = level >= ESP_LOG_MAX ? 'N' : s_log_prefix[level];
ret = asprintf(&pbuf, "%c (%d) %s: ", prefix, esp_log_timestamp(), tag); ret = asprintf(&pbuf, "%c (%d) %s: ", prefix, esp_log_timestamp(), tag);
if (ret < 0) if (ret < 0)
goto out; goto out;
@ -159,7 +285,7 @@ out:
s_putchar_func('\n'); s_putchar_func('\n');
exit: exit:
_lock_release(&s_lock); _lock_release_recursive(&s_lock);
} }
/** /**
@ -169,10 +295,10 @@ putchar_like_t esp_log_set_putchar(putchar_like_t func)
{ {
putchar_like_t tmp; putchar_like_t tmp;
_lock_acquire(&s_lock); _lock_acquire_recursive(&s_lock);
tmp = s_putchar_func; tmp = s_putchar_func;
s_putchar_func = func; s_putchar_func = func;
_lock_release(&s_lock); _lock_release_recursive(&s_lock);
return tmp; return tmp;
} }

View File

@ -0,0 +1,5 @@
#
#Component Makefile
#
COMPONENT_ADD_LDFLAGS = -Wl,--whole-archive -l$(COMPONENT_NAME) -Wl,--no-whole-archive

View File

@ -0,0 +1,64 @@
// Copyright 2018-2019 Espressif Systems (Shanghai) PTE LTD
//
// Licensed 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 <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sys/param.h>
#include <unity.h>
#include <esp_spi_flash.h>
#include "esp_attr.h"
#include "esp_log.h"
TEST_CASE("Test set tag level", "[log]")
{
esp_log_level_t level;
size_t tag_off;
const char *TAG[] = {"TAG-0", "TAG-1", "TAG-2", "TAG-3"};
const char tag_max = sizeof(TAG) / sizeof(TAG[0]);
esp_log_level_set("*", ESP_LOG_NONE);
for (tag_off = 0; tag_off < tag_max; tag_off++) {
esp_log_level_set(TAG[tag_off], ESP_LOG_MAX);
}
for (level = ESP_LOG_VERBOSE; level > ESP_LOG_NONE; level--) {
for (tag_off = 0; tag_off < tag_max; tag_off++) {
ESP_LOGE(TAG[tag_off], "Test TAG ERROR with level %d", level);
ESP_LOGW(TAG[tag_off], "Test TAG WARN with level %d", level);
ESP_LOGI(TAG[tag_off], "Test TAG INFO with level %d", level);
ESP_LOGD(TAG[tag_off], "Test TAG DEBUG with level %d", level);
ESP_LOGV(TAG[tag_off], "Test TAG VERBOSE with level %d", level);
esp_log_level_set(TAG[tag_off], level - 1);
}
}
printf("\n\n");
esp_log_level_set("*", ESP_LOG_MAX);
level = ESP_LOG_MAX;
for (tag_off = 0; tag_off < tag_max; tag_off++) {
ESP_LOGE(TAG[tag_off], "Test TAG ERROR with global level %d", level);
ESP_LOGW(TAG[tag_off], "Test TAG WARN with global level %d", level);
ESP_LOGI(TAG[tag_off], "Test TAG INFO with global level %d", level);
ESP_LOGD(TAG[tag_off], "Test TAG DEBUG with global level %d", level);
ESP_LOGV(TAG[tag_off], "Test TAG VERBOSE with global level %d", level);
}
}