1 /*
2  * FileTraceLogWriter.h
3  *
4  * This source file is part of the FoundationDB open source project
5  *
6  * Copyright 2018 Apple Inc. and the FoundationDB project authors
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 
22 #ifndef FLOW_FILE_TRACE_LOG_WRITER_H
23 #define FLOW_FILE_TRACE_LOG_WRITER_H
24 #pragma once
25 
26 #include "flow/FastRef.h"
27 #include "flow/Trace.h"
28 
29 #include <functional>
30 
31 class FileTraceLogWriter : public ITraceLogWriter, ReferenceCounted<FileTraceLogWriter> {
32 private:
33 	std::string directory;
34 	std::string processName;
35 	std::string basename;
36 	std::string extension;
37 
38 	uint64_t maxLogsSize;
39 	int traceFileFD;
40 	int index;
41 
42 	std::function<void()> onError;
43 
44 public:
45 	FileTraceLogWriter(std::string directory, std::string processName, std::string basename, std::string extension, uint64_t maxLogsSize, std::function<void()> onError);
46 
47 	void addref();
48 	void delref();
49 
50 	void lastError(int err);
51 
52 	void write(const std::string& str);
53 	void open();
54 	void close();
55 	void roll();
56 	void sync();
57 
58 	static void extractTraceFileNameInfo(std::string const& filename, std::string &root, int &index);
59 	static bool compareTraceFileName (std::string const& f1, std::string const& f2);
60 	static bool reverseCompareTraceFileName(std::string f1, std::string f2);
61 
62 	void cleanupTraceFiles();
63 };
64 
65 #endif
66