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);
    }
}
Revision:
4:9360fdf3a818
Parent:
3:e1ed8ae7691e
Child:
5:f232b826e1f2
--- a/logger.cpp	Tue Dec 30 20:50:31 2014 +0000
+++ b/logger.cpp	Wed Dec 31 22:14:48 2014 +0000
@@ -60,6 +60,11 @@
  * @retval -ERR linux errno error message
  */
 int32_t log_msg(uint8_t plc, uint8_t lvl, char *msg, uint32_t length) {
+    if((msg == NULL) || (length == 0))
+    {
+        return -EINVAL;    
+    }
+    
     char send_message[length+5]; // add as much as you need to get all chars in (dont forget the \n on end)
 
     switch(lvl) {
@@ -110,8 +115,6 @@
     return 1;
 }
 
-/// stash_buffer size
-static char stash_buffer[STASH_BUFFER_LEN];
 /**
  * Log_stash is meant to stash logs until they are ready to be sent, using log_pop function
  * Remember that this function can return also buffer full as result, which means you have to keep
@@ -119,16 +122,24 @@
  * @param[in] lvl Debug level (ERROR,WARNING,DEBUG)
  * @param[in] msg pointer to message
  * @param[in] length length of message
+ * @param[in] *stash_buffer Place where stashed messages were saved
+ * @param[in] stash_buffer_len how many characters can be saved on stashed message buffer
  *
  * @retval 1 success
  * @retval -ERR linux errno error message - also includes buffer full!
  *
  * @warning STASH_BUFFER_LEN sets the size of stash buffer - in case you are lacking space consider making the buffer smaller
  */
-int32_t log_stash(uint8_t lvl, char *msg, uint32_t length) {
+int32_t log_stash(uint8_t lvl, char *msg, uint32_t length, char *stash_buffer,size_t stash_buffer_len) {
+    if((msg == NULL) || (length == 0) || (stash_buffer == NULL) || (stash_buffer_len == 0))
+    {
+        return -EINVAL;    
+    }
+    
     char stash_buffer_tmp[length+8];
+    
     // check if buffer is full and set starting point
-    if(strlen(stash_buffer)>STASH_BUFFER_LEN) {
+    if((strlen(stash_buffer) > (stash_buffer_len-5)) || (stash_buffer_len < length)) {
         return -EFBIG;
     }
 
@@ -159,12 +170,19 @@
 /**
  * Log_pop function sends everything log_stash stashed
  * @param[in] plc Place to where you pop stashed messages
+ * @param[in] *stash_buffer Place where stashed messages were saved
+ * @param[in] stash_buffer_len how many characters can be saved on stashed message buffer
  *
  * @retval 1 for success
  * @retval 2 for partial printout - did not send everything. You need to recall the function within timer so that you do not flood the serial port. So far it is only on BTSerial.
  * @retval -ERR errno.h error message
  */
-int32_t log_pop(uint8_t plc) {
+int32_t log_pop(uint8_t plc,char *stash_buffer,size_t stash_buffer_len) {
+    if((stash_buffer == NULL) || (stash_buffer_len == 0))
+    {
+        return -EINVAL;    
+    }
+    
     char *tmp_needle;
     size_t tmp_len;
     
@@ -192,7 +210,7 @@
                     // we pray for complete printf definition
                     BTSerial.printf("%.*s", tmp_len,stash_buffer);
                     // move whole array to remove out the line needle is increased before tmp_len to include the endline string
-                    memmove(stash_buffer,tmp_needle+strlen(endline),STASH_BUFFER_LEN-tmp_len);
+                    memmove(stash_buffer,tmp_needle+strlen(endline),stash_buffer_len-tmp_len);
                     return 2;
                 }
             } else {