Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add eventlog_sink for logging to Windows Event Log (local only) #1418

Merged
merged 5 commits into from
Feb 9, 2020

Conversation

ban-dana
Copy link
Contributor

@ban-dana ban-dana commented Feb 7, 2020

adressing issue #989

include/spdlog/sinks/eventlog_sink.h Outdated Show resolved Hide resolved
// Copyright(c) 2015-present, Gabi Melman & spdlog contributors.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)

// Requires the following registry entries to be present, with the following modifications:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw that the add_registry_info already creates such entry? why is it "Requires the following registry entries to be present" ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registering a source is something that requires elevation (aka "running as administrator") so it's usually done during the setup. However I wanted to give the user an option to do it in code.

explicit eventlog_sink(
std::string const& source,
std::string const& log = "Application",
std::string const& message_file_path = "%windir%\\System32\\mscoree.dll");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this dll in particualr ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a part of Microsoft .NET Framework that is installed on pretty much every Windows system out there, and this dll happens to contain the needed resource. I don't think this is going to change any time soon, but since it is still an undocumented hack, I also give a way to do that with a custom built dll if so desired or if Microsoft changes their old dll or whatever.

Copy link
Owner

@gabime gabime Feb 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still a bit confusing to me. If we expect the user to set those reg entries beforehand using regedit, then there is no real benefit for all this registry code, and it can be simplified dramatically. Add to this the fact that user must run as admin for it to even work.
I suggest removing the registry code completely and let the user take care of it as written in your top comment (use regedit).

include/spdlog/sinks/eventlog_sink_win32.h Outdated Show resolved Hide resolved

struct sid
{
static SID* duplicate(SID* p_src_sid)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add some comments It is really hard to understand what is the purpose of those function

if (!GetTokenInformation(current_process_token.hToken_, TokenUser, (LPVOID) buffer.data(), tusize, &tusize))
SPDLOG_THROW(win32_error("GetTokenInformation"));

return sid::duplicate((SID *) ((TOKEN_USER*) buffer.data())->User.Sid);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need all those complicated sid functions? The "ReportEventA" docs state it can be NULL.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, but then the user information won't be available in the event viewer, which may sometimes be useful as the event log is a global facility of multi-user environment.

include/spdlog/sinks/eventlog_sink_win32.h Outdated Show resolved Hide resolved
include/spdlog/sinks/eventlog_sink_win32.h Outdated Show resolved Hide resolved
include/spdlog/sinks/eventlog_sink_win32.h Outdated Show resolved Hide resolved
include/spdlog/sinks/eventlog_sink_win32.h Outdated Show resolved Hide resolved
include/spdlog/sinks/eventlog_sink_win32.h Outdated Show resolved Hide resolved
include/spdlog/sinks/win_eventlog_sink.h Outdated Show resolved Hide resolved
}

/** Reports a message to stderr */
static void report(std::string const& message) SPDLOG_NOEXCEPT
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not needed. just throw spdlog_ex

include/spdlog/sinks/win_eventlog_sink.h Show resolved Hide resolved
include/spdlog/sinks/win_eventlog_sink.h Outdated Show resolved Hide resolved
@gabime gabime merged commit fccee95 into gabime:v1.x Feb 9, 2020
@gabime
Copy link
Owner

gabime commented Feb 9, 2020

@ban-dana Thanks. Merged.
I also cleaned some warnings here and optimized to avoid generating std::string on each call here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants