Skip to content

Commit

Permalink
Improved ESP Log Backend with cloud logging option default to 0
Browse files Browse the repository at this point in the history
Signed-off-by: Rashmi Vaidya <[email protected]>
  • Loading branch information
rashmivaidya72 committed May 8, 2024
1 parent 6e6c7c2 commit 1f87b8e
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 58 deletions.
3 changes: 3 additions & 0 deletions examples/esp_idf/hello/sdkconfig.defaults
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ CONFIG_COMPILER_OPTIMIZATION_SIZE=y
CONFIG_GOLIOTH_AUTO_LOG_TO_CLOUD=1
CONFIG_GOLIOTH_COAP_REQUEST_QUEUE_MAX_ITEMS=20

# Uncomment to automatically upload ESP_LOGX statements to Golioth Logs service
# CONFIG_GOLIOTH_ESPLOG_AUTO_LOG_TO_CLOUD=1

# To enable hardcode credentials, set to y
CONFIG_GOLIOTH_SAMPLE_HARDCODED_CREDENTIALS=n

Expand Down
7 changes: 7 additions & 0 deletions port/esp_idf/components/golioth_sdk/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ menu "Golioth SDK Configuration"

rsource '../../../../src/Kconfig'

config GOLIOTH_ESPLOG_AUTO_LOG_TO_CLOUD
int "Option to send logs to cloud"
default 0
help
If set to 1, ESP_LOGX statements will automatically upload to
Golioth cloud

config GOLIOTH_ESPLOG_MAX_BUFF_SIZE
int "Maximum log buffer size allowed for ESP log backend"
default 128
Expand Down
56 changes: 25 additions & 31 deletions port/esp_idf/golioth_log_esp_idf.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ static struct golioth_client *_client = NULL;

