From fb53cd37e7a4558439216af133390bed1933008b Mon Sep 17 00:00:00 2001 From: Deomid Ryabkov <rojer@cesanta.com> Date: Wed, 30 Mar 2016 13:30:39 +0100 Subject: [PATCH] Make it possible to set log file And use stdout for CC3200 demo because (1) an apparent bug which causes output sent to stderr to be printed v-e-r-y s-l-o-w-l-y (yes, it is really like that; https://e2e.ti.com/support/development_tools/code_composer_studio/f/81/t/501881) (2) in CCS it is printed in BLOOD RED, which is annoying PUBLISHED_FROM=36a86744bc8ea193e99e98670dadc7f3ab6ed53e --- examples/CC3200/main.c | 1 + mongoose.c | 27 +++++++++++++++++++-------- mongoose.h | 24 ++++++++++++------------ 3 files changed, 32 insertions(+), 20 deletions(-) diff --git a/examples/CC3200/main.c b/examples/CC3200/main.c index d67475b62..7101b8347 100644 --- a/examples/CC3200/main.c +++ b/examples/CC3200/main.c @@ -366,6 +366,7 @@ int main() { setvbuf(stdout, NULL, _IONBF, 0); setvbuf(stderr, NULL, _IONBF, 0); + cs_log_set_file(stdout); cs_log_set_level(LL_INFO); MAP_PinTypeI2C(PIN_01, PIN_MODE_1); /* SDA */ diff --git a/mongoose.c b/mongoose.c index 5de0bc682..5748f4fdd 100644 --- a/mongoose.c +++ b/mongoose.c @@ -346,41 +346,52 @@ int cs_base64_decode(const unsigned char *s, int len, char *dst) { /* Amalgamated: #include "common/cs_time.h" */ -enum cs_log_level s_cs_log_level = +enum cs_log_level cs_log_level = #ifdef CS_ENABLE_DEBUG LL_VERBOSE_DEBUG; #else LL_ERROR; #endif +#ifndef CS_DISABLE_STDIO + +FILE *cs_log_file = NULL; + #ifdef CS_LOG_TS_DIFF double cs_log_ts; #endif -#ifndef CS_DISABLE_STDIO void cs_log_printf(const char *fmt, ...) { va_list ap; #ifdef CS_LOG_TS_DIFF double now = cs_time(); - fprintf(stderr, "%7u ", (unsigned int) ((now - cs_log_ts) * 1000000)); +#endif + + if (cs_log_file == NULL) cs_log_file = stderr; +#ifdef CS_LOG_TS_DIFF + fprintf(cs_log_file, "%7u ", (unsigned int) ((now - cs_log_ts) * 1000000)); #endif va_start(ap, fmt); - vfprintf(stderr, fmt, ap); + vfprintf(cs_log_file, fmt, ap); va_end(ap); - fputc('\n', stderr); + fputc('\n', cs_log_file); #ifdef CS_LOG_TS_DIFF cs_log_ts = now; #endif - fflush(stderr); + fflush(cs_log_file); } #endif /* !CS_DISABLE_STDIO */ void cs_log_set_level(enum cs_log_level level) { - s_cs_log_level = level; -#ifdef CS_LOG_TS_DIFF + cs_log_level = level; +#if defined(CS_LOG_TS_DIFF) && !defined(CS_DISABLE_STDIO) cs_log_ts = cs_time(); #endif } + +void cs_log_set_file(FILE *file) { + cs_log_file = file; +} #ifdef MG_MODULE_LINES #line 1 "./src/../../common/cs_dirent.c" #endif diff --git a/mongoose.h b/mongoose.h index 2a827edf2..e5b39b5d1 100644 --- a/mongoose.h +++ b/mongoose.h @@ -637,29 +637,29 @@ enum cs_log_level { _LL_MAX = 5, }; -extern enum cs_log_level s_cs_log_level; void cs_log_set_level(enum cs_log_level level); #ifndef CS_DISABLE_STDIO -#ifdef CS_LOG_TS_DIFF -extern double cs_log_ts; -#endif +#include <stdio.h> + +void cs_log_set_file(FILE *file); +extern enum cs_log_level cs_log_level; void cs_log_printf(const char *fmt, ...); -#define LOG(l, x) \ - if (s_cs_log_level >= l) { \ - fprintf(stderr, "%-20s ", __func__); \ - cs_log_printf x; \ +#define LOG(l, x) \ + if (cs_log_level >= l) { \ + cs_log_printf("%-20s ", __func__); \ + cs_log_printf x; \ } #ifndef CS_NDEBUG -#define DBG(x) \ - if (s_cs_log_level >= LL_VERBOSE_DEBUG) { \ - fprintf(stderr, "%-20s ", __func__); \ - cs_log_printf x; \ +#define DBG(x) \ + if (cs_log_level >= LL_VERBOSE_DEBUG) { \ + cs_log_printf("%-20s ", __func__); \ + cs_log_printf x; \ } #else /* NDEBUG */ -- GitLab