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.

LOG()

#define LOG(level, args)
#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. Usage example:

MG_INFO(("Hello %s!", "world"));  // Output "Hello, world"

mg_log_set()

void mg_log_set(int level);

Set Mongoose logging level.

Parameters:

  • level - log level, can be one of the following values:
    • MG_LL_NONE - Disable logging
    • MG_LL_ERROR - Log errors only
    • MG_LL_INFO - Log errors and info messages
    • MG_LL_DEBUG - Log errors, info and debug messages
    • MG_LL_VERBOSE - Log everything, and more

Return value: None

Usage example:

mg_log_set(MG_LL_INFO);                  // Set log level to info

mg_hexdump()

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

Log a hex dump of binary data buf, len.

Parameters:

  • buf - Data pointer
  • len - Data length

Return value: none

Usage example:

mg_hexdump(c->recv.buf, c->recv.len);  // Hex dump incoming data

mg_log_set_fn()

void mg_log_set_fn(mg_pfn_t logfunc, void *param);

Redirect logs to a custom function. Parameters:

  • logfunc - a pointer to a function that logs a single character
  • param - a parameter for a logging function

Usage example: redirecting logs to syslog.

static void mylog(char ch, void *param) {
  static char buf[256];
  static size_t len;
  buf[len++] = ch;
  if (ch == '\n' || len >= sizeof(buf)) {
    syslog(LOG_INFO, "%.*s", (int) len, buf); // Send logs
    len = 0;
  }
}
...
mg_log_set_fn(mylog, NULL);