1800b99b8Sopenharmony_ci/*
2800b99b8Sopenharmony_ci * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
3800b99b8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4800b99b8Sopenharmony_ci * you may not use this file except in compliance with the License.
5800b99b8Sopenharmony_ci * You may obtain a copy of the License at
6800b99b8Sopenharmony_ci *
7800b99b8Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
8800b99b8Sopenharmony_ci *
9800b99b8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10800b99b8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11800b99b8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12800b99b8Sopenharmony_ci * See the License for the specific language governing permissions and
13800b99b8Sopenharmony_ci * limitations under the License.
14800b99b8Sopenharmony_ci */
15800b99b8Sopenharmony_ci
16800b99b8Sopenharmony_ci#ifndef FAULT_LOGGER_PIPE_H
17800b99b8Sopenharmony_ci#define FAULT_LOGGER_PIPE_H
18800b99b8Sopenharmony_ci
19800b99b8Sopenharmony_ci#include <map>
20800b99b8Sopenharmony_ci#include <memory>
21800b99b8Sopenharmony_ci#include <mutex>
22800b99b8Sopenharmony_ci#include <string>
23800b99b8Sopenharmony_ci
24800b99b8Sopenharmony_cinamespace OHOS {
25800b99b8Sopenharmony_cinamespace HiviewDFX {
26800b99b8Sopenharmony_ciclass FaultLoggerPipe {
27800b99b8Sopenharmony_cipublic:
28800b99b8Sopenharmony_ci    FaultLoggerPipe();
29800b99b8Sopenharmony_ci    ~FaultLoggerPipe();
30800b99b8Sopenharmony_ci
31800b99b8Sopenharmony_ci    int GetReadFd(void);
32800b99b8Sopenharmony_ci    int GetWriteFd(void);
33800b99b8Sopenharmony_ci
34800b99b8Sopenharmony_ci    void Close(int fd) const;
35800b99b8Sopenharmony_ciprivate:
36800b99b8Sopenharmony_ci    bool Init(void);
37800b99b8Sopenharmony_ci    bool SetSize(long sz);
38800b99b8Sopenharmony_ci    void Destroy(void);
39800b99b8Sopenharmony_ci
40800b99b8Sopenharmony_ci    int fds_[2] = {-1, -1};
41800b99b8Sopenharmony_ci    bool init_;
42800b99b8Sopenharmony_ci    bool write_;
43800b99b8Sopenharmony_ci};
44800b99b8Sopenharmony_ci
45800b99b8Sopenharmony_ciclass FaultLoggerPipe2 {
46800b99b8Sopenharmony_cipublic:
47800b99b8Sopenharmony_ci    explicit FaultLoggerPipe2(uint64_t time, bool isJson = false);
48800b99b8Sopenharmony_ci    ~FaultLoggerPipe2();
49800b99b8Sopenharmony_ci    std::unique_ptr<FaultLoggerPipe> faultLoggerPipeBuf_;
50800b99b8Sopenharmony_ci    std::unique_ptr<FaultLoggerPipe> faultLoggerPipeRes_;
51800b99b8Sopenharmony_ci    std::unique_ptr<FaultLoggerPipe> faultLoggerJsonPipeBuf_;
52800b99b8Sopenharmony_ci    std::unique_ptr<FaultLoggerPipe> faultLoggerJsonPipeRes_;
53800b99b8Sopenharmony_ci    uint64_t time_;
54800b99b8Sopenharmony_ci};
55800b99b8Sopenharmony_ci
56800b99b8Sopenharmony_ciclass FaultLoggerPipeMap {
57800b99b8Sopenharmony_cipublic:
58800b99b8Sopenharmony_ci    FaultLoggerPipeMap();
59800b99b8Sopenharmony_ci    ~FaultLoggerPipeMap();
60800b99b8Sopenharmony_ci
61800b99b8Sopenharmony_ci    bool Check(int pid, uint64_t time);
62800b99b8Sopenharmony_ci    void Set(int pid, uint64_t time, bool isJson = false);
63800b99b8Sopenharmony_ci    FaultLoggerPipe2* Get(int pid);
64800b99b8Sopenharmony_ci    void Del(int pid);
65800b99b8Sopenharmony_ci
66800b99b8Sopenharmony_ciprivate:
67800b99b8Sopenharmony_ci    bool Find(int pid) const;
68800b99b8Sopenharmony_ci
69800b99b8Sopenharmony_ciprivate:
70800b99b8Sopenharmony_ci    std::map<int, std::unique_ptr<FaultLoggerPipe2> > faultLoggerPipes_;
71800b99b8Sopenharmony_ci    std::mutex pipeMapsMutex_;
72800b99b8Sopenharmony_ci};
73800b99b8Sopenharmony_ci} // namespace HiviewDFX
74800b99b8Sopenharmony_ci} // namespace OHOS
75800b99b8Sopenharmony_ci#endif
76