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
5370443e
Commit
5370443e
authored
Feb 10, 2020
by
gabime
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
clang-format
parent
ad4fb1cf
Changes
8
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
219 additions
and
219 deletions
+219
-219
include/spdlog/details/os-inl.h
include/spdlog/details/os-inl.h
+1
-1
include/spdlog/logger.h
include/spdlog/logger.h
+1
-1
include/spdlog/sinks/daily_file_sink.h
include/spdlog/sinks/daily_file_sink.h
+2
-1
include/spdlog/sinks/win_eventlog_sink.h
include/spdlog/sinks/win_eventlog_sink.h
+48
-60
src/fmt.cpp
src/fmt.cpp
+144
-137
tests/test_eventlog.cpp
tests/test_eventlog.cpp
+15
-13
tests/test_file_logging.cpp
tests/test_file_logging.cpp
+3
-4
tests/utils.cpp
tests/utils.cpp
+5
-2
No files found.
include/spdlog/details/os-inl.h
View file @
5370443e
...
...
@@ -150,7 +150,7 @@ SPDLOG_INLINE bool fopen_s(FILE **fp, const filename_t &filename, const filename
#if defined(SPDLOG_PREVENT_CHILD_FD)
const
int
mode_flag
=
mode
==
SPDLOG_FILENAME_T
(
"ab"
)
?
O_APPEND
:
O_TRUNC
;
const
int
fd
=
::
open
((
filename
.
c_str
()),
O_CREAT
|
O_WRONLY
|
O_CLOEXEC
|
mode_flag
,
mode_t
(
0644
));
if
(
fd
==
-
1
)
if
(
fd
==
-
1
)
{
return
false
;
}
...
...
include/spdlog/logger.h
View file @
5370443e
...
...
@@ -144,7 +144,7 @@ public:
template
<
class
T
,
typename
std
::
enable_if
<
std
::
is_convertible
<
const
T
&
,
spdlog
::
string_view_t
>
::
value
,
T
>::
type
*
=
nullptr
>
void
log
(
source_loc
loc
,
level
::
level_enum
lvl
,
const
T
&
msg
)
{
log
(
loc
,
lvl
,
string_view_t
{
msg
});
log
(
loc
,
lvl
,
string_view_t
{
msg
});
}
void
log
(
source_loc
loc
,
level
::
level_enum
lvl
,
string_view_t
msg
)
...
...
include/spdlog/sinks/daily_file_sink.h
View file @
5370443e
...
...
@@ -120,7 +120,8 @@ private:
filenames
.
emplace_back
(
filename
);
now
-=
std
::
chrono
::
hours
(
24
);
}
for
(
auto
iter
=
filenames
.
rbegin
();
iter
!=
filenames
.
rend
();
++
iter
)
{
for
(
auto
iter
=
filenames
.
rbegin
();
iter
!=
filenames
.
rend
();
++
iter
)
{
filenames_q_
.
push_back
(
std
::
move
(
*
iter
));
}
}
...
...
include/spdlog/sinks/win_eventlog_sink.h
View file @
5370443e
...
...
@@ -5,12 +5,12 @@
// 1. <log_name> should be replaced with your log name (e.g. your application name)
// 2. <source_name> should be replaced with the specific source name and the key should be duplicated for
// each source used in the application
//
//
// Since typically modifications of this kind require elevation, it's better to do it as a part of setup procedure.
// The snippet below uses mscoree.dll as the message file as it exists on most of the Windows systems anyway and
// The snippet below uses mscoree.dll as the message file as it exists on most of the Windows systems anyway and
// happens to contain the needed resource.
//
// You can also specify a custom message file if needed.
//
// You can also specify a custom message file if needed.
// Please refer to Event Log functions descriptions in MSDN for more details on custom message files.
/*---------------------------------------------------------------------------------------
...
...
@@ -44,21 +44,20 @@ namespace sinks {
namespace
win_eventlog
{
namespace
internal
{
namespace
internal
{
/** Windows error */
struct
win32_error
:
public
spdlog_ex
{
/** Formats an error report line: "user-message: error-code (system message)" */
static
std
::
string
format
(
std
::
string
const
&
user_message
,
DWORD
error_code
=
GetLastError
())
static
std
::
string
format
(
std
::
string
const
&
user_message
,
DWORD
error_code
=
GetLastError
())
{
std
::
string
system_message
;
LPSTR
format_message_result
{};
auto
format_message_succeeded
=
::
FormatMessage
(
FORMAT_MESSAGE_ALLOCATE_BUFFER
|
FORMAT_MESSAGE_FROM_SYSTEM
|
FORMAT_MESSAGE_IGNORE_INSERTS
,
nullptr
,
error_code
,
MAKELANGID
(
LANG_NEUTRAL
,
SUBLANG_DEFAULT
),
(
LPSTR
)
&
format_message_result
,
0
,
nullptr
);
LPSTR
format_message_result
{};
auto
format_message_succeeded
=
::
FormatMessage
(
FORMAT_MESSAGE_ALLOCATE_BUFFER
|
FORMAT_MESSAGE_FROM_SYSTEM
|
FORMAT_MESSAGE_IGNORE_INSERTS
,
nullptr
,
error_code
,
MAKELANGID
(
LANG_NEUTRAL
,
SUBLANG_DEFAULT
),
(
LPSTR
)
&
format_message_result
,
0
,
nullptr
);
if
(
format_message_succeeded
&&
format_message_result
)
{
...
...
@@ -73,7 +72,7 @@ struct win32_error : public spdlog_ex
return
fmt
::
format
(
"{}: {}{}"
,
user_message
,
error_code
,
system_message
);
}
win32_error
(
std
::
string
const
&
func_name
,
DWORD
error
=
GetLastError
())
win32_error
(
std
::
string
const
&
func_name
,
DWORD
error
=
GetLastError
())
:
spdlog_ex
(
format
(
func_name
,
error
))
{}
};
...
...
@@ -84,8 +83,7 @@ struct sid_t
std
::
vector
<
char
>
buffer_
;
public:
sid_t
()
{}
sid_t
()
{}
/** creates a wrapped SID copy */
static
sid_t
duplicate_sid
(
PSID
psid
)
...
...
@@ -95,7 +93,7 @@ public:
SPDLOG_THROW
(
spdlog_ex
(
"sid_t::sid_t(): invalid SID received"
));
}
auto
const
sid_length
{
::
GetLengthSid
(
psid
)};
auto
const
sid_length
{
::
GetLengthSid
(
psid
)};
sid_t
result
;
result
.
buffer_
.
resize
(
sid_length
);
...
...
@@ -108,9 +106,9 @@ public:
}
/** Retrieves pointer to the internal buffer contents as SID* */
SID
*
as_sid
()
const
SID
*
as_sid
()
const
{
return
buffer_
.
empty
()
?
nullptr
:
(
SID
*
)
buffer_
.
data
();
return
buffer_
.
empty
()
?
nullptr
:
(
SID
*
)
buffer_
.
data
();
}
/** Get SID for the current user */
...
...
@@ -119,11 +117,11 @@ public:
/* create and init RAII holder for process token */
struct
process_token_t
{
HANDLE
token_handle_
=
INVALID_HANDLE_VALUE
;
explicit
process_token_t
(
HANDLE
process
)
HANDLE
token_handle_
=
INVALID_HANDLE_VALUE
;
explicit
process_token_t
(
HANDLE
process
)
{
if
(
!::
OpenProcessToken
(
process
,
TOKEN_QUERY
,
&
token_handle_
))
{
{
SPDLOG_THROW
(
win32_error
(
"OpenProcessToken"
));
}
}
...
...
@@ -137,7 +135,7 @@ public:
// Get the required size, this is expected to fail with ERROR_INSUFFICIENT_BUFFER and return the token size
DWORD
tusize
=
0
;
if
(
::
GetTokenInformation
(
current_process_token
.
token_handle_
,
TokenUser
,
NULL
,
0
,
&
tusize
))
if
(
::
GetTokenInformation
(
current_process_token
.
token_handle_
,
TokenUser
,
NULL
,
0
,
&
tusize
))
{
SPDLOG_THROW
(
win32_error
(
"GetTokenInformation should fail"
));
}
...
...
@@ -150,57 +148,56 @@ public:
}
// create a wrapper of the SID data as stored in the user token
return
sid_t
::
duplicate_sid
(((
TOKEN_USER
*
)
buffer
.
data
())
->
User
.
Sid
);
return
sid_t
::
duplicate_sid
(((
TOKEN_USER
*
)
buffer
.
data
())
->
User
.
Sid
);
}
};
struct
eventlog
{
static
WORD
get_event_type
(
details
::
log_msg
const
&
msg
)
static
WORD
get_event_type
(
details
::
log_msg
const
&
msg
)
{
switch
(
msg
.
level
)
{
case
level
:
:
trace
:
case
level
:
:
debug
:
return
EVENTLOG_SUCCESS
;
case
level
:
:
trace
:
case
level
:
:
debug
:
return
EVENTLOG_SUCCESS
;
case
level
:
:
info
:
return
EVENTLOG_INFORMATION_TYPE
;
case
level
:
:
info
:
return
EVENTLOG_INFORMATION_TYPE
;
case
level
:
:
warn
:
return
EVENTLOG_WARNING_TYPE
;
case
level
:
:
warn
:
return
EVENTLOG_WARNING_TYPE
;
case
level
:
:
err
:
case
level
:
:
critical
:
case
level
:
:
off
:
return
EVENTLOG_ERROR_TYPE
;
case
level
:
:
err
:
case
level
:
:
critical
:
case
level
:
:
off
:
return
EVENTLOG_ERROR_TYPE
;
default:
// should be unreachable
SPDLOG_THROW
(
std
::
logic_error
(
fmt
::
format
(
"Unsupported log level {}"
,
msg
.
level
)));
default:
// should be unreachable
SPDLOG_THROW
(
std
::
logic_error
(
fmt
::
format
(
"Unsupported log level {}"
,
msg
.
level
)));
}
}
static
WORD
get_event_category
(
details
::
log_msg
const
&
msg
)
static
WORD
get_event_category
(
details
::
log_msg
const
&
msg
)
{
return
(
WORD
)
msg
.
level
;
return
(
WORD
)
msg
.
level
;
}
};
}
// namespace internal
/*
* Windows Event Log sink
*/
template
<
typename
Mutex
>
template
<
typename
Mutex
>
class
win_eventlog_sink
:
public
base_sink
<
Mutex
>
{
private:
HANDLE
hEventLog_
{
NULL
};
HANDLE
hEventLog_
{
NULL
};
internal
::
sid_t
current_user_sid_
;
std
::
string
source_
;
WORD
event_id_
;
std
::
string
source_
;
WORD
event_id_
;
HANDLE
event_log_handle
()
{
...
...
@@ -225,17 +222,9 @@ protected:
formatter_
->
format
(
msg
,
formatted
);
formatted
.
push_back
(
'\0'
);
LPCSTR
lp_str
=
static_cast
<
LPCSTR
>
(
formatted
.
data
());
auto
succeeded
=
::
ReportEvent
(
event_log_handle
(),
eventlog
::
get_event_type
(
msg
),
eventlog
::
get_event_category
(
msg
),
event_id_
,
current_user_sid_
.
as_sid
(),
1
,
0
,
&
lp_str
,
nullptr
);
auto
succeeded
=
::
ReportEvent
(
event_log_handle
(),
eventlog
::
get_event_type
(
msg
),
eventlog
::
get_event_category
(
msg
),
event_id_
,
current_user_sid_
.
as_sid
(),
1
,
0
,
&
lp_str
,
nullptr
);
if
(
!
succeeded
)
{
...
...
@@ -246,9 +235,9 @@ protected:
void
flush_
()
override
{}
public:
win_eventlog_sink
(
std
::
string
const
&
source
,
WORD
event_id
=
1000
/* according to mscoree.dll */
)
win_eventlog_sink
(
std
::
string
const
&
source
,
WORD
event_id
=
1000
/* according to mscoree.dll */
)
:
source_
(
source
)
,
event_id_
(
event_id
)
,
event_id_
(
event_id
)
{
try
{
...
...
@@ -256,8 +245,8 @@ public:
}
catch
(...)
{
// get_current_user_sid() is unlikely to fail and if it does, we can still proceed without
// current_user_sid but in the event log the record will have no user name
// get_current_user_sid() is unlikely to fail and if it does, we can still proceed without
// current_user_sid but in the event log the record will have no user name
}
}
...
...
@@ -275,4 +264,3 @@ using win_eventlog_sink_st = win_eventlog::win_eventlog_sink<details::null_mutex
}
// namespace sinks
}
// namespace spdlog
src/fmt.cpp
View file @
5370443e
This diff is collapsed.
Click to expand it.
tests/test_eventlog.cpp
View file @
5370443e
...
...
@@ -7,7 +7,7 @@
static
const
LPCSTR
TEST_SOURCE
=
"spdlog_test"
;
static
void
test_single_print
(
std
::
function
<
void
(
std
::
string
const
&
)
>
do_log
,
std
::
string
const
&
expected_contents
,
WORD
expected_ev_type
)
static
void
test_single_print
(
std
::
function
<
void
(
std
::
string
const
&
)
>
do_log
,
std
::
string
const
&
expected_contents
,
WORD
expected_ev_type
)
{
using
namespace
std
::
chrono
;
do_log
(
expected_contents
);
...
...
@@ -24,26 +24,28 @@ static void test_single_print(std::function<void(std::string const&)> do_log, st
REQUIRE
(
CloseEventLog
(
handle_
));
}
}
}
event_log
{
::
OpenEventLog
(
nullptr
,
TEST_SOURCE
)};
}
event_log
{
::
OpenEventLog
(
nullptr
,
TEST_SOURCE
)};
REQUIRE
(
event_log
.
handle_
);
DWORD
read_bytes
{},
size_needed
{};
auto
ok
=
::
ReadEventLog
(
event_log
.
handle_
,
EVENTLOG_SEQUENTIAL_READ
|
EVENTLOG_BACKWARDS_READ
,
0
,
&
read_bytes
,
0
,
&
read_bytes
,
&
size_needed
);
REQUIRE
(
!
ok
);
DWORD
read_bytes
{},
size_needed
{};
auto
ok
=
::
ReadEventLog
(
event_log
.
handle_
,
EVENTLOG_SEQUENTIAL_READ
|
EVENTLOG_BACKWARDS_READ
,
0
,
&
read_bytes
,
0
,
&
read_bytes
,
&
size_needed
);
REQUIRE
(
!
ok
);
REQUIRE
(
::
GetLastError
()
==
ERROR_INSUFFICIENT_BUFFER
);
std
::
vector
<
char
>
record_buffer
(
size_needed
);
PEVENTLOGRECORD
record
=
(
PEVENTLOGRECORD
)
record_buffer
.
data
();
ok
=
::
ReadEventLog
(
event_log
.
handle_
,
EVENTLOG_SEQUENTIAL_READ
|
EVENTLOG_BACKWARDS_READ
,
0
,
record
,
size_needed
,
&
read_bytes
,
&
size_needed
);
ok
=
::
ReadEventLog
(
event_log
.
handle_
,
EVENTLOG_SEQUENTIAL_READ
|
EVENTLOG_BACKWARDS_READ
,
0
,
record
,
size_needed
,
&
read_bytes
,
&
size_needed
);
REQUIRE
(
ok
);
REQUIRE
(
record
->
NumStrings
==
1
);
REQUIRE
(
record
->
EventType
==
expected_ev_type
);
REQUIRE
(
record
->
TimeGenerated
==
expected_time_generated
);
std
::
string
message_in_log
(((
char
*
)
record
+
record
->
StringOffset
));
std
::
string
message_in_log
(((
char
*
)
record
+
record
->
StringOffset
));
REQUIRE
(
message_in_log
==
expected_contents
+
spdlog
::
details
::
os
::
default_eol
);
}
...
...
@@ -58,12 +60,12 @@ TEST_CASE("eventlog", "[eventlog]")
test_sink
->
set_pattern
(
"%v"
);
test_single_print
([
&
test_logger
]
(
std
::
string
const
&
msg
)
{
test_logger
.
trace
(
msg
);
},
"my trace message"
,
EVENTLOG_SUCCESS
);
test_single_print
([
&
test_logger
]
(
std
::
string
const
&
msg
)
{
test_logger
.
debug
(
msg
);
},
"my debug message"
,
EVENTLOG_SUCCESS
);
test_single_print
([
&
test_logger
]
(
std
::
string
const
&
msg
)
{
test_logger
.
info
(
msg
);
},
"my info message"
,
EVENTLOG_INFORMATION_TYPE
);
test_single_print
([
&
test_logger
]
(
std
::
string
const
&
msg
)
{
test_logger
.
warn
(
msg
);
},
"my warn message"
,
EVENTLOG_WARNING_TYPE
);
test_single_print
([
&
test_logger
]
(
std
::
string
const
&
msg
)
{
test_logger
.
error
(
msg
);
},
"my error message"
,
EVENTLOG_ERROR_TYPE
);
test_single_print
([
&
test_logger
]
(
std
::
string
const
&
msg
)
{
test_logger
.
critical
(
msg
);
},
"my critical message"
,
EVENTLOG_ERROR_TYPE
);
test_single_print
([
&
test_logger
]
(
std
::
string
const
&
msg
)
{
test_logger
.
trace
(
msg
);
},
"my trace message"
,
EVENTLOG_SUCCESS
);
test_single_print
([
&
test_logger
]
(
std
::
string
const
&
msg
)
{
test_logger
.
debug
(
msg
);
},
"my debug message"
,
EVENTLOG_SUCCESS
);
test_single_print
([
&
test_logger
]
(
std
::
string
const
&
msg
)
{
test_logger
.
info
(
msg
);
},
"my info message"
,
EVENTLOG_INFORMATION_TYPE
);
test_single_print
([
&
test_logger
]
(
std
::
string
const
&
msg
)
{
test_logger
.
warn
(
msg
);
},
"my warn message"
,
EVENTLOG_WARNING_TYPE
);
test_single_print
([
&
test_logger
]
(
std
::
string
const
&
msg
)
{
test_logger
.
error
(
msg
);
},
"my error message"
,
EVENTLOG_ERROR_TYPE
);
test_single_print
([
&
test_logger
]
(
std
::
string
const
&
msg
)
{
test_logger
.
critical
(
msg
);
},
"my critical message"
,
EVENTLOG_ERROR_TYPE
);
}
#endif //_WIN32
\ No newline at end of file
tests/test_file_logging.cpp
View file @
5370443e
...
...
@@ -17,8 +17,7 @@ TEST_CASE("simple_file_logger", "[simple_logger]]")
logger
->
flush
();
require_message_count
(
filename
,
2
);
using
spdlog
::
details
::
os
::
default_eol
;
REQUIRE
(
file_contents
(
filename
)
==
fmt
::
format
(
"Test message 1{}Test message 2{}"
,
default_eol
,
default_eol
));
REQUIRE
(
file_contents
(
filename
)
==
fmt
::
format
(
"Test message 1{}Test message 2{}"
,
default_eol
,
default_eol
));
}
TEST_CASE
(
"flush_on"
,
"[flush_on]]"
)
...
...
@@ -38,8 +37,8 @@ TEST_CASE("flush_on", "[flush_on]]")
require_message_count
(
filename
,
3
);
using
spdlog
::
details
::
os
::
default_eol
;
REQUIRE
(
file_contents
(
filename
)
==
fmt
::
format
(
"Should not be flushed{}Test message 1{}Test message 2{}"
,
default_eol
,
default_eol
,
default_eol
));
REQUIRE
(
file_contents
(
filename
)
==
fmt
::
format
(
"Should not be flushed{}Test message 1{}Test message 2{}"
,
default_eol
,
default_eol
,
default_eol
));
}
TEST_CASE
(
"rotating_file_logger1"
,
"[rotating_logger]]"
)
...
...
tests/utils.cpp
View file @
5370443e
...
...
@@ -46,9 +46,12 @@ std::size_t count_lines(const std::string &filename)
void
require_message_count
(
const
std
::
string
&
filename
,
const
std
::
size_t
messages
)
{
if
(
strlen
(
spdlog
::
details
::
os
::
default_eol
)
==
0
)
{
if
(
strlen
(
spdlog
::
details
::
os
::
default_eol
)
==
0
)
{
REQUIRE
(
count_lines
(
filename
)
==
1
);
}
else
{
}
else
{
REQUIRE
(
count_lines
(
filename
)
==
messages
);
}
}
...
...
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