• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 
16 /* This files contains process dump hrader. */
17 
18 #ifndef DFX_RING_BUFFER_WRAPPER_H
19 #define DFX_RING_BUFFER_WRAPPER_H
20 
21 #include <cinttypes>
22 #include <condition_variable>
23 #include <memory>
24 #include <mutex>
25 #include <string>
26 #include <thread>
27 #include "dfx_define.h"
28 #include "dfx_ring_buffer.h"
29 #include "nocopyable.h"
30 
31 typedef int (*RingBufferWriteFunc) (int32_t fd, const char *buf, const int len);
32 
33 namespace OHOS {
34 namespace HiviewDFX {
35 class DfxRingBufferWrapper final {
36 public:
37     static DfxRingBufferWrapper &GetInstance();
38     ~DfxRingBufferWrapper() = default;
39 
40     void StartThread();
41     void StopThread();
42 
43     void SetWriteBufFd(int32_t fd);
44     void SetWriteFunc(RingBufferWriteFunc func);
45 
46     void AppendMsg(const std::string& msg);
47     int AppendBuf(const char *format, ...);
48 private:
49     static void LoopPrintRingBuffer();
50     static int DefaultWrite(int32_t fd, const char *buf, const int len);
51 
52     DfxRingBufferWrapper() = default;
53     DISALLOW_COPY_AND_MOVE(DfxRingBufferWrapper);
54 
55     RingBufferWriteFunc writeFunc_ = nullptr;
56 
57     std::thread thread_;
58     DfxRingBuffer<BACK_TRACE_RING_BUFFER_SIZE, std::string> ringBuffer_;
59     int32_t fd_ = -1;
60     volatile bool hasFinished_ = false;
61 
62     static std::condition_variable printCV_;
63     static std::mutex printMutex_;
64 };
65 } // namespace HiviewDFX
66 } // namespace OHOS
67 
68 #endif  // DFX_PROCESSDUMP_H
69