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 //
11 #include <anna/core/tracing/Logger.hpp>
13 #include <anna/xml/Node.hpp>
14 #include <anna/xml/Attribute.hpp>
16 #include <anna/timex/Engine.hpp>
18 #include <anna/core/oam/CounterScope.hpp>
19 #include <anna/core/oam/Counter.hpp>
24 #define test_range(index) \
25 if (index < 0 || index >= MaxCounter) { \
26 string msg (asString ()); \
27 msg += functions::asText (" | Index: ", index); \
28 msg += functions::asText (" | Out of range [0, MaxCounter): ", index); \
29 throw RuntimeException (msg, __FILE__, __LINE__); \
32 #define test_instance(index) \
33 if (a_counters [index] == NULL) { \
34 string msg (asString ()); \
35 msg += functions::asText (" | CounterId: ", index); \
36 msg += " | Counter Id is not defined yet"; \
37 throw RuntimeException (msg, __FILE__, __LINE__); \
41 oam::CounterScope::~CounterScope() {
42 for(int i = 0; i < MaxCounter; i ++) {
43 if(a_counters [i] != NULL)
44 delete a_counters [i];
48 void oam::CounterScope::create(const int counter, const char* name)
52 if(a_counters [counter] != NULL) {
53 string msg("Counter '");
56 msg += functions::asString(counter);
57 msg += ") already registered as ";
58 msg += a_counters [counter]->asString();
59 throw RuntimeException(msg, ANNA_FILE_LOCATION);
62 a_counters [counter] = new Counter(*this, counter, name);
64 string msg("Creation | ");
67 msg += a_counters [counter]->asString();
68 Logger::write(Logger::Debug, msg, ANNA_FILE_LOCATION)
72 oam::Counter::type_t oam::CounterScope::increment(const int counter, const oam::Counter::type_t value)
75 test_instance(counter);
76 a_counters [counter]->a_value += value;
77 a_counters [counter]->a_accValue += value;
78 LOGINFORMATION(a_counters [counter]->debug());
79 return a_counters [counter]->a_value;
82 oam::Counter::type_t oam::CounterScope::assign(const int counter, const oam::Counter::type_t value)
85 test_instance(counter);
86 a_counters [counter]->a_value = value;
87 a_counters [counter]->a_accValue = value;
88 LOGINFORMATION(a_counters [counter]->debug());
92 oam::Counter::type_t oam::CounterScope::getValue(const int counter) const
95 test_instance(counter);
96 return a_counters [counter]->a_value;
99 U64 oam::CounterScope::getAccValue(const int counter) const
102 test_instance(counter);
103 return a_counters [counter]->a_accValue;
106 int oam::CounterScope::resetAccValues() noexcept(false) {
107 int result = 0; // affected counters
109 for(int ii = 0; ii < MaxCounter; ii ++)
111 result += (a_counters [ii]->resetAcc()) ? 1 : 0;
116 const oam::Counter* oam::CounterScope::getCounter(const int counter) const
119 test_instance(counter);
120 return a_counters [counter];
123 string oam::CounterScope::asString() const
125 string result("oam::CounterScope { Id: ");
126 result += functions::asString(a_id);
127 result += " | Name: ";
129 return result += " }";
132 xml::Node* oam::CounterScope::asXML(xml::Node* parent) const
134 xml::Node* result = parent->createChild("Scope");
137 for(int ii = 0; ii < MaxCounter; ii ++) {
138 if(a_counters [ii] == NULL)
141 counter = result->createChild("Counter");
142 counter->createAttribute("Id", ii);
143 counter->createAttribute("Name", a_counters [ii]->getName());
144 counter->createAttribute("Value", a_counters [ii]->getValue());
145 counter->createAttribute("AccValue", a_counters [ii]->getAccumulatedValue());