1 // ANNA - Anna is Not Nothingness Anymore //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
9 #include <anna/core/tracing/TraceWriter.hpp>
10 #include <anna/core/functions.hpp>
13 #include <anna/core/tracing/TraceLevelChecker.hpp>
14 #include <anna/core/tracing/Configuration.hpp>
21 using namespace anna::tracing;
26 //------------------------------------------------------------------------------
27 //------------------------------------ TraceLevelChecker::changeLevelCondition()
28 //------------------------------------------------------------------------------
29 bool TraceLevelChecker::changeLevelCondition(const std::string & contextData, anna::Logger::Level & targetLevel) const throw() {
30 Configuration & conf = Configuration::instantiate();
31 Configuration::trace_trigger_map_t::const_iterator it;
32 Configuration::trace_trigger_map_t & traceTriggers = (Configuration::trace_trigger_map_t &)conf.getTraceTriggers();
34 if(traceTriggers.size() != 0) {
35 if(conf.readRegexpForTraceTriggers()) {
36 for(it = traceTriggers.begin(); it != traceTriggers.end(); ++it) {
37 //std::string triggerReference = (*it).first;
38 anna::RegularExpression & triggerReference = (anna::RegularExpression &)(*it).first;
40 // /*contain*/ if (contextData.find(triggerReference) != std::string::npos /*-1*/) {
41 //if (anna::functions::isLike(triggerReference.c_str(), contextData)) {
42 if(triggerReference.isLike(contextData)) {
43 targetLevel = (*it).second;
48 Configuration::trace_trigger_map_t::const_iterator it = traceTriggers.find(contextData);
50 if(it != traceTriggers.end()) { // found (strict match)
51 targetLevel = (*it).second;
61 //------------------------------------------------------------------------------
62 //---------------------------------------------------- TraceLevelChecker::load()
63 //------------------------------------------------------------------------------
64 bool TraceLevelChecker::load(const char * contextData) throw() {
65 a_previousLevel = Logger::getLevel();
67 if(contextData) a_contextData = contextData; // update with valid string provided
69 anna::Logger::Level targetLevel;
71 if(changeLevelCondition(a_contextData, targetLevel)) {
72 Logger::setLevel(targetLevel);
75 std::string msg = "TraceLevelChecker::changeLevelCondition() is true for selective tracing: context data = '";
77 msg += "', trigger reference set = '";
78 msg += (Configuration::instantiate()).asString();
80 Logger::debug(msg, ANNA_FILE_LOCATION);
82 LOGNOTICE(Logger::notice(anna::functions::asString("Dynamic trace level changed (%s -> %s): context matchs condition for selective tracing", Logger::asString(a_previousLevel), Logger::asString(targetLevel)), ANNA_FILE_LOCATION));
90 //------------------------------------------------------------------------------
91 //------------------------------------------------- TraceLevelChecker::restore()
92 //------------------------------------------------------------------------------
93 bool TraceLevelChecker::restore() throw() {
94 anna::Logger::Level currentLevel = Logger::getLevel();
96 if(currentLevel != a_previousLevel /* level at constructor begining */) {
97 LOGNOTICE(Logger::notice(anna::functions::asString("Dynamic trace level restored (%s -> %s)", Logger::asString(currentLevel), Logger::asString(a_previousLevel)), ANNA_FILE_LOCATION));
98 Logger::setLevel(a_previousLevel);