From b0dbfa0852012bf9f4e500ca87a376998f7f0f71 Mon Sep 17 00:00:00 2001 From: Maurice ONeal Date: Fri, 10 Mar 2023 19:05:54 -0500 Subject: [PATCH] v2.0.t2 Logs are not rotating correctly. Changed up the code to append the logs in memory and then dump them to permanent storage on every loop of upkeep(). Hopefully this fixes the issue. --- src/common.cpp | 40 +++++++++++++++------------------------- src/common.h | 17 ++++++++--------- src/logger.cpp | 34 +++++++++++++++++++--------------- src/logger.h | 2 +- src/main.cpp | 19 +++++++++++-------- 5 files changed, 54 insertions(+), 58 deletions(-) diff --git a/src/common.cpp b/src/common.cpp index 5057722..375bd1b 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -138,17 +138,20 @@ void enforceMaxEvents(shared_t *share) while (names.size() > share->maxEvents) { - // removes the playlist file extension. - auto nameOnly = names[0].substr(0, names[0].size() - 5); - auto imgFile = nameOnly + ".jpg"; - auto webFile = nameOnly + ".html"; + if (names[0] != "stream.m3u8") + { + // removes the playlist file extension. + auto nameOnly = names[0].substr(0, names[0].size() - 5); + auto imgFile = nameOnly + ".jpg"; + auto webFile = nameOnly + ".html"; - cleanupStream(names[0]); - remove(names[0]); - - if (exists(imgFile)) remove(imgFile); - if (exists(webFile)) remove(webFile); + cleanupStream(names[0]); + remove(names[0]); + if (exists(imgFile)) remove(imgFile); + if (exists(webFile)) remove(webFile); + } + names.erase(names.begin()); } } @@ -220,13 +223,11 @@ bool rdConf(const string &filePath, shared_t *share) rdLine("web_font = ", line, &share->webFont); rdLine("sch_sec = ", line, &share->schSec); rdLine("post_cmd = ", line, &share->postCmd); - rdLine("frame_gap = ", line, &share->frameGap); rdLine("pix_thresh = ", line, &share->pixThresh); rdLine("img_thresh = ", line, &share->imgThresh); rdLine("max_days = ", line, &share->maxDays); rdLine("max_events = ", line, &share->maxEvents); rdLine("max_log_size = ", line, &share->maxLogSize); - rdLine("vid_codec = ", line, &share->vidCodec); } } while(!line.empty()); @@ -240,24 +241,16 @@ bool rdConf(shared_t *share) share->recordUrl.clear(); share->postCmd.clear(); share->camName.clear(); - share->recLogPath.clear(); - share->detLogPath.clear(); - share->upkLogPath.clear(); - share->recLogFile.close(); - share->detLogFile.close(); - share->upkLogFile.close(); share->retCode = 0; - share->frameGap = 20; - share->pixThresh = 150; - share->imgThresh = 80000; + share->pixThresh = 50; + share->imgThresh = 800; share->maxDays = 15; share->maxEvents = 20; share->maxLogSize = 50000; share->skipCmd = false; share->schSec = 60; share->webRoot = "/var/www/html"; - share->vidCodec = "copy"; share->webBg = "#485564"; share->webTxt = "#dee5ee"; share->webFont = "courier"; @@ -269,10 +262,7 @@ bool rdConf(shared_t *share) share->camName = path(share->conf).filename(); } - share->outDir = cleanDir(share->webRoot) + "/" + share->camName; - share->recLogPath = share->outDir + "/rec_log_lines.html"; - share->detLogPath = share->outDir + "/det_log_lines.html"; - share->upkLogPath = share->outDir + "/upk_log_lines.html"; + share->outDir = cleanDir(share->webRoot) + "/" + share->camName; error_code ec; diff --git a/src/common.h b/src/common.h index bea0239..9c712f2 100644 --- a/src/common.h +++ b/src/common.h @@ -37,8 +37,11 @@ using namespace std; using namespace std::filesystem; using namespace std::chrono; -#define APP_VER "2.0.t1" -#define APP_NAME "Motion Watch" +#define APP_VER "2.0.t2" +#define APP_NAME "Motion Watch" +#define REC_LOG_NAME "rec_log_lines.html" +#define DET_LOG_NAME "det_log_lines.html" +#define UPK_LOG_NAME "upk_log_lines.html" struct pls_t { @@ -53,17 +56,13 @@ struct pls_t struct shared_t { vector recList; - ofstream recLogFile; - ofstream detLogFile; - ofstream upkLogFile; string conf; - string recLogPath; - string detLogPath; - string upkLogPath; + string recLog; + string detLog; + string upkLog; string recordUrl; string outDir; string postCmd; - string vidCodec; string camName; string webBg; string webTxt; diff --git a/src/logger.cpp b/src/logger.cpp index 1bbbbd8..de68b2a 100644 --- a/src/logger.cpp +++ b/src/logger.cpp @@ -14,17 +14,17 @@ void recLog(const string &line, shared_t *share) { - share->recLogFile << genTimeStr("[%Y-%m-%d-%H-%M-%S] ") << line << "
" << endl; + share->recLog += genTimeStr("[%Y-%m-%d-%H-%M-%S] ") + line + "
\n"; } void detLog(const string &line, shared_t *share) { - share->detLogFile << genTimeStr("[%Y-%m-%d-%H-%M-%S] ") << line << "
" << endl; + share->detLog += genTimeStr("[%Y-%m-%d-%H-%M-%S] ") + line + "
\n"; } void upkLog(const string &line, shared_t *share) { - share->upkLogFile << genTimeStr("[%Y-%m-%d-%H-%M-%S] ") << line << "
" << endl; + share->upkLog += genTimeStr("[%Y-%m-%d-%H-%M-%S] ") + line + "
\n"; } void enforceMaxLogSize(const string &filePath, shared_t *share) @@ -38,16 +38,24 @@ void enforceMaxLogSize(const string &filePath, shared_t *share) } } -void initLogFile(const string &filePath, ofstream &fileObj) +void dumpLogs(const char *fileName, const string &lines) { - if (!fileObj.is_open()) + if (!lines.empty()) { - if (!exists(filePath)) + ofstream outFile; + + if (exists(fileName)) { - system(string("touch " + filePath).c_str()); + outFile.open(fileName, ofstream::app); + } + else + { + outFile.open(fileName); } - fileObj.open(filePath.c_str(), ofstream::app | ofstream::out); + outFile << lines; + + outFile.close(); } } @@ -108,11 +116,7 @@ void initLogFrontPage(const string &filePath, const string &logLinesFile) void initLogFrontPages(shared_t *share) { - auto recLogFilePath = share->outDir + "/recording_log.html"; - auto detLogFilePath = share->outDir + "/detection_log.html"; - auto upkLogFilePath = share->outDir + "/upkeep_log.html"; - - initLogFrontPage(recLogFilePath, path(share->recLogPath).filename().string()); - initLogFrontPage(detLogFilePath, path(share->detLogPath).filename().string()); - initLogFrontPage(upkLogFilePath, path(share->upkLogPath).filename().string()); + initLogFrontPage("recording_log.html", REC_LOG_NAME); + initLogFrontPage("detection_log.html", DET_LOG_NAME); + initLogFrontPage("upkeep_log.html", UPK_LOG_NAME); } diff --git a/src/logger.h b/src/logger.h index 3c81557..28c60f4 100644 --- a/src/logger.h +++ b/src/logger.h @@ -18,8 +18,8 @@ void recLog(const string &line, shared_t *share); void detLog(const string &line, shared_t *share); void upkLog(const string &line, shared_t *share); +void dumpLogs(const char *fileName, const string &lines); void enforceMaxLogSize(const string &filePath, shared_t *share); -void initLogFile(const string &filePath, ofstream &fileObj); void initLogFrontPages(shared_t *share); #endif // lOGGER_H diff --git a/src/main.cpp b/src/main.cpp index 4885df7..29d7d31 100755 --- a/src/main.cpp +++ b/src/main.cpp @@ -93,13 +93,17 @@ void upkeep(shared_t *share) { while (share->retCode == 0) { - enforceMaxLogSize(share->recLogPath, share); - enforceMaxLogSize(share->detLogPath, share); - enforceMaxLogSize(share->upkLogPath, share); + enforceMaxLogSize(REC_LOG_NAME, share); + enforceMaxLogSize(DET_LOG_NAME, share); + enforceMaxLogSize(UPK_LOG_NAME, share); - initLogFile(share->recLogPath, share->recLogFile); - initLogFile(share->detLogPath, share->detLogFile); - initLogFile(share->upkLogPath, share->upkLogFile); + dumpLogs(REC_LOG_NAME, share->recLog); + dumpLogs(DET_LOG_NAME, share->detLog); + dumpLogs(UPK_LOG_NAME, share->upkLog); + + share->recLog.clear(); + share->detLog.clear(); + share->upkLog.clear(); initLogFrontPages(share); enforceMaxEvents(share); @@ -137,8 +141,7 @@ void recLoop(shared_t *share) " -strftime 1" + " -strftime_mkdir 1" + " -hls_segment_filename 'VIDEO_TS/live/%Y/%j/%H/%M%S.ts'" + - " -y -vcodec " + - share->vidCodec + + " -y -vcodec copy" + " -f hls -hls_time 6 -hls_list_size " + to_string((share->maxDays * 86400) / 6) + // 86400 seconds in a day. " stream.m3u8";