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 //
10 #include <MyCounterRecorder.hpp>
13 MyCounterRecorder::MyCounterRecorder(const std::string &fnp) : a_stream(-1), a_fileNamePrefix(fnp) {
14 a_previousTime = ::time(NULL);
17 // pure virtual definitions:
18 void MyCounterRecorder::open() throw(anna::RuntimeException) {
19 static char str [256];
20 const time_t now = ::time(NULL);
23 anna_memcpy(&tmNow, localtime(&now), sizeof(tmNow));
24 anna_memcpy(&tmPrevious, localtime(&a_previousTime), sizeof(tmPrevious));
26 str, ".Date%04d%02d%02d.Time%02d%02d%02d",
27 1900 + (tmNow.tm_year), (tmNow.tm_mon) + 1,
28 tmNow.tm_mday, tmNow.tm_hour, tmNow.tm_min, tmNow.tm_sec);
29 a_fileName = a_fileNamePrefix;
32 std::string msg("Flush counters | ");
34 anna::Logger::debug(msg, ANNA_FILE_LOCATION);
37 if((a_stream = ::open(a_fileName.c_str(), O_WRONLY | O_CREAT, S_IWUSR)) == -1)
38 throw RuntimeException(anna::functions::asString("Error opening file '%s'; errno = %d", a_fileName.c_str(), errno), ANNA_FILE_LOCATION);
40 sprintf(str, "%04d-%02d-%02d %02d:%02d|%04d-%02d-%02d %02d:%02d",
41 1900 + (tmPrevious.tm_year), (tmPrevious.tm_mon) + 1,
42 tmPrevious.tm_mday, tmPrevious.tm_hour, tmPrevious.tm_min,
43 1900 + (tmNow.tm_year), (tmNow.tm_mon) + 1,
44 tmNow.tm_mday, tmNow.tm_hour, tmNow.tm_min
49 void MyCounterRecorder::apply(const anna::oam::Counter& counter) throw(anna::RuntimeException) {
50 static char line [356];
51 anna::oam::Counter::type_t value = counter;
52 sprintf(line, "%s|%06d|%07u|%s\n", a_fixedLine.c_str(), counter.getReference(), value, counter.getName().c_str());
54 if(write(a_stream, line, anna_strlen(line)) == -1)
55 throw RuntimeException(anna::functions::asString("Error writting to file '%s'; errno = %d", a_fileName.c_str(), errno), ANNA_FILE_LOCATION);
58 void MyCounterRecorder::close() throw() {
64 chmod(a_fileName.c_str(), S_IWUSR | S_IRUSR);
65 a_previousTime = ::time(NULL);
68 std::string MyCounterRecorder::asString() const throw() {
69 std::string result = "Physical counters dump at file '";
71 result += "'. Another way to see counters: context dump (kill -10 <pid>";