1 /* 2 * Copyright (C) 2021 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 #ifndef HIVIEW_PLUGIN_EVENT_LOG_COLLECTOR_H 16 #define HIVIEW_PLUGIN_EVENT_LOG_COLLECTOR_H 17 18 #include <ctime> 19 #include <map> 20 #include <memory> 21 #include <string> 22 #include <unordered_map> 23 24 #include "event.h" 25 #include "event_loop.h" 26 #include "log_store_ex.h" 27 #include "logger.h" 28 #include "plugin.h" 29 #include "sys_event.h" 30 namespace OHOS { 31 namespace HiviewDFX { 32 struct BinderInfo { 33 int client; 34 int server; 35 unsigned long wait; 36 }; 37 38 class EventLogger : public Plugin, public FileDescriptorEventCallback { 39 public: EventLogger()40 EventLogger() : logStore_(std::make_unique<LogStoreEx>(LOGGER_EVENT_LOG_PATH, true)), 41 startTime_(time(nullptr)), 42 inotifyFd_(0) {}; ~EventLogger()43 ~EventLogger() {}; 44 bool OnEvent(std::shared_ptr<Event> &event) override; 45 void OnLoad() override; 46 void OnUnload() override; 47 bool CanProcessEvent(std::shared_ptr<Event> event) override; 48 bool OnFileDescriptorEvent(int fd, int type) override; 49 int32_t GetPollFd() override; 50 int32_t GetPollType() override; 51 private: 52 static const inline std::string LOGGER_EVENT_LOG_PATH = "/data/log/eventlog"; 53 static const inline std::string MONITOR_STACK_LOG_PATH = "/data/log/faultlog/temp"; 54 static const inline std::string MONITOR_STACK_FLIE_NAME[] = { 55 "stacktrace", 56 }; 57 static const inline std::string MONITOR_LOG_PATH[] = { 58 MONITOR_STACK_LOG_PATH, 59 }; 60 static constexpr int EVENT_MAX_ID = 1000000; 61 static constexpr int MAX_FILE_NUM = 500; 62 static constexpr int MAX_FOLDER_SIZE = 50 * 1024 * 1024; 63 64 std::unique_ptr<LogStoreEx> logStore_; 65 uint64_t startTime_; 66 std::unordered_map<std::string, std::time_t> eventTagTime_; 67 int inotifyFd_; 68 std::unordered_map<int, std::string> fileMap_; 69 70 void StartLogCollect(std::shared_ptr<SysEvent> event); 71 bool JudgmentRateLimiting(std::shared_ptr<SysEvent> event); 72 std::string GetFormatTime(uint64_t timestamp) const; 73 bool WriteCommonHead(int fd, std::shared_ptr<SysEvent> event); 74 bool PostEvent(std::shared_ptr<SysEvent> event); 75 bool UpdateDB(std::shared_ptr<SysEvent> event, std::string logFile); 76 void CreateAndPublishEvent(std::string& dirPath, std::string& fileName); 77 }; 78 } // namespace HiviewDFX 79 } // namespace OHOS 80 #endif // HIVIEW_PLUGIN_EVENT_LOG_COLLECTOR_H 81