Log

Dependents:   oldheating gps motorhome heating

log.c

Committer:
andrewboyson
Date:
2018-01-11
Revision:
12:19c2d4943124
Parent:
log.cpp@ 10:cf815db8ed57
Child:
13:fb7f40c2e446

File content as of revision 12:19c2d4943124:

#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <time.h>
#include <stdbool.h>
#include "uart.h"

#define BUFFER_LENGTH 0x4000

__attribute__((section("AHBSRAM0"))) static char buffer[BUFFER_LENGTH]; //Pop the buffer into the USB area
static char* pPush; //Initialised in init
static char* pPull; //Initialised in init
static char *pUart; //Initialised in init

static bool enable = true;

bool LogUart = true;

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);
}
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 = false;
    
    //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 -1;
    char c = *pEnumerate;
    pEnumerate = incrementPushPullPointer(pEnumerate, buffer, BUFFER_LENGTH); 
    return c;
}
void LogEnable(bool value)
{
    enable = value;
}
void LogInit()
{
    Uart0Init();
    pPush = buffer;
    pPull = buffer;
    pUart = buffer;
}
void LogMain()
{
    if (!LogUart)       return;    //Do nothing if uart is not enabled
    if (pUart == pPush) return;    //Do nothing if all characters have been sent
    int result = Uart0PutC(*pUart); //Attempt to write the character
    if (result == 0) pUart = incrementPushPullPointer(pUart, buffer, BUFFER_LENGTH); //If the character was written to the uart then move to the next
}
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');
    }
}
void (*LogTmFunction)(struct tm* ptm);

static int logTimeOnly()
{
    if (!LogTmFunction) return 0;
    
    struct tm tm;
    LogTmFunction(&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;
}