Log

Dependents:   oldheating gps motorhome heating

Committer:
andrewboyson
Date:
Mon Dec 12 15:44:51 2016 +0000
Revision:
0:9907e344c82a
Child:
1:4d81afe3859e
Added log library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andrewboyson 0:9907e344c82a 1 #include "mbed.h"
andrewboyson 0:9907e344c82a 2 #include "rtc.h"
andrewboyson 0:9907e344c82a 3 #define LOG_FILE "/local/log.txt"
andrewboyson 0:9907e344c82a 4
andrewboyson 0:9907e344c82a 5 LocalFileSystem local("local");
andrewboyson 0:9907e344c82a 6
andrewboyson 0:9907e344c82a 7 #define BUFFER_LENGTH 4096
andrewboyson 0:9907e344c82a 8 static char buffer[BUFFER_LENGTH];
andrewboyson 0:9907e344c82a 9 static char* pPush; //Initialised in init
andrewboyson 0:9907e344c82a 10 static char* pPull; //Initialised in init
andrewboyson 0:9907e344c82a 11 static int enable = true;
andrewboyson 0:9907e344c82a 12 static char* incrementPushPullPointer(char* p, char* buffer, int bufferLength)
andrewboyson 0:9907e344c82a 13 {
andrewboyson 0:9907e344c82a 14 p++; //increment the pointer by one
andrewboyson 0:9907e344c82a 15 if (p == buffer + bufferLength) p = buffer; //if the pointer is now beyond the end then point it back to the start
andrewboyson 0:9907e344c82a 16 return p;
andrewboyson 0:9907e344c82a 17 }
andrewboyson 0:9907e344c82a 18 void LogPush(char c)
andrewboyson 0:9907e344c82a 19 {
andrewboyson 0:9907e344c82a 20 //Only add if allowed
andrewboyson 0:9907e344c82a 21 if (!enable) return;
andrewboyson 0:9907e344c82a 22
andrewboyson 0:9907e344c82a 23 //Move the pull position if about to run into it
andrewboyson 0:9907e344c82a 24 char* pNext = incrementPushPullPointer(pPush, buffer, BUFFER_LENGTH);
andrewboyson 0:9907e344c82a 25 if (pNext == pPull) pPull = incrementPushPullPointer(pPull, buffer, BUFFER_LENGTH);
andrewboyson 0:9907e344c82a 26
andrewboyson 0:9907e344c82a 27 //Add the character at the push position
andrewboyson 0:9907e344c82a 28 *pPush = c;
andrewboyson 0:9907e344c82a 29 pPush = incrementPushPullPointer(pPush, buffer, BUFFER_LENGTH);
andrewboyson 0:9907e344c82a 30 }
andrewboyson 0:9907e344c82a 31 static char *pEnumerate;
andrewboyson 0:9907e344c82a 32 static int hadNull;
andrewboyson 0:9907e344c82a 33 void LogEnumerateStart()
andrewboyson 0:9907e344c82a 34 {
andrewboyson 0:9907e344c82a 35 pEnumerate = pPull;
andrewboyson 0:9907e344c82a 36 hadNull = false;
andrewboyson 0:9907e344c82a 37 }
andrewboyson 0:9907e344c82a 38 int LogEnumerate()
andrewboyson 0:9907e344c82a 39 {
andrewboyson 0:9907e344c82a 40 if (hadNull)
andrewboyson 0:9907e344c82a 41 {
andrewboyson 0:9907e344c82a 42 hadNull = false;
andrewboyson 0:9907e344c82a 43 return '0';
andrewboyson 0:9907e344c82a 44 }
andrewboyson 0:9907e344c82a 45 if (pEnumerate == pPush) return EOF;
andrewboyson 0:9907e344c82a 46 char c = *pEnumerate;
andrewboyson 0:9907e344c82a 47 pEnumerate = incrementPushPullPointer(pEnumerate, buffer, BUFFER_LENGTH);
andrewboyson 0:9907e344c82a 48 if (c)
andrewboyson 0:9907e344c82a 49 {
andrewboyson 0:9907e344c82a 50 return c;
andrewboyson 0:9907e344c82a 51 }
andrewboyson 0:9907e344c82a 52 else
andrewboyson 0:9907e344c82a 53 {
andrewboyson 0:9907e344c82a 54 hadNull = true;
andrewboyson 0:9907e344c82a 55 return '\\';
andrewboyson 0:9907e344c82a 56 }
andrewboyson 0:9907e344c82a 57 }
andrewboyson 0:9907e344c82a 58 void LogEnable(int on)
andrewboyson 0:9907e344c82a 59 {
andrewboyson 0:9907e344c82a 60 enable = on;
andrewboyson 0:9907e344c82a 61 }
andrewboyson 0:9907e344c82a 62 int LogInit()
andrewboyson 0:9907e344c82a 63 {
andrewboyson 0:9907e344c82a 64 pPush = buffer;
andrewboyson 0:9907e344c82a 65 pPull = buffer;
andrewboyson 0:9907e344c82a 66 return 0;
andrewboyson 0:9907e344c82a 67 }
andrewboyson 0:9907e344c82a 68 void LogV(char *fmt, va_list argptr)
andrewboyson 0:9907e344c82a 69 {
andrewboyson 0:9907e344c82a 70 int size = vsnprintf(NULL, 0, fmt, argptr); //Find the size required
andrewboyson 0:9907e344c82a 71 char snd[size + 1]; //Allocate enough memory for the size required with an extra byte for the terminating null
andrewboyson 0:9907e344c82a 72 vsprintf(snd, fmt, argptr); //Fill the new buffer
andrewboyson 0:9907e344c82a 73 for (char* ptr = snd; *ptr; ptr++) LogPush(*ptr); //Send the string to the log buffer
andrewboyson 0:9907e344c82a 74 }
andrewboyson 0:9907e344c82a 75 void LogF(char *fmt, ...)
andrewboyson 0:9907e344c82a 76 {
andrewboyson 0:9907e344c82a 77 va_list argptr;
andrewboyson 0:9907e344c82a 78 va_start(argptr, fmt);
andrewboyson 0:9907e344c82a 79 LogV(fmt, argptr);
andrewboyson 0:9907e344c82a 80 va_end(argptr);
andrewboyson 0:9907e344c82a 81 }
andrewboyson 0:9907e344c82a 82 void LogCrLf (char * text)
andrewboyson 0:9907e344c82a 83 {
andrewboyson 0:9907e344c82a 84 LogF("%s\r\n", text);
andrewboyson 0:9907e344c82a 85 }
andrewboyson 0:9907e344c82a 86 static void pushuint4(int value)
andrewboyson 0:9907e344c82a 87 {
andrewboyson 0:9907e344c82a 88 if (value > 9999) { LogPush('+'); LogPush('+'); LogPush('+'); LogPush('+'); }
andrewboyson 0:9907e344c82a 89 else if (value < 0) { LogPush('-'); LogPush('-'); LogPush('-'); LogPush('-'); }
andrewboyson 0:9907e344c82a 90 else
andrewboyson 0:9907e344c82a 91 {
andrewboyson 0:9907e344c82a 92 div_t divres;
andrewboyson 0:9907e344c82a 93 int k, c, t, u;
andrewboyson 0:9907e344c82a 94 divres = div(value , 10); u = divres.rem;
andrewboyson 0:9907e344c82a 95 divres = div(divres.quot, 10); t = divres.rem;
andrewboyson 0:9907e344c82a 96 divres = div(divres.quot, 10); c = divres.rem;
andrewboyson 0:9907e344c82a 97 k = divres.quot;
andrewboyson 0:9907e344c82a 98 LogPush(k + '0'); LogPush(c + '0'); LogPush(t + '0'); LogPush(u + '0');
andrewboyson 0:9907e344c82a 99 }
andrewboyson 0:9907e344c82a 100 }
andrewboyson 0:9907e344c82a 101 static void pushuint3(int value)
andrewboyson 0:9907e344c82a 102 {
andrewboyson 0:9907e344c82a 103 if (value > 999) { LogPush('+'); LogPush('+'); LogPush('+'); }
andrewboyson 0:9907e344c82a 104 else if (value < 0) { LogPush('-'); LogPush('-'); LogPush('-'); }
andrewboyson 0:9907e344c82a 105 else
andrewboyson 0:9907e344c82a 106 {
andrewboyson 0:9907e344c82a 107 div_t divres;
andrewboyson 0:9907e344c82a 108 int c, t, u;
andrewboyson 0:9907e344c82a 109 divres = div(value , 10); u = divres.rem;
andrewboyson 0:9907e344c82a 110 divres = div(divres.quot, 10); t = divres.rem;
andrewboyson 0:9907e344c82a 111 c = divres.quot;
andrewboyson 0:9907e344c82a 112 LogPush(c + '0'); LogPush(t + '0'); LogPush(u + '0');
andrewboyson 0:9907e344c82a 113 }
andrewboyson 0:9907e344c82a 114 }
andrewboyson 0:9907e344c82a 115 static void pushuint2(int value)
andrewboyson 0:9907e344c82a 116 {
andrewboyson 0:9907e344c82a 117 if (value > 99) { LogPush('+'); LogPush('+'); }
andrewboyson 0:9907e344c82a 118 else if (value < 0) { LogPush('-'); LogPush('-'); }
andrewboyson 0:9907e344c82a 119 else
andrewboyson 0:9907e344c82a 120 {
andrewboyson 0:9907e344c82a 121 div_t divres;
andrewboyson 0:9907e344c82a 122 int t, u;
andrewboyson 0:9907e344c82a 123 divres = div(value , 10); u = divres.rem;
andrewboyson 0:9907e344c82a 124 t = divres.quot;
andrewboyson 0:9907e344c82a 125 LogPush(t + '0'); LogPush(u + '0');
andrewboyson 0:9907e344c82a 126 }
andrewboyson 0:9907e344c82a 127 }
andrewboyson 0:9907e344c82a 128 void LogTime()
andrewboyson 0:9907e344c82a 129 {
andrewboyson 0:9907e344c82a 130 struct tm tm;
andrewboyson 0:9907e344c82a 131 RtcGetTmUtc(&tm);
andrewboyson 0:9907e344c82a 132
andrewboyson 0:9907e344c82a 133 pushuint4(tm.tm_year + 1900);
andrewboyson 0:9907e344c82a 134 LogPush('-');
andrewboyson 0:9907e344c82a 135 pushuint3(tm.tm_yday + 1);
andrewboyson 0:9907e344c82a 136 LogPush(' ');
andrewboyson 0:9907e344c82a 137 pushuint2(tm.tm_hour);
andrewboyson 0:9907e344c82a 138 LogPush(':');
andrewboyson 0:9907e344c82a 139 pushuint2(tm.tm_min);
andrewboyson 0:9907e344c82a 140 LogPush(':');
andrewboyson 0:9907e344c82a 141 pushuint2(tm.tm_sec);
andrewboyson 0:9907e344c82a 142 }
andrewboyson 0:9907e344c82a 143 void LogTimeCrLf(char * text)
andrewboyson 0:9907e344c82a 144 {
andrewboyson 0:9907e344c82a 145 LogTime();
andrewboyson 0:9907e344c82a 146 LogF(" %s\r\n", text);
andrewboyson 0:9907e344c82a 147 }
andrewboyson 0:9907e344c82a 148 void LogTimeF(char *fmt, ...)
andrewboyson 0:9907e344c82a 149 {
andrewboyson 0:9907e344c82a 150 va_list argptr;
andrewboyson 0:9907e344c82a 151 va_start(argptr, fmt);
andrewboyson 0:9907e344c82a 152 LogTime();
andrewboyson 0:9907e344c82a 153 LogPush(' ');
andrewboyson 0:9907e344c82a 154 LogV(fmt, argptr);
andrewboyson 0:9907e344c82a 155 va_end(argptr);
andrewboyson 0:9907e344c82a 156 }
andrewboyson 0:9907e344c82a 157 void LogSave()
andrewboyson 0:9907e344c82a 158 {
andrewboyson 0:9907e344c82a 159 FILE *fp = fopen(LOG_FILE, "w");
andrewboyson 0:9907e344c82a 160 LogEnumerateStart();
andrewboyson 0:9907e344c82a 161 while(1)
andrewboyson 0:9907e344c82a 162 {
andrewboyson 0:9907e344c82a 163 int c = LogEnumerate();
andrewboyson 0:9907e344c82a 164 if (c == EOF) break;
andrewboyson 0:9907e344c82a 165 putc(c, fp);
andrewboyson 0:9907e344c82a 166 }
andrewboyson 0:9907e344c82a 167 fclose(fp);
andrewboyson 0:9907e344c82a 168 }