Andrew Boyson
/
iot
Backing up an unused program in case of future need
Diff: log.cpp
- Revision:
- 0:09f915e6f9f6
- Child:
- 1:94282484baae
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/log.cpp Wed Apr 13 09:21:02 2016 +0000 @@ -0,0 +1,109 @@ +#include "mbed.h" +#include <stdarg.h> +#include "time.h" +#define LOG_FILE "/local/log.txt" +#define DATE_FORMAT "%05d " + +LocalFileSystem local("local"); + +#define BUFFER_LENGTH 512 +static char buffer[BUFFER_LENGTH]; +static char* pPush; //Initialised in init +static char* pPull; //Initialised in init +static int enable = 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; +} +void LogPush(char c) +{ + //Only add if allowed + if (!enable) return; + + //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); +} +static char *pEnumerate; +static int hadNull; +void LogEnumerateStart() +{ + pEnumerate = pPull; + hadNull = false; +} +int LogEnumerate() +{ + if (hadNull) + { + hadNull = false; + return '0'; + } + if (pEnumerate == pPush) return EOF; + char c = *pEnumerate; + pEnumerate = incrementPushPullPointer(pEnumerate, buffer, BUFFER_LENGTH); + if (c) + { + return c; + } + else + { + hadNull = true; + return '\\'; + } +} +void LogEnable(int on) +{ + enable = on; +} +void LogInit() +{ + pPush = buffer; + pPull = buffer; +} +void LogF(char *fmt, ...) +{ + //Set up variable arguments + uint32_t now = TimeGet(); + va_list argptr; + va_start(argptr, fmt); + + //Find the size required + int sizeTime = snprintf(NULL, 0, DATE_FORMAT, now); + int sizeLog = vsnprintf(NULL, 0, fmt, argptr); + + //Allocate enough memory for the size required with an extra byte for the terminating null + char snd[sizeTime + sizeLog + 1]; + + //Fill the new buffer + sprintf(snd, DATE_FORMAT, now); + vsprintf(snd + sizeTime, fmt, argptr); + + //Finish with variable arguments + va_end(argptr); + + //Send the string to the log buffer + for (char* ptr = snd; *ptr; ptr++) LogPush(*ptr); + +} +void LogCrLf (char * text) +{ + LogF("%s\r\n", text); +} +void LogSave() +{ + FILE *fp = fopen(LOG_FILE, "w"); + LogEnumerateStart(); + while(1) + { + int c = LogEnumerate(); + if (c == EOF) break; + putc(c, fp); + } + fclose(fp); +}