mirror of
https://github.com/gabime/spdlog.git
synced 2024-12-25 01:51:38 +08:00
better support for file sizes in 32/64 bits
This commit is contained in:
parent
b2c40fcedf
commit
730f0e02a6
@ -96,24 +96,13 @@ public:
|
||||
std::fflush(_fd);
|
||||
}
|
||||
|
||||
long size()
|
||||
size_t size()
|
||||
{
|
||||
if (!_fd)
|
||||
throw spdlog_ex("Cannot use size() on closed file " + os::filename_to_str(_filename));
|
||||
|
||||
auto pos = ftell(_fd);
|
||||
if (fseek(_fd, 0, SEEK_END) != 0)
|
||||
throw spdlog_ex("fseek failed on file " + os::filename_to_str(_filename), errno);
|
||||
|
||||
auto file_size = ftell(_fd);
|
||||
|
||||
if(fseek(_fd, pos, SEEK_SET) !=0)
|
||||
throw spdlog_ex("fseek failed on file " + os::filename_to_str(_filename), errno);
|
||||
|
||||
if (file_size == -1)
|
||||
throw spdlog_ex("ftell failed on file " + os::filename_to_str(_filename), errno);
|
||||
|
||||
return file_size;
|
||||
return os::filesize(_fd);
|
||||
|
||||
}
|
||||
|
||||
const filename_t& filename() const
|
||||
|
@ -10,7 +10,10 @@
|
||||
#include <ctime>
|
||||
#include <functional>
|
||||
#include <string>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
|
||||
#ifdef _WIN32
|
||||
|
||||
@ -27,10 +30,11 @@
|
||||
#include <share.h>
|
||||
#endif
|
||||
|
||||
#include <sys/types.h>
|
||||
|
||||
#elif __linux__
|
||||
|
||||
#include <sys/syscall.h> //Use gettid() syscall under linux to get thread id
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#include <chrono>
|
||||
|
||||
@ -182,6 +186,48 @@ inline bool file_exists(const filename_t& filename)
|
||||
#endif
|
||||
}
|
||||
|
||||
//Return file size according to open FILE* object
|
||||
inline size_t filesize(FILE *f)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
#if _WIN64 //64 bits
|
||||
int fd = _fileno(f);
|
||||
struct _stat64 st;
|
||||
if (_fstat64(fd, &st) == 0)
|
||||
return st.st_size;
|
||||
else
|
||||
throw spdlog_ex("Failed getting file size from fd", errno);
|
||||
#else //windows 32 bits
|
||||
int fd = _fileno(f);
|
||||
struct _stat st;
|
||||
if (_fstat(fd, &st) == 0)
|
||||
return st.st_size;
|
||||
else
|
||||
throw spdlog_ex("Failed getting file size from fd", errno);
|
||||
#endif
|
||||
|
||||
#else// common unix
|
||||
#if __x86_64__ || __ppc64__ //64 bits
|
||||
int fd = fileno(f)
|
||||
struct stat64 st;
|
||||
if (fstat64(fd, &st) == 0)
|
||||
return st.st_size;
|
||||
else
|
||||
throw spdlog_ex("Failed getting file size from fd", errno);
|
||||
#else //unix 32 bits
|
||||
int fd = fileno(f)
|
||||
struct stat st;
|
||||
if (fstat(fd, &st) == 0)
|
||||
return st.st_size;
|
||||
else
|
||||
throw spdlog_ex("Failed getting file size from fd", errno);
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
//Return utc offset in minutes or throw spdlog_ex on failure
|
||||
inline int utc_minutes_offset(const std::tm& tm = details::os::localtime())
|
||||
{
|
||||
|
@ -63,7 +63,7 @@ TEST_CASE("file_helper_reopen", "[file_helper::reopen()]]")
|
||||
TEST_CASE("file_helper_reopen2", "[file_helper::reopen(false)]]")
|
||||
{
|
||||
prepare_logdir();
|
||||
auto expected_size = 14;
|
||||
size_t expected_size = 14;
|
||||
file_helper helper(true);
|
||||
helper.open(target_filename);
|
||||
write_with_helper(helper, expected_size);
|
||||
|
Loading…
Reference in New Issue
Block a user