spdlog
Loading...
Searching...
No Matches
file_helper.h
Go to the documentation of this file.
1// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
2// Distributed under the MIT License (http://opensource.org/licenses/MIT)
3
4#pragma once
5
6#include <spdlog/common.h>
7#include <tuple>
8
9namespace spdlog {
10namespace details {
11
12// Helper class for file sinks.
13// When failing to open a file, retry several times(5) with a delay interval(10 ms).
14// Throw spdlog_ex exception on errors.
15
17{
18public:
19 explicit file_helper() = default;
20
21 file_helper(const file_helper &) = delete;
22 file_helper &operator=(const file_helper &) = delete;
24
25 void open(const filename_t &fname, bool truncate = false);
26 void reopen(bool truncate);
27 void flush();
28 void close();
29 void write(const memory_buf_t &buf);
30 size_t size() const;
31 const filename_t &filename() const;
32
33 //
34 // return file path and its extension:
35 //
36 // "mylog.txt" => ("mylog", ".txt")
37 // "mylog" => ("mylog", "")
38 // "mylog." => ("mylog.", "")
39 // "/dir1/dir2/mylog.txt" => ("/dir1/dir2/mylog", ".txt")
40 //
41 // the starting dot in filenames is ignored (hidden files):
42 //
43 // ".mylog" => (".mylog". "")
44 // "my_folder/.mylog" => ("my_folder/.mylog", "")
45 // "my_folder/.mylog.txt" => ("my_folder/.mylog", ".txt")
46 static std::tuple<filename_t, filename_t> split_by_extension(const filename_t &fname);
47
48private:
49 const int open_tries_ = 5;
50 const unsigned int open_interval_ = 10;
51 std::FILE *fd_{nullptr};
53};
54} // namespace details
55} // namespace spdlog
56
57#ifdef SPDLOG_HEADER_ONLY
58# include "file_helper-inl.h"
59#endif
file_helper(const file_helper &)=delete
file_helper & operator=(const file_helper &)=delete
#define SPDLOG_API
Definition common.h:31
FMT_CONSTEXPR auto write(OutputIt out, Char value, const basic_format_specs< Char > &specs, locale_ref loc={}) -> OutputIt
Definition format.h:1338
Definition async.h:25
fmt::basic_memory_buffer< char, 250 > memory_buf_t
Definition common.h:116