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