Qubit 2020 / presensfirmwareupdate

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers debug.cpp Source File

debug.cpp

00001 #include <stdarg.h>
00002 #include "mbed.h"
00003 #include "cisme.h"
00004 #include "debug.h"
00005 
00006 #define DEBUG_OUTPUT_FILE_NAME "/"FSNAME"/debug.log"
00007 
00008 
00009 static int debugLvl = DEBUG_LEVEL_INFO;
00010 /* Main debug port. */
00011 static Serial debugComm(USBTX,USBRX);
00012 
00013 void debugInit(int initDebugLvl)
00014 {
00015     /* Actually this baud rate is used by default according to the documentation. */
00016     debugComm.baud(DEBUG_BAUD);
00017     /* Set the communication settings. */
00018     debugComm.format(DEBUG_NO_BITS, DEBUG_PARITY, DEBUG_NO_STOP_BITS);
00019 
00020     debugLvl = initDebugLvl;
00021 }
00022 
00023 void debugSetLvl(int newDebugLvl)
00024 {
00025     debugLvl = newDebugLvl;
00026 }
00027 
00028 int debugGetCurrLvl()
00029 {
00030     return debugLvl;
00031 }
00032 
00033 void debugPrintf(char* file, int line, const char* func, int traceLvl, char* formatStr, ...)
00034 {
00035     static char traceBuffer[DEBUG_MAX_TRACE_LENGTH];
00036     va_list params;
00037     time_t  seconds;
00038 
00039     if (traceLvl > debugLvl) {
00040         return;
00041     }
00042 
00043     va_start(params, formatStr);
00044 
00045     vsprintf(traceBuffer, formatStr, params);
00046 
00047     va_end(params);
00048 
00049     seconds = time(NULL);
00050 
00051     debugComm.printf("[%.24s]:%s:%s%d:%s %s\n\r", ctime(&seconds), debugFormatTraceLevel(traceLvl), file, line, func, traceBuffer);
00052     
00053 #ifdef DEBUG_TO_FILE
00054     static FILE* debugOutFile_p = NULL;
00055     debugOutFile_p = fopen(DEBUG_OUTPUT_FILE_NAME, "a");
00056 
00057     if (debugOutFile_p != NULL) {
00058         fprintf(debugOutFile_p, "[%.24s]:%s:%s:%d:%s %s\n", ctime(&seconds), debugFormatTraceLevel(traceLvl), file, line, func, traceBuffer);
00059         fclose(debugOutFile_p);
00060         debugOutFile_p = NULL;
00061     }
00062 #endif
00063 }
00064 
00065 const char* debugFormatTraceLevel(int val)
00066 {
00067     switch (val) {
00068         case DEBUG_LEVEL_ERROR:
00069             return "ERROR";
00070         case DEBUG_LEVEL_INFO:
00071             return "INFO";
00072         case DEBUG_LEVEL_1:
00073             return "LEVEL1";
00074         case DEBUG_LEVEL_2:
00075             return "LEVEL2";
00076         case DEBUG_LEVEL_3:
00077             return "LEVEL3";
00078         case DEBUG_LEVEL_4:
00079             return "LEVEL4";
00080         case DEBUG_LEVEL_5:
00081             return "LEVEL5";
00082         default:
00083             return "Unknown";
00084     }
00085 }