• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved.
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     explicit TraceFileWriter(int32_t fd);
36 
37     explicit TraceFileWriter(const std::string& path, bool splitFile, uint32_t splitFileMaxSizeMb,
38         uint32_t splitFileMaxNum);
39 
40     ~TraceFileWriter();
41 
42     std::string Path() const;
43 
44     bool SetPluginConfig(const void* data, size_t size);
45 
46     void WriteStandalonePluginData(const std::string& pluginName,
47                                    const std::string& data,
48                                    const std::string& pluginVersion = "");
49 
50     bool WriteHeader();
51 
52     long Write(const MessageLite& message);
53 
54     long Write(const void* data, size_t size) override;
55 
56     long WriteStandalonePluginFile(const std::string& file,
57                                    const std::string& name,
58                                    const std::string& version, DataType type);
59 
60     bool Flush() override;
61 
62     bool Finish();
63 
64     bool IsSplitFile(uint32_t size);
65 
66     void SetStopSplitFile(bool isStop);
67 
68     void SetTimeSource();
69     void SetDurationTime();
70 
71 private:
72     void SetTimeStamp();
73 
74     void LogDiskUsage();
75 
76     void DeleteOldSplitFile();
77 
78     bool FlushStream();
79 
80 private:
81     std::string path_ {};
82     std::string oldPath_ {};
83     std::ofstream stream_ {};
84     uint64_t writeBytes_ = 0;
85     uint64_t writeCount_ = 0;
86     TraceFileHeader header_ {};
87     TraceFileHelper helper_ {};
88     uint32_t dataSize_ = 0;
89     bool isSplitFile_ = false;
90     uint32_t splitFileMaxSize_;
91     uint32_t splitFileMaxNum_;
92     std::queue<std::string> splitFilePaths_;
93     std::vector<std::vector<char>> pluginConfigsData_;
94     bool isStop_ = false;
95     int fileNum_;
96     TraceFileHeader::HeaderData headerDataTime_ = {}; // used to store the clock source and collection time.
97     int32_t fd_{-1};
98     bool isWriteFd_{false};
99 
100     DISALLOW_COPY_AND_MOVE(TraceFileWriter);
101 };
102 
103 using TraceFileWriterPtr = STD_PTR(shared, TraceFileWriter);
104 
105 #endif // !TRANCE_FILER_WRITER_H