Andrew Boyson
/
iot
Backing up an unused program in case of future need
log.cpp@4:e076884ef8bd, 2016-05-03 (annotated)
- Committer:
- andrewboyson
- Date:
- Tue May 03 09:23:26 2016 +0000
- Revision:
- 4:e076884ef8bd
- Parent:
- 2:06fa34661f19
- Child:
- 6:be97d38e0b01
Added 1-wire
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 | 2:06fa34661f19 | 64 | int LogInit() |
andrewboyson | 0:09f915e6f9f6 | 65 | { |
andrewboyson | 0:09f915e6f9f6 | 66 | pPush = buffer; |
andrewboyson | 0:09f915e6f9f6 | 67 | pPull = buffer; |
andrewboyson | 2:06fa34661f19 | 68 | return 0; |
andrewboyson | 0:09f915e6f9f6 | 69 | } |
andrewboyson | 0:09f915e6f9f6 | 70 | void LogF(char *fmt, ...) |
andrewboyson | 0:09f915e6f9f6 | 71 | { |
andrewboyson | 0:09f915e6f9f6 | 72 | //Set up variable arguments |
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 | 4:e076884ef8bd | 77 | int size = vsnprintf(NULL, 0, fmt, argptr); |
andrewboyson | 0:09f915e6f9f6 | 78 | |
andrewboyson | 0:09f915e6f9f6 | 79 | //Allocate enough memory for the size required with an extra byte for the terminating null |
andrewboyson | 4:e076884ef8bd | 80 | char snd[size + 1]; |
andrewboyson | 0:09f915e6f9f6 | 81 | |
andrewboyson | 0:09f915e6f9f6 | 82 | //Fill the new buffer |
andrewboyson | 4:e076884ef8bd | 83 | vsprintf(snd, fmt, argptr); |
andrewboyson | 0:09f915e6f9f6 | 84 | |
andrewboyson | 0:09f915e6f9f6 | 85 | //Finish with variable arguments |
andrewboyson | 0:09f915e6f9f6 | 86 | va_end(argptr); |
andrewboyson | 0:09f915e6f9f6 | 87 | |
andrewboyson | 0:09f915e6f9f6 | 88 | //Send the string to the log buffer |
andrewboyson | 0:09f915e6f9f6 | 89 | for (char* ptr = snd; *ptr; ptr++) LogPush(*ptr); |
andrewboyson | 0:09f915e6f9f6 | 90 | |
andrewboyson | 0:09f915e6f9f6 | 91 | } |
andrewboyson | 4:e076884ef8bd | 92 | void LogTime() |
andrewboyson | 4:e076884ef8bd | 93 | { |
andrewboyson | 4:e076884ef8bd | 94 | uint32_t now = TimeGet(); |
andrewboyson | 4:e076884ef8bd | 95 | int size = snprintf(NULL, 0, DATE_FORMAT, now); |
andrewboyson | 4:e076884ef8bd | 96 | char snd[size + 1]; |
andrewboyson | 4:e076884ef8bd | 97 | sprintf(snd, DATE_FORMAT, now); |
andrewboyson | 4:e076884ef8bd | 98 | for (char* ptr = snd; *ptr; ptr++) LogPush(*ptr); |
andrewboyson | 4:e076884ef8bd | 99 | } |
andrewboyson | 0:09f915e6f9f6 | 100 | void LogCrLf (char * text) |
andrewboyson | 0:09f915e6f9f6 | 101 | { |
andrewboyson | 0:09f915e6f9f6 | 102 | LogF("%s\r\n", text); |
andrewboyson | 0:09f915e6f9f6 | 103 | } |
andrewboyson | 0:09f915e6f9f6 | 104 | void LogSave() |
andrewboyson | 0:09f915e6f9f6 | 105 | { |
andrewboyson | 0:09f915e6f9f6 | 106 | FILE *fp = fopen(LOG_FILE, "w"); |
andrewboyson | 0:09f915e6f9f6 | 107 | LogEnumerateStart(); |
andrewboyson | 0:09f915e6f9f6 | 108 | while(1) |
andrewboyson | 0:09f915e6f9f6 | 109 | { |
andrewboyson | 0:09f915e6f9f6 | 110 | int c = LogEnumerate(); |
andrewboyson | 0:09f915e6f9f6 | 111 | if (c == EOF) break; |
andrewboyson | 0:09f915e6f9f6 | 112 | putc(c, fp); |
andrewboyson | 0:09f915e6f9f6 | 113 | } |
andrewboyson | 0:09f915e6f9f6 | 114 | fclose(fp); |
andrewboyson | 0:09f915e6f9f6 | 115 | } |