Logger library enables you to use unified interface to send out or receive data using one function with different parameters. You leave buffer overflows to this library as it handles it all.

Logger library howto guide.

Below is a simple program that outlines the usage of the logger library, The whole function was head-written so there might be some errors, but it is more for a live representation of usage than anything else. If someone would write a nice example program feel free to link it here.

simple usage of logger library

#include "mbed.h"
#include "logger.h"
#include "errno.h"

Ticker printout;
/**
 * \def STASH_BUFFER_LEN
 * size of stash buffer that can be used to store logged data
 */
#define STASH_BUFFER_LEN 70000
/// stash_buffer size
static char stash_buffer[STASH_BUFFER_LEN];

/**
 * Log_pop_periodic is periodically printing the messages until the buffer is empty. 
 * Once the buffer is empty it detaches and waits for another command.
*/
void log_pop_periodic(void)
{
    if( log_pop(L_BTSERIAL,stash_buffer,STASH_BUFFER_LEN) == 1 )
    {
        printout.detach();
    }
}

static int i=0;
static char read_buffer[20];
/**
 * read data from BT serial
*/
void read_from_BT()
{

    if (BTSerial.readable()) {
        read_buffer[i++] = BTSerial.getc();
        // if we read the desired character
        if (read_buffer[i-1] == 'p')
        {
            // start the printout process which will log_pop logged data
            printout.attach(&log_pop_periodic,0.5);
        }
    }    
}

int main()
{
    log_init(L_SERIAL);
    log_init(L_BTSERIAL);
    BTSerial.attach(&read_from_BT);

    // while loop to fill in loads of stinky data
    while (1) {
            // read something, then stash it
            log_stash(L_INFO,print_buffer,strlen(print_buffer),stash_buffer,STASH_BUFFER_LEN);
    }
}

logger.h

Committer:
Letme
Date:
2014-12-31
Revision:
5:f232b826e1f2
Parent:
4:9360fdf3a818

File content as of revision 5:f232b826e1f2:

#ifndef _LOGGER_H_
#define _LOGGER_H_

/**
 * @addtogroup logger_lib
 * @{
 */

/**
 * Possible outputs of microcontroller
 * @todo include also output to LCD display and some other displays
 * \def L_SERIAL
 * @ingroup logger_lib
 * Output to Serial port
 * \def L_FILE
 * @ingroup logger_lib
 * Output to file
 * \def L_LED
 * Output to LED (morse code;))
 */
#define L_SERIAL    1
#define L_FILE      2
#define L_LED       3
#define L_BTSERIAL    4

/**
 * Error level indication
 * @def L_CRIT_ERROR
 * Critical error will append CE: to output
 * @def L_ERROR
 * Error will append E: to output
 * @def L_WARNING
 * Warning will append W: to output
 * @def L_DEBUG
 * Debug message will append D: to output
 * @def L_INFO
 * Information output will append I: to output
 */
#define L_CRIT_ERROR    1
#define L_ERROR         2
#define L_WARNING       3
#define L_DEBUG         4
#define L_INFO          5

extern Serial logSerial;
extern Serial BTSerial;

int32_t log_msg(uint8_t plc,uint8_t lvl, char *msg, uint32_t length);
int32_t log_init(uint8_t plc);
int32_t log_stash(uint8_t lvl, char *msg, uint32_t length, char *stash_buffer,size_t stash_buffer_len);
int32_t log_pop(uint8_t plc, char *stash_buffer,size_t stash_buffer_len);
///@}
#endif