• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include "server_cmd_log.h"
16 #include <sstream>
17 #include <unistd.h>
18 #include "log.h"
19 
20 namespace Hdc {
GetInstance()21 ServerCmdLog& ServerCmdLog::GetInstance()
22 {
23     static ServerCmdLog serverCmdLog;
24     return serverCmdLog;
25 }
26 
ServerCmdLog()27 ServerCmdLog::ServerCmdLog()
28 {
29     lastFlushTime = std::chrono::system_clock::now();
30 }
31 
~ServerCmdLog()32 ServerCmdLog::~ServerCmdLog()
33 {
34 }
35 
PushCmdLogStr(const std::string & cmdLogStr)36 void ServerCmdLog::PushCmdLogStr(const std::string& cmdLogStr)
37 {
38     constexpr size_t queueSizeMax = 1500;
39     std::unique_lock<std::mutex> lock(pushCmdLogStrRecordMutex);
40     size_t pushCmdLogStrQueueSize = pushCmdLogStrQueue.size();
41     if (pushCmdLogStrQueueSize >= queueSizeMax) {
42         WRITE_LOG(LOG_INFO, "!!!pushCmdLogStrQueue exceeds threshold:%zu,Do not save CmdLog.", pushCmdLogStrQueueSize);
43         return;
44     }
45     pushCmdLogStrQueue.push(cmdLogStr);
46 }
47 
PopCmdLogStr()48 std::string ServerCmdLog::PopCmdLogStr()
49 {
50     std::unique_lock<std::mutex> lock(pushCmdLogStrRecordMutex);
51     if (pushCmdLogStrQueue.empty()) {
52         return "";
53     }
54     std::string cmdLogStr = pushCmdLogStrQueue.front();
55     pushCmdLogStrQueue.pop();
56     lastFlushTime = std::chrono::system_clock::now();
57     return cmdLogStr;
58 }
59 
CmdLogStrSize()60 size_t ServerCmdLog::CmdLogStrSize()
61 {
62     std::unique_lock<std::mutex> lock(pushCmdLogStrRecordMutex);
63     return pushCmdLogStrQueue.size();
64 }
65 
GetLastFlushTime()66 std::chrono::system_clock::time_point ServerCmdLog::GetLastFlushTime()
67 {
68     return lastFlushTime;
69 }
70 
71 } // namespace Hdc