Selaa lähdekoodia

Cleaned up timestamp functions

master
Pete McNeil 4 vuotta sitten
vanhempi
commit
978f01d630
3 muutettua tiedostoa jossa 35 lisäystä ja 20 poistoa
  1. 4
    5
      SNFMulti.cpp
  2. 11
    2
      snfLOGmgr.cpp
  3. 20
    13
      snfNETmgr.cpp

+ 4
- 5
SNFMulti.cpp Näytä tiedosto

@@ -284,7 +284,7 @@ int snf_RulebaseHandler::getRetiringCount(){

// FileUTC(FileName) - utility function for tagging the active rulebase

RuntimeCheck GoodTimestampLength("SNFMulti.cpp:FileUTC snprintf(...) != CorrectTimestampLength");
RuntimeCheck FileUTCGoodTimestampLength("SNFMulti.cpp:FileUTC snprintf(...) != CorrectTimestampLength");

string FileUTC(string FileName) { // Gets a files UTC.
struct stat FileNameStat; // First we need a stat buffer.
@@ -295,11 +295,10 @@ string FileUTC(string FileName) {
struct tm FileNameTime; // Allocate a time structure.
FileNameTime = *(gmtime(&FileNameStat.st_mtime)); // Copy the file time to it as UTC.

const size_t TimestampBufferSize = 16;
char TimestampBfr[TimestampBufferSize]; // Timestamp buffer.
char TimestampBfr[16]; // Timestamp buffer.

size_t l = snprintf( // Format yyyymmddhhmmss
TimestampBfr, TimestampBufferSize,
TimestampBfr, sizeof(TimestampBfr),
"%04d%02d%02d%02d%02d%02d",
FileNameTime.tm_year+1900,
FileNameTime.tm_mon+1,
@@ -310,7 +309,7 @@ string FileUTC(string FileName) {
);

const size_t CorrectTimestampLength = 4+2+2+2+2+2;
GoodTimestampLength(l == CorrectTimestampLength);
FileUTCGoodTimestampLength(l == CorrectTimestampLength);

t.append(TimestampBfr); // Append the timestamp to t
return t; // and return it to the caller.

+ 11
- 2
snfLOGmgr.cpp Näytä tiedosto

@@ -1765,11 +1765,16 @@ string& snfLOGmgr::Timestamp(string& s) {
return s; // return it.
}

RuntimeCheck LocalTimestampGoodTimestampLength("snfLOGmgr.cpp:LocalTimestamp snprintf(...) != CorrectTimestampLength");

string snfLOGmgr::LocalTimestamp(time_t t) { // Convert time_t to a local timestamp s.
char TimestampBfr[20]; // Create a small buffer.
char TimestampBfr[16]; // Create a small buffer.
tm* localt; // Get a ptr to a tm structure.
localt = localtime(&t); // Fill it with local time.
sprintf(TimestampBfr,"%04d%02d%02d%02d%02d%02d", // Format yyyymmddhhmmss

size_t l = snprintf( // Format yyyymmddhhmmss
TimestampBfr, sizeof(TimestampBfr),
"%04d%02d%02d%02d%02d%02d",
localt->tm_year+1900,
localt->tm_mon+1,
localt->tm_mday,
@@ -1777,6 +1782,10 @@ string snfLOGmgr::LocalTimestamp(time_t t) {
localt->tm_min,
localt->tm_sec
);

const size_t CorrectTimestampLength = 4+2+2+2+2+2;
LocalTimestampGoodTimestampLength(l == CorrectTimestampLength);

return string(TimestampBfr); // Return a string.
}


+ 20
- 13
snfNETmgr.cpp Näytä tiedosto

@@ -20,14 +20,14 @@
// #include "tcp_watchdog.hpp" No longer using TCPWatchdog -- see below _M

using namespace std;
///// utilities ////////////////////////////////////////////////////////////////
const int MSecsInSecs = 1000; // Multiplier - seconds to milliseconds.
unsigned long long int SecsAsMSecs(unsigned int Secs) {
return (MSecsInSecs * Secs);
}
///// utilities ////////////////////////////////////////////////////////////////
const int MSecsInSecs = 1000; // Multiplier - seconds to milliseconds.
unsigned long long int SecsAsMSecs(unsigned int Secs) {
return (MSecsInSecs * Secs);
}

//// snfNETmgr /////////////////////////////////////////////////////////////////

@@ -50,11 +50,11 @@ const ThreadState snfNETmgr::SYNC_Log_Event("Logging SYNC");

snfNETmgr::snfNETmgr() : // Starting up the NETmgr
Thread(snfNETmgr::Type, "NET Manager"), // Network manager and Name.
myLOGmgr(NULL), // No LOGmgr yet.
myLOGmgr(NULL), // No LOGmgr yet.
isTimeToStop(false), // Not time to stop yet.
isConfigured(false), // Not configured yet.
SYNCTimer(30000), // Sync every 30 secs by default.
SyncSecsOverride(-1) { // Override is -1 (off) by default.
SYNCTimer(30000), // Sync every 30 secs by default.
SyncSecsOverride(-1) { // Override is -1 (off) by default.
run(); // Run the thread.
}

@@ -246,6 +246,8 @@ string snfNETmgr::getReports() {
return ReportsBatch; // Return a batch of Reports.
}

RuntimeCheck RulebaseUTCGoodTimestampLength("SNFNetmgr.cpp:RulebaseUTC snprintf(...) != CorrectTimestampLength");

string& snfNETmgr::RulebaseUTC(string& t) { // Gets local rulebase file UTC.
struct stat RulebaseStat; // First we need a stat buffer.
if(0 != stat(RulebaseFilePath.c_str(), &RulebaseStat)) { // If we can't get the stat we
@@ -254,9 +256,11 @@ string& snfNETmgr::RulebaseUTC(string& t) {
struct tm RulebaseTime; // Allocate a time structure.
RulebaseTime = *(gmtime(&RulebaseStat.st_mtime)); // Copy the file time to it as UTC.

char TimestampBfr[20]; // Timestamp buffer.
char TimestampBfr[16]; // Timestamp buffer.

sprintf(TimestampBfr,"%04d%02d%02d%02d%02d%02d", // Format yyyymmddhhmmss
size_t l = snprintf( // Format yyyymmddhhmmss
TimestampBfr, sizeof(TimestampBfr),
"%04d%02d%02d%02d%02d%02d",
RulebaseTime.tm_year+1900,
RulebaseTime.tm_mon+1,
RulebaseTime.tm_mday,
@@ -265,6 +269,9 @@ string& snfNETmgr::RulebaseUTC(string& t) {
RulebaseTime.tm_sec
);

const size_t CorrectTimestampLength = 4+2+2+2+2+2;
RulebaseUTCGoodTimestampLength(l == CorrectTimestampLength);

t.append(TimestampBfr); // Append the timestamp to t
return t; // and return it to the caller.
}

Loading…
Peruuta
Tallenna