Commit 0ea3cdd2 authored by Deomid Ryabkov's avatar Deomid Ryabkov Committed by Cesanta Bot

Implement sending stdout and stderr to MQTT topics

Configured via `debug.std{out,err}_topic`

PUBLISHED_FROM=66c581c38c5039aa9ecc406f8aac0b7e3c6541fc
parent ddef0654
...@@ -40,22 +40,22 @@ void cs_log_set_level(enum cs_log_level level); ...@@ -40,22 +40,22 @@ void cs_log_set_level(enum cs_log_level level);
void cs_log_set_file(FILE *file); void cs_log_set_file(FILE *file);
extern enum cs_log_level cs_log_level; extern enum cs_log_level cs_log_threshold;
void cs_log_print_prefix(const char *func); void cs_log_print_prefix(const char *func);
void cs_log_printf(const char *fmt, ...); void cs_log_printf(const char *fmt, ...);
#define LOG(l, x) \ #define LOG(l, x) \
if (cs_log_level >= l) { \ if (cs_log_threshold >= l) { \
cs_log_print_prefix(__func__); \ cs_log_print_prefix(__func__); \
cs_log_printf x; \ cs_log_printf x; \
} }
#ifndef CS_NDEBUG #ifndef CS_NDEBUG
#define DBG(x) \ #define DBG(x) \
if (cs_log_level >= LL_VERBOSE_DEBUG) { \ if (cs_log_threshold >= LL_VERBOSE_DEBUG) { \
cs_log_print_prefix(__func__); \ cs_log_print_prefix(__func__); \
cs_log_printf x; \ cs_log_printf x; \
} }
#else /* NDEBUG */ #else /* NDEBUG */
......
...@@ -228,26 +228,26 @@ void cs_log_set_level(enum cs_log_level level); ...@@ -228,26 +228,26 @@ void cs_log_set_level(enum cs_log_level level);
#if CS_ENABLE_STDIO #if CS_ENABLE_STDIO
void cs_log_set_file(FILE *file); void cs_log_set_file(FILE *file);
extern enum cs_log_level cs_log_level; extern enum cs_log_level cs_log_threshold;
void cs_log_print_prefix(const char *func); void cs_log_print_prefix(enum cs_log_level level, const char *func);
void cs_log_printf(const char *fmt, ...); void cs_log_printf(const char *fmt, ...);
#define LOG(l, x) \ #define LOG(l, x) \
do { \ do { \
if (cs_log_level >= l) { \ if (cs_log_threshold >= l) { \
cs_log_print_prefix(__func__); \ cs_log_print_prefix(l, __func__); \
cs_log_printf x; \ cs_log_printf x; \
} \ } \
} while (0) } while (0)
#ifndef CS_NDEBUG #ifndef CS_NDEBUG
#define DBG(x) \ #define DBG(x) \
do { \ do { \
if (cs_log_level >= LL_VERBOSE_DEBUG) { \ if (cs_log_threshold >= LL_VERBOSE_DEBUG) { \
cs_log_print_prefix(__func__); \ cs_log_print_prefix(LL_VERBOSE_DEBUG, __func__); \
cs_log_printf x; \ cs_log_printf x; \
} \ } \
} while (0) } while (0)
#else /* NDEBUG */ #else /* NDEBUG */
...@@ -284,7 +284,7 @@ void cs_log_printf(const char *fmt, ...); ...@@ -284,7 +284,7 @@ void cs_log_printf(const char *fmt, ...);
/* Amalgamated: #include "common/cs_time.h" */ /* Amalgamated: #include "common/cs_time.h" */
enum cs_log_level cs_log_level WEAK = enum cs_log_level cs_log_threshold WEAK =
#if CS_ENABLE_DEBUG #if CS_ENABLE_DEBUG
LL_VERBOSE_DEBUG; LL_VERBOSE_DEBUG;
#else #else
...@@ -299,12 +299,15 @@ FILE *cs_log_file WEAK = NULL; ...@@ -299,12 +299,15 @@ FILE *cs_log_file WEAK = NULL;
double cs_log_ts WEAK; double cs_log_ts WEAK;
#endif #endif
void cs_log_print_prefix(const char *func) WEAK; enum cs_log_level cs_log_cur_msg_level WEAK = LL_NONE;
void cs_log_print_prefix(const char *func) {
void cs_log_print_prefix(enum cs_log_level level, const char *func) WEAK;
void cs_log_print_prefix(enum cs_log_level level, const char *func) {
char prefix[21]; char prefix[21];
strncpy(prefix, func, 20); strncpy(prefix, func, 20);
prefix[20] = '\0'; prefix[20] = '\0';
if (cs_log_file == NULL) cs_log_file = stderr; if (cs_log_file == NULL) cs_log_file = stderr;
cs_log_cur_msg_level = level;
fprintf(cs_log_file, "%-20s ", prefix); fprintf(cs_log_file, "%-20s ", prefix);
#if CS_LOG_ENABLE_TS_DIFF #if CS_LOG_ENABLE_TS_DIFF
{ {
...@@ -323,6 +326,7 @@ void cs_log_printf(const char *fmt, ...) { ...@@ -323,6 +326,7 @@ void cs_log_printf(const char *fmt, ...) {
va_end(ap); va_end(ap);
fputc('\n', cs_log_file); fputc('\n', cs_log_file);
fflush(cs_log_file); fflush(cs_log_file);
cs_log_cur_msg_level = LL_NONE;
} }
void cs_log_set_file(FILE *file) WEAK; void cs_log_set_file(FILE *file) WEAK;
...@@ -334,7 +338,7 @@ void cs_log_set_file(FILE *file) { ...@@ -334,7 +338,7 @@ void cs_log_set_file(FILE *file) {
void cs_log_set_level(enum cs_log_level level) WEAK; void cs_log_set_level(enum cs_log_level level) WEAK;
void cs_log_set_level(enum cs_log_level level) { void cs_log_set_level(enum cs_log_level level) {
cs_log_level = level; cs_log_threshold = level;
#if CS_LOG_ENABLE_TS_DIFF && CS_ENABLE_STDIO #if CS_LOG_ENABLE_TS_DIFF && CS_ENABLE_STDIO
cs_log_ts = cs_time(); cs_log_ts = cs_time();
#endif #endif
...@@ -12797,7 +12801,7 @@ void fs_slfs_set_new_file_size(const char *name, size_t size) { ...@@ -12797,7 +12801,7 @@ void fs_slfs_set_new_file_size(const char *name, size_t size) {
#define SPIFFS_FD_BASE 10 #define SPIFFS_FD_BASE 10
#define SLFS_FD_BASE 100 #define SLFS_FD_BASE 100
#ifndef MG_UART_CHAR_PUT #if !defined(MG_UART_CHAR_PUT) && !defined(MG_UART_WRITE)
#if CS_PLATFORM == CS_P_CC3200 #if CS_PLATFORM == CS_P_CC3200
#include <inc/hw_types.h> #include <inc/hw_types.h>
#include <inc/hw_memmap.h> #include <inc/hw_memmap.h>
...@@ -12806,7 +12810,7 @@ void fs_slfs_set_new_file_size(const char *name, size_t size) { ...@@ -12806,7 +12810,7 @@ void fs_slfs_set_new_file_size(const char *name, size_t size) {
#include <driverlib/uart.h> #include <driverlib/uart.h>
#define MG_UART_CHAR_PUT(fd, c) MAP_UARTCharPut(UARTA0_BASE, c); #define MG_UART_CHAR_PUT(fd, c) MAP_UARTCharPut(UARTA0_BASE, c);
#else #else
#define MG_UART_CHAR_PUT(fd, c) #define MG_UART_WRITE(fd, buf, len)
#endif /* CS_PLATFORM == CS_P_CC3200 */ #endif /* CS_PLATFORM == CS_P_CC3200 */
#endif /* !MG_UART_CHAR_PUT */ #endif /* !MG_UART_CHAR_PUT */
...@@ -13035,7 +13039,6 @@ int write(int fd, const char *buf, unsigned count) { ...@@ -13035,7 +13039,6 @@ int write(int fd, const char *buf, unsigned count) {
ssize_t _write(int fd, const void *buf, size_t count) { ssize_t _write(int fd, const void *buf, size_t count) {
#endif #endif
int r = -1; int r = -1;
size_t i = 0;
switch (fd_type(fd)) { switch (fd_type(fd)) {
case FD_INVALID: case FD_INVALID:
r = set_errno(EBADF); r = set_errno(EBADF);
...@@ -13045,11 +13048,18 @@ ssize_t _write(int fd, const void *buf, size_t count) { ...@@ -13045,11 +13048,18 @@ ssize_t _write(int fd, const void *buf, size_t count) {
r = set_errno(EACCES); r = set_errno(EACCES);
break; break;
} }
for (i = 0; i < count; i++) { #ifdef MG_UART_WRITE
const char c = ((const char *) buf)[i]; MG_UART_WRITE(fd, buf, count);
if (c == '\n') MG_UART_CHAR_PUT(fd, '\r'); #elif defined(MG_UART_CHAR_PUT)
MG_UART_CHAR_PUT(fd, c); {
size_t i;
for (i = 0; i < count; i++) {
const char c = ((const char *) buf)[i];
if (c == '\n') MG_UART_CHAR_PUT(fd, '\r');
MG_UART_CHAR_PUT(fd, c);
}
} }
#endif
r = count; r = count;
break; break;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment