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 <string> 22 23 #include "logging.h" 24 #include "nocopyable.h" 25 #include "trace_file_helper.h" 26 #include "writer.h" 27 28 using google::protobuf::MessageLite; 29 30 class TraceFileWriter : public Writer { 31 public: 32 explicit TraceFileWriter(const std::string& path); 33 34 ~TraceFileWriter(); 35 36 std::string Path() const; 37 38 bool Open(const std::string& path); 39 40 long Write(const MessageLite& message); 41 42 long Write(const void* data, size_t size) override; 43 44 bool Flush() override; 45 46 bool Finish(); 47 48 private: 49 std::string path_ {}; 50 std::ofstream stream_ {}; 51 uint64_t writeBytes_ = 0; 52 uint64_t writeCount_ = 0; 53 TraceFileHeader header_ {}; 54 TraceFileHelper helper_ {}; 55 56 DISALLOW_COPY_AND_MOVE(TraceFileWriter); 57 }; 58 59 using TraceFileWriterPtr = STD_PTR(shared, TraceFileWriter); 60 61 #endif // !TRANCE_FILER_WRITER_H