• 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 typedef int (*RingBufferWriteFunc) (int32_t fd, const char *buf, const int len);
28 
29 namespace OHOS {
30 namespace HiviewDFX {
31 namespace {
32 const int BACK_TRACE_RING_BUFFER_SIZE = 32 * 1024;
33 const int BACK_TRACE_RING_BUFFER_PRINT_WAIT_TIME_MS = 10;
34 }
35 
36 class DfxRingBufferWrapper final {
37 public:
38     static DfxRingBufferWrapper &GetInstance();
39     ~DfxRingBufferWrapper() = default;
40 
41     void StartThread();
42     void StopThread();
43 
44     void SetWriteBufFd(int32_t fd);
45     void SetWriteFunc(RingBufferWriteFunc func);
46 
47     void AppendMsg(const std::string& msg);
48     int AppendBuf(const char *format, ...);
49 private:
50     static void LoopPrintRingBuffer();
51     static int DefaultWrite(int32_t fd, const char *buf, const int len);
52 
53     DfxRingBufferWrapper() = default;
54     DISALLOW_COPY_AND_MOVE(DfxRingBufferWrapper);
55 
56     RingBufferWriteFunc writeFunc_ = nullptr;
57 
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