Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
spdlog
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
spdlog
Commits
19dae969
Commit
19dae969
authored
Apr 10, 2016
by
gabime
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
wchar filenames support - minor improvements
parent
23200b08
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
62 additions
and
62 deletions
+62
-62
include/spdlog/common.h
include/spdlog/common.h
+9
-9
include/spdlog/details/file_helper.h
include/spdlog/details/file_helper.h
+11
-11
include/spdlog/details/os.h
include/spdlog/details/os.h
+15
-15
include/spdlog/details/spdlog_impl.h
include/spdlog/details/spdlog_impl.h
+4
-4
include/spdlog/sinks/file_sinks.h
include/spdlog/sinks/file_sinks.h
+18
-18
include/spdlog/spdlog.h
include/spdlog/spdlog.h
+4
-4
include/spdlog/tweakme.h
include/spdlog/tweakme.h
+1
-1
No files found.
include/spdlog/common.h
View file @
19dae969
...
...
@@ -11,7 +11,7 @@
#include <memory>
#include <exception>
#if defined(_WIN32) && defined(SPDLOG_
USE_WCHAR
)
#if defined(_WIN32) && defined(SPDLOG_
WCHAR_FILENAMES
)
#include <codecvt>
#include <locale>
#endif
...
...
@@ -100,22 +100,22 @@ private:
};
#if defined(_WIN32) && defined(SPDLOG_USE_WCHAR)
//
// wchar support for windows file names (SPDLOG_WCHAR_FILENAMES must be defined)
//
#if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES)
#define SPDLOG_FILENAME_T(s) L ## s
typedef
std
::
wstring
filename_str_t
;
typedef
wchar_t
filename_char_t
;
inline
std
::
string
filename_to_bytes
(
const
filename_str_t
&
filename
)
using
filename_t
=
std
::
wstring
;
inline
std
::
string
filename_to_str
(
const
filename_t
&
filename
)
{
std
::
wstring_convert
<
std
::
codecvt_utf8
<
wchar_t
>
,
wchar_t
>
c
;
return
c
.
to_bytes
(
filename
);
}
#else
#define SPDLOG_FILENAME_T(s) s
typedef
std
::
string
filename_str_t
;
typedef
char
filename_char_t
;
using
filename_t
=
std
::
string
;
inline
std
::
string
filename_to_
bytes
(
const
filename_str
_t
&
filename
)
inline
std
::
string
filename_to_
str
(
const
filename
_t
&
filename
)
{
return
filename
;
}
...
...
include/spdlog/details/file_helper.h
View file @
19dae969
...
...
@@ -43,11 +43,11 @@ public:
}
void
open
(
const
filename_
str_
t
&
fname
,
bool
truncate
=
false
)
void
open
(
const
filename_t
&
fname
,
bool
truncate
=
false
)
{
close
();
const
filename_char_t
*
mode
=
truncate
?
SPDLOG_FILENAME_T
(
"wb"
)
:
SPDLOG_FILENAME_T
(
"ab"
);
auto
*
mode
=
truncate
?
SPDLOG_FILENAME_T
(
"wb"
)
:
SPDLOG_FILENAME_T
(
"ab"
);
_filename
=
fname
;
for
(
int
tries
=
0
;
tries
<
open_tries
;
++
tries
)
{
...
...
@@ -57,7 +57,7 @@ public:
std
::
this_thread
::
sleep_for
(
std
::
chrono
::
milliseconds
(
open_interval
));
}
throw
spdlog_ex
(
"Failed opening file "
+
filename_to_
bytes
(
_filename
)
+
" for writing"
);
throw
spdlog_ex
(
"Failed opening file "
+
filename_to_
str
(
_filename
)
+
" for writing"
);
}
void
reopen
(
bool
truncate
)
...
...
@@ -88,7 +88,7 @@ public:
size_t
msg_size
=
msg
.
formatted
.
size
();
auto
data
=
msg
.
formatted
.
data
();
if
(
std
::
fwrite
(
data
,
1
,
msg_size
,
_fd
)
!=
msg_size
)
throw
spdlog_ex
(
"Failed writing to file "
+
filename_to_
bytes
(
_filename
));
throw
spdlog_ex
(
"Failed writing to file "
+
filename_to_
str
(
_filename
));
if
(
_force_flush
)
std
::
fflush
(
_fd
);
...
...
@@ -98,19 +98,19 @@ public:
long
size
()
{
if
(
!
_fd
)
throw
spdlog_ex
(
"Cannot use size() on closed file "
+
filename_to_
bytes
(
_filename
));
throw
spdlog_ex
(
"Cannot use size() on closed file "
+
filename_to_
str
(
_filename
));
auto
pos
=
ftell
(
_fd
);
if
(
fseek
(
_fd
,
0
,
SEEK_END
)
!=
0
)
throw
spdlog_ex
(
"fseek failed on file "
+
filename_to_
bytes
(
_filename
));
throw
spdlog_ex
(
"fseek failed on file "
+
filename_to_
str
(
_filename
));
auto
file_size
=
ftell
(
_fd
);
if
(
fseek
(
_fd
,
pos
,
SEEK_SET
)
!=
0
)
throw
spdlog_ex
(
"fseek failed on file "
+
filename_to_
bytes
(
_filename
));
throw
spdlog_ex
(
"fseek failed on file "
+
filename_to_
str
(
_filename
));
if
(
file_size
==
-
1
)
throw
spdlog_ex
(
"ftell failed on file "
+
filename_to_
bytes
(
_filename
));
throw
spdlog_ex
(
"ftell failed on file "
+
filename_to_
str
(
_filename
));
return
file_size
;
...
...
@@ -118,12 +118,12 @@ public:
}
const
filename_
str_
t
&
filename
()
const
const
filename_t
&
filename
()
const
{
return
_filename
;
}
static
bool
file_exists
(
const
filename_
str_
t
&
name
)
static
bool
file_exists
(
const
filename_t
&
name
)
{
return
os
::
file_exists
(
name
);
...
...
@@ -133,7 +133,7 @@ public:
private:
FILE
*
_fd
;
filename_
str_
t
_filename
;
filename_t
_filename
;
bool
_force_flush
;
...
...
include/spdlog/details/os.h
View file @
19dae969
...
...
@@ -137,45 +137,45 @@ constexpr inline unsigned short eol_size()
#endif
//fopen_s on non windows for writing
inline
int
fopen_s
(
FILE
**
fp
,
const
filename_
str_t
&
filename
,
const
filename_char_t
*
mode
)
inline
int
fopen_s
(
FILE
**
fp
,
const
filename_
t
&
filename
,
const
filename_t
&
mode
)
{
#ifdef _WIN32
#ifdef SPDLOG_
USE_WCHAR
*
fp
=
_wfsopen
((
filename
.
c_str
()),
mode
,
_SH_DENYWR
);
#ifdef SPDLOG_
WCHAR_FILENAMES
*
fp
=
_wfsopen
((
filename
.
c_str
()),
mode
.
c_str
()
,
_SH_DENYWR
);
#else
*
fp
=
_fsopen
((
filename
.
c_str
()),
mode
,
_SH_DENYWR
);
*
fp
=
_fsopen
((
filename
.
c_str
()),
mode
.
c_str
()
,
_SH_DENYWR
);
#endif
return
*
fp
==
nullptr
;
#else
*
fp
=
fopen
((
filename
.
c_str
()),
mode
);
*
fp
=
fopen
((
filename
.
c_str
()),
mode
.
c_str
()
);
return
*
fp
==
nullptr
;
#endif
}
inline
int
remove
(
const
filename_
char_t
*
filename
)
inline
int
remove
(
const
filename_
t
&
filename
)
{
#if defined(_WIN32) && defined(SPDLOG_
USE_WCHAR
)
return
_wremove
(
filename
);
#if defined(_WIN32) && defined(SPDLOG_
WCHAR_FILENAMES
)
return
_wremove
(
filename
.
c_str
()
);
#else
return
std
::
remove
(
filename
);
return
std
::
remove
(
filename
.
c_str
()
);
#endif
}
inline
int
rename
(
const
filename_
char_t
*
filename1
,
const
filename_char_t
*
filename2
)
inline
int
rename
(
const
filename_
t
&
filename1
,
const
filename_t
&
filename2
)
{
#if defined(_WIN32) && defined(SPDLOG_
USE_WCHAR
)
return
_wrename
(
filename1
,
filename2
);
#if defined(_WIN32) && defined(SPDLOG_
WCHAR_FILENAMES
)
return
_wrename
(
filename1
.
c_str
(),
filename2
.
c_str
()
);
#else
return
std
::
rename
(
filename1
,
filename2
);
return
std
::
rename
(
filename1
.
c_str
(),
filename2
.
c_str
()
);
#endif
}
//Return if file exists
inline
bool
file_exists
(
const
filename_
str_
t
&
filename
)
inline
bool
file_exists
(
const
filename_t
&
filename
)
{
#ifdef _WIN32
#ifdef SPDLOG_
USE_WCHAR
#ifdef SPDLOG_
WCHAR_FILENAMES
auto
attribs
=
GetFileAttributesW
(
filename
.
c_str
());
#else
auto
attribs
=
GetFileAttributesA
(
filename
.
c_str
());
...
...
include/spdlog/details/spdlog_impl.h
View file @
19dae969
...
...
@@ -36,23 +36,23 @@ inline void spdlog::drop(const std::string &name)
}
// Create multi/single threaded rotating file logger
inline
std
::
shared_ptr
<
spdlog
::
logger
>
spdlog
::
rotating_logger_mt
(
const
std
::
string
&
logger_name
,
const
filename_
str_
t
&
filename
,
size_t
max_file_size
,
size_t
max_files
,
bool
force_flush
)
inline
std
::
shared_ptr
<
spdlog
::
logger
>
spdlog
::
rotating_logger_mt
(
const
std
::
string
&
logger_name
,
const
filename_t
&
filename
,
size_t
max_file_size
,
size_t
max_files
,
bool
force_flush
)
{
return
create
<
spdlog
::
sinks
::
rotating_file_sink_mt
>
(
logger_name
,
filename
,
SPDLOG_FILENAME_T
(
"txt"
),
max_file_size
,
max_files
,
force_flush
);
}
inline
std
::
shared_ptr
<
spdlog
::
logger
>
spdlog
::
rotating_logger_st
(
const
std
::
string
&
logger_name
,
const
filename_
str_
t
&
filename
,
size_t
max_file_size
,
size_t
max_files
,
bool
force_flush
)
inline
std
::
shared_ptr
<
spdlog
::
logger
>
spdlog
::
rotating_logger_st
(
const
std
::
string
&
logger_name
,
const
filename_t
&
filename
,
size_t
max_file_size
,
size_t
max_files
,
bool
force_flush
)
{
return
create
<
spdlog
::
sinks
::
rotating_file_sink_st
>
(
logger_name
,
filename
,
SPDLOG_FILENAME_T
(
"txt"
),
max_file_size
,
max_files
,
force_flush
);
}
// Create file logger which creates new file at midnight):
inline
std
::
shared_ptr
<
spdlog
::
logger
>
spdlog
::
daily_logger_mt
(
const
std
::
string
&
logger_name
,
const
filename_
str_
t
&
filename
,
int
hour
,
int
minute
,
bool
force_flush
)
inline
std
::
shared_ptr
<
spdlog
::
logger
>
spdlog
::
daily_logger_mt
(
const
std
::
string
&
logger_name
,
const
filename_t
&
filename
,
int
hour
,
int
minute
,
bool
force_flush
)
{
return
create
<
spdlog
::
sinks
::
daily_file_sink_mt
>
(
logger_name
,
filename
,
SPDLOG_FILENAME_T
(
"txt"
),
hour
,
minute
,
force_flush
);
}
inline
std
::
shared_ptr
<
spdlog
::
logger
>
spdlog
::
daily_logger_st
(
const
std
::
string
&
logger_name
,
const
filename_
str_
t
&
filename
,
int
hour
,
int
minute
,
bool
force_flush
)
inline
std
::
shared_ptr
<
spdlog
::
logger
>
spdlog
::
daily_logger_st
(
const
std
::
string
&
logger_name
,
const
filename_t
&
filename
,
int
hour
,
int
minute
,
bool
force_flush
)
{
return
create
<
spdlog
::
sinks
::
daily_file_sink_st
>
(
logger_name
,
filename
,
SPDLOG_FILENAME_T
(
"txt"
),
hour
,
minute
,
force_flush
);
}
...
...
include/spdlog/sinks/file_sinks.h
View file @
19dae969
...
...
@@ -28,7 +28,7 @@ template<class Mutex>
class
simple_file_sink
:
public
base_sink
<
Mutex
>
{
public:
explicit
simple_file_sink
(
const
filename_
str_
t
&
filename
,
explicit
simple_file_sink
(
const
filename_t
&
filename
,
bool
force_flush
=
false
)
:
_file_helper
(
force_flush
)
{
...
...
@@ -58,7 +58,7 @@ template<class Mutex>
class
rotating_file_sink
:
public
base_sink
<
Mutex
>
{
public:
rotating_file_sink
(
const
filename_
str_t
&
base_filename
,
const
filename_str
_t
&
extension
,
rotating_file_sink
(
const
filename_
t
&
base_filename
,
const
filename
_t
&
extension
,
std
::
size_t
max_size
,
std
::
size_t
max_files
,
bool
force_flush
=
false
)
:
_base_filename
(
base_filename
),
...
...
@@ -90,9 +90,9 @@ protected:
}
private:
static
filename_
str_t
calc_filename
(
const
filename_str_t
&
filename
,
std
::
size_t
index
,
const
filename_str
_t
&
extension
)
static
filename_
t
calc_filename
(
const
filename_t
&
filename
,
std
::
size_t
index
,
const
filename
_t
&
extension
)
{
std
::
conditional
<
std
::
is_same
<
filename_
char_t
,
char
>::
value
,
fmt
::
MemoryWriter
,
fmt
::
WMemoryWriter
>::
type
w
;
std
::
conditional
<
std
::
is_same
<
filename_
t
::
value_type
,
char
>::
value
,
fmt
::
MemoryWriter
,
fmt
::
WMemoryWriter
>::
type
w
;
if
(
index
)
w
.
write
(
SPDLOG_FILENAME_T
(
"{}.{}.{}"
),
filename
,
index
,
extension
);
else
...
...
@@ -111,25 +111,25 @@ private:
_file_helper
.
close
();
for
(
auto
i
=
_max_files
;
i
>
0
;
--
i
)
{
filename_
str_
t
src
=
calc_filename
(
_base_filename
,
i
-
1
,
_extension
);
filename_
str_
t
target
=
calc_filename
(
_base_filename
,
i
,
_extension
);
filename_t
src
=
calc_filename
(
_base_filename
,
i
-
1
,
_extension
);
filename_t
target
=
calc_filename
(
_base_filename
,
i
,
_extension
);
if
(
details
::
file_helper
::
file_exists
(
target
))
{
if
(
details
::
os
::
remove
(
target
.
c_str
()
)
!=
0
)
if
(
details
::
os
::
remove
(
target
)
!=
0
)
{
throw
spdlog_ex
(
"rotating_file_sink: failed removing "
+
filename_to_
bytes
(
target
));
throw
spdlog_ex
(
"rotating_file_sink: failed removing "
+
filename_to_
str
(
target
));
}
}
if
(
details
::
file_helper
::
file_exists
(
src
)
&&
details
::
os
::
rename
(
src
.
c_str
(),
target
.
c_str
()
))
if
(
details
::
file_helper
::
file_exists
(
src
)
&&
details
::
os
::
rename
(
src
,
target
))
{
throw
spdlog_ex
(
"rotating_file_sink: failed renaming "
+
filename_to_
bytes
(
src
)
+
" to "
+
filename_to_bytes
(
target
));
throw
spdlog_ex
(
"rotating_file_sink: failed renaming "
+
filename_to_
str
(
src
)
+
" to "
+
filename_to_str
(
target
));
}
}
_file_helper
.
reopen
(
true
);
}
filename_
str_
t
_base_filename
;
filename_
str_
t
_extension
;
filename_t
_base_filename
;
filename_t
_extension
;
std
::
size_t
_max_size
;
std
::
size_t
_max_files
;
std
::
size_t
_current_size
;
...
...
@@ -148,8 +148,8 @@ class daily_file_sink :public base_sink < Mutex >
public:
//create daily file sink which rotates on given time
daily_file_sink
(
const
filename_
str_
t
&
base_filename
,
const
filename_
str_
t
&
extension
,
const
filename_t
&
base_filename
,
const
filename_t
&
extension
,
int
rotation_hour
,
int
rotation_minute
,
bool
force_flush
=
false
)
:
_base_filename
(
base_filename
),
...
...
@@ -198,16 +198,16 @@ private:
}
//Create filename for the form basename.YYYY-MM-DD.extension
static
filename_
str_t
calc_filename
(
const
filename_str_t
&
basename
,
const
filename_str
_t
&
extension
)
static
filename_
t
calc_filename
(
const
filename_t
&
basename
,
const
filename
_t
&
extension
)
{
std
::
tm
tm
=
spdlog
::
details
::
os
::
localtime
();
std
::
conditional
<
std
::
is_same
<
filename_char_t
,
char
>::
value
,
fmt
::
MemoryWriter
,
fmt
::
WMemoryWriter
>::
type
w
;
std
::
conditional
<
std
::
is_same
<
filename_t
::
value_type
,
char
>::
value
,
fmt
::
MemoryWriter
,
fmt
::
WMemoryWriter
>::
type
w
;
w
.
write
(
SPDLOG_FILENAME_T
(
"{}_{:04d}-{:02d}-{:02d}_{:02d}-{:02d}.{}"
),
basename
,
tm
.
tm_year
+
1900
,
tm
.
tm_mon
+
1
,
tm
.
tm_mday
,
tm
.
tm_hour
,
tm
.
tm_min
,
extension
);
return
w
.
str
();
}
filename_
str_
t
_base_filename
;
filename_
str_
t
_extension
;
filename_t
_base_filename
;
filename_t
_extension
;
int
_rotation_h
;
int
_rotation_m
;
std
::
chrono
::
system_clock
::
time_point
_rotation_tp
;
...
...
include/spdlog/spdlog.h
View file @
19dae969
...
...
@@ -61,14 +61,14 @@ void set_sync_mode();
//
// Create and register multi/single threaded rotating file logger
//
std
::
shared_ptr
<
logger
>
rotating_logger_mt
(
const
std
::
string
&
logger_name
,
const
filename_
str_
t
&
filename
,
size_t
max_file_size
,
size_t
max_files
,
bool
force_flush
=
false
);
std
::
shared_ptr
<
logger
>
rotating_logger_st
(
const
std
::
string
&
logger_name
,
const
filename_
str_
t
&
filename
,
size_t
max_file_size
,
size_t
max_files
,
bool
force_flush
=
false
);
std
::
shared_ptr
<
logger
>
rotating_logger_mt
(
const
std
::
string
&
logger_name
,
const
filename_t
&
filename
,
size_t
max_file_size
,
size_t
max_files
,
bool
force_flush
=
false
);
std
::
shared_ptr
<
logger
>
rotating_logger_st
(
const
std
::
string
&
logger_name
,
const
filename_t
&
filename
,
size_t
max_file_size
,
size_t
max_files
,
bool
force_flush
=
false
);
//
// Create file logger which creates new file on the given time (default in midnight):
//
std
::
shared_ptr
<
logger
>
daily_logger_mt
(
const
std
::
string
&
logger_name
,
const
filename_
str_
t
&
filename
,
int
hour
=
0
,
int
minute
=
0
,
bool
force_flush
=
false
);
std
::
shared_ptr
<
logger
>
daily_logger_st
(
const
std
::
string
&
logger_name
,
const
filename_
str_
t
&
filename
,
int
hour
=
0
,
int
minute
=
0
,
bool
force_flush
=
false
);
std
::
shared_ptr
<
logger
>
daily_logger_mt
(
const
std
::
string
&
logger_name
,
const
filename_t
&
filename
,
int
hour
=
0
,
int
minute
=
0
,
bool
force_flush
=
false
);
std
::
shared_ptr
<
logger
>
daily_logger_st
(
const
std
::
string
&
logger_name
,
const
filename_t
&
filename
,
int
hour
=
0
,
int
minute
=
0
,
bool
force_flush
=
false
);
//
// Create and register stdout/stderr loggers
...
...
include/spdlog/tweakme.h
View file @
19dae969
...
...
@@ -55,5 +55,5 @@
///////////////////////////////////////////////////////////////////////////////
// Uncomment to enable usage of wchar_t for file names on Windows.
// #define SPDLOG_
USE_WCHAR
// #define SPDLOG_
WCHAR_FILENAMES
///////////////////////////////////////////////////////////////////////////////
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