file_helper.h
Go to the documentation of this file.
1 //
2 // Copyright(c) 2015 Gabi Melman.
3 // Distributed under the MIT License (http://opensource.org/licenses/MIT)
4 //
5 
6 #pragma once
7 
8 // Helper class for file sink
9 // When failing to open a file, retry several times(5) with small delay between the tries(10 ms)
10 // Throw spdlog_ex exception on errors
11 
12 #include "opc/spdlog/details/os.h"
14 
15 #include <chrono>
16 #include <cstdio>
17 #include <string>
18 #include <thread>
19 #include <cerrno>
20 
21 namespace spdlog
22 {
23 namespace details
24 {
25 
27 {
28 
29 public:
30  const int open_tries = 5;
31  const int open_interval = 10;
32 
33  explicit file_helper() :
34  _fd(nullptr)
35  {}
36 
37  file_helper(const file_helper&) = delete;
38  file_helper& operator=(const file_helper&) = delete;
39 
41  {
42  close();
43  }
44 
45 
46  void open(const filename_t& fname, bool truncate = false)
47  {
48 
49  close();
50  auto *mode = truncate ? SPDLOG_FILENAME_T("wb") : SPDLOG_FILENAME_T("ab");
51  _filename = fname;
52  for (int tries = 0; tries < open_tries; ++tries)
53  {
54  if (!os::fopen_s(&_fd, fname, mode))
55  return;
56 
57  std::this_thread::sleep_for(std::chrono::milliseconds(open_interval));
58  }
59 
60  throw spdlog_ex("Failed opening file " + os::filename_to_str(_filename) + " for writing", errno);
61  }
62 
63  void reopen(bool truncate)
64  {
65  if (_filename.empty())
66  throw spdlog_ex("Failed re opening file - was not opened before");
67  open(_filename, truncate);
68 
69  }
70 
71  void flush()
72  {
73  std::fflush(_fd);
74  }
75 
76  void close()
77  {
78  if (_fd)
79  {
80  std::fclose(_fd);
81  _fd = nullptr;
82  }
83  }
84 
85  void write(const log_msg& msg)
86  {
87 
88  size_t msg_size = msg.formatted.size();
89  auto data = msg.formatted.data();
90  if (std::fwrite(data, 1, msg_size, _fd) != msg_size)
91  throw spdlog_ex("Failed writing to file " + os::filename_to_str(_filename), errno);
92  }
93 
94  size_t size()
95  {
96  if (!_fd)
97  throw spdlog_ex("Cannot use size() on closed file " + os::filename_to_str(_filename));
98  return os::filesize(_fd);
99  }
100 
101  const filename_t& filename() const
102  {
103  return _filename;
104  }
105 
106  static bool file_exists(const filename_t& name)
107  {
108 
109  return os::file_exists(name);
110  }
111 
112 private:
113  FILE* _fd;
115 };
116 }
117 }
void open(const filename_t &fname, bool truncate=false)
Definition: file_helper.h:46
bool file_exists(const filename_t &filename)
Definition: os.h:201
size_t filesize(FILE *f)
Definition: os.h:220
void reopen(bool truncate)
Definition: file_helper.h:63
const Char * data() const FMT_NOEXCEPT
Definition: format.h:2866
name
Definition: setup.py:38
static bool file_exists(const filename_t &name)
Definition: file_helper.h:106
std::string filename_t
const filename_t & filename() const
Definition: file_helper.h:101
file_helper & operator=(const file_helper &)=delete
fmt::MemoryWriter formatted
Definition: log_msg.h:46
std::size_t size() const
Definition: format.h:2857
void write(const log_msg &msg)
Definition: file_helper.h:85
int fopen_s(FILE **fp, const filename_t &filename, const filename_t &mode)
Definition: os.h:161
#define SPDLOG_FILENAME_T(s)
Definition: os.h:365
std::string filename_to_str(const filename_t &filename)
Definition: os.h:366


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Tue Jan 19 2021 03:06:04