Library for serial logging via macros.

A simple library for logging to a USB serial connection using macros.

Example usage

#include "mbed.h"
#define LOG_LEVEL_INFO
#include "logger.h"

SerialLogger logger;

int main() {
    logger.start();
    INFO("Logging at info level.");
}

Log levels

This library supports five log levels: TRACE, DEBUG, INFO, WARN and ERROR.

Log levels are hierarchical (in the order given above) meaning that when a given level is enabled all levels below are enabled. For example:

#define LOG_LEVEL_TRACE

Will enable all levels since trace is at the top of the hierarchy, where as:

#define LOG_LEVEL_WARN

Will enable from WARN down (WARN and ERROR). If no log level is defined then no logging is performed at all and the logging code can remain in place with no impact on performance. This hierarchy means that the any performance hit of writing data down a serial line can be reduced to differing levels during development and removed entirely in production by not specifying a log level.

logger.h

Committer:
ollie8
Date:
2015-08-18
Revision:
2:aa7033f339e1
Parent:
1:2e4615f18293

File content as of revision 2:aa7033f339e1:

#ifndef __LOGGER_H__
#define __LOGGER_H__

#include "mbed.h"

#if defined LOG_LEVEL_TRACE
#define TRACE(x, ...) std::printf("[TRACE: %s:%d]"x"\r\n", __FILE__, __LINE__, ##__VA_ARGS__);
#define DEBUG(x, ...) std::printf("[DEBUG: %s:%d]"x"\r\n", __FILE__, __LINE__, ##__VA_ARGS__);
#define INFO(x, ...) std::printf("[INFO: %s:%d]"x"\r\n", __FILE__, __LINE__, ##__VA_ARGS__);
#define WARN(x, ...) std::printf("[WARN: %s:%d]"x"\r\n", __FILE__, __LINE__, ##__VA_ARGS__);
#define ERROR(x, ...) std::printf("[ERROR: %s:%d]"x"\r\n", __FILE__, __LINE__, ##__VA_ARGS__);

#elif defined LOG_LEVEL_DEBUG
#define TRACE(x, ...)
#define DEBUG(x, ...) std::printf("[DEBUG: %s:%d]"x"\r\n", __FILE__, __LINE__, ##__VA_ARGS__);
#define INFO(x, ...) std::printf("[INFO: %s:%d]"x"\r\n", __FILE__, __LINE__, ##__VA_ARGS__);
#define WARN(x, ...) std::printf("[WARN: %s:%d]"x"\r\n", __FILE__, __LINE__, ##__VA_ARGS__);
#define ERROR(x, ...) std::printf("[ERROR: %s:%d]"x"\r\n", __FILE__, __LINE__, ##__VA_ARGS__);

#elif defined LOG_LEVEL_INFO
#define TRACE(x, ...)
#define DEBUG(x, ...)
#define INFO(x, ...) std::printf("[INFO: %s:%d]"x"\r\n", __FILE__, __LINE__, ##__VA_ARGS__);
#define WARN(x, ...) std::printf("[WARN: %s:%d]"x"\r\n", __FILE__, __LINE__, ##__VA_ARGS__);
#define ERROR(x, ...) std::printf("[ERROR: %s:%d]"x"\r\n", __FILE__, __LINE__, ##__VA_ARGS__);

#elif defined LOG_LEVEL_WARN
#define TRACE(x, ...)
#define DEBUG(x, ...)
#define INFO(x, ...)
#define WARN(x, ...) std::printf("[WARN: %s:%d]"x"\r\n", __FILE__, __LINE__, ##__VA_ARGS__);
#define ERROR(x, ...) std::printf("[ERROR: %s:%d]"x"\r\n", __FILE__, __LINE__, ##__VA_ARGS__);

#elif defined LOG_LEVEL_ERROR
#define TRACE(x, ...)
#define DEBUG(x, ...)
#define INFO(x, ...)
#define WARN(x, ...)
#define ERROR(x, ...) std::printf("[ERROR: %s:%d]"x"\r\n", __FILE__, __LINE__, ##__VA_ARGS__);

#else
#define TRACE(x, ...)
#define DEBUG(x, ...)
#define INFO(x, ...)
#define WARN(x, ...)
#define ERROR(x, ...)
#endif

class SerialLogger {

    private: 
        Serial *pc;
    
    public:
        SerialLogger() {
            pc = new Serial(USBTX, USBRX);    
        }
        
        void start(int baudRate = 460800) {
            pc->baud(baudRate);
        }
};


#endif