Very simple logging library. Allows logging of 4 levels.

Dependents:   NeoPixelI2cSlave NeoPixelI2cSlave

Committer:
dwini
Date:
Fri Oct 23 09:33:22 2015 +0000
Revision:
0:72f7f0c5204b
Very simple logging.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dwini 0:72f7f0c5204b 1 #pragma once
dwini 0:72f7f0c5204b 2
dwini 0:72f7f0c5204b 3 #include "mbed.h"
dwini 0:72f7f0c5204b 4 #include <stdio.h>
dwini 0:72f7f0c5204b 5 #include <stdarg.h>
dwini 0:72f7f0c5204b 6 #include <ctime>
dwini 0:72f7f0c5204b 7
dwini 0:72f7f0c5204b 8 // Define this before loading the header file to enable debug mode
dwini 0:72f7f0c5204b 9 //#define DEBUG_MODE 1
dwini 0:72f7f0c5204b 10
dwini 0:72f7f0c5204b 11 namespace SimplyLog
dwini 0:72f7f0c5204b 12 {
dwini 0:72f7f0c5204b 13 class Log
dwini 0:72f7f0c5204b 14 {
dwini 0:72f7f0c5204b 15 public:
dwini 0:72f7f0c5204b 16 static void e(char* fmt, ...) {
dwini 0:72f7f0c5204b 17 char buffer [80];
dwini 0:72f7f0c5204b 18 Log::getTimestamp(buffer);
dwini 0:72f7f0c5204b 19
dwini 0:72f7f0c5204b 20 va_list args;
dwini 0:72f7f0c5204b 21 va_start(args,fmt);
dwini 0:72f7f0c5204b 22 printf("%s ", buffer);
dwini 0:72f7f0c5204b 23 printf("[ERROR] ");
dwini 0:72f7f0c5204b 24 vprintf(fmt,args);
dwini 0:72f7f0c5204b 25 va_end(args);
dwini 0:72f7f0c5204b 26 }
dwini 0:72f7f0c5204b 27
dwini 0:72f7f0c5204b 28 static void w(char* fmt, ...) {
dwini 0:72f7f0c5204b 29 char buffer [80];
dwini 0:72f7f0c5204b 30 Log::getTimestamp(buffer);
dwini 0:72f7f0c5204b 31
dwini 0:72f7f0c5204b 32 va_list args;
dwini 0:72f7f0c5204b 33 va_start(args,fmt);
dwini 0:72f7f0c5204b 34 printf("%s ", buffer);
dwini 0:72f7f0c5204b 35 printf("[WARNING] ");
dwini 0:72f7f0c5204b 36 vprintf(fmt,args);
dwini 0:72f7f0c5204b 37 va_end(args);
dwini 0:72f7f0c5204b 38 }
dwini 0:72f7f0c5204b 39
dwini 0:72f7f0c5204b 40 static void d(char* fmt, ...) {
dwini 0:72f7f0c5204b 41 #ifdef DEBUG_MODE
dwini 0:72f7f0c5204b 42 char buffer [80];
dwini 0:72f7f0c5204b 43 Log::getTimestamp(buffer);
dwini 0:72f7f0c5204b 44
dwini 0:72f7f0c5204b 45 va_list args;
dwini 0:72f7f0c5204b 46 va_start(args,fmt);
dwini 0:72f7f0c5204b 47 printf("%s ", buffer);
dwini 0:72f7f0c5204b 48 printf("[DEBUG] ");
dwini 0:72f7f0c5204b 49 vprintf(fmt,args);
dwini 0:72f7f0c5204b 50 va_end(args);
dwini 0:72f7f0c5204b 51 #endif
dwini 0:72f7f0c5204b 52 }
dwini 0:72f7f0c5204b 53
dwini 0:72f7f0c5204b 54 static void i(char* fmt, ...) {
dwini 0:72f7f0c5204b 55 #ifdef DEBUG_MODE
dwini 0:72f7f0c5204b 56 char buffer [80];
dwini 0:72f7f0c5204b 57 Log::getTimestamp(buffer);
dwini 0:72f7f0c5204b 58
dwini 0:72f7f0c5204b 59 va_list args;
dwini 0:72f7f0c5204b 60 va_start(args,fmt);
dwini 0:72f7f0c5204b 61 printf("%s ", buffer);
dwini 0:72f7f0c5204b 62 printf("[INFO] ");
dwini 0:72f7f0c5204b 63 vprintf(fmt,args);
dwini 0:72f7f0c5204b 64 va_end(args);
dwini 0:72f7f0c5204b 65 #endif
dwini 0:72f7f0c5204b 66 }
dwini 0:72f7f0c5204b 67
dwini 0:72f7f0c5204b 68 private:
dwini 0:72f7f0c5204b 69 static void getTimestamp(char * buffer) {
dwini 0:72f7f0c5204b 70 time_t rawtime;
dwini 0:72f7f0c5204b 71 struct tm * timeinfo;
dwini 0:72f7f0c5204b 72
dwini 0:72f7f0c5204b 73 time (&rawtime);
dwini 0:72f7f0c5204b 74 timeinfo = localtime (&rawtime);
dwini 0:72f7f0c5204b 75
dwini 0:72f7f0c5204b 76 strftime(buffer, 80, "%d-%m-%Y|%H:%M:%S", timeinfo);
dwini 0:72f7f0c5204b 77 }
dwini 0:72f7f0c5204b 78 };
dwini 0:72f7f0c5204b 79 }