Public Member Functions | Static Public Member Functions | Public Attributes | Private Attributes | List of all members
spdlog::details::file_helper Class Reference

#include <file_helper.h>

Public Member Functions

 file_helper ()=default
 
 file_helper (const file_helper &)=delete
 
file_helperoperator= (const file_helper &)=delete
 
 ~file_helper ()
 
void open (const filename_t &fname, bool truncate=false)
 
void reopen (bool truncate)
 
void flush ()
 
void close ()
 
void write (const fmt::memory_buffer &buf)
 
size_t size () const
 
const filename_tfilename () const
 

Static Public Member Functions

static bool file_exists (const filename_t &fname)
 
static std::tuple< filename_t, filename_tsplit_by_extension (const spdlog::filename_t &fname)
 

Public Attributes

const int open_tries = 5
 
const int open_interval = 10
 

Private Attributes

std::FILE * fd_ {nullptr}
 
filename_t _filename
 

Detailed Description

Definition at line 25 of file file_helper.h.

Constructor & Destructor Documentation

spdlog::details::file_helper::file_helper ( )
explicitdefault
spdlog::details::file_helper::file_helper ( const file_helper )
delete
spdlog::details::file_helper::~file_helper ( )
inline

Definition at line 37 of file file_helper.h.

38  {
39  close();
40  }

Member Function Documentation

void spdlog::details::file_helper::close ( )
inline

Definition at line 74 of file file_helper.h.

75  {
76  if (fd_ != nullptr)
77  {
78  std::fclose(fd_);
79  fd_ = nullptr;
80  }
81  }
static bool spdlog::details::file_helper::file_exists ( const filename_t fname)
inlinestatic

Definition at line 107 of file file_helper.h.

108  {
109  return os::file_exists(fname);
110  }
bool file_exists(const filename_t &filename) SPDLOG_NOEXCEPT
Definition: os.h:188
const filename_t& spdlog::details::file_helper::filename ( ) const
inline

Definition at line 102 of file file_helper.h.

103  {
104  return _filename;
105  }
void spdlog::details::file_helper::flush ( )
inline

Definition at line 69 of file file_helper.h.

70  {
71  std::fflush(fd_);
72  }
void spdlog::details::file_helper::open ( const filename_t fname,
bool  truncate = false 
)
inline

Definition at line 42 of file file_helper.h.

43  {
44  close();
45  auto *mode = truncate ? SPDLOG_FILENAME_T("wb") : SPDLOG_FILENAME_T("ab");
46  _filename = fname;
47  for (int tries = 0; tries < open_tries; ++tries)
48  {
49  if (!os::fopen_s(&fd_, fname, mode))
50  {
51  return;
52  }
53 
55  }
56 
57  throw spdlog_ex("Failed opening file " + os::filename_to_str(_filename) + " for writing", errno);
58  }
int errno
Contains the last error code.
Definition: structcmd.h:53
bool fopen_s(FILE **fp, const filename_t &filename, const filename_t &mode)
Definition: os.h:148
#define SPDLOG_FILENAME_T(s)
Definition: os.h:369
void sleep_for_millis(int milliseconds) SPDLOG_NOEXCEPT
Definition: os.h:351
std::string filename_to_str(const filename_t &filename)
Definition: os.h:370
file_helper& spdlog::details::file_helper::operator= ( const file_helper )
delete
void spdlog::details::file_helper::reopen ( bool  truncate)
inline

Definition at line 60 of file file_helper.h.

61  {
62  if (_filename.empty())
63  {
64  throw spdlog_ex("Failed re opening file - was not opened before");
65  }
66  open(_filename, truncate);
67  }
void open(const filename_t &fname, bool truncate=false)
Definition: file_helper.h:42
size_t spdlog::details::file_helper::size ( void  ) const
inline

Definition at line 93 of file file_helper.h.

94  {
95  if (fd_ == nullptr)
96  {
97  throw spdlog_ex("Cannot use size() on closed file " + os::filename_to_str(_filename));
98  }
99  return os::filesize(fd_);
100  }
size_t filesize(FILE *f)
Definition: os.h:204
std::string filename_to_str(const filename_t &filename)
Definition: os.h:370
static std::tuple<filename_t, filename_t> spdlog::details::file_helper::split_by_extension ( const spdlog::filename_t fname)
inlinestatic

Definition at line 125 of file file_helper.h.

126  {
127  auto ext_index = fname.rfind('.');
128 
129  // no valid extension found - return whole path and empty string as
130  // extension
131  if (ext_index == filename_t::npos || ext_index == 0 || ext_index == fname.size() - 1)
132  {
133  return std::make_tuple(fname, spdlog::filename_t());
134  }
135 
136  // treat casese like "/etc/rc.d/somelogfile or "/abc/.hiddenfile"
137  auto folder_index = fname.rfind(details::os::folder_sep);
138  if (folder_index != filename_t::npos && folder_index >= ext_index - 1)
139  {
140  return std::make_tuple(fname, spdlog::filename_t());
141  }
142 
143  // finally - return a valid base and extension tuple
144  return std::make_tuple(fname.substr(0, ext_index), fname.substr(ext_index));
145  }
std::string filename_t
Definition: common.h:202
static SPDLOG_CONSTEXPR const char folder_sep
Definition: os.h:126
void spdlog::details::file_helper::write ( const fmt::memory_buffer buf)
inline

Definition at line 83 of file file_helper.h.

84  {
85  size_t msg_size = buf.size();
86  auto data = buf.data();
87  if (std::fwrite(data, 1, msg_size, fd_) != msg_size)
88  {
89  throw spdlog_ex("Failed writing to file " + os::filename_to_str(_filename), errno);
90  }
91  }
int errno
Contains the last error code.
Definition: structcmd.h:53
basic_data data
Definition: format.h:764
std::string filename_to_str(const filename_t &filename)
Definition: os.h:370

Member Data Documentation

filename_t spdlog::details::file_helper::_filename
private

Definition at line 149 of file file_helper.h.

std::FILE* spdlog::details::file_helper::fd_ {nullptr}
private

Definition at line 148 of file file_helper.h.

const int spdlog::details::file_helper::open_interval = 10

Definition at line 30 of file file_helper.h.

const int spdlog::details::file_helper::open_tries = 5

Definition at line 29 of file file_helper.h.


The documentation for this class was generated from the following file: