Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
S
spdlog
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
spdlog
Commits
730f0e02
Commit
730f0e02
authored
Jul 30, 2016
by
gabime
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
better support for file sizes in 32/64 bits
parent
b2c40fce
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
51 additions
and
16 deletions
+51
-16
file_helper.h
include/spdlog/details/file_helper.h
+3
-14
os.h
include/spdlog/details/os.h
+47
-1
file_helper.cpp
tests/file_helper.cpp
+1
-1
No files found.
include/spdlog/details/file_helper.h
View file @
730f0e02
...
@@ -96,24 +96,13 @@ public:
...
@@ -96,24 +96,13 @@ public:
std
::
fflush
(
_fd
);
std
::
fflush
(
_fd
);
}
}
long
size
()
size_t
size
()
{
{
if
(
!
_fd
)
if
(
!
_fd
)
throw
spdlog_ex
(
"Cannot use size() on closed file "
+
os
::
filename_to_str
(
_filename
));
throw
spdlog_ex
(
"Cannot use size() on closed file "
+
os
::
filename_to_str
(
_filename
));
auto
pos
=
ftell
(
_fd
);
return
os
::
filesize
(
_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
;
}
}
const
filename_t
&
filename
()
const
const
filename_t
&
filename
()
const
...
...
include/spdlog/details/os.h
View file @
730f0e02
...
@@ -10,7 +10,10 @@
...
@@ -10,7 +10,10 @@
#include <ctime>
#include <ctime>
#include <functional>
#include <functional>
#include <string>
#include <string>
#include <stdio.h>
#include <string.h>
#include <string.h>
#include <sys/stat.h>
#ifdef _WIN32
#ifdef _WIN32
...
@@ -27,10 +30,11 @@
...
@@ -27,10 +30,11 @@
#include <share.h>
#include <share.h>
#endif
#endif
#include <sys/types.h>
#elif __linux__
#elif __linux__
#include <sys/syscall.h> //Use gettid() syscall under linux to get thread id
#include <sys/syscall.h> //Use gettid() syscall under linux to get thread id
#include <sys/stat.h>
#include <unistd.h>
#include <unistd.h>
#include <chrono>
#include <chrono>
...
@@ -182,6 +186,48 @@ inline bool file_exists(const filename_t& filename)
...
@@ -182,6 +186,48 @@ inline bool file_exists(const filename_t& filename)
#endif
#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
//Return utc offset in minutes or throw spdlog_ex on failure
inline
int
utc_minutes_offset
(
const
std
::
tm
&
tm
=
details
::
os
::
localtime
())
inline
int
utc_minutes_offset
(
const
std
::
tm
&
tm
=
details
::
os
::
localtime
())
{
{
...
...
tests/file_helper.cpp
View file @
730f0e02
...
@@ -63,7 +63,7 @@ TEST_CASE("file_helper_reopen", "[file_helper::reopen()]]")
...
@@ -63,7 +63,7 @@ TEST_CASE("file_helper_reopen", "[file_helper::reopen()]]")
TEST_CASE
(
"file_helper_reopen2"
,
"[file_helper::reopen(false)]]"
)
TEST_CASE
(
"file_helper_reopen2"
,
"[file_helper::reopen(false)]]"
)
{
{
prepare_logdir
();
prepare_logdir
();
auto
expected_size
=
14
;
size_t
expected_size
=
14
;
file_helper
helper
(
true
);
file_helper
helper
(
true
);
helper
.
open
(
target_filename
);
helper
.
open
(
target_filename
);
write_with_helper
(
helper
,
expected_size
);
write_with_helper
(
helper
,
expected_size
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment