First commit
[anna.git] / include / anna / core / tracing / Configuration.hpp
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 #ifndef anna_core_tracing_Configuration_hpp
38 #define anna_core_tracing_Configuration_hpp
39
40
41
42 #include <anna/core/Singleton.hpp>
43 #include <anna/core/tracing/Logger.hpp>
44
45 #include <anna/core/util/RegularExpression.hpp>
46
47 // STL
48 #include <string>
49 #include <map>
50
51
52
53 namespace anna {
54
55 namespace tracing {
56
57
58 /**
59 * Class used for selective tracing configuration
60 */
61 class Configuration : public anna::Singleton <Configuration> {
62
63
64 public:
65
66   typedef std::map<anna::RegularExpression, anna::Logger::Level> trace_trigger_map_t;
67
68 private:
69
70   trace_trigger_map_t a_traceTriggers; // configured triggers map, for selective tracing
71   bool a_readTraceTriggersAsRegexp; // boolean to configure the way in which trace triggers are interpreted
72
73
74   Configuration() { a_readTraceTriggersAsRegexp = false; }  // private constructor
75
76   friend class anna::Singleton <Configuration>;
77   friend class TraceLevelChecker;
78
79
80   const trace_trigger_map_t & getTraceTriggers() const throw() { return a_traceTriggers; }
81
82
83 public:
84
85   /**
86   * Deletes all added triggers for selective tracing.
87   */
88   void resetTraceTriggers() throw() { a_traceTriggers.clear(); }
89
90
91   /**
92   * Activates a trigger for selective tracing. The normal way is to activate only one
93   * (usually applications register MSISDN, but we prefer the name 'trigger' because
94   * selective tracing capabilities are generalized for any context data we want to detect).
95   * Anyway, it is possible to add several string references for multiple context log activation.
96   *
97   * @param trigger String reference for contexts comparison
98   * @param accumulate Boolean for trigger filter accumulation over internal set. I.e., application
99   * could test any MSISDN within a configured set. False by default, means activation for only one
100   * trigger reference.
101   * @param level Desired trace level when context fulfill this trigger reference. Debug by default
102   */
103   void activateTraceTrigger(const std::string & trigger, bool accumulate = false, anna::Logger::Level level = anna::Logger::Debug) throw();
104
105
106   /**
107   * Deletes a trigger for selective tracing.
108   *
109   * @param trigger String reference to be erased from internal trigger set
110   */
111   void deactivateTraceTrigger(const std::string & trigger) throw();
112
113
114   /**
115   * Interpret all trace triggers as regular expressions.
116   * Is used from TraceLevelChecker to provide strict or regexp-based checkings.
117   * If not configured, regexp is disabled.
118   *
119   * @return Boolean about if regular expressions interpretation are activated.
120   */
121   bool readRegexpForTraceTriggers() const throw() { return (a_readTraceTriggersAsRegexp); }
122
123
124   /**
125   * Configure all trace triggers to be interpreted as regular expressions.
126   * Application can set this depending on trace filtering needs.
127   * If not configured, regexp is disabled for TraceLevelChecker.
128   *
129   * @param readRegexp Boolean for activation/deactivation of regexp funtionality
130   */
131   void useRegexpForTraceTriggers(bool readRegexp = true) throw() { a_readTraceTriggersAsRegexp = readRegexp; }
132
133
134   /**
135   * Class string representation
136   *
137   * @return String with class content
138   */
139   std::string asString(void) const throw();
140 };
141
142 }
143 }
144
145 #endif
146