• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 TRANCE_FILE_WRITER_H
16 #define TRANCE_FILE_WRITER_H
17 
18 #include <cstdint>
19 #include <fstream>
20 #include <google/protobuf/message_lite.h>
21 #include <mutex>
22 #include <queue>
23 #include <string>
24 
25 #include "logging.h"
26 #include "nocopyable.h"
27 #include "trace_file_helper.h"
28 #include "writer.h"
29 
30 using google::protobuf::MessageLite;
31 
32 class TraceFileWriter : public Writer {
33 public:
34     explicit TraceFileWriter(const std::string& path);
35 
36     explicit TraceFileWriter(const std::string& path, bool splitFile, uint32_t splitFileMaxSizeMb,
37         uint32_t splitFileMaxNum);
38 
39     ~TraceFileWriter();
40 
41     std::string Path() const;
42 
43     bool SetPluginConfig(const void* data, size_t size);
44 
45     bool WriteHeader();
46 
47     long Write(const MessageLite& message);
48 
49     long Write(const void* data, size_t size) override;
50 
51     bool Flush() override;
52 
53     bool Finish();
54 
55     bool IsSplitFile(uint32_t size);
56 
57     void SetStopSplitFile(bool isStop);
58 
59 private:
60     void SetTimeStamp();
61 
62     void LogDiskUsage();
63 
64     void DeleteOldSplitFile();
65 
66     bool FlushStream();
67 
68 private:
69     std::string path_ {};
70     std::string oldPath_ {};
71     std::ofstream stream_ {};
72     uint64_t writeBytes_ = 0;
73     uint64_t writeCount_ = 0;
74     TraceFileHeader header_ {};
75     TraceFileHelper helper_ {};
76     uint32_t dataSize_ = 0;
77     bool isSplitFile_ = false;
78     uint32_t splitFileMaxSize_;
79     uint32_t splitFileMaxNum_;
80     std::queue<std::string> splitFilePaths_;
81     std::vector<std::vector<char>> pluginConfigsData_;
82     bool isStop_ = false;
83     int fileNum_;
84 
85     DISALLOW_COPY_AND_MOVE(TraceFileWriter);
86 };
87 
88 using TraceFileWriterPtr = STD_PTR(shared, TraceFileWriter);
89 
90 #endif // !TRANCE_FILER_WRITER_H