Allow to log statistics samples as a event operation
[anna.git] / example / diameter / launcher / Launcher.cpp
index 88e4c44..116c325 100644 (file)
@@ -745,34 +745,11 @@ throw(anna::RuntimeException) {
     a_timeEngine->activate(a_counterRecorderClock); // start clock
   }
 
-  // Log statistics concepts
-  if(cl.exists("logStatisticSamples")) {
-    std::string list = cl.getValue("logStatisticSamples");
-    anna::statistics::Engine &statEngine = anna::statistics::Engine::instantiate();
-
-    if(list == "all") {
-      if(statEngine.enableSampleLog(/* -1: all concepts */))
-        LOGDEBUG(anna::Logger::debug("Sample log activation for all statistic concepts", ANNA_FILE_LOCATION));
-    } else {
-      anna::Tokenizer lst;
-      lst.apply(cl.getValue("logStatisticSamples"), ",");
-
-      if(lst.size() >= 1) {
-        anna::Tokenizer::const_iterator tok_min(lst.begin());
-        anna::Tokenizer::const_iterator tok_max(lst.end());
-        anna::Tokenizer::const_iterator tok_iter;
-        int conceptId;
-
-        for(tok_iter = tok_min; tok_iter != tok_max; tok_iter++) {
-          conceptId = atoi(anna::Tokenizer::data(tok_iter));
-
-          if(statEngine.enableSampleLog(conceptId))
-            LOGDEBUG(anna::Logger::debug(anna::functions::asString("Sample log activation for statistic concept id = %d", conceptId), ANNA_FILE_LOCATION));
-        }
-      }
-    }
-  }
-
+  /////////////////////////////
+  // Log statistics concepts //
+  /////////////////////////////
+  if(cl.exists("logStatisticSamples"))
+    logStatisticsSamples(cl.getValue("logStatisticSamples"));
 
   // Start client connections //////////////////////////////////////////////////////////////////////////////////
   MyDiameterEntity *entity;
@@ -1036,6 +1013,11 @@ std::string Launcher::help() const throw() {
   result += "\n                                     This operation applies over all the registered host nodes";
   result += "\n                                      except if one specific working node has been set.";
   result += "\nforceCountersRecord                  Forces dump to file the current counters of the process.";
+  result += "\nlog-statistics-samples|<list>        Log statistics samples for the provided comma-separated concept id";
+  result += "\n                                      list, over './sample.<concept id>.csv' files. For example: \"1,2\"";
+  result += "\n                                      will log concepts 1 and 2. Reserved words \"all\"/\"none\" activates/";
+  result += "\n                                      deactivates all registered statistics concept identifiers. That ids";
+  result += "\n                                      are shown at context dump.";
   result += "\nchange-dir[|directory]               Changes the execution point which could be fine to ease some";
   result += "\n                                     file system interaction tasks. Be care about some requirements";
   result += "\n                                     (for example if you have a user defined counters directory as";
@@ -1476,6 +1458,38 @@ std::string Launcher::help() const throw() {
   return result;
 }
 
+
+void Launcher::logStatisticsSamples(const std::string &conceptsList) throw() {
+  anna::statistics::Engine &statEngine = anna::statistics::Engine::instantiate();
+
+  if(conceptsList == "all") {
+    if(statEngine.enableSampleLog(/* -1: all concepts */))
+      LOGDEBUG(anna::Logger::debug("Sample log activation for all statistic concepts", ANNA_FILE_LOCATION));
+  }
+  else if(conceptsList == "none") {
+      if(statEngine.disableSampleLog(/* -1: all concepts */))
+        LOGDEBUG(anna::Logger::debug("Sample log deactivation for all statistic concepts", ANNA_FILE_LOCATION));
+  } else {
+    anna::Tokenizer lst;
+    lst.apply(conceptsList, ",");
+
+    if(lst.size() >= 1) {
+      anna::Tokenizer::const_iterator tok_min(lst.begin());
+      anna::Tokenizer::const_iterator tok_max(lst.end());
+      anna::Tokenizer::const_iterator tok_iter;
+      int conceptId;
+
+      for(tok_iter = tok_min; tok_iter != tok_max; tok_iter++) {
+        conceptId = atoi(anna::Tokenizer::data(tok_iter));
+
+        if(statEngine.enableSampleLog(conceptId))
+          LOGDEBUG(anna::Logger::debug(anna::functions::asString("Sample log activation for statistic concept id = %d", conceptId), ANNA_FILE_LOCATION));
+      }
+    }
+  }
+}
+
+
 void Launcher::eventOperation(const std::string &operation, std::string &response_content) throw(anna::RuntimeException) {
   LOGMETHOD(anna::TraceMethod tm("Launcher", "eventOperation", ANNA_FILE_LOCATION));
   if (operation == "") return; // ignore
@@ -1573,6 +1587,7 @@ void Launcher::eventOperation(const std::string &operation, std::string &respons
   bool wrongBody = false;
 
   if((opType == "change-dir") && (numParams > 1)) wrongBody = true;
+  if((opType == "log-statistics-samples") && (numParams != 1)) wrongBody = true;
   if((opType == "node") && (numParams > 1)) wrongBody = true;
 
   if((opType == "node_auto") && (numParams > 0)) wrongBody = true;
@@ -1603,6 +1618,12 @@ void Launcher::eventOperation(const std::string &operation, std::string &respons
     return;
   }
 
+  if(opType == "log-statistics-samples") {
+    logStatisticsSamples(param1);
+    response_content = anna::functions::asString("Log statistics samples for '%s' concepts", param1.c_str());
+    return;
+  }
+
   // Change execution directory:
   if(opType == "change-dir") {
     if (param1 == "") param1 = a_initialWorkingDirectory;