From a1191b5fb008ea8ce480ebff2679d7d4479de7be Mon Sep 17 00:00:00 2001 From: Jobin Johnson Date: Mon, 28 Jan 2019 05:18:25 +0530 Subject: [PATCH] Include log levels for debug messages Signed-off-by: Jobin Johnson --- QSim.cpp | 5 ++--- utils/include/util.h | 12 +++++++++++- utils/util.cpp | 35 +++++++++++++++++++++++++++++++++-- 3 files changed, 46 insertions(+), 6 deletions(-) diff --git a/QSim.cpp b/QSim.cpp index 90529f4..f1fbe19 100644 --- a/QSim.cpp +++ b/QSim.cpp @@ -11,12 +11,11 @@ using namespace std; -extern int a; -int a=30; +int LOG_LEVEL = LOG_LEVEL_VERBOSE; int main(int argc, char **argv) { - printdebug("Hey There : "); + printdebug(LOG_LEVEL_INFO, "Simulator Started"); return 0; } diff --git a/utils/include/util.h b/utils/include/util.h index d141a6a..aca1cfd 100644 --- a/utils/include/util.h +++ b/utils/include/util.h @@ -8,6 +8,16 @@ #ifndef UTILS_UTIL_H_ #define UTILS_UTIL_H_ -extern void printdebug(const char*); +// LOG LEVELS +#define LOG_LEVEL_VERBOSE 0 +#define LOG_LEVEL_INFO 1 +#define LOG_LEVEL_WARN 2 +#define LOG_LEVEL_ERROR 3 +#define LOG_LEVEL_FATAL 4 + +extern int LOG_LEVEL; + +extern void printdebug(unsigned short int, const char*); +extern char getLevel(unsigned short int); #endif /* UTILS_UTIL_H_ */ diff --git a/utils/util.cpp b/utils/util.cpp index 9be29ca..75d1455 100644 --- a/utils/util.cpp +++ b/utils/util.cpp @@ -8,8 +8,39 @@ #include #include -void printdebug(const char* message){ - std::cout << message; +char getLevel(unsigned short int level) { + char type; + switch (level) { + case LOG_LEVEL_VERBOSE: + type = 'V'; + break; + case LOG_LEVEL_INFO: + type = 'I'; + break; + case LOG_LEVEL_WARN: + type = 'W'; + break; + case LOG_LEVEL_ERROR: + type = 'E'; + break; + case LOG_LEVEL_FATAL: + type = 'F'; + break; + default: + type = 'V'; + } + + return type; +} + +void printdebug(unsigned short int level, const char* message) { + char type = getLevel(level); + + if (level < LOG_LEVEL) { + return; + } + + std::cout << type << ": " << message << "\n"; // TODO add debugging options }