spdlog
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 {
21namespace details {
22
27
28SPDLOG_INLINE void file_helper::open(const filename_t &fname, bool truncate)
29{
30 close();
31 filename_ = fname;
32
33 auto *mode = SPDLOG_FILENAME_T("ab");
34 auto *trunc_mode = SPDLOG_FILENAME_T("wb");
35
36 for (int tries = 0; tries < open_tries_; ++tries)
37 {
38 // create containing folder if not exists already.
40 if (truncate)
41 {
42 // Truncate by opening-and-closing a tmp file in "wb" mode, always
43 // opening the actual log-we-write-to in "ab" mode, since that
44 // interacts more politely with eternal processes that might
45 // rotate/truncate the file underneath us.
46 std::FILE *tmp;
47 if (os::fopen_s(&tmp, fname, trunc_mode))
48 {
49 continue;
50 }
51 std::fclose(tmp);
52 }
53 if (!os::fopen_s(&fd_, fname, mode))
54 {
55 return;
56 }
57
59 }
60
61 throw_spdlog_ex("Failed opening file " + os::filename_to_str(filename_) + " for writing", errno);
62}
63
65{
66 if (filename_.empty())
67 {
68 throw_spdlog_ex("Failed re opening file - was not opened before");
69 }
70 this->open(filename_, truncate);
71}
72
77
79{
80 if (fd_ != nullptr)
81 {
83 fd_ = nullptr;
84 }
85}
86
88{
89 size_t msg_size = buf.size();
90 auto data = buf.data();
91 if (std::fwrite(data, 1, msg_size, fd_) != msg_size)
92 {
93 throw_spdlog_ex("Failed writing to file " + os::filename_to_str(filename_), errno);
94 }
95}
96
98{
99 if (fd_ == nullptr)
100 {
101 throw_spdlog_ex("Cannot use size() on closed file " + os::filename_to_str(filename_));
102 }
103 return os::filesize(fd_);
104}
105
107{
108 return filename_;
109}
110
111//
112// return file path and its extension:
113//
114// "mylog.txt" => ("mylog", ".txt")
115// "mylog" => ("mylog", "")
116// "mylog." => ("mylog.", "")
117// "/dir1/dir2/mylog.txt" => ("/dir1/dir2/mylog", ".txt")
118//
119// the starting dot in filenames is ignored (hidden files):
120//
121// ".mylog" => (".mylog". "")
122// "my_folder/.mylog" => ("my_folder/.mylog", "")
123// "my_folder/.mylog.txt" => ("my_folder/.mylog", ".txt")
125{
126 auto ext_index = fname.rfind('.');
127
128 // no valid extension found - return whole path and empty string as
129 // extension
130 if (ext_index == filename_t::npos || ext_index == 0 || ext_index == fname.size() - 1)
131 {
132 return std::make_tuple(fname, filename_t());
133 }
134
135 // treat cases like "/etc/rc.d/somelogfile or "/abc/.hiddenfile"
136 auto folder_index = fname.find_last_of(details::os::folder_seps_filename);
137 if (folder_index != filename_t::npos && folder_index >= ext_index - 1)
138 {
139 return std::make_tuple(fname, filename_t());
140 }
141
142 // finally - return a valid base and extension tuple
143 return std::make_tuple(fname.substr(0, ext_index), fname.substr(ext_index));
144}
145
146} // namespace details
147} // namespace spdlog
const filename_t & filename() const
void write(const memory_buf_t &buf)
static std::tuple< filename_t, filename_t > split_by_extension(const filename_t &fname)
const unsigned int open_interval_
Definition file_helper.h:50
void open(const filename_t &fname, bool truncate=false)
#define SPDLOG_FILENAME_T(s)
Definition common.h:107
#define SPDLOG_INLINE
Definition common.h:33
T empty(T... args)
T fclose(T... args)
T fflush(T... args)
T find_last_of(T... args)
T fwrite(T... args)
T make_tuple(T... args)
SPDLOG_INLINE std::string filename_to_str(const filename_t &filename)
Definition os-inl.h:387
SPDLOG_INLINE bool fopen_s(FILE **fp, const filename_t &filename, const filename_t &mode)
Definition os-inl.h:123
SPDLOG_INLINE size_t filesize(FILE *f)
Definition os-inl.h:209
SPDLOG_INLINE filename_t dir_name(filename_t path)
Definition os-inl.h:573
SPDLOG_INLINE bool create_dir(filename_t path)
Definition os-inl.h:534
SPDLOG_INLINE void sleep_for_millis(unsigned int milliseconds) SPDLOG_NOEXCEPT
Definition os-inl.h:369
static SPDLOG_CONSTEXPR const filename_t::value_type folder_seps_filename[]
Definition details/os.h:44
Definition async.h:25
SPDLOG_INLINE void throw_spdlog_ex(const std::string &msg, int last_errno)
Definition common-inl.h:68
std::string filename_t
Definition common.h:106
fmt::basic_memory_buffer< char, 250 > memory_buf_t
Definition common.h:116
T rfind(T... args)
T size(T... args)
Definition format.h:897
T substr(T... args)