diff options
-rwxr-xr-x | smarttvfactory.c | 37 | ||||
-rwxr-xr-x | smarttvfactory.h | 2 |
2 files changed, 26 insertions, 13 deletions
diff --git a/smarttvfactory.c b/smarttvfactory.c index 7afb07d..eba6329 100755 --- a/smarttvfactory.c +++ b/smarttvfactory.c @@ -366,6 +366,7 @@ sUsageStatistics::sUsageStatistics(string fn, Log* l) : collectionDay(), clientE time (&rawtime); struct tm * now = localtime (&rawtime); + now->tm_min = 0; now->tm_sec = 0; collectionDay = mktime(now); @@ -384,12 +385,13 @@ sUsageStatistics::sUsageStatistics(string fn, Log* l) : collectionDay(), clientE << endl; }; +sUsageStatistics::~sUsageStatistics() { + writeEntries(); +} + void sUsageStatistics::checkDay() { time_t rawtime; - struct tm * now; - time (&rawtime); - now = localtime (&rawtime); if ((collectionDay + collectionWindow ) > rawtime) { *(mLog->log()) << mLog->getTimeString() @@ -399,28 +401,34 @@ void sUsageStatistics::checkDay() { return; } + writeEntries(); + + while (collectionDay < rawtime) { + collectionDay += collectionWindow; + } + + // Flush and reset + clientEntry.clear(); +} + +void sUsageStatistics::writeEntries() { ofstream ofs; ofs.open(usageStatLogFilename.c_str(), ios::out | ios::app); + char timebuf [80]; + strftime(timebuf, sizeof(timebuf), "%Y-%m-%d %H:%M:%S", localtime(&collectionDay)); + *(mLog->log()) << mLog->getTimeString() - << ": UsageStats: appending " << clientEntry.size() << " entries to log" << endl; - now->tm_sec = 0; + << ": UsageStats: "<< timebuf << " appending " << clientEntry.size() << " entries to log" << endl; for (uint i = 0; i < clientEntry.size(); i++) { - ofs << mLog->getTimeString() << " client " << clientEntry[i].ipAddr + ofs << timebuf << " client " << clientEntry[i].ipAddr << " Dur= " << clientEntry[i].usage << " sec Count= " << clientEntry[i].count << endl; } ofs.close(); - - while (collectionDay < rawtime) { - collectionDay += collectionWindow; - } - - // Flush and reset - clientEntry.clear(); } void sUsageStatistics::addUsageInfo (string ip, double dur) { @@ -470,6 +478,9 @@ SmartTvServer::~SmartTvServer() { for (uint i =0; i < mCmdCmds.size(); i++) delete mCmdCmds[i]; + + if (mUsageStatistics != NULL) + delete mUsageStatistics; } // Status methods diff --git a/smarttvfactory.h b/smarttvfactory.h index c4699e1..5356bb6 100755 --- a/smarttvfactory.h +++ b/smarttvfactory.h @@ -167,9 +167,11 @@ class sUsageStatistics { Log *mLog; void checkDay(); + void writeEntries(); public: sUsageStatistics(string fn, Log* mLog); + ~sUsageStatistics(); void addUsageInfo (string ip, double dur); }; |