First commit
[anna.git] / source / core / tracing / TraceLevelChecker.cpp
1 // ANNA - Anna is Not 'N' Anymore
2 //
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
4 //
5 // https://bitbucket.org/testillano/anna
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
10 //
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
16 // distribution.
17 //     * Neither the name of Google Inc. 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.
20 //
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.
32 //
33 // Authors: eduardo.ramos.testillano@gmail.com
34 //          cisco.tierra@gmail.com
35
36
37 #include <anna/core/tracing/TraceWriter.hpp>
38 #include <anna/core/functions.hpp>
39
40 // Local
41 #include <anna/core/tracing/TraceLevelChecker.hpp>
42 #include <anna/core/tracing/Configuration.hpp>
43
44 // STL
45 #include <map>
46 #include <string>
47
48
49 using namespace anna::tracing;
50 using namespace anna;
51
52
53
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();
61
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;
67
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;
72           return true;
73         }
74       }
75     } else {
76       Configuration::trace_trigger_map_t::const_iterator it = traceTriggers.find(contextData);
77
78       if(it != traceTriggers.end()) {  // found (strict match)
79         targetLevel = (*it).second;
80         return true;
81       }
82     }
83   }
84
85   return (false);
86 }
87
88
89 //------------------------------------------------------------------------------
90 //---------------------------------------------------- TraceLevelChecker::load()
91 //------------------------------------------------------------------------------
92 bool TraceLevelChecker::load(const char * contextData) throw() {
93   a_previousLevel = Logger::getLevel();
94
95   if(contextData) a_contextData = contextData;  // update with valid string provided
96
97   anna::Logger::Level targetLevel;
98
99   if(changeLevelCondition(a_contextData, targetLevel)) {
100     Logger::setLevel(targetLevel);
101     LOGDEBUG
102     (
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();
107       msg += "'";
108       Logger::debug(msg, ANNA_FILE_LOCATION);
109     );
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));
111     return (true);
112   }
113
114   return (false);
115 }
116
117
118 //------------------------------------------------------------------------------
119 //------------------------------------------------- TraceLevelChecker::restore()
120 //------------------------------------------------------------------------------
121 bool TraceLevelChecker::restore() throw() {
122   anna::Logger::Level currentLevel = Logger::getLevel();
123
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);
127     return (true);
128   }
129
130   return (false);
131 }
132