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 <string> 23 24 #include "logging.h" 25 #include "nocopyable.h" 26 #include "trace_file_helper.h" 27 #include "writer.h" 28 29 using google::protobuf::MessageLite; 30 31 class TraceFileWriter : public Writer { 32 public: 33 explicit TraceFileWriter(const std::string& path, bool splitFile = false, uint32_t singleFileMaxSizeMb = 0); 34 35 ~TraceFileWriter(); 36 37 std::string Path() const; 38 39 bool SetPluginConfig(const void* data, size_t size); 40 41 bool WriteHeader(); 42 43 long Write(const MessageLite& message); 44 45 long Write(const void* data, size_t size) override; 46 47 bool Flush() override; 48 49 bool Finish(); 50 51 bool IsSplitFile(uint32_t size); 52 53 void SetStopSplitFile(bool isStop); 54 55 private: 56 void SetTimeStamp(); 57 58 private: 59 std::string path_ {}; 60 std::string oldPath_ {}; 61 std::ofstream stream_ {}; 62 uint64_t writeBytes_ = 0; 63 uint64_t writeCount_ = 0; 64 TraceFileHeader header_ {}; 65 TraceFileHelper helper_ {}; 66 uint32_t dataSize_; 67 bool isSplitFile_ = false; 68 uint32_t singleFileMaxSize_; 69 std::vector<std::vector<char>> pluginConfigsData_; 70 bool isStop_ = false; 71 int fileNum_; 72 73 DISALLOW_COPY_AND_MOVE(TraceFileWriter); 74 }; 75 76 using TraceFileWriterPtr = STD_PTR(shared, TraceFileWriter); 77 78 #endif // !TRANCE_FILER_WRITER_H