Added runtime debug level support.

This commit is contained in:
Thulinma 2014-05-29 13:33:31 +02:00
parent 97d2fc864c
commit 064c4ecdcc
3 changed files with 29 additions and 3 deletions

View file

@ -34,9 +34,21 @@
#include <dirent.h> //for getMyExec
bool Util::Config::is_active = false;
unsigned int Util::Config::printDebugLevel = DEBUG;//
std::string Util::Config::libver = PACKAGE_VERSION;
Util::Config::Config(){}
Util::Config::Config(){
//global options here
vals["debug"]["long"] = "debug";
vals["debug"]["short"] = "g";
vals["debug"]["arg"] = "integer";
vals["debug"]["help"] = "The debug level at which messages need to be printed.";
vals["debug"]["value"].append((long long)DEBUG);
/*capabilities["optional"]["debug level"]["name"] = "debug";
capabilities["optional"]["debug level"]["help"] = "The debug level at which messages need to be printed.";
capabilities["optional"]["debug level"]["option"] = "--debug";
capabilities["optional"]["debug level"]["type"] = "integer";*/
}
/// Creates a new configuration manager.
Util::Config::Config(std::string cmd, std::string version){
@ -51,6 +63,11 @@ Util::Config::Config(std::string cmd, std::string version){
vals["help"]["help"] = "Display usage and version information, then exit.";
vals["version"]["value"].append((std::string)PACKAGE_VERSION);
vals["version"]["value"].append(version);
vals["debug"]["long"] = "debug";
vals["debug"]["short"] = "g";
vals["debug"]["arg"] = "integer";
vals["debug"]["help"] = "The debug level at which messages need to be printed.";
vals["debug"]["value"].append((long long)DEBUG);
}
/// Adds an option to the configuration parser.
@ -265,6 +282,7 @@ bool Util::Config::parseArgs(int & argc, char ** & argv){
if (long_i <= arg_count){
return false;
}
printDebugLevel = getInteger("debug");
return true;
}
@ -512,6 +530,8 @@ void Util::Config::addBasicConnectorOptions(JSON::Value & capabilities){
addOption("json", option);
}
/// Gets directory the current executable is stored in.
std::string Util::getMyPath(){
char mypath[500];

View file

@ -23,6 +23,7 @@ namespace Util {
//variables
static std::string libver; ///< Version number of the library as a string.
static bool is_active; ///< Set to true by activate(), set to false by the signal handler.
static unsigned int printDebugLevel;
//functions
Config();
Config(std::string cmd, std::string version);

View file

@ -1,4 +1,6 @@
// Defines to print debug messages.
#ifndef MIST_DEBUG
#define MIST_DEBUG 1
#define DLVL_NONE 0 // All debugging disabled.
#define DLVL_FAIL 1 // Only messages about failed operations.
#define DLVL_ERROR 2 // Only messages about errors and failed operations.
@ -10,10 +12,13 @@
#define DLVL_EXTREME 8 // Everything is reported in extreme detail.
#define DLVL_INSANE 9 // Everything is reported in insane detail.
#define DLVL_DONTEVEN 10 // All messages enabled, even pointless ones.
#if DEBUG > 0
#if DEBUG > -1
#include <stdio.h>
#include <unistd.h>
#define DEBUG_MSG(lvl, msg, ...) if (DEBUG >= lvl){fprintf(stderr, "[%d][%s:%d] " msg "\n", getpid(), __FILE__, __LINE__, ##__VA_ARGS__);}
#include "config.h"
static const char* DBG_LVL_LIST[] = {"NONE","FAIL","ERROR","WARN","DEVEL","MEDIUM","HIGH","VERYHIGH","EXTREME","INSANE","DONTEVEN"};
#define DEBUG_MSG(lvl, msg, ...) if (Util::Config::printDebugLevel >= lvl){fprintf(stderr, "%s [%d][%s:%d] " msg "\n", DBG_LVL_LIST[lvl],getpid(), __FILE__, __LINE__, ##__VA_ARGS__);}
#else
#define DEBUG_MSG(lvl, msg, ...) // Debugging disabled.
#endif
#endif