Photon 1.0.0
Loading...
Searching...
No Matches
file_helper-inl.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#ifndef SPDLOG_HEADER_ONLY
8#endif
9
10#include <spdlog/details/os.h>
11#include <spdlog/common.h>
12
13#include <cerrno>
14#include <chrono>
15#include <cstdio>
16#include <string>
17#include <thread>
18#include <tuple>
19
20namespace spdlog
21{
22 namespace details
23 {
24
26 : event_handlers_(event_handlers)
27 {
28 }
29
34
35 SPDLOG_INLINE void file_helper::open(const filename_t& fname, bool truncate)
36 {
37 close();
38 filename_ = fname;
39
40 auto* mode = SPDLOG_FILENAME_T("ab");
41 auto* trunc_mode = SPDLOG_FILENAME_T("wb");
42
44 {
46 }
47 for (int tries = 0; tries < open_tries_; ++tries)
48 {
49 // create containing folder if not exists already.
51 if (truncate)
52 {
53 // Truncate by opening-and-closing a tmp file in "wb" mode, always
54 // opening the actual log-we-write-to in "ab" mode, since that
55 // interacts more politely with eternal processes that might
56 // rotate/truncate the file underneath us.
57 std::FILE* tmp;
58 if (os::fopen_s(&tmp, fname, trunc_mode))
59 {
60 continue;
61 }
62 std::fclose(tmp);
63 }
64 if (!os::fopen_s(&fd_, fname, mode))
65 {
67 {
69 }
70 return;
71 }
72
74 }
75
76 throw_spdlog_ex("Failed opening file " + os::filename_to_str(filename_) + " for writing", errno);
77 }
78
80 {
81 if (filename_.empty())
82 {
83 throw_spdlog_ex("Failed re opening file - was not opened before");
84 }
85 this->open(filename_, truncate);
86 }
87
89 {
90 if (std::fflush(fd_) != 0)
91 {
92 throw_spdlog_ex("Failed flush to file " + os::filename_to_str(filename_), errno);
93 }
94 }
95
97 {
98 if (!os::fsync(fd_))
99 {
100 throw_spdlog_ex("Failed to fsync file " + os::filename_to_str(filename_), errno);
101 }
102 }
103
105 {
106 if (fd_ != nullptr)
107 {
109 {
111 }
112
113 std::fclose(fd_);
114 fd_ = nullptr;
115
117 {
119 }
120 }
121 }
122
124 {
125 size_t msg_size = buf.size();
126 auto data = buf.data();
127 if (std::fwrite(data, 1, msg_size, fd_) != msg_size)
128 {
129 throw_spdlog_ex("Failed writing to file " + os::filename_to_str(filename_), errno);
130 }
131 }
132
134 {
135 if (fd_ == nullptr)
136 {
137 throw_spdlog_ex("Cannot use size() on closed file " + os::filename_to_str(filename_));
138 }
139 return os::filesize(fd_);
140 }
141
143 {
144 return filename_;
145 }
146
147 //
148 // return file path and its extension:
149 //
150 // "mylog.txt" => ("mylog", ".txt")
151 // "mylog" => ("mylog", "")
152 // "mylog." => ("mylog.", "")
153 // "/dir1/dir2/mylog.txt" => ("/dir1/dir2/mylog", ".txt")
154 //
155 // the starting dot in filenames is ignored (hidden files):
156 //
157 // ".mylog" => (".mylog". "")
158 // "my_folder/.mylog" => ("my_folder/.mylog", "")
159 // "my_folder/.mylog.txt" => ("my_folder/.mylog", ".txt")
160 SPDLOG_INLINE std::tuple<filename_t, filename_t> file_helper::split_by_extension(const filename_t& fname)
161 {
162 auto ext_index = fname.rfind('.');
163
164 // no valid extension found - return whole path and empty string as
165 // extension
166 if (ext_index == filename_t::npos || ext_index == 0 || ext_index == fname.size() - 1)
167 {
168 return std::make_tuple(fname, filename_t());
169 }
170
171 // treat cases like "/etc/rc.d/somelogfile or "/abc/.hiddenfile"
172 auto folder_index = fname.find_last_of(details::os::folder_seps_filename);
173 if (folder_index != filename_t::npos && folder_index >= ext_index - 1)
174 {
175 return std::make_tuple(fname, filename_t());
176 }
177
178 // finally - return a valid base and extension tuple
179 return std::make_tuple(fname.substr(0, ext_index), fname.substr(ext_index));
180 }
181
182 } // namespace details
183} // namespace spdlog
const filename_t & filename() const
Definition file_helper-inl.h:142
size_t size() const
Definition file_helper-inl.h:133
void reopen(bool truncate)
Definition file_helper-inl.h:79
void flush()
Definition file_helper-inl.h:88
void write(const memory_buf_t &buf)
Definition file_helper-inl.h:123
static std::tuple< filename_t, filename_t > split_by_extension(const filename_t &fname)
Definition file_helper-inl.h:160
const int open_tries_
Definition file_helper.h:53
const unsigned int open_interval_
Definition file_helper.h:54
filename_t filename_
Definition file_helper.h:56
~file_helper()
Definition file_helper-inl.h:30
void close()
Definition file_helper-inl.h:104
void sync()
Definition file_helper-inl.h:96
void open(const filename_t &fname, bool truncate=false)
Definition file_helper-inl.h:35
std::FILE * fd_
Definition file_helper.h:55
file_event_handlers event_handlers_
Definition file_helper.h:57
#define SPDLOG_FILENAME_T(s)
Definition common.h:132
#define SPDLOG_INLINE
Definition common.h:47
SPDLOG_INLINE filename_t dir_name(const filename_t &path)
Definition os-inl.h:581
SPDLOG_INLINE std::string filename_to_str(const filename_t &filename)
Definition os-inl.h:399
SPDLOG_INLINE bool fopen_s(FILE **fp, const filename_t &filename, const filename_t &mode)
Definition os-inl.h:128
SPDLOG_INLINE size_t filesize(FILE *f)
Definition os-inl.h:214
SPDLOG_INLINE bool create_dir(const filename_t &path)
Definition os-inl.h:542
SPDLOG_INLINE bool fsync(FILE *fp)
Definition os-inl.h:607
SPDLOG_INLINE void sleep_for_millis(unsigned int milliseconds) SPDLOG_NOEXCEPT
Definition os-inl.h:381
Definition async.h:26
SPDLOG_INLINE void throw_spdlog_ex(const std::string &msg, int last_errno)
Definition common-inl.h:75
std::string filename_t
Definition common.h:131
fmt::basic_memory_buffer< char, 250 > memory_buf_t
Definition common.h:173
Definition format.h:1901
Definition common.h:329
std::function< void(const filename_t &filename, std::FILE *file_stream)> before_close
Definition common.h:337
std::function< void(const filename_t &filename)> before_open
Definition common.h:335
std::function< void(const filename_t &filename, std::FILE *file_stream)> after_open
Definition common.h:336
std::function< void(const filename_t &filename)> after_close
Definition common.h:338
annotation details
Definition tag_strings.h:125