1 // ANNA - Anna is Not Nothingness Anymore
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
5 // http://redmine.teslayout.com/projects/anna-suite
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
11 // * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
17 // * Neither the name of the copyright holder nor the names of its
18 // contributors may be used to endorse or promote products derived from
19 // this software without specific prior written permission.
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 // Authors: eduardo.ramos.testillano@gmail.com
34 // cisco.tierra@gmail.com
37 #include <anna/core/tracing/TraceWriter.hpp>
38 #include <anna/core/functions.hpp>
41 #include <anna/core/tracing/TraceLevelChecker.hpp>
42 #include <anna/core/tracing/Configuration.hpp>
49 using namespace anna::tracing;
54 //------------------------------------------------------------------------------
55 //------------------------------------ TraceLevelChecker::changeLevelCondition()
56 //------------------------------------------------------------------------------
57 bool TraceLevelChecker::changeLevelCondition(const std::string & contextData, anna::Logger::Level & targetLevel) const throw() {
58 Configuration & conf = Configuration::instantiate();
59 Configuration::trace_trigger_map_t::const_iterator it;
60 Configuration::trace_trigger_map_t & traceTriggers = (Configuration::trace_trigger_map_t &)conf.getTraceTriggers();
62 if(traceTriggers.size() != 0) {
63 if(conf.readRegexpForTraceTriggers()) {
64 for(it = traceTriggers.begin(); it != traceTriggers.end(); ++it) {
65 //std::string triggerReference = (*it).first;
66 anna::RegularExpression & triggerReference = (anna::RegularExpression &)(*it).first;
68 // /*contain*/ if (contextData.find(triggerReference) != std::string::npos /*-1*/) {
69 //if (anna::functions::isLike(triggerReference.c_str(), contextData)) {
70 if(triggerReference.isLike(contextData)) {
71 targetLevel = (*it).second;
76 Configuration::trace_trigger_map_t::const_iterator it = traceTriggers.find(contextData);
78 if(it != traceTriggers.end()) { // found (strict match)
79 targetLevel = (*it).second;
89 //------------------------------------------------------------------------------
90 //---------------------------------------------------- TraceLevelChecker::load()
91 //------------------------------------------------------------------------------
92 bool TraceLevelChecker::load(const char * contextData) throw() {
93 a_previousLevel = Logger::getLevel();
95 if(contextData) a_contextData = contextData; // update with valid string provided
97 anna::Logger::Level targetLevel;
99 if(changeLevelCondition(a_contextData, targetLevel)) {
100 Logger::setLevel(targetLevel);
103 std::string msg = "TraceLevelChecker::changeLevelCondition() is true for selective tracing: context data = '";
104 msg += a_contextData;
105 msg += "', trigger reference set = '";
106 msg += (Configuration::instantiate()).asString();
108 Logger::debug(msg, ANNA_FILE_LOCATION);
110 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));
118 //------------------------------------------------------------------------------
119 //------------------------------------------------- TraceLevelChecker::restore()
120 //------------------------------------------------------------------------------
121 bool TraceLevelChecker::restore() throw() {
122 anna::Logger::Level currentLevel = Logger::getLevel();
124 if(currentLevel != a_previousLevel /* level at constructor begining */) {
125 LOGNOTICE(Logger::notice(anna::functions::asString("Dynamic trace level restored (%s -> %s)", Logger::asString(currentLevel), Logger::asString(a_previousLevel)), ANNA_FILE_LOCATION));
126 Logger::setLevel(a_previousLevel);