Skip to content

Commit

Permalink
Add sync to file_helper
Browse files Browse the repository at this point in the history
  • Loading branch information
espkk committed Jan 15, 2023
1 parent 5a63426 commit 83ffa9d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
8 changes: 8 additions & 0 deletions include/spdlog/details/file_helper-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ SPDLOG_INLINE void file_helper::flush()
}
}

SPDLOG_INLINE void file_helper::sync()
{
if(!os::fsync(fd_))
{
throw_spdlog_ex("Failed to fsync file " + os::filename_to_str(filename_), errno);
}
}

SPDLOG_INLINE void file_helper::close()
{
if (fd_ != nullptr)
Expand Down
1 change: 1 addition & 0 deletions include/spdlog/details/file_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class SPDLOG_API file_helper
void open(const filename_t &fname, bool truncate = false);
void reopen(bool truncate);
void flush();
void sync();
void close();
void write(const memory_buf_t &buf);
size_t size() const;
Expand Down
16 changes: 14 additions & 2 deletions include/spdlog/details/os-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@

#ifdef _WIN32

# include <io.h> // _get_osfhandle and _isatty support
# include <process.h> // _get_pid support
# include <io.h> // for _get_osfhandle, _isatty, _fileno
# include <process.h> // for _get_pid
# include <spdlog/details/windows_include.h>
# include <fileapi.h> // for FlushFileBuffers

# ifdef __MINGW32__
# include <share.h>
Expand Down Expand Up @@ -601,6 +602,17 @@ std::string SPDLOG_INLINE getenv(const char *field)
#endif
}

// Do fsync by FILE descriptor
// Return true on success
SPDLOG_INLINE bool fsync(FILE *fd)
{
#ifdef _WIN32
return FlushFileBuffers(reinterpret_cast<HANDLE>(_get_osfhandle(_fileno(fd)))) != 0;
#else
return fsync(fileno(fd)) == 0;
#endif
}

} // namespace os
} // namespace details
} // namespace spdlog
4 changes: 4 additions & 0 deletions include/spdlog/details/os.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ SPDLOG_API bool create_dir(const filename_t &path);
// return empty string if field not found
SPDLOG_API std::string getenv(const char *field);

// Do fsync by FILE descriptor
// Return true on success
SPDLOG_API bool fsync(FILE * fd);

} // namespace os
} // namespace details
} // namespace spdlog
Expand Down

0 comments on commit 83ffa9d

Please sign in to comment.