Log

Dependents:   oldheating gps motorhome heating

Committer:
andrewboyson
Date:
Tue May 09 15:08:38 2017 +0000
Revision:
3:7806ff3f1a2d
Parent:
2:ee4ec2d72525
Child:
4:c95b09342a93
Removed FileSystem from within library and moved open to outside.

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