Logging

Mongoose provides a set of functions and macros for logging. The application can use these functions for its own purposes as well as the rest of Mongoose API.

mg_log_level, mg_log_set()

extern int mg_log_level;  // Current log level, one of MG_LL_*
#define mg_log_set(level_) mg_log_level = (level_)

Set Mongoose logging level. Example: mg_log_set(MG_LL_INFO);

mg_hexdump()

void mg_hexdump(const void *buf, size_t len);

Log a hex dump of binary data buf, len.

mg_log_set_fn()

void mg_log_set_fn(mg_pfn_t fn, void *param);

Set log printer function which prints one byte. Example:

static void print_char(char ch, void *param) {
  hal_uart_write_char(param, ch);
}
...
mg_log_set_fn(print_char, USART3);

MG_ERROR(), MG_INFO(), MG_DEBUG(), MG_VERBOSE()

#define MG_ERROR(args) MG_LOG(MG_LL_ERROR, args)
#define MG_INFO(args) MG_LOG(MG_LL_INFO, args)
#define MG_DEBUG(args) MG_LOG(MG_LL_DEBUG, args)
#define MG_VERBOSE(args) MG_LOG(MG_LL_VERBOSE, args)

Logging macros. Note: the argument is exactly like for mg_snprintf(), and it should be enclosed in double parenthesis. Example:

MG_INFO(("Conn %lu, recv buf: %.*s", c->id, c->recv.len, c->recv.buf));