Andrew Boyson
/
iot
Backing up an unused program in case of future need
log.cpp@1:94282484baae, 2016-04-15 (annotated)
- Committer:
- andrewboyson
- Date:
- Fri Apr 15 09:03:54 2016 +0000
- Revision:
- 1:94282484baae
- Parent:
- 0:09f915e6f9f6
- Child:
- 2:06fa34661f19
Streamed the log in chunks to allow the send buffer to be reduced. That in turn allowed the log buffer to be increased.; Added content type text/html to http responses.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andrewboyson | 0:09f915e6f9f6 | 1 | #include "mbed.h" |
andrewboyson | 0:09f915e6f9f6 | 2 | #include <stdarg.h> |
andrewboyson | 0:09f915e6f9f6 | 3 | #include "time.h" |
andrewboyson | 0:09f915e6f9f6 | 4 | #define LOG_FILE "/local/log.txt" |
andrewboyson | 0:09f915e6f9f6 | 5 | #define DATE_FORMAT "%05d " |
andrewboyson | 0:09f915e6f9f6 | 6 | |
andrewboyson | 0:09f915e6f9f6 | 7 | LocalFileSystem local("local"); |
andrewboyson | 0:09f915e6f9f6 | 8 | |
andrewboyson | 1:94282484baae | 9 | #define BUFFER_LENGTH 2048 |
andrewboyson | 0:09f915e6f9f6 | 10 | static char buffer[BUFFER_LENGTH]; |
andrewboyson | 0:09f915e6f9f6 | 11 | static char* pPush; //Initialised in init |
andrewboyson | 0:09f915e6f9f6 | 12 | static char* pPull; //Initialised in init |
andrewboyson | 0:09f915e6f9f6 | 13 | static int enable = true; |
andrewboyson | 0:09f915e6f9f6 | 14 | static char* incrementPushPullPointer(char* p, char* buffer, int bufferLength) |
andrewboyson | 0:09f915e6f9f6 | 15 | { |
andrewboyson | 0:09f915e6f9f6 | 16 | p++; //increment the pointer by one |
andrewboyson | 0:09f915e6f9f6 | 17 | if (p == buffer + bufferLength) p = buffer; //if the pointer is now beyond the end then point it back to the start |
andrewboyson | 0:09f915e6f9f6 | 18 | return p; |
andrewboyson | 0:09f915e6f9f6 | 19 | } |
andrewboyson | 0:09f915e6f9f6 | 20 | void LogPush(char c) |
andrewboyson | 0:09f915e6f9f6 | 21 | { |
andrewboyson | 0:09f915e6f9f6 | 22 | //Only add if allowed |
andrewboyson | 0:09f915e6f9f6 | 23 | if (!enable) return; |
andrewboyson | 0:09f915e6f9f6 | 24 | |
andrewboyson | 0:09f915e6f9f6 | 25 | //Move the pull position if about to run into it |
andrewboyson | 0:09f915e6f9f6 | 26 | char* pNext = incrementPushPullPointer(pPush, buffer, BUFFER_LENGTH); |
andrewboyson | 0:09f915e6f9f6 | 27 | if (pNext == pPull) pPull = incrementPushPullPointer(pPull, buffer, BUFFER_LENGTH); |
andrewboyson | 0:09f915e6f9f6 | 28 | |
andrewboyson | 0:09f915e6f9f6 | 29 | //Add the character at the push position |
andrewboyson | 0:09f915e6f9f6 | 30 | *pPush = c; |
andrewboyson | 0:09f915e6f9f6 | 31 | pPush = incrementPushPullPointer(pPush, buffer, BUFFER_LENGTH); |
andrewboyson | 0:09f915e6f9f6 | 32 | } |
andrewboyson | 0:09f915e6f9f6 | 33 | static char *pEnumerate; |
andrewboyson | 0:09f915e6f9f6 | 34 | static int hadNull; |
andrewboyson | 0:09f915e6f9f6 | 35 | void LogEnumerateStart() |
andrewboyson | 0:09f915e6f9f6 | 36 | { |
andrewboyson | 0:09f915e6f9f6 | 37 | pEnumerate = pPull; |
andrewboyson | 0:09f915e6f9f6 | 38 | hadNull = false; |
andrewboyson | 0:09f915e6f9f6 | 39 | } |
andrewboyson | 0:09f915e6f9f6 | 40 | int LogEnumerate() |
andrewboyson | 0:09f915e6f9f6 | 41 | { |
andrewboyson | 0:09f915e6f9f6 | 42 | if (hadNull) |
andrewboyson | 0:09f915e6f9f6 | 43 | { |
andrewboyson | 0:09f915e6f9f6 | 44 | hadNull = false; |
andrewboyson | 0:09f915e6f9f6 | 45 | return '0'; |
andrewboyson | 0:09f915e6f9f6 | 46 | } |
andrewboyson | 0:09f915e6f9f6 | 47 | if (pEnumerate == pPush) return EOF; |
andrewboyson | 0:09f915e6f9f6 | 48 | char c = *pEnumerate; |
andrewboyson | 0:09f915e6f9f6 | 49 | pEnumerate = incrementPushPullPointer(pEnumerate, buffer, BUFFER_LENGTH); |
andrewboyson | 0:09f915e6f9f6 | 50 | if (c) |
andrewboyson | 0:09f915e6f9f6 | 51 | { |
andrewboyson | 0:09f915e6f9f6 | 52 | return c; |
andrewboyson | 0:09f915e6f9f6 | 53 | } |
andrewboyson | 0:09f915e6f9f6 | 54 | else |
andrewboyson | 0:09f915e6f9f6 | 55 | { |
andrewboyson | 0:09f915e6f9f6 | 56 | hadNull = true; |
andrewboyson | 0:09f915e6f9f6 | 57 | return '\\'; |
andrewboyson | 0:09f915e6f9f6 | 58 | } |
andrewboyson | 0:09f915e6f9f6 | 59 | } |
andrewboyson | 0:09f915e6f9f6 | 60 | void LogEnable(int on) |
andrewboyson | 0:09f915e6f9f6 | 61 | { |
andrewboyson | 0:09f915e6f9f6 | 62 | enable = on; |
andrewboyson | 0:09f915e6f9f6 | 63 | } |
andrewboyson | 0:09f915e6f9f6 | 64 | void LogInit() |
andrewboyson | 0:09f915e6f9f6 | 65 | { |
andrewboyson | 0:09f915e6f9f6 | 66 | pPush = buffer; |
andrewboyson | 0:09f915e6f9f6 | 67 | pPull = buffer; |
andrewboyson | 0:09f915e6f9f6 | 68 | } |
andrewboyson | 0:09f915e6f9f6 | 69 | void LogF(char *fmt, ...) |
andrewboyson | 0:09f915e6f9f6 | 70 | { |
andrewboyson | 0:09f915e6f9f6 | 71 | //Set up variable arguments |
andrewboyson | 0:09f915e6f9f6 | 72 | uint32_t now = TimeGet(); |
andrewboyson | 0:09f915e6f9f6 | 73 | va_list argptr; |
andrewboyson | 0:09f915e6f9f6 | 74 | va_start(argptr, fmt); |
andrewboyson | 0:09f915e6f9f6 | 75 | |
andrewboyson | 0:09f915e6f9f6 | 76 | //Find the size required |
andrewboyson | 0:09f915e6f9f6 | 77 | int sizeTime = snprintf(NULL, 0, DATE_FORMAT, now); |
andrewboyson | 0:09f915e6f9f6 | 78 | int sizeLog = vsnprintf(NULL, 0, fmt, argptr); |
andrewboyson | 0:09f915e6f9f6 | 79 | |
andrewboyson | 0:09f915e6f9f6 | 80 | //Allocate enough memory for the size required with an extra byte for the terminating null |
andrewboyson | 0:09f915e6f9f6 | 81 | char snd[sizeTime + sizeLog + 1]; |
andrewboyson | 0:09f915e6f9f6 | 82 | |
andrewboyson | 0:09f915e6f9f6 | 83 | //Fill the new buffer |
andrewboyson | 0:09f915e6f9f6 | 84 | sprintf(snd, DATE_FORMAT, now); |
andrewboyson | 0:09f915e6f9f6 | 85 | vsprintf(snd + sizeTime, fmt, argptr); |
andrewboyson | 0:09f915e6f9f6 | 86 | |
andrewboyson | 0:09f915e6f9f6 | 87 | //Finish with variable arguments |
andrewboyson | 0:09f915e6f9f6 | 88 | va_end(argptr); |
andrewboyson | 0:09f915e6f9f6 | 89 | |
andrewboyson | 0:09f915e6f9f6 | 90 | //Send the string to the log buffer |
andrewboyson | 0:09f915e6f9f6 | 91 | for (char* ptr = snd; *ptr; ptr++) LogPush(*ptr); |
andrewboyson | 0:09f915e6f9f6 | 92 | |
andrewboyson | 0:09f915e6f9f6 | 93 | } |
andrewboyson | 0:09f915e6f9f6 | 94 | void LogCrLf (char * text) |
andrewboyson | 0:09f915e6f9f6 | 95 | { |
andrewboyson | 0:09f915e6f9f6 | 96 | LogF("%s\r\n", text); |
andrewboyson | 0:09f915e6f9f6 | 97 | } |
andrewboyson | 0:09f915e6f9f6 | 98 | void LogSave() |
andrewboyson | 0:09f915e6f9f6 | 99 | { |
andrewboyson | 0:09f915e6f9f6 | 100 | FILE *fp = fopen(LOG_FILE, "w"); |
andrewboyson | 0:09f915e6f9f6 | 101 | LogEnumerateStart(); |
andrewboyson | 0:09f915e6f9f6 | 102 | while(1) |
andrewboyson | 0:09f915e6f9f6 | 103 | { |
andrewboyson | 0:09f915e6f9f6 | 104 | int c = LogEnumerate(); |
andrewboyson | 0:09f915e6f9f6 | 105 | if (c == EOF) break; |
andrewboyson | 0:09f915e6f9f6 | 106 | putc(c, fp); |
andrewboyson | 0:09f915e6f9f6 | 107 | } |
andrewboyson | 0:09f915e6f9f6 | 108 | fclose(fp); |
andrewboyson | 0:09f915e6f9f6 | 109 | } |