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
3632d019
Commit
3632d019
authored
Jul 27, 2015
by
Gabi Melman
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #111 from GreatFruitOmsk/master
Add support for wchar_t on Windows
parents
08b6b0be
d905ad91
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
85 additions
and
43 deletions
+85
-43
include/spdlog/common.h
include/spdlog/common.h
+9
-0
include/spdlog/details/file_helper.h
include/spdlog/details/file_helper.h
+8
-8
include/spdlog/details/format.h
include/spdlog/details/format.h
+7
-1
include/spdlog/details/os.h
include/spdlog/details/os.h
+22
-2
include/spdlog/details/spdlog_impl.h
include/spdlog/details/spdlog_impl.h
+8
-8
include/spdlog/sinks/file_sinks.h
include/spdlog/sinks/file_sinks.h
+21
-20
include/spdlog/spdlog.h
include/spdlog/spdlog.h
+4
-4
include/spdlog/tweakme.h
include/spdlog/tweakme.h
+6
-0
No files found.
include/spdlog/common.h
View file @
3632d019
...
...
@@ -36,6 +36,15 @@
#define SPDLOG_NOEXCEPT throw()
#endif
#if defined(WIN32) && defined(SPDLOG_USE_WCHAR)
typedef
std
::
wstring
tstring
;
typedef
wchar_t
tchar
;
#define S(s) L ## s
#else
#define S(s) s
typedef
std
::
string
tstring
;
typedef
char
tchar
;
#endif
namespace
spdlog
{
...
...
include/spdlog/details/file_helper.h
View file @
3632d019
...
...
@@ -62,11 +62,11 @@ public:
}
void
open
(
const
std
::
string
&
fname
,
bool
truncate
=
false
)
void
open
(
const
t
string
&
fname
,
bool
truncate
=
false
)
{
close
();
const
char
*
mode
=
truncate
?
"wb"
:
"ab"
;
const
tchar
*
mode
=
truncate
?
S
(
"wb"
)
:
S
(
"ab"
)
;
_filename
=
fname
;
for
(
int
tries
=
0
;
tries
<
open_tries
;
++
tries
)
{
...
...
@@ -76,7 +76,7 @@ public:
std
::
this_thread
::
sleep_for
(
std
::
chrono
::
milliseconds
(
open_interval
));
}
throw
spdlog_ex
(
"Failed opening file
"
+
fname
+
"
for writing"
);
throw
spdlog_ex
(
"Failed opening file for writing"
);
}
void
reopen
(
bool
truncate
)
...
...
@@ -106,22 +106,22 @@ public:
size_t
size
=
msg
.
formatted
.
size
();
auto
data
=
msg
.
formatted
.
data
();
if
(
std
::
fwrite
(
data
,
1
,
size
,
_fd
)
!=
size
)
throw
spdlog_ex
(
"Failed writing to file
"
+
_filename
);
throw
spdlog_ex
(
"Failed writing to file
"
);
if
(
_force_flush
)
std
::
fflush
(
_fd
);
}
const
std
::
string
&
filename
()
const
const
t
string
&
filename
()
const
{
return
_filename
;
}
static
bool
file_exists
(
const
std
::
string
&
name
)
static
bool
file_exists
(
const
t
string
&
name
)
{
FILE
*
file
;
if
(
!
os
::
fopen_s
(
&
file
,
name
.
c_str
(),
"r"
))
if
(
!
os
::
fopen_s
(
&
file
,
name
.
c_str
(),
S
(
"r"
)
))
{
fclose
(
file
);
return
true
;
...
...
@@ -134,7 +134,7 @@ public:
private:
FILE
*
_fd
;
std
::
string
_filename
;
t
string
_filename
;
bool
_force_flush
;
...
...
include/spdlog/details/format.h
View file @
3632d019
...
...
@@ -2638,6 +2638,12 @@ public:
typedef
BasicMemoryWriter
<
char
>
MemoryWriter
;
typedef
BasicMemoryWriter
<
wchar_t
>
WMemoryWriter
;
#if defined(WIN32) && defined(SPDLOG_USE_WCHAR)
#define TMemoryWriter WMemoryWriter
#else
#define TMemoryWriter MemoryWriter
#endif
/**
\rst
This class template provides operations for formatting and writing data
...
...
@@ -3152,4 +3158,4 @@ FMT_VARIADIC(int, fprintf, std::FILE *, CStringRef)
# include "format.cc"
#endif
#endif // FMT_FORMAT_H_
\ No newline at end of file
#endif // FMT_FORMAT_H_
include/spdlog/details/os.h
View file @
3632d019
...
...
@@ -147,17 +147,37 @@ constexpr inline unsigned short eol_size()
#endif
//fopen_s on non windows for writing
inline
int
fopen_s
(
FILE
**
fp
,
const
std
::
string
&
filename
,
const
char
*
mode
)
inline
int
fopen_s
(
FILE
**
fp
,
const
tstring
&
filename
,
const
t
char
*
mode
)
{
#ifdef _WIN32
#if defined(WIN32)
#if defined(SPDLOG_USE_WCHAR)
*
fp
=
_wfsopen
((
filename
.
c_str
()),
mode
,
_SH_DENYWR
);
#else
*
fp
=
_fsopen
((
filename
.
c_str
()),
mode
,
_SH_DENYWR
);
#endif
return
*
fp
==
nullptr
;
#else
*
fp
=
fopen
((
filename
.
c_str
()),
mode
);
return
*
fp
==
nullptr
;
#endif
}
inline
int
remove
(
const
tchar
*
filename
)
{
#if defined(WIN32) && defined(SPDLOG_USE_WCHAR)
return
_wremove
(
filename
);
#else
return
std
::
remove
(
filename
);
#endif
}
inline
int
rename
(
const
tchar
*
filename1
,
const
tchar
*
filename2
)
{
#if defined(WIN32) && defined(SPDLOG_USE_WCHAR)
return
_wrename
(
filename1
,
filename2
);
#else
return
std
::
rename
(
filename1
,
filename2
);
#endif
}
//Return utc offset in minutes or -1 on failure
...
...
include/spdlog/details/spdlog_impl.h
View file @
3632d019
...
...
@@ -48,24 +48,24 @@ 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
std
::
string
&
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
t
string
&
filename
,
size_t
max_file_size
,
size_t
max_files
,
bool
force_flush
)
{
return
create
<
spdlog
::
sinks
::
rotating_file_sink_mt
>
(
logger_name
,
filename
,
"txt"
,
max_file_size
,
max_files
,
force_flush
);
return
create
<
spdlog
::
sinks
::
rotating_file_sink_mt
>
(
logger_name
,
filename
,
S
(
"txt"
)
,
max_file_size
,
max_files
,
force_flush
);
}
inline
std
::
shared_ptr
<
spdlog
::
logger
>
spdlog
::
rotating_logger_st
(
const
std
::
string
&
logger_name
,
const
std
::
string
&
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
t
string
&
filename
,
size_t
max_file_size
,
size_t
max_files
,
bool
force_flush
)
{
return
create
<
spdlog
::
sinks
::
rotating_file_sink_st
>
(
logger_name
,
filename
,
"txt"
,
max_file_size
,
max_files
,
force_flush
);
return
create
<
spdlog
::
sinks
::
rotating_file_sink_st
>
(
logger_name
,
filename
,
S
(
"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
std
::
string
&
filename
,
int
hour
,
int
minute
,
bool
force_flush
)
inline
std
::
shared_ptr
<
spdlog
::
logger
>
spdlog
::
daily_logger_mt
(
const
std
::
string
&
logger_name
,
const
t
string
&
filename
,
int
hour
,
int
minute
,
bool
force_flush
)
{
return
create
<
spdlog
::
sinks
::
daily_file_sink_mt
>
(
logger_name
,
filename
,
"txt"
,
hour
,
minute
,
force_flush
);
return
create
<
spdlog
::
sinks
::
daily_file_sink_mt
>
(
logger_name
,
filename
,
S
(
"txt"
)
,
hour
,
minute
,
force_flush
);
}
inline
std
::
shared_ptr
<
spdlog
::
logger
>
spdlog
::
daily_logger_st
(
const
std
::
string
&
logger_name
,
const
std
::
string
&
filename
,
int
hour
,
int
minute
,
bool
force_flush
)
inline
std
::
shared_ptr
<
spdlog
::
logger
>
spdlog
::
daily_logger_st
(
const
std
::
string
&
logger_name
,
const
t
string
&
filename
,
int
hour
,
int
minute
,
bool
force_flush
)
{
return
create
<
spdlog
::
sinks
::
daily_file_sink_st
>
(
logger_name
,
filename
,
"txt"
,
hour
,
minute
,
force_flush
);
return
create
<
spdlog
::
sinks
::
daily_file_sink_st
>
(
logger_name
,
filename
,
S
(
"txt"
)
,
hour
,
minute
,
force_flush
);
}
...
...
include/spdlog/sinks/file_sinks.h
View file @
3632d019
...
...
@@ -29,6 +29,7 @@
#include "../details/null_mutex.h"
#include "../details/file_helper.h"
#include "../details/format.h"
#include "../details/os.h"
namespace
spdlog
{
...
...
@@ -71,7 +72,7 @@ template<class Mutex>
class
rotating_file_sink
:
public
base_sink
<
Mutex
>
{
public:
rotating_file_sink
(
const
std
::
string
&
base_filename
,
const
std
::
string
&
extension
,
rotating_file_sink
(
const
tstring
&
base_filename
,
const
t
string
&
extension
,
std
::
size_t
max_size
,
std
::
size_t
max_files
,
bool
force_flush
=
false
)
:
_base_filename
(
base_filename
),
...
...
@@ -102,13 +103,13 @@ protected:
}
private:
static
std
::
string
calc_filename
(
const
std
::
string
&
filename
,
std
::
size_t
index
,
const
std
::
string
&
extension
)
static
tstring
calc_filename
(
const
tstring
&
filename
,
std
::
size_t
index
,
const
t
string
&
extension
)
{
fmt
::
MemoryWriter
w
;
fmt
::
T
MemoryWriter
w
;
if
(
index
)
w
.
write
(
"{}.{}.{}"
,
filename
,
index
,
extension
);
w
.
write
(
S
(
"{}.{}.{}"
)
,
filename
,
index
,
extension
);
else
w
.
write
(
"{}.{}"
,
filename
,
extension
);
w
.
write
(
S
(
"{}.{}"
)
,
filename
,
extension
);
return
w
.
str
();
}
...
...
@@ -123,25 +124,25 @@ private:
_file_helper
.
close
();
for
(
auto
i
=
_max_files
;
i
>
0
;
--
i
)
{
std
::
string
src
=
calc_filename
(
_base_filename
,
i
-
1
,
_extension
);
std
::
string
target
=
calc_filename
(
_base_filename
,
i
,
_extension
);
t
string
src
=
calc_filename
(
_base_filename
,
i
-
1
,
_extension
);
t
string
target
=
calc_filename
(
_base_filename
,
i
,
_extension
);
if
(
details
::
file_helper
::
file_exists
(
target
))
{
if
(
std
::
remove
(
target
.
c_str
())
!=
0
)
if
(
details
::
os
::
remove
(
target
.
c_str
())
!=
0
)
{
throw
spdlog_ex
(
"rotating_file_sink: failed removing
"
+
target
);
throw
spdlog_ex
(
"rotating_file_sink: failed removing
"
);
}
}
if
(
details
::
file_helper
::
file_exists
(
src
)
&&
std
::
rename
(
src
.
c_str
(),
target
.
c_str
()))
if
(
details
::
file_helper
::
file_exists
(
src
)
&&
details
::
os
::
rename
(
src
.
c_str
(),
target
.
c_str
()))
{
throw
spdlog_ex
(
"rotating_file_sink: failed renaming
"
+
src
+
" to "
+
target
);
throw
spdlog_ex
(
"rotating_file_sink: failed renaming
"
);
}
}
_file_helper
.
reopen
(
true
);
}
std
::
string
_base_filename
;
std
::
string
_extension
;
t
string
_base_filename
;
t
string
_extension
;
std
::
size_t
_max_size
;
std
::
size_t
_max_files
;
std
::
size_t
_current_size
;
...
...
@@ -160,8 +161,8 @@ class daily_file_sink :public base_sink < Mutex >
public:
//create daily file sink which rotates on given time
daily_file_sink
(
const
std
::
string
&
base_filename
,
const
std
::
string
&
extension
,
const
t
string
&
base_filename
,
const
t
string
&
extension
,
int
rotation_hour
,
int
rotation_minute
,
bool
force_flush
=
false
)
:
_base_filename
(
base_filename
),
...
...
@@ -210,16 +211,16 @@ private:
}
//Create filename for the form basename.YYYY-MM-DD.extension
static
std
::
string
calc_filename
(
const
std
::
string
&
basename
,
const
std
::
string
&
extension
)
static
tstring
calc_filename
(
const
tstring
&
basename
,
const
t
string
&
extension
)
{
std
::
tm
tm
=
spdlog
::
details
::
os
::
localtime
();
fmt
::
MemoryWriter
w
;
w
.
write
(
"{}_{:04d}-{:02d}-{:02d}_{:02d}-{:02d}.{}"
,
basename
,
tm
.
tm_year
+
1900
,
tm
.
tm_mon
+
1
,
tm
.
tm_mday
,
tm
.
tm_hour
,
tm
.
tm_min
,
extension
);
fmt
::
T
MemoryWriter
w
;
w
.
write
(
S
(
"{}_{: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
();
}
std
::
string
_base_filename
;
std
::
string
_extension
;
t
string
_base_filename
;
t
string
_extension
;
int
_rotation_h
;
int
_rotation_m
;
std
::
chrono
::
system_clock
::
time_point
_rotation_tp
;
...
...
include/spdlog/spdlog.h
View file @
3632d019
...
...
@@ -76,14 +76,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
std
::
string
&
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
std
::
string
&
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
t
string
&
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
t
string
&
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
std
::
string
&
filename
,
int
hour
=
0
,
int
minute
=
0
,
bool
force_flush
=
false
);
std
::
shared_ptr
<
logger
>
daily_logger_st
(
const
std
::
string
&
logger_name
,
const
std
::
string
&
filename
,
int
hour
=
0
,
int
minute
=
0
,
bool
force_flush
=
false
);
std
::
shared_ptr
<
logger
>
daily_logger_mt
(
const
std
::
string
&
logger_name
,
const
t
string
&
filename
,
int
hour
=
0
,
int
minute
=
0
,
bool
force_flush
=
false
);
std
::
shared_ptr
<
logger
>
daily_logger_st
(
const
std
::
string
&
logger_name
,
const
t
string
&
filename
,
int
hour
=
0
,
int
minute
=
0
,
bool
force_flush
=
false
);
//
...
...
include/spdlog/tweakme.h
View file @
3632d019
...
...
@@ -72,3 +72,9 @@
// Note that upon creating a logger the registry is modified by spdlog..
// #define SPDLOG_NO_REGISTRY_MUTEX
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// Uncomment to enable usage of wchar_t for file names on Windows.
// #define SPDLOG_USE_WCHAR
///////////////////////////////////////////////////////////////////////////////
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