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);
    }
}
Committer:
Letme
Date:
Wed Dec 31 22:23:19 2014 +0000
Revision:
5:f232b826e1f2
Parent:
4:9360fdf3a818
Fixed some minor bugz and added clearing of stash buffer once whole chunk is printed to L_SERIAL. Also expanded documentation a bit.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Letme 0:272c70e2366c 1 #ifndef _LOGGER_H_
Letme 0:272c70e2366c 2 #define _LOGGER_H_
Letme 0:272c70e2366c 3
Letme 2:9d04753a3ad2 4 /**
Letme 5:f232b826e1f2 5 * @addtogroup logger_lib
Letme 5:f232b826e1f2 6 * @{
Letme 5:f232b826e1f2 7 */
Letme 5:f232b826e1f2 8
Letme 5:f232b826e1f2 9 /**
Letme 2:9d04753a3ad2 10 * Possible outputs of microcontroller
Letme 2:9d04753a3ad2 11 * @todo include also output to LCD display and some other displays
Letme 2:9d04753a3ad2 12 * \def L_SERIAL
Letme 2:9d04753a3ad2 13 * @ingroup logger_lib
Letme 2:9d04753a3ad2 14 * Output to Serial port
Letme 2:9d04753a3ad2 15 * \def L_FILE
Letme 2:9d04753a3ad2 16 * @ingroup logger_lib
Letme 2:9d04753a3ad2 17 * Output to file
Letme 2:9d04753a3ad2 18 * \def L_LED
Letme 2:9d04753a3ad2 19 * Output to LED (morse code;))
Letme 2:9d04753a3ad2 20 */
Letme 0:272c70e2366c 21 #define L_SERIAL 1
Letme 0:272c70e2366c 22 #define L_FILE 2
Letme 0:272c70e2366c 23 #define L_LED 3
Letme 3:e1ed8ae7691e 24 #define L_BTSERIAL 4
Letme 0:272c70e2366c 25
Letme 2:9d04753a3ad2 26 /**
Letme 2:9d04753a3ad2 27 * Error level indication
Letme 2:9d04753a3ad2 28 * @def L_CRIT_ERROR
Letme 2:9d04753a3ad2 29 * Critical error will append CE: to output
Letme 2:9d04753a3ad2 30 * @def L_ERROR
Letme 2:9d04753a3ad2 31 * Error will append E: to output
Letme 2:9d04753a3ad2 32 * @def L_WARNING
Letme 2:9d04753a3ad2 33 * Warning will append W: to output
Letme 2:9d04753a3ad2 34 * @def L_DEBUG
Letme 2:9d04753a3ad2 35 * Debug message will append D: to output
Letme 2:9d04753a3ad2 36 * @def L_INFO
Letme 2:9d04753a3ad2 37 * Information output will append I: to output
Letme 2:9d04753a3ad2 38 */
Letme 0:272c70e2366c 39 #define L_CRIT_ERROR 1
Letme 0:272c70e2366c 40 #define L_ERROR 2
Letme 0:272c70e2366c 41 #define L_WARNING 3
Letme 0:272c70e2366c 42 #define L_DEBUG 4
Letme 0:272c70e2366c 43 #define L_INFO 5
Letme 0:272c70e2366c 44
Letme 1:8837abc3f763 45 extern Serial logSerial;
Letme 3:e1ed8ae7691e 46 extern Serial BTSerial;
Letme 1:8837abc3f763 47
Letme 0:272c70e2366c 48 int32_t log_msg(uint8_t plc,uint8_t lvl, char *msg, uint32_t length);
Letme 0:272c70e2366c 49 int32_t log_init(uint8_t plc);
Letme 4:9360fdf3a818 50 int32_t log_stash(uint8_t lvl, char *msg, uint32_t length, char *stash_buffer,size_t stash_buffer_len);
Letme 4:9360fdf3a818 51 int32_t log_pop(uint8_t plc, char *stash_buffer,size_t stash_buffer_len);
Letme 5:f232b826e1f2 52 ///@}
Letme 0:272c70e2366c 53 #endif