Log
Dependents: oldheating gps motorhome heating
log.cpp@2:ee4ec2d72525, 2017-02-04 (annotated)
- Committer:
- andrewboyson
- Date:
- Sat Feb 04 18:45:41 2017 +0000
- Revision:
- 2:ee4ec2d72525
- Parent:
- 1:4d81afe3859e
- Child:
- 3:7806ff3f1a2d
Display non-ascii characters as hex
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andrewboyson | 0:9907e344c82a | 1 | #include "mbed.h" |
andrewboyson | 1:4d81afe3859e | 2 | #include "time.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 | 2:ee4ec2d72525 | 23 | char* pNext; |
andrewboyson | 2:ee4ec2d72525 | 24 | |
andrewboyson | 2:ee4ec2d72525 | 25 | //Work out what has to be sent |
andrewboyson | 2:ee4ec2d72525 | 26 | bool delimited = false; |
andrewboyson | 2:ee4ec2d72525 | 27 | if (c < ' ' && c != '\r' && c != '\n') delimited = true; |
andrewboyson | 2:ee4ec2d72525 | 28 | if (c > 126) delimited = true; |
andrewboyson | 2:ee4ec2d72525 | 29 | |
andrewboyson | 0:9907e344c82a | 30 | //Move the pull position if about to run into it |
andrewboyson | 2:ee4ec2d72525 | 31 | pNext = incrementPushPullPointer(pPush, buffer, BUFFER_LENGTH); |
andrewboyson | 2:ee4ec2d72525 | 32 | if (pNext == pPull) pPull = incrementPushPullPointer(pPull, buffer, BUFFER_LENGTH); |
andrewboyson | 2:ee4ec2d72525 | 33 | |
andrewboyson | 2:ee4ec2d72525 | 34 | //Add the character at the push position |
andrewboyson | 2:ee4ec2d72525 | 35 | if (delimited) *pPush = '^'; |
andrewboyson | 2:ee4ec2d72525 | 36 | else *pPush = c; |
andrewboyson | 2:ee4ec2d72525 | 37 | pPush = incrementPushPullPointer(pPush, buffer, BUFFER_LENGTH); |
andrewboyson | 2:ee4ec2d72525 | 38 | |
andrewboyson | 2:ee4ec2d72525 | 39 | if (!delimited) return; |
andrewboyson | 2:ee4ec2d72525 | 40 | |
andrewboyson | 2:ee4ec2d72525 | 41 | //Move the pull position if about to run into it |
andrewboyson | 2:ee4ec2d72525 | 42 | pNext = incrementPushPullPointer(pPush, buffer, BUFFER_LENGTH); |
andrewboyson | 0:9907e344c82a | 43 | if (pNext == pPull) pPull = incrementPushPullPointer(pPull, buffer, BUFFER_LENGTH); |
andrewboyson | 0:9907e344c82a | 44 | |
andrewboyson | 0:9907e344c82a | 45 | //Add the character at the push position |
andrewboyson | 2:ee4ec2d72525 | 46 | char h = c >> 4; |
andrewboyson | 2:ee4ec2d72525 | 47 | if (h < 10) h += '0'; |
andrewboyson | 2:ee4ec2d72525 | 48 | else h += 'A' - 10; |
andrewboyson | 2:ee4ec2d72525 | 49 | *pPush = h; |
andrewboyson | 0:9907e344c82a | 50 | pPush = incrementPushPullPointer(pPush, buffer, BUFFER_LENGTH); |
andrewboyson | 2:ee4ec2d72525 | 51 | |
andrewboyson | 2:ee4ec2d72525 | 52 | //Move the pull position if about to run into it |
andrewboyson | 2:ee4ec2d72525 | 53 | pNext = incrementPushPullPointer(pPush, buffer, BUFFER_LENGTH); |
andrewboyson | 2:ee4ec2d72525 | 54 | if (pNext == pPull) pPull = incrementPushPullPointer(pPull, buffer, BUFFER_LENGTH); |
andrewboyson | 2:ee4ec2d72525 | 55 | |
andrewboyson | 2:ee4ec2d72525 | 56 | //Add the character at the push position |
andrewboyson | 2:ee4ec2d72525 | 57 | h = c & 0x0F; |
andrewboyson | 2:ee4ec2d72525 | 58 | if (h < 10) h += '0'; |
andrewboyson | 2:ee4ec2d72525 | 59 | else h += 'A' - 10; |
andrewboyson | 2:ee4ec2d72525 | 60 | *pPush = h; |
andrewboyson | 2:ee4ec2d72525 | 61 | pPush = incrementPushPullPointer(pPush, buffer, BUFFER_LENGTH); |
andrewboyson | 2:ee4ec2d72525 | 62 | |
andrewboyson | 0:9907e344c82a | 63 | } |
andrewboyson | 0:9907e344c82a | 64 | static char *pEnumerate; |
andrewboyson | 0:9907e344c82a | 65 | void LogEnumerateStart() |
andrewboyson | 0:9907e344c82a | 66 | { |
andrewboyson | 0:9907e344c82a | 67 | pEnumerate = pPull; |
andrewboyson | 0:9907e344c82a | 68 | } |
andrewboyson | 0:9907e344c82a | 69 | int LogEnumerate() |
andrewboyson | 0:9907e344c82a | 70 | { |
andrewboyson | 0:9907e344c82a | 71 | if (pEnumerate == pPush) return EOF; |
andrewboyson | 0:9907e344c82a | 72 | char c = *pEnumerate; |
andrewboyson | 0:9907e344c82a | 73 | pEnumerate = incrementPushPullPointer(pEnumerate, buffer, BUFFER_LENGTH); |
andrewboyson | 2:ee4ec2d72525 | 74 | return c; |
andrewboyson | 0:9907e344c82a | 75 | } |
andrewboyson | 0:9907e344c82a | 76 | void LogEnable(int on) |
andrewboyson | 0:9907e344c82a | 77 | { |
andrewboyson | 0:9907e344c82a | 78 | enable = on; |
andrewboyson | 0:9907e344c82a | 79 | } |
andrewboyson | 0:9907e344c82a | 80 | int LogInit() |
andrewboyson | 0:9907e344c82a | 81 | { |
andrewboyson | 0:9907e344c82a | 82 | pPush = buffer; |
andrewboyson | 0:9907e344c82a | 83 | pPull = buffer; |
andrewboyson | 0:9907e344c82a | 84 | return 0; |
andrewboyson | 0:9907e344c82a | 85 | } |
andrewboyson | 0:9907e344c82a | 86 | void LogV(char *fmt, va_list argptr) |
andrewboyson | 0:9907e344c82a | 87 | { |
andrewboyson | 0:9907e344c82a | 88 | int size = vsnprintf(NULL, 0, fmt, argptr); //Find the size required |
andrewboyson | 0:9907e344c82a | 89 | char snd[size + 1]; //Allocate enough memory for the size required with an extra byte for the terminating null |
andrewboyson | 0:9907e344c82a | 90 | vsprintf(snd, fmt, argptr); //Fill the new buffer |
andrewboyson | 0:9907e344c82a | 91 | for (char* ptr = snd; *ptr; ptr++) LogPush(*ptr); //Send the string to the log buffer |
andrewboyson | 0:9907e344c82a | 92 | } |
andrewboyson | 0:9907e344c82a | 93 | void LogF(char *fmt, ...) |
andrewboyson | 0:9907e344c82a | 94 | { |
andrewboyson | 0:9907e344c82a | 95 | va_list argptr; |
andrewboyson | 0:9907e344c82a | 96 | va_start(argptr, fmt); |
andrewboyson | 0:9907e344c82a | 97 | LogV(fmt, argptr); |
andrewboyson | 0:9907e344c82a | 98 | va_end(argptr); |
andrewboyson | 0:9907e344c82a | 99 | } |
andrewboyson | 0:9907e344c82a | 100 | void LogCrLf (char * text) |
andrewboyson | 0:9907e344c82a | 101 | { |
andrewboyson | 0:9907e344c82a | 102 | LogF("%s\r\n", text); |
andrewboyson | 0:9907e344c82a | 103 | } |
andrewboyson | 0:9907e344c82a | 104 | static void pushuint4(int value) |
andrewboyson | 0:9907e344c82a | 105 | { |
andrewboyson | 0:9907e344c82a | 106 | if (value > 9999) { LogPush('+'); LogPush('+'); LogPush('+'); LogPush('+'); } |
andrewboyson | 0:9907e344c82a | 107 | else if (value < 0) { LogPush('-'); LogPush('-'); LogPush('-'); LogPush('-'); } |
andrewboyson | 0:9907e344c82a | 108 | else |
andrewboyson | 0:9907e344c82a | 109 | { |
andrewboyson | 0:9907e344c82a | 110 | div_t divres; |
andrewboyson | 0:9907e344c82a | 111 | int k, c, t, u; |
andrewboyson | 0:9907e344c82a | 112 | divres = div(value , 10); u = divres.rem; |
andrewboyson | 0:9907e344c82a | 113 | divres = div(divres.quot, 10); t = divres.rem; |
andrewboyson | 0:9907e344c82a | 114 | divres = div(divres.quot, 10); c = divres.rem; |
andrewboyson | 0:9907e344c82a | 115 | k = divres.quot; |
andrewboyson | 0:9907e344c82a | 116 | LogPush(k + '0'); LogPush(c + '0'); LogPush(t + '0'); LogPush(u + '0'); |
andrewboyson | 0:9907e344c82a | 117 | } |
andrewboyson | 0:9907e344c82a | 118 | } |
andrewboyson | 0:9907e344c82a | 119 | static void pushuint3(int value) |
andrewboyson | 0:9907e344c82a | 120 | { |
andrewboyson | 0:9907e344c82a | 121 | if (value > 999) { LogPush('+'); LogPush('+'); LogPush('+'); } |
andrewboyson | 0:9907e344c82a | 122 | else if (value < 0) { LogPush('-'); LogPush('-'); LogPush('-'); } |
andrewboyson | 0:9907e344c82a | 123 | else |
andrewboyson | 0:9907e344c82a | 124 | { |
andrewboyson | 0:9907e344c82a | 125 | div_t divres; |
andrewboyson | 0:9907e344c82a | 126 | int c, t, u; |
andrewboyson | 0:9907e344c82a | 127 | divres = div(value , 10); u = divres.rem; |
andrewboyson | 0:9907e344c82a | 128 | divres = div(divres.quot, 10); t = divres.rem; |
andrewboyson | 0:9907e344c82a | 129 | c = divres.quot; |
andrewboyson | 0:9907e344c82a | 130 | LogPush(c + '0'); LogPush(t + '0'); LogPush(u + '0'); |
andrewboyson | 0:9907e344c82a | 131 | } |
andrewboyson | 0:9907e344c82a | 132 | } |
andrewboyson | 0:9907e344c82a | 133 | static void pushuint2(int value) |
andrewboyson | 0:9907e344c82a | 134 | { |
andrewboyson | 0:9907e344c82a | 135 | if (value > 99) { LogPush('+'); LogPush('+'); } |
andrewboyson | 0:9907e344c82a | 136 | else if (value < 0) { LogPush('-'); LogPush('-'); } |
andrewboyson | 0:9907e344c82a | 137 | else |
andrewboyson | 0:9907e344c82a | 138 | { |
andrewboyson | 0:9907e344c82a | 139 | div_t divres; |
andrewboyson | 0:9907e344c82a | 140 | int t, u; |
andrewboyson | 0:9907e344c82a | 141 | divres = div(value , 10); u = divres.rem; |
andrewboyson | 0:9907e344c82a | 142 | t = divres.quot; |
andrewboyson | 0:9907e344c82a | 143 | LogPush(t + '0'); LogPush(u + '0'); |
andrewboyson | 0:9907e344c82a | 144 | } |
andrewboyson | 0:9907e344c82a | 145 | } |
andrewboyson | 0:9907e344c82a | 146 | void LogTime() |
andrewboyson | 0:9907e344c82a | 147 | { |
andrewboyson | 0:9907e344c82a | 148 | struct tm tm; |
andrewboyson | 1:4d81afe3859e | 149 | TimeToTmUtc(time(NULL), &tm); |
andrewboyson | 0:9907e344c82a | 150 | |
andrewboyson | 0:9907e344c82a | 151 | pushuint4(tm.tm_year + 1900); |
andrewboyson | 0:9907e344c82a | 152 | LogPush('-'); |
andrewboyson | 0:9907e344c82a | 153 | pushuint3(tm.tm_yday + 1); |
andrewboyson | 0:9907e344c82a | 154 | LogPush(' '); |
andrewboyson | 0:9907e344c82a | 155 | pushuint2(tm.tm_hour); |
andrewboyson | 0:9907e344c82a | 156 | LogPush(':'); |
andrewboyson | 0:9907e344c82a | 157 | pushuint2(tm.tm_min); |
andrewboyson | 0:9907e344c82a | 158 | LogPush(':'); |
andrewboyson | 0:9907e344c82a | 159 | pushuint2(tm.tm_sec); |
andrewboyson | 0:9907e344c82a | 160 | } |
andrewboyson | 0:9907e344c82a | 161 | void LogTimeCrLf(char * text) |
andrewboyson | 0:9907e344c82a | 162 | { |
andrewboyson | 0:9907e344c82a | 163 | LogTime(); |
andrewboyson | 0:9907e344c82a | 164 | LogF(" %s\r\n", text); |
andrewboyson | 0:9907e344c82a | 165 | } |
andrewboyson | 0:9907e344c82a | 166 | void LogTimeF(char *fmt, ...) |
andrewboyson | 0:9907e344c82a | 167 | { |
andrewboyson | 0:9907e344c82a | 168 | va_list argptr; |
andrewboyson | 0:9907e344c82a | 169 | va_start(argptr, fmt); |
andrewboyson | 0:9907e344c82a | 170 | LogTime(); |
andrewboyson | 0:9907e344c82a | 171 | LogPush(' '); |
andrewboyson | 0:9907e344c82a | 172 | LogV(fmt, argptr); |
andrewboyson | 0:9907e344c82a | 173 | va_end(argptr); |
andrewboyson | 0:9907e344c82a | 174 | } |
andrewboyson | 0:9907e344c82a | 175 | void LogSave() |
andrewboyson | 0:9907e344c82a | 176 | { |
andrewboyson | 0:9907e344c82a | 177 | FILE *fp = fopen(LOG_FILE, "w"); |
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 | } |