Browse Source

Cleaned up timestamp functions

master
Pete McNeil 4 years ago
parent
commit
978f01d630
3 changed files with 35 additions and 20 deletions
  1. 4
    5
      SNFMulti.cpp
  2. 11
    2
      snfLOGmgr.cpp
  3. 20
    13
      snfNETmgr.cpp

+ 4
- 5
SNFMulti.cpp View File



// FileUTC(FileName) - utility function for tagging the active rulebase // 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. string FileUTC(string FileName) { // Gets a files UTC.
struct stat FileNameStat; // First we need a stat buffer. struct stat FileNameStat; // First we need a stat buffer.
struct tm FileNameTime; // Allocate a time structure. struct tm FileNameTime; // Allocate a time structure.
FileNameTime = *(gmtime(&FileNameStat.st_mtime)); // Copy the file time to it as UTC. 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 size_t l = snprintf( // Format yyyymmddhhmmss
TimestampBfr, TimestampBufferSize,
TimestampBfr, sizeof(TimestampBfr),
"%04d%02d%02d%02d%02d%02d", "%04d%02d%02d%02d%02d%02d",
FileNameTime.tm_year+1900, FileNameTime.tm_year+1900,
FileNameTime.tm_mon+1, FileNameTime.tm_mon+1,
); );


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


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

+ 11
- 2
snfLOGmgr.cpp View File

return s; // return it. 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. 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. tm* localt; // Get a ptr to a tm structure.
localt = localtime(&t); // Fill it with local time. 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_year+1900,
localt->tm_mon+1, localt->tm_mon+1,
localt->tm_mday, localt->tm_mday,
localt->tm_min, localt->tm_min,
localt->tm_sec localt->tm_sec
); );

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

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



+ 20
- 13
snfNETmgr.cpp View File

// #include "tcp_watchdog.hpp" No longer using TCPWatchdog -- see below _M // #include "tcp_watchdog.hpp" No longer using TCPWatchdog -- see below _M


using namespace std; 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 ///////////////////////////////////////////////////////////////// //// snfNETmgr /////////////////////////////////////////////////////////////////




snfNETmgr::snfNETmgr() : // Starting up the NETmgr snfNETmgr::snfNETmgr() : // Starting up the NETmgr
Thread(snfNETmgr::Type, "NET Manager"), // Network manager and Name. 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. isTimeToStop(false), // Not time to stop yet.
isConfigured(false), // Not configured 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. run(); // Run the thread.
} }


return ReportsBatch; // Return a batch of Reports. return ReportsBatch; // Return a batch of Reports.
} }


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

string& snfNETmgr::RulebaseUTC(string& t) { // Gets local rulebase file UTC. string& snfNETmgr::RulebaseUTC(string& t) { // Gets local rulebase file UTC.
struct stat RulebaseStat; // First we need a stat buffer. struct stat RulebaseStat; // First we need a stat buffer.
if(0 != stat(RulebaseFilePath.c_str(), &RulebaseStat)) { // If we can't get the stat we if(0 != stat(RulebaseFilePath.c_str(), &RulebaseStat)) { // If we can't get the stat we
struct tm RulebaseTime; // Allocate a time structure. struct tm RulebaseTime; // Allocate a time structure.
RulebaseTime = *(gmtime(&RulebaseStat.st_mtime)); // Copy the file time to it as UTC. 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_year+1900,
RulebaseTime.tm_mon+1, RulebaseTime.tm_mon+1,
RulebaseTime.tm_mday, RulebaseTime.tm_mday,
RulebaseTime.tm_sec RulebaseTime.tm_sec
); );


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

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

Loading…
Cancel
Save