Log

Dependents:   oldheating gps motorhome heating

Committer:
andrewboyson
Date:
Fri Sep 22 13:40:50 2017 +0000
Revision:
8:e793e9005f89
Parent:
7:417a6a65e942
Child:
9:d2c41855ed09
Removed LogCrLf and added a plain Log for text.

Who changed what in which revision?

UserRevisionLine numberNew 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 8:e793e9005f89 84 void Log(char* snd)
andrewboyson 8:e793e9005f89 85 {
andrewboyson 8:e793e9005f89 86 for (char* ptr = snd; *ptr; ptr++) LogPush(*ptr); //Send the string to the log buffer
andrewboyson 8:e793e9005f89 87 }
andrewboyson 0:9907e344c82a 88 void LogV(char *fmt, va_list argptr)
andrewboyson 0:9907e344c82a 89 {
andrewboyson 8:e793e9005f89 90 int size = vsnprintf(NULL, 0, fmt, argptr); //Find the size required
andrewboyson 8:e793e9005f89 91 char snd[size + 1]; //Allocate enough memory for the size required with an extra byte for the terminating null
andrewboyson 8:e793e9005f89 92 vsprintf(snd, fmt, argptr); //Fill the new buffer
andrewboyson 8:e793e9005f89 93 Log(snd); //Send the string to the log buffer
andrewboyson 0:9907e344c82a 94 }
andrewboyson 0:9907e344c82a 95 void LogF(char *fmt, ...)
andrewboyson 0:9907e344c82a 96 {
andrewboyson 0:9907e344c82a 97 va_list argptr;
andrewboyson 0:9907e344c82a 98 va_start(argptr, fmt);
andrewboyson 0:9907e344c82a 99 LogV(fmt, argptr);
andrewboyson 0:9907e344c82a 100 va_end(argptr);
andrewboyson 0:9907e344c82a 101 }
andrewboyson 0:9907e344c82a 102 static void pushuint4(int value)
andrewboyson 0:9907e344c82a 103 {
andrewboyson 0:9907e344c82a 104 if (value > 9999) { LogPush('+'); LogPush('+'); LogPush('+'); LogPush('+'); }
andrewboyson 0:9907e344c82a 105 else if (value < 0) { LogPush('-'); LogPush('-'); LogPush('-'); LogPush('-'); }
andrewboyson 0:9907e344c82a 106 else
andrewboyson 0:9907e344c82a 107 {
andrewboyson 0:9907e344c82a 108 div_t divres;
andrewboyson 0:9907e344c82a 109 int k, c, t, u;
andrewboyson 0:9907e344c82a 110 divres = div(value , 10); u = divres.rem;
andrewboyson 0:9907e344c82a 111 divres = div(divres.quot, 10); t = divres.rem;
andrewboyson 0:9907e344c82a 112 divres = div(divres.quot, 10); c = divres.rem;
andrewboyson 0:9907e344c82a 113 k = divres.quot;
andrewboyson 0:9907e344c82a 114 LogPush(k + '0'); LogPush(c + '0'); LogPush(t + '0'); LogPush(u + '0');
andrewboyson 0:9907e344c82a 115 }
andrewboyson 0:9907e344c82a 116 }
andrewboyson 0:9907e344c82a 117 static void pushuint3(int value)
andrewboyson 0:9907e344c82a 118 {
andrewboyson 0:9907e344c82a 119 if (value > 999) { LogPush('+'); LogPush('+'); LogPush('+'); }
andrewboyson 0:9907e344c82a 120 else if (value < 0) { LogPush('-'); LogPush('-'); LogPush('-'); }
andrewboyson 0:9907e344c82a 121 else
andrewboyson 0:9907e344c82a 122 {
andrewboyson 0:9907e344c82a 123 div_t divres;
andrewboyson 0:9907e344c82a 124 int c, t, u;
andrewboyson 0:9907e344c82a 125 divres = div(value , 10); u = divres.rem;
andrewboyson 0:9907e344c82a 126 divres = div(divres.quot, 10); t = divres.rem;
andrewboyson 0:9907e344c82a 127 c = divres.quot;
andrewboyson 0:9907e344c82a 128 LogPush(c + '0'); LogPush(t + '0'); LogPush(u + '0');
andrewboyson 0:9907e344c82a 129 }
andrewboyson 0:9907e344c82a 130 }
andrewboyson 0:9907e344c82a 131 static void pushuint2(int value)
andrewboyson 0:9907e344c82a 132 {
andrewboyson 0:9907e344c82a 133 if (value > 99) { LogPush('+'); LogPush('+'); }
andrewboyson 0:9907e344c82a 134 else if (value < 0) { LogPush('-'); LogPush('-'); }
andrewboyson 0:9907e344c82a 135 else
andrewboyson 0:9907e344c82a 136 {
andrewboyson 0:9907e344c82a 137 div_t divres;
andrewboyson 0:9907e344c82a 138 int t, u;
andrewboyson 0:9907e344c82a 139 divres = div(value , 10); u = divres.rem;
andrewboyson 0:9907e344c82a 140 t = divres.quot;
andrewboyson 0:9907e344c82a 141 LogPush(t + '0'); LogPush(u + '0');
andrewboyson 0:9907e344c82a 142 }
andrewboyson 0:9907e344c82a 143 }
andrewboyson 0:9907e344c82a 144 void LogTime()
andrewboyson 0:9907e344c82a 145 {
andrewboyson 0:9907e344c82a 146 struct tm tm;
andrewboyson 7:417a6a65e942 147 ClockTmUtc(&tm);
andrewboyson 0:9907e344c82a 148
andrewboyson 0:9907e344c82a 149 pushuint4(tm.tm_year + 1900);
andrewboyson 0:9907e344c82a 150 LogPush('-');
andrewboyson 0:9907e344c82a 151 pushuint3(tm.tm_yday + 1);
andrewboyson 0:9907e344c82a 152 LogPush(' ');
andrewboyson 0:9907e344c82a 153 pushuint2(tm.tm_hour);
andrewboyson 0:9907e344c82a 154 LogPush(':');
andrewboyson 0:9907e344c82a 155 pushuint2(tm.tm_min);
andrewboyson 0:9907e344c82a 156 LogPush(':');
andrewboyson 0:9907e344c82a 157 pushuint2(tm.tm_sec);
andrewboyson 0:9907e344c82a 158 }
andrewboyson 0:9907e344c82a 159 void LogTimeCrLf(char * text)
andrewboyson 0:9907e344c82a 160 {
andrewboyson 0:9907e344c82a 161 LogTime();
andrewboyson 0:9907e344c82a 162 LogF(" %s\r\n", text);
andrewboyson 0:9907e344c82a 163 }
andrewboyson 0:9907e344c82a 164 void LogTimeF(char *fmt, ...)
andrewboyson 0:9907e344c82a 165 {
andrewboyson 0:9907e344c82a 166 va_list argptr;
andrewboyson 0:9907e344c82a 167 va_start(argptr, fmt);
andrewboyson 0:9907e344c82a 168 LogTime();
andrewboyson 0:9907e344c82a 169 LogPush(' ');
andrewboyson 0:9907e344c82a 170 LogV(fmt, argptr);
andrewboyson 0:9907e344c82a 171 va_end(argptr);
andrewboyson 0:9907e344c82a 172 }