static int golioth_log_espidf(const char *format, va_list args)
{

const char *new_format = format;
const char *tag_arg = NULL;

if (!CONFIG_GOLIOTH_AUTO_LOG_TO_CLOUD)
{
return -1;
Expand All @@ -31,52 +27,50 @@ static int golioth_log_espidf(const char *format, va_list args)
return -1;
}

va_list new_args;
va_copy(new_args, args);

// Since the format contains time (%lu) and tag (%s) values that need not
// be sent to the cloud like so : "[0;32mI (%lu) %s: Actual Log %d [0m",
// we remove the string upto colon : and update the args list
// Also, the 7th character in the string tells the log level as
// The 7th character in the format tells the log level as
// E = Error, W = Warning, I = info, D = Debug, V = Verbose
char log_level = format[7];
const char *colon_pos = strchr(format, ':');
if (colon_pos != NULL)
{
new_format = colon_pos + 2; // Skip ':' and space

// Move va_list to correspond to the new format
va_arg(new_args, unsigned long); // Skip %lu
tag_arg = va_arg(new_args, const char *); // Extract tag value
}

// Temporarily buffer to store the message
// Temporary buffer to store the message
char msg_buffer[CONFIG_GOLIOTH_ESPLOG_MAX_BUFF_SIZE] = {0};
vsnprintf(msg_buffer, CONFIG_GOLIOTH_ESPLOG_MAX_BUFF_SIZE, format, args);
printf("%s", msg_buffer);

vsnprintf(msg_buffer, CONFIG_GOLIOTH_ESPLOG_MAX_BUFF_SIZE, new_format, new_args);

// The formatted string looks like this: "[0;32mI (%lu) %s: Actual Log %d [0m"
// The start and end characters convey formatting and color information
// This information should not be sent to the cloud.
// Insert a null char 5 places behind to remove ESC[0m at the end of the string
msg_buffer[strlen(msg_buffer) - 5] = '\0';
// Here strlen(msg_buffer) - 5 will not go out of bounds because the format will
// always have at least 5 characters, even with an empty string
msg_buffer[strlen(msg_buffer) - 5] = '\0';

printf("%s\n", msg_buffer);
// Find ) and : characters in the string for extracting tag
char tag_str[64] = {0};
const char *tag = strchr(msg_buffer, ')');
const char *msg_str = strchr(msg_buffer, ':');
if (tag != NULL && msg_str != NULL)
{
tag = tag + 1; // Skip space after )
strncpy(tag_str, tag, msg_str - tag);
// Ignore everything before ':'
msg_str = msg_str + 2; // Skip ':' and space
}

// Log to Golioth asynchronously.
switch (log_level)
{
case 'E':
golioth_log_error_async(_client, tag_arg, msg_buffer, NULL, NULL);
golioth_log_error_async(_client, tag_str, msg_str, NULL, NULL);
break;
case 'W':
golioth_log_warn_async(_client, tag_arg, msg_buffer, NULL, NULL);
golioth_log_warn_async(_client, tag_str, msg_str, NULL, NULL);
break;
case 'I':
golioth_log_info_async(_client, tag_arg, msg_buffer, NULL, NULL);
golioth_log_info_async(_client, tag_str, msg_str, NULL, NULL);
break;
case 'V': // fallthrough
case 'D':
golioth_log_debug_async(_client, tag_arg, msg_buffer, NULL, NULL);
golioth_log_debug_async(_client, tag_str, msg_str, NULL, NULL);
break;
default:
break;
Expand All @@ -98,15 +92,15 @@ vprintf_like_t golioth_log_reset_esp_vprintf(vprintf_like_t original_log_func)

void golioth_sys_client_connected(void *client)
{
if (CONFIG_GOLIOTH_AUTO_LOG_TO_CLOUD == 1)
if (CONFIG_GOLIOTH_ESPLOG_AUTO_LOG_TO_CLOUD == 1)
{
original_log_func = golioth_log_set_esp_vprintf(client);
}
}

void golioth_sys_client_disconnected(void *client)
{
if (CONFIG_GOLIOTH_AUTO_LOG_TO_CLOUD == 1)
if (CONFIG_GOLIOTH_ESPLOG_AUTO_LOG_TO_CLOUD == 1)
{
golioth_log_reset_esp_vprintf(original_log_func);
}
Expand Down
59 changes: 32 additions & 27 deletions port/esp_idf/golioth_port_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,31 +34,36 @@
} \
} while (0)

#define GLTH_LOGX(COLOR, LEVEL, LEVEL_STR, TAG, ...) \
do \
{ \
if ((LEVEL) <= golioth_debug_get_log_level()) \
{ \
switch (LEVEL) \
{ \
case GOLIOTH_DEBUG_LOG_LEVEL_ERROR: \
ESP_LOGE(TAG, __VA_ARGS__); \
break; \
case GOLIOTH_DEBUG_LOG_LEVEL_WARN: \
ESP_LOGW(TAG, __VA_ARGS__); \
break; \
case GOLIOTH_DEBUG_LOG_LEVEL_INFO: \
ESP_LOGI(TAG, __VA_ARGS__); \
break; \
case GOLIOTH_DEBUG_LOG_LEVEL_DEBUG: \
ESP_LOGD(TAG, __VA_ARGS__); \
break; \
case GOLIOTH_DEBUG_LOG_LEVEL_VERBOSE: \
ESP_LOGV(TAG, __VA_ARGS__); \
break; \
case GOLIOTH_DEBUG_LOG_LEVEL_NONE: \
default: \
break; \
} \
} \
#define GLTH_LOGX(COLOR, LEVEL, LEVEL_STR, TAG, ...) \
do \
{ \
if ((LEVEL) <= golioth_debug_get_log_level()) \
{ \
switch (LEVEL) \
{ \
case GOLIOTH_DEBUG_LOG_LEVEL_ERROR: \
ESP_LOGE(TAG, __VA_ARGS__); \
break; \
case GOLIOTH_DEBUG_LOG_LEVEL_WARN: \
ESP_LOGW(TAG, __VA_ARGS__); \
break; \
case GOLIOTH_DEBUG_LOG_LEVEL_INFO: \
ESP_LOGI(TAG, __VA_ARGS__); \
break; \
case GOLIOTH_DEBUG_LOG_LEVEL_DEBUG: \
ESP_LOGD(TAG, __VA_ARGS__); \
break; \
case GOLIOTH_DEBUG_LOG_LEVEL_VERBOSE: \
ESP_LOGV(TAG, __VA_ARGS__); \
break; \
case GOLIOTH_DEBUG_LOG_LEVEL_NONE: \
default: \
break; \
} \
if (!CONFIG_GOLIOTH_ESPLOG_AUTO_LOG_TO_CLOUD) \
{ \
uint64_t now_ms = golioth_sys_now_ms(); \
golioth_debug_printf(now_ms, LEVEL, TAG, __VA_ARGS__); \
} \
} \
} while (0)

0 comments on commit 1f87b8e

Please sign in to comment.