Andrew Boyson / log

Dependents:   oldheating gps motorhome heating

Revision:
12:19c2d4943124
Parent:
11:aff6dd8238ef
Child:
13:fb7f40c2e446
--- a/log.cpp	Sat Dec 16 14:41:46 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,181 +0,0 @@
-#include "mbed.h"
-#include "clock.h"
-
-#define BUFFER_LENGTH 4096
-static char buffer[BUFFER_LENGTH];
-static char* pPush; //Initialised in init
-static char* pPull; //Initialised in init
-static int enable = true;
-
-static Serial uart(USBTX, USBRX);
-bool LogUart = false;
-
-static char* incrementPushPullPointer(char* p, char* buffer, int bufferLength)
-{
-    p++; //increment the pointer by one
-    if (p == buffer + bufferLength) p = buffer; //if the pointer is now beyond the end then point it back to the start
-    return p;
-}
-static void push(char c)
-{
-    //Move the pull position if about to run into it
-    char* pNext = incrementPushPullPointer(pPush, buffer, BUFFER_LENGTH);
-    if (pNext == pPull) pPull = incrementPushPullPointer(pPull, buffer, BUFFER_LENGTH);
-    
-    //Add the character at the push position
-    *pPush = c;
-    pPush = incrementPushPullPointer(pPush, buffer, BUFFER_LENGTH);
-    
-    if (LogUart) uart.putc(c);
-}
-void LogPush(char c)
-{
-    //Only add if allowed
-    if (!enable) return;
-    
-    //Work out if the character needs to be delimited
-    bool delimited = false;
-    if (c < ' ' && c != '\r' && c != '\n') delimited = true;
-    if (c > 126) delimited = true;
-    
-    //Push the delimiter or the character
-    if (delimited) push('^');
-    else           push(c);
-    
-    //Stop if its not delimited
-    if (!delimited) return;
-    
-    //Push the first digit
-    char h = c >> 4;
-    if (h < 10) h += '0';
-    else        h += 'A' - 10;
-    push(h);
-        
-    //Push the second digit
-    h = c & 0x0F;
-    if (h < 10) h += '0';
-    else        h += 'A' - 10;
-    push(h);
-    
-}
-static char *pEnumerate;
-void LogEnumerateStart()
-{
-    pEnumerate = pPull;
-}
-int LogEnumerate()
-{
-    if (pEnumerate == pPush) return EOF;
-    char c = *pEnumerate;
-    pEnumerate = incrementPushPullPointer(pEnumerate, buffer, BUFFER_LENGTH); 
-    return c;
-}
-void LogEnable(int on)
-{
-    enable = on;
-}
-int LogInit()
-{
-    uart.baud(115200);
-    pPush = buffer;
-    pPull = buffer;
-    return 0;
-}
-int Log(char* snd)
-{
-    char* ptr = snd;
-    while (*ptr) LogPush(*ptr++); //Send the string to the log buffer
-    return ptr - snd;
-}
-int LogV(char *fmt, va_list argptr)
-{
-    int size  = vsnprintf(NULL, 0, fmt, argptr);  //Find the size required
-    char snd[size + 1];                           //Allocate enough memory for the size required with an extra byte for the terminating null
-    vsprintf(snd, fmt, argptr);                   //Fill the new buffer
-    return Log(snd);                              //Send the string to the log buffer
-}
-int LogF(char *fmt, ...)
-{
-    va_list argptr;
-    va_start(argptr, fmt);
-    int size = LogV(fmt, argptr);
-    va_end(argptr);
-    return size;
-}
-static void pushuint4(int value)
-{    
-    if      (value > 9999) { LogPush('+'); LogPush('+'); LogPush('+'); LogPush('+'); }
-    else if (value <    0) { LogPush('-'); LogPush('-'); LogPush('-'); LogPush('-'); }
-    else
-    {
-        div_t divres;
-        int k, c, t, u;
-        divres = div(value      , 10); u = divres.rem;
-        divres = div(divres.quot, 10); t = divres.rem;
-        divres = div(divres.quot, 10); c = divres.rem;
-                                       k = divres.quot;                           
-        LogPush(k + '0'); LogPush(c + '0'); LogPush(t + '0'); LogPush(u + '0');
-    }
-}
-static void pushuint3(int value)
-{
-    if      (value > 999) { LogPush('+'); LogPush('+'); LogPush('+'); }
-    else if (value <   0) { LogPush('-'); LogPush('-'); LogPush('-'); }
-    else
-    {
-        div_t divres;
-        int c, t, u;
-        divres = div(value      , 10); u = divres.rem;
-        divres = div(divres.quot, 10); t = divres.rem;
-                                       c = divres.quot;
-        LogPush(c + '0'); LogPush(t + '0'); LogPush(u + '0');
-    }
-}
-static void pushuint2(int value)
-{
-    if      (value > 99) { LogPush('+'); LogPush('+'); }
-    else if (value <  0) { LogPush('-'); LogPush('-'); }
-    else
-    {
-        div_t divres;
-        int t, u;
-        divres = div(value      , 10); u = divres.rem;
-                                       t = divres.quot;
-        LogPush(t + '0'); LogPush(u + '0');
-    }
-}
-static int logTimeOnly()
-{
-    struct tm tm;
-    ClockTmUtc(&tm);
-    
-    pushuint4(tm.tm_year + 1900);
-    LogPush('-');
-    pushuint3(tm.tm_yday + 1);
-    LogPush(' ');
-    pushuint2(tm.tm_hour);
-    LogPush(':');
-    pushuint2(tm.tm_min);
-    LogPush(':');
-    pushuint2(tm.tm_sec);
-    return 17;
-}
-int LogTime(char *snd)
-{
-    int size = 0;
-    size += logTimeOnly();
-    size++; LogPush(' ');
-    size += Log(snd);
-    return size;
-}
-int LogTimeF(char *fmt, ...)
-{
-    int size = 0;
-    va_list argptr;
-    va_start(argptr, fmt);
-    size == logTimeOnly();
-    size++; LogPush(' ');
-    size += LogV(fmt, argptr);
-    va_end(argptr);
-    return size;
-}