Log
Dependents: oldheating gps motorhome heating
log.cpp@10:cf815db8ed57, 2017-10-26 (annotated)
- Committer:
- andrewboyson
- Date:
- Thu Oct 26 07:17:43 2017 +0000
- Revision:
- 10:cf815db8ed57
- Parent:
- 9:d2c41855ed09
Modified add functions to return the length added.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andrewboyson | 0:9907e344c82a | 1 | #include "mbed.h" |
andrewboyson | 7:417a6a65e942 | 2 | #include "clock.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 | 6:b98aa0ec75a1 | 9 | |
andrewboyson | 7:417a6a65e942 | 10 | static Serial uart(USBTX, USBRX); |
andrewboyson | 7:417a6a65e942 | 11 | bool LogUart = false; |
andrewboyson | 6:b98aa0ec75a1 | 12 | |
andrewboyson | 0:9907e344c82a | 13 | static char* incrementPushPullPointer(char* p, char* buffer, int bufferLength) |
andrewboyson | 0:9907e344c82a | 14 | { |
andrewboyson | 0:9907e344c82a | 15 | p++; //increment the pointer by one |
andrewboyson | 0:9907e344c82a | 16 | if (p == buffer + bufferLength) p = buffer; //if the pointer is now beyond the end then point it back to the start |
andrewboyson | 0:9907e344c82a | 17 | return p; |
andrewboyson | 0:9907e344c82a | 18 | } |
andrewboyson | 6:b98aa0ec75a1 | 19 | static void push(char c) |
andrewboyson | 6:b98aa0ec75a1 | 20 | { |
andrewboyson | 6:b98aa0ec75a1 | 21 | //Move the pull position if about to run into it |
andrewboyson | 6:b98aa0ec75a1 | 22 | char* pNext = incrementPushPullPointer(pPush, buffer, BUFFER_LENGTH); |
andrewboyson | 6:b98aa0ec75a1 | 23 | if (pNext == pPull) pPull = incrementPushPullPointer(pPull, buffer, BUFFER_LENGTH); |
andrewboyson | 6:b98aa0ec75a1 | 24 | |
andrewboyson | 6:b98aa0ec75a1 | 25 | //Add the character at the push position |
andrewboyson | 6:b98aa0ec75a1 | 26 | *pPush = c; |
andrewboyson | 6:b98aa0ec75a1 | 27 | pPush = incrementPushPullPointer(pPush, buffer, BUFFER_LENGTH); |
andrewboyson | 6:b98aa0ec75a1 | 28 | |
andrewboyson | 7:417a6a65e942 | 29 | if (LogUart) uart.putc(c); |
andrewboyson | 6:b98aa0ec75a1 | 30 | } |
andrewboyson | 0:9907e344c82a | 31 | void LogPush(char c) |
andrewboyson | 0:9907e344c82a | 32 | { |
andrewboyson | 0:9907e344c82a | 33 | //Only add if allowed |
andrewboyson | 0:9907e344c82a | 34 | if (!enable) return; |
andrewboyson | 0:9907e344c82a | 35 | |
andrewboyson | 6:b98aa0ec75a1 | 36 | //Work out if the character needs to be delimited |
andrewboyson | 2:ee4ec2d72525 | 37 | bool delimited = false; |
andrewboyson | 2:ee4ec2d72525 | 38 | if (c < ' ' && c != '\r' && c != '\n') delimited = true; |
andrewboyson | 2:ee4ec2d72525 | 39 | if (c > 126) delimited = true; |
andrewboyson | 2:ee4ec2d72525 | 40 | |
andrewboyson | 6:b98aa0ec75a1 | 41 | //Push the delimiter or the character |
andrewboyson | 6:b98aa0ec75a1 | 42 | if (delimited) push('^'); |
andrewboyson | 6:b98aa0ec75a1 | 43 | else push(c); |
andrewboyson | 2:ee4ec2d72525 | 44 | |
andrewboyson | 6:b98aa0ec75a1 | 45 | //Stop if its not delimited |
andrewboyson | 2:ee4ec2d72525 | 46 | if (!delimited) return; |
andrewboyson | 2:ee4ec2d72525 | 47 | |
andrewboyson | 6:b98aa0ec75a1 | 48 | //Push the first digit |
andrewboyson | 2:ee4ec2d72525 | 49 | char h = c >> 4; |
andrewboyson | 2:ee4ec2d72525 | 50 | if (h < 10) h += '0'; |
andrewboyson | 2:ee4ec2d72525 | 51 | else h += 'A' - 10; |
andrewboyson | 6:b98aa0ec75a1 | 52 | push(h); |
andrewboyson | 6:b98aa0ec75a1 | 53 | |
andrewboyson | 6:b98aa0ec75a1 | 54 | //Push the second digit |
andrewboyson | 2:ee4ec2d72525 | 55 | h = c & 0x0F; |
andrewboyson | 2:ee4ec2d72525 | 56 | if (h < 10) h += '0'; |
andrewboyson | 2:ee4ec2d72525 | 57 | else h += 'A' - 10; |
andrewboyson | 6:b98aa0ec75a1 | 58 | push(h); |
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 | 7:417a6a65e942 | 79 | uart.baud(115200); |
andrewboyson | 0:9907e344c82a | 80 | pPush = buffer; |
andrewboyson | 0:9907e344c82a | 81 | pPull = buffer; |
andrewboyson | 0:9907e344c82a | 82 | return 0; |
andrewboyson | 0:9907e344c82a | 83 | } |
andrewboyson | 10:cf815db8ed57 | 84 | int Log(char* snd) |
andrewboyson | 8:e793e9005f89 | 85 | { |
andrewboyson | 10:cf815db8ed57 | 86 | char* ptr = snd; |
andrewboyson | 10:cf815db8ed57 | 87 | while (*ptr) LogPush(*ptr++); //Send the string to the log buffer |
andrewboyson | 10:cf815db8ed57 | 88 | return ptr - snd; |
andrewboyson | 8:e793e9005f89 | 89 | } |
andrewboyson | 10:cf815db8ed57 | 90 | int LogV(char *fmt, va_list argptr) |
andrewboyson | 0:9907e344c82a | 91 | { |
andrewboyson | 8:e793e9005f89 | 92 | int size = vsnprintf(NULL, 0, fmt, argptr); //Find the size required |
andrewboyson | 8:e793e9005f89 | 93 | char snd[size + 1]; //Allocate enough memory for the size required with an extra byte for the terminating null |
andrewboyson | 8:e793e9005f89 | 94 | vsprintf(snd, fmt, argptr); //Fill the new buffer |
andrewboyson | 10:cf815db8ed57 | 95 | return Log(snd); //Send the string to the log buffer |
andrewboyson | 0:9907e344c82a | 96 | } |
andrewboyson | 10:cf815db8ed57 | 97 | int LogF(char *fmt, ...) |
andrewboyson | 0:9907e344c82a | 98 | { |
andrewboyson | 0:9907e344c82a | 99 | va_list argptr; |
andrewboyson | 0:9907e344c82a | 100 | va_start(argptr, fmt); |
andrewboyson | 10:cf815db8ed57 | 101 | int size = LogV(fmt, argptr); |
andrewboyson | 0:9907e344c82a | 102 | va_end(argptr); |
andrewboyson | 10:cf815db8ed57 | 103 | return size; |
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 | 10:cf815db8ed57 | 147 | static int logTimeOnly() |
andrewboyson | 0:9907e344c82a | 148 | { |
andrewboyson | 0:9907e344c82a | 149 | struct tm tm; |
andrewboyson | 7:417a6a65e942 | 150 | ClockTmUtc(&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 | 10:cf815db8ed57 | 161 | return 17; |
andrewboyson | 0:9907e344c82a | 162 | } |
andrewboyson | 10:cf815db8ed57 | 163 | int LogTime(char *snd) |
andrewboyson | 0:9907e344c82a | 164 | { |
andrewboyson | 10:cf815db8ed57 | 165 | int size = 0; |
andrewboyson | 10:cf815db8ed57 | 166 | size += logTimeOnly(); |
andrewboyson | 10:cf815db8ed57 | 167 | size++; LogPush(' '); |
andrewboyson | 10:cf815db8ed57 | 168 | size += Log(snd); |
andrewboyson | 10:cf815db8ed57 | 169 | return size; |
andrewboyson | 0:9907e344c82a | 170 | } |
andrewboyson | 10:cf815db8ed57 | 171 | int LogTimeF(char *fmt, ...) |
andrewboyson | 0:9907e344c82a | 172 | { |
andrewboyson | 10:cf815db8ed57 | 173 | int size = 0; |
andrewboyson | 0:9907e344c82a | 174 | va_list argptr; |
andrewboyson | 0:9907e344c82a | 175 | va_start(argptr, fmt); |
andrewboyson | 10:cf815db8ed57 | 176 | size == logTimeOnly(); |
andrewboyson | 10:cf815db8ed57 | 177 | size++; LogPush(' '); |
andrewboyson | 10:cf815db8ed57 | 178 | size += LogV(fmt, argptr); |
andrewboyson | 0:9907e344c82a | 179 | va_end(argptr); |
andrewboyson | 10:cf815db8ed57 | 180 | return size; |
andrewboyson | 0:9907e344c82a | 181 | } |