Log

Dependents:   oldheating gps motorhome heating

Committer:
andrewboyson
Date:
Tue May 09 15:22:21 2017 +0000
Revision:
5:a1f0298e0995
Parent:
4:c95b09342a93
Child:
6:b98aa0ec75a1
Removed debug uart from LogPush

Who changed what in which revision?

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