Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sync to file_helper #2343

Merged
merged 1 commit into from
Jan 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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