diff --git a/src/common/server_cmd_log.cpp b/src/common/server_cmd_log.cpp new file mode 100644 index 0000000000000000000000000000000000000000..145f5f4a193afba41a4a4b91f24822e2a172b4ae --- /dev/null +++ b/src/common/server_cmd_log.cpp @@ -0,0 +1,81 @@ +/* + * Copyright (C) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include +#include +#include +#include +#include "server_cmd_log.h" +#include "log.h" + +namespace Hdc { +ServerCmdLog& ServerCmdLog::GetInstance() +{ + static ServerCmdLog serverCmdLog; + return serverCmdLog; +} + +ServerCmdLog::ServerCmdLog() +{ + lastFlushTime = std::chrono::system_clock::now(); + running_ = true; +} + +ServerCmdLog::~ServerCmdLog() +{ +} +void ServerCmdLog::PushCmdLogStr(const std::string& cmdLogStr) +{ + std::unique_lock lock(PushCmdLogStrRecordMutex); + PushCmdLogStrQueue.push(cmdLogStr); +} + +std::string ServerCmdLog::PopCmdLogStr() +{ + std::unique_lock lock(PushCmdLogStrRecordMutex); + if (PushCmdLogStrQueue.empty()) { + return ""; + } + std::string cmdLogStr = PushCmdLogStrQueue.front(); + PushCmdLogStrQueue.pop(); + lastFlushTime = std::chrono::system_clock::now(); + return cmdLogStr; +} + +size_t ServerCmdLog::CmdLogStrSize() +{ + std::unique_lock lock(PushCmdLogStrRecordMutex); + return PushCmdLogStrQueue.size(); +} + +bool ServerCmdLog::GetRunningStatus() +{ + return running_; +} + +void ServerCmdLog::SetRunningStatus(bool running) +{ + if (running == true) { + running_ = true; + } else { + running_ = false; + } +} + +std::chrono::system_clock::time_point ServerCmdLog::GetLastFlushTime() +{ + return lastFlushTime; +} + +} // namespace Hdc \ No newline at end of file diff --git a/src/common/server_cmd_log.h b/src/common/server_cmd_log.h new file mode 100644 index 0000000000000000000000000000000000000000..3e26717b12cf31f6450c2f2dfd7fa8afa5da76c0 --- /dev/null +++ b/src/common/server_cmd_log.h @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef HDC_CMD_LOG_H +#define HDC_CMD_LOG_H +#include +#include +#include +#include + +namespace Hdc { +class ServerCmdLog { +public: + static ServerCmdLog& GetInstance(); + ServerCmdLog(); + ~ServerCmdLog(); + void PushCmdLogStr(const std::string& cmdLogStr); + std::string PopCmdLogStr(); + size_t CmdLogStrSize(); + bool GetRunningStatus(); + void SetRunningStatus(bool running); + std::chrono::system_clock::time_point GetLastFlushTime(); + +private: + bool running_ = false; + std::chrono::system_clock::time_point lastFlushTime = std::chrono::system_clock::now(); + std::mutex PushCmdLogStrRecordMutex; + std::queue PushCmdLogStrQueue; +}; +} // namespace Hdc +#endif // HDC_CMD_LOG_H \ No newline at end of file