Revert "Remove mysql and oracle resources for anna-ericsson project"
[anna.git] / example / diameter / launcher / MyCounterRecorder.cpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
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 //
7
8
9 // Process
10 #include <MyCounterRecorder.hpp>
11
12
13 MyCounterRecorder::MyCounterRecorder(const std::string &fnp) : a_stream(-1), a_fileNamePrefix(fnp) {
14   a_previousTime = ::time(NULL);
15 }
16
17 // pure virtual definitions:
18 void MyCounterRecorder::open() throw(anna::RuntimeException) {
19         static char str [256];
20         const time_t now = ::time(NULL);
21         struct tm tmNow;
22         struct tm tmPrevious;
23         anna_memcpy(&tmNow, localtime(&now), sizeof(tmNow));
24         anna_memcpy(&tmPrevious, localtime(&a_previousTime), sizeof(tmPrevious));
25         sprintf(
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;
30         a_fileName += str;
31         LOGDEBUG(
32           std::string msg("Flush counters | ");
33           msg += a_fileName;
34           anna::Logger::debug(msg, ANNA_FILE_LOCATION);
35         );
36
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);
39
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
45                    );
46         a_fixedLine = str;
47 }
48
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());
53
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);
56 }
57
58 void MyCounterRecorder::close() throw() {
59         if(a_stream != -1) {
60           ::close(a_stream);
61           a_stream = -1;
62         }
63
64 chmod(a_fileName.c_str(), S_IWUSR | S_IRUSR);
65 a_previousTime = ::time(NULL);
66 }
67
68 std::string MyCounterRecorder::asString() const throw() {
69   std::string result = "Physical counters dump at file '";
70   result += a_fileName;
71   result += "'. Another way to see counters: context dump (kill -10 <pid>";
72   return result;
73 }