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
640921cd
Commit
640921cd
authored
Feb 10, 2020
by
gabime
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Optimize win_eventlog to avoid string allocation
parent
fccee959
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
31 additions
and
23 deletions
+31
-23
include/spdlog/sinks/win_eventlog_sink.h
include/spdlog/sinks/win_eventlog_sink.h
+31
-23
No files found.
include/spdlog/sinks/win_eventlog_sink.h
View file @
640921cd
...
@@ -37,6 +37,7 @@ Windows Registry Editor Version 5.00
...
@@ -37,6 +37,7 @@ Windows Registry Editor Version 5.00
#include <mutex>
#include <mutex>
#include <string>
#include <string>
#include <vector>
namespace
spdlog
{
namespace
spdlog
{
namespace
sinks
{
namespace
sinks
{
...
@@ -55,7 +56,7 @@ struct win32_error : public spdlog_ex
...
@@ -55,7 +56,7 @@ struct win32_error : public spdlog_ex
std
::
string
system_message
;
std
::
string
system_message
;
LPSTR
format_message_result
{};
LPSTR
format_message_result
{};
auto
format_message_succeeded
=
FormatMessage
(
auto
format_message_succeeded
=
::
FormatMessage
(
FORMAT_MESSAGE_ALLOCATE_BUFFER
|
FORMAT_MESSAGE_FROM_SYSTEM
|
FORMAT_MESSAGE_IGNORE_INSERTS
,
nullptr
,
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
);
error_code
,
MAKELANGID
(
LANG_NEUTRAL
,
SUBLANG_DEFAULT
),
(
LPSTR
)
&
format_message_result
,
0
,
nullptr
);
...
@@ -65,7 +66,9 @@ struct win32_error : public spdlog_ex
...
@@ -65,7 +66,9 @@ struct win32_error : public spdlog_ex
}
}
if
(
format_message_result
)
if
(
format_message_result
)
LocalFree
((
HLOCAL
)
format_message_result
);
{
LocalFree
((
HLOCAL
)
format_message_result
);
}
return
fmt
::
format
(
"{}: {}{}"
,
user_message
,
error_code
,
system_message
);
return
fmt
::
format
(
"{}: {}{}"
,
user_message
,
error_code
,
system_message
);
}
}
...
@@ -87,15 +90,19 @@ public:
...
@@ -87,15 +90,19 @@ public:
/** creates a wrapped SID copy */
/** creates a wrapped SID copy */
static
sid_t
duplicate_sid
(
PSID
psid
)
static
sid_t
duplicate_sid
(
PSID
psid
)
{
{
if
(
!
IsValidSid
(
psid
))
if
(
!::
IsValidSid
(
psid
))
{
SPDLOG_THROW
(
spdlog_ex
(
"sid_t::sid_t(): invalid SID received"
));
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
;
sid_t
result
;
result
.
buffer_
.
resize
(
sid_length
);
result
.
buffer_
.
resize
(
sid_length
);
if
(
!
CopySid
(
sid_length
,
(
PSID
)
result
.
as_sid
(),
psid
))
if
(
!::
CopySid
(
sid_length
,
(
PSID
)
result
.
as_sid
(),
psid
))
{
SPDLOG_THROW
(
win32_error
(
"CopySid"
));
SPDLOG_THROW
(
win32_error
(
"CopySid"
));
}
return
result
;
return
result
;
}
}
...
@@ -112,34 +119,36 @@ public:
...
@@ -112,34 +119,36 @@ public:
/* create and init RAII holder for process token */
/* create and init RAII holder for process token */
struct
process_token_t
struct
process_token_t
{
{
HANDLE
hToken_
;
HANDLE
token_handle_
=
INVALID_HANDLE_VALUE
;
bool
hasToken_
;
explicit
process_token_t
(
HANDLE
process
)
process_token_t
(
HANDLE
process
)
:
hToken_
(
0
)
,
hasToken_
(
OpenProcessToken
(
process
,
TOKEN_QUERY
,
&
hToken_
))
{
{
if
(
!
hasToken_
)
if
(
!::
OpenProcessToken
(
process
,
TOKEN_QUERY
,
&
token_handle_
))
{
SPDLOG_THROW
(
win32_error
(
"OpenProcessToken"
));
SPDLOG_THROW
(
win32_error
(
"OpenProcessToken"
));
}
}
}
~
process_token_t
()
~
process_token_t
()
{
{
if
(
hasToken_
)
::
CloseHandle
(
token_handle_
);
CloseHandle
(
hToken_
);
}
}
}
current_process_token
(
GetCurrentProcess
());
// GetCurrentProcess returns pseudohandle, no leak here!
}
current_process_token
(
::
GetCurrentProcess
());
// GetCurrentProcess returns pseudohandle, no leak here!
// Get the required size, this is expected to fail with ERROR_INSUFFICIENT_BUFFER and return the token size
// Get the required size, this is expected to fail with ERROR_INSUFFICIENT_BUFFER and return the token size
DWORD
tusize
=
0
;
DWORD
tusize
=
0
;
GetTokenInformation
(
current_process_token
.
hToken_
,
TokenUser
,
NULL
,
0
,
&
tusize
);
::
GetTokenInformation
(
current_process_token
.
token_handle_
,
TokenUser
,
NULL
,
0
,
&
tusize
);
if
(
GetLastError
()
!=
ERROR_INSUFFICIENT_BUFFER
)
if
(
::
GetLastError
()
!=
ERROR_INSUFFICIENT_BUFFER
)
{
SPDLOG_THROW
(
win32_error
(
"GetTokenInformation"
));
SPDLOG_THROW
(
win32_error
(
"GetTokenInformation"
));
}
// get user token
// get user token
std
::
vector
<
unsigned
char
>
buffer
(
tusize
);
std
::
vector
<
unsigned
char
>
buffer
(
static_cast
<
size_t
>
(
tusize
));
if
(
!
GetTokenInformation
(
current_process_token
.
hToken_
,
TokenUser
,
(
LPVOID
)
buffer
.
data
(),
tusize
,
&
tusize
))
if
(
!
GetTokenInformation
(
current_process_token
.
token_handle_
,
TokenUser
,
(
LPVOID
)
buffer
.
data
(),
tusize
,
&
tusize
))
{
SPDLOG_THROW
(
win32_error
(
"GetTokenInformation"
));
SPDLOG_THROW
(
win32_error
(
"GetTokenInformation"
));
}
// create a wrapper of the SID data as stored in the user token
// 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
);
...
@@ -213,10 +222,9 @@ protected:
...
@@ -213,10 +222,9 @@ protected:
memory_buf_t
formatted
;
memory_buf_t
formatted
;
formatter_
->
format
(
msg
,
formatted
);
formatter_
->
format
(
msg
,
formatted
);
formatted
.
push_back
(
'\0'
);
auto
formatted_string
=
fmt
::
to_string
(
formatted
);
LPCSTR
lp_str
=
static_cast
<
LPCSTR
>
(
formatted
.
data
());
auto
formatted_string_lpsz
=
formatted_string
.
c_str
();
bool
succeeded
=
ReportEvent
(
bool
succeeded
=
ReportEvent
(
event_log_handle
(),
event_log_handle
(),
eventlog
::
get_event_type
(
msg
),
eventlog
::
get_event_type
(
msg
),
...
@@ -225,7 +233,7 @@ protected:
...
@@ -225,7 +233,7 @@ protected:
current_user_sid_
.
as_sid
(),
current_user_sid_
.
as_sid
(),
1
,
1
,
0
,
0
,
&
formatted_string_lpsz
,
&
lp_str
,
nullptr
);
nullptr
);
if
(
!
succeeded
)
if
(
!
succeeded
)
...
...
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