diff --git a/services/hilogd/include/log_persister.h b/services/hilogd/include/log_persister.h index bc80893249d3e7431a6112d40052100abd56380b..8af781c8a4c1a0f2ad4aea7e5c332c7850700556 100644 --- a/services/hilogd/include/log_persister.h +++ b/services/hilogd/include/log_persister.h @@ -60,6 +60,7 @@ public: uint8_t GetType() const; std::string getPath(); int SaveInfo(LogPersistStartMsg& pMsg); + void SetRestore(bool flag); LogPersisterBuffer *buffer; LogPersisterBuffer *compressBuffer; private: diff --git a/services/hilogd/include/log_persister_rotator.h b/services/hilogd/include/log_persister_rotator.h index fd2a147015861ab859acfa303fc769abe4aeb8ca..a6ee48af9e9560796ed981ec911757cdf3efdffe 100644 --- a/services/hilogd/include/log_persister_rotator.h +++ b/services/hilogd/include/log_persister_rotator.h @@ -16,17 +16,15 @@ #define _HILOG_PERSISTER_ROTATOR_H #include #include +#include "hilog_common.h" namespace OHOS { namespace HiviewDFX { const std::string ANXILLARY_FILE_NAME = "persisterInfo_"; class LogPersisterRotator { public: LogPersisterRotator(std::string path, uint32_t fileSize, uint32_t fileNum, std::string suffix = ""); - ~LogPersisterRotator() - { - fclose(fdinfo); - } - void Init(); + ~LogPersisterRotator(); + int Init(); int Input(const char *buf, uint32_t length); void FillInfo(uint32_t *size, uint32_t *num); void FinishInput(); diff --git a/services/hilogd/log_persister.cpp b/services/hilogd/log_persister.cpp index 1ac6fe48568a9666a2ed1e6158eb6c1bbdc18361..7be69211d4d83d8d6dd5ed30703fc44aa5eac756 100644 --- a/services/hilogd/log_persister.cpp +++ b/services/hilogd/log_persister.cpp @@ -123,7 +123,6 @@ int LogPersister::Init() if (errno == EEXIST) { cout << "File already exists!" << endl; fd = open(mmapPath.c_str(), O_RDWR, 0); - restore = true; } } else { #ifdef DEBUG @@ -426,5 +425,10 @@ int LogPersister::SaveInfo(LogPersistStartMsg& pMsg) cout << "Saved Path=" << info.msg.filePath << endl; return RET_SUCCESS; } + +void LogPersister::SetRestore(bool flag) +{ + restore = flag; +} } // namespace HiviewDFX } // namespace OHOS diff --git a/services/hilogd/log_persister_rotator.cpp b/services/hilogd/log_persister_rotator.cpp index 67e098206d701ae134c8f1fb2738bd7cd8d6b790..1be6d5ab339e26e277441584abdb75412d741b77 100644 --- a/services/hilogd/log_persister_rotator.cpp +++ b/services/hilogd/log_persister_rotator.cpp @@ -32,7 +32,14 @@ LogPersisterRotator::LogPersisterRotator(string path, uint32_t fileSize, uint32_ needRotate = false; } -void LogPersisterRotator::Init() +LogPersisterRotator::~LogPersisterRotator() +{ + if (fdinfo != nullptr) { + fclose(fdinfo); + } +} + +int LogPersisterRotator::Init() { int nPos = fileName.find_last_of('/'); std::string mmapPath = fileName.substr(0, nPos) + "/." + ANXILLARY_FILE_NAME + to_string(id); @@ -41,7 +48,9 @@ void LogPersisterRotator::Init() mkdir(fileName.substr(0, nPos).c_str(), S_IRUSR | S_IWUSR | S_IXUSR | S_IRWXG | S_IRWXO); } } - fdinfo = fopen((mmapPath + ".info").c_str(), "w+"); + fdinfo = fopen((mmapPath + ".info").c_str(), "r+"); + if (fdinfo == nullptr) return RET_FAIL; + return RET_SUCCESS; } int LogPersisterRotator::Input(const char *buf, uint32_t length) @@ -91,10 +100,10 @@ void LogPersisterRotator::Rotate() ss << fileName << "." << index << fileSuffix; cout << "THE FILE NAME !!!!!!! " << ss.str() << endl; output.open(ss.str(), ios::app); - fseek(fdinfo, 0, SEEK_SET); - fwrite(&index, sizeof(uint8_t), 1, fdinfo); - fsync(fileno(fdinfo)); } + fseek(fdinfo, 0, SEEK_SET); + fwrite(&index, sizeof(uint8_t), 1, fdinfo); + fsync(fileno(fdinfo)); } void LogPersisterRotator::FillInfo(uint32_t *size, uint32_t *num) diff --git a/services/hilogd/log_querier.cpp b/services/hilogd/log_querier.cpp index e788b2d34898222437e4db65877c1eafbae8bc59..d05fb8fc34019e0f6e2b643fbeb084e57de4245e 100644 --- a/services/hilogd/log_querier.cpp +++ b/services/hilogd/log_querier.cpp @@ -134,7 +134,6 @@ void HandlePersistStartRequest(char* reqMsg, std::shared_ptr logReade strcpy_s(pLogPersistStartMsg->filePath, FILE_PATH_MAX_LEN, logPersisterPath.c_str()); rotator = MakeRotator(*pLogPersistStartMsg); rotator->SetId(pLogPersistStartMsg->jobId); - rotator->Init(); std::shared_ptr persister = make_shared( pLogPersistStartMsg->jobId, pLogPersistStartMsg->filePath, @@ -146,8 +145,9 @@ void HandlePersistStartRequest(char* reqMsg, std::shared_ptr logReade int saveInfoRes = persister->SaveInfo(*pLogPersistStartMsg); pLogPersistStartRst->jobId = pLogPersistStartMsg->jobId; pLogPersistStartRst->result = persister->Init(); - if (pLogPersistStartRst->result == RET_FAIL || saveInfoRes == RET_FAIL) { - cout << "Error initializing log persister!" << endl; + int rotatorRes = rotator->Init(); + if (pLogPersistStartRst->result == RET_FAIL || saveInfoRes == RET_FAIL || rotatorRes == RET_FAIL) { + cout << "LogPersister failed to initialize!" << endl; persister.reset(); } else { persister->Start(); @@ -589,7 +589,6 @@ int LogQuerier::RestorePersistJobs(HilogBuffer& _buffer) LogPersisterRotator* rotator = rotator = MakeRotator(info.msg); rotator->SetIndex(info.index + 1); rotator->SetId(info.msg.jobId); - rotator->Init(); printf("Recovery Info:\njobId=%u\nfilePath=%s\n", info.msg.jobId, info.msg.filePath); std::shared_ptr persister = make_shared( @@ -598,11 +597,13 @@ int LogQuerier::RestorePersistJobs(HilogBuffer& _buffer) info.msg.fileSize, info.msg.compressAlg, SLEEP_TIME, *rotator, _buffer); - int result = persister->Init(); + persister->SetRestore(true); + int persisterRes = persister->Init(); + int rotatorRes = rotator->Init(); persister->queryCondition.types = info.types; persister->queryCondition.levels = info.levels; - if (result == RET_FAIL) { - cout << "LogPersister Start Failed, result=" << result << endl; + if (persisterRes == RET_FAIL || rotatorRes == RET_FAIL) { + cout << "LogPersister failed to initialize!" << endl; persister.reset(); } else { persister->Start();