Skip to content

Commit

Permalink
fix to unblock Event Log Failure in ltcs 2019 image
Browse files Browse the repository at this point in the history
  • Loading branch information
bobsira committed Sep 13, 2023
1 parent 8735dc0 commit b061bce
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 5 deletions.
144 changes: 140 additions & 4 deletions LogMonitor/src/LogMonitor/EventMonitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -599,18 +599,152 @@ EventMonitor::EnableEventLogChannels()
{
for (const auto& eventChannel : m_eventChannels)
{
EnableEventLogChannel(eventChannel.Name.c_str());
DWORD status = EnableEventLogChannel(eventChannel.Name.c_str());

if (status == RPC_S_SERVER_UNAVAILABLE) {

Check warning on line 605 in LogMonitor/src/LogMonitor/EventMonitor.cpp

View workflow job for this annotation

GitHub Actions / cpplint

[cpplint] LogMonitor/src/LogMonitor/EventMonitor.cpp#L605

Redundant blank line at the start of a code block should be deleted. [whitespace/blank_line] [2]
Raw output
LogMonitor/src/LogMonitor/EventMonitor.cpp:605:  Redundant blank line at the start of a code block should be deleted.  [whitespace/blank_line] [2]
HANDLE timerEvent = CreateWaitableTimer(NULL, FALSE, NULL);


if (!timerEvent) {
status = GetLastError();
logWriter.TraceError(
Utility::FormatString(
L"Failed to create timer object.", status).c_str());
}

std::double_t waitInSeconds = 300;

int elapsedTime = 0;

const int eventsCount = 2;
HANDLE dirOpenEvents[eventsCount] = {m_stopEvent, timerEvent};

while (elapsedTime < waitInSeconds) {

Check warning on line 624 in LogMonitor/src/LogMonitor/EventMonitor.cpp

View workflow job for this annotation

GitHub Actions / cpplint

[cpplint] LogMonitor/src/LogMonitor/EventMonitor.cpp#L624

Redundant blank line at the start of a code block should be deleted. [whitespace/blank_line] [2]
Raw output
LogMonitor/src/LogMonitor/EventMonitor.cpp:624:  Redundant blank line at the start of a code block should be deleted.  [whitespace/blank_line] [2]
int waitInterval = EventMonitor::_GetWaitInterval(waitInSeconds, elapsedTime);
LARGE_INTEGER timeToWait = EventMonitor::_ConvertWaitIntervalToLargeInt(waitInterval);

BOOL waitableTimer = SetWaitableTimer(timerEvent, &timeToWait, 0, NULL, NULL, 0);
if (!waitableTimer) {
status = GetLastError();
logWriter.TraceError(
Utility::FormatString(
L"Failed to set timer object to enable %s event channel. Error: %lu",
eventChannel.Name.c_str(),
status).c_str());
break;
}

DWORD wait = WaitForMultipleObjects(eventsCount, dirOpenEvents, FALSE, INFINITE);
switch(wait)
{
case WAIT_OBJECT_0:
{
//
// The process is exiting. Stop the timer and return.
//
CancelWaitableTimer(timerEvent);
CloseHandle(timerEvent);
// return INVALID_HANDLE_VALUE;
}

case WAIT_OBJECT_0 + 1:
{
//
// Timer event. Retry opening directory handle.
//
break;
}

default:
{
//
// Wait failed, return the failure.
//
status = GetLastError();

CancelWaitableTimer(timerEvent);
CloseHandle(timerEvent);

Check warning on line 669 in LogMonitor/src/LogMonitor/EventMonitor.cpp

View workflow job for this annotation

GitHub Actions / cpplint

[cpplint] LogMonitor/src/LogMonitor/EventMonitor.cpp#L669

Redundant blank line at the end of a code block should be deleted. [whitespace/blank_line] [3]
Raw output
LogMonitor/src/LogMonitor/EventMonitor.cpp:669:  Redundant blank line at the end of a code block should be deleted.  [whitespace/blank_line] [3]
}
}

DWORD status = EnableEventLogChannel(eventChannel.Name.c_str());

if (status == RPC_S_SERVER_UNAVAILABLE)
{
elapsedTime += WAIT_INTERVAL;
}
else
{
logWriter.TraceInfo(
Utility::FormatString(
L"Enabled %s event channel after %d seconds.",
eventChannel.Name.c_str(),
elapsedTime).c_str()
);
status = ERROR_SUCCESS;
break;
}

Check warning on line 690 in LogMonitor/src/LogMonitor/EventMonitor.cpp

View workflow job for this annotation

GitHub Actions / cpplint

[cpplint] LogMonitor/src/LogMonitor/EventMonitor.cpp#L690

Redundant blank line at the end of a code block should be deleted. [whitespace/blank_line] [3]
Raw output
LogMonitor/src/LogMonitor/EventMonitor.cpp:690:  Redundant blank line at the end of a code block should be deleted.  [whitespace/blank_line] [3]
}

CancelWaitableTimer(timerEvent);
CloseHandle(timerEvent);

if(elapsedTime < waitInSeconds)

Check warning on line 696 in LogMonitor/src/LogMonitor/EventMonitor.cpp

View workflow job for this annotation

GitHub Actions / cpplint

[cpplint] LogMonitor/src/LogMonitor/EventMonitor.cpp#L696

Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4]
Raw output
LogMonitor/src/LogMonitor/EventMonitor.cpp:696:  Line ends in whitespace.  Consider deleting these extra spaces.  [whitespace/end_of_line] [4]
{
logWriter.TraceError(
Utility::FormatString(
L"Failed to enable event channel. Channel: %ws Error: 0x%X",
eventChannel.Name.c_str(),
status
).c_str()
);
}

Check warning on line 706 in LogMonitor/src/LogMonitor/EventMonitor.cpp

View workflow job for this annotation

GitHub Actions / cpplint

[cpplint] LogMonitor/src/LogMonitor/EventMonitor.cpp#L706

Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4]
Raw output
LogMonitor/src/LogMonitor/EventMonitor.cpp:706:  Line ends in whitespace.  Consider deleting these extra spaces.  [whitespace/end_of_line] [4]

Check warning on line 706 in LogMonitor/src/LogMonitor/EventMonitor.cpp

View workflow job for this annotation

GitHub Actions / cpplint

[cpplint] LogMonitor/src/LogMonitor/EventMonitor.cpp#L706

Redundant blank line at the end of a code block should be deleted. [whitespace/blank_line] [3]
Raw output
LogMonitor/src/LogMonitor/EventMonitor.cpp:706:  Redundant blank line at the end of a code block should be deleted.  [whitespace/blank_line] [3]
}


Check warning on line 709 in LogMonitor/src/LogMonitor/EventMonitor.cpp

View workflow job for this annotation

GitHub Actions / cpplint

[cpplint] LogMonitor/src/LogMonitor/EventMonitor.cpp#L709

Line ends in whitespace. Consider deleting these extra spaces. [whitespace/end_of_line] [4]
Raw output
LogMonitor/src/LogMonitor/EventMonitor.cpp:709:  Line ends in whitespace.  Consider deleting these extra spaces.  [whitespace/end_of_line] [4]

Check warning on line 710 in LogMonitor/src/LogMonitor/EventMonitor.cpp

View workflow job for this annotation

GitHub Actions / cpplint

[cpplint] LogMonitor/src/LogMonitor/EventMonitor.cpp#L710

Redundant blank line at the end of a code block should be deleted. [whitespace/blank_line] [3]
Raw output
LogMonitor/src/LogMonitor/EventMonitor.cpp:710:  Redundant blank line at the end of a code block should be deleted.  [whitespace/blank_line] [3]
}
}


// Converts the time to wait to a large integer
LARGE_INTEGER EventMonitor::_ConvertWaitIntervalToLargeInt(int timeInterval)
{
LARGE_INTEGER liDueTime{};

int millisecondsToWait = timeInterval * 1000;
liDueTime.QuadPart = -millisecondsToWait * 10000LL; // wait time in 100 nanoseconds
return liDueTime;
}

// Returns the time (in seconds) to wait based on the specified waitInSeconds
int EventMonitor::_GetWaitInterval(std::double_t waitInSeconds, int elapsedTime)
{
if (isinf(waitInSeconds)) {
return int(WAIT_INTERVAL);
}

if (waitInSeconds < WAIT_INTERVAL)
{
return int(waitInSeconds);
}

const auto remainingTime = int(waitInSeconds - elapsedTime);
return remainingTime <= WAIT_INTERVAL ? remainingTime : WAIT_INTERVAL;
}

/// Enables or disables an Event Log channel.
///
/// \param ChannelPath Full path to the event log channel.
///
/// \return None
///
void
DWORD
EventMonitor::EnableEventLogChannel(
_In_ LPCWSTR ChannelPath
)
Expand Down Expand Up @@ -700,13 +834,15 @@ EventMonitor::EnableEventLogChannel(

if (ERROR_SUCCESS != status)
{
logWriter.TraceError(
Utility::FormatString(L"Failed to enable event channel %ws: 0x%X", ChannelPath, status).c_str()
logWriter.TraceInfo(
Utility::FormatString(L"Waiting for %ws event channel to be enabled", ChannelPath).c_str()
);
}

if (channelConfig != NULL)
{
EvtClose(channelConfig);
}

return status;
}
12 changes: 11 additions & 1 deletion LogMonitor/src/LogMonitor/EventMonitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
class EventMonitor final
{
public:

Check warning on line 11 in LogMonitor/src/LogMonitor/EventMonitor.h

View workflow job for this annotation

GitHub Actions / cpplint

[cpplint] LogMonitor/src/LogMonitor/EventMonitor.h#L11

Do not leave a blank line after "public:" [whitespace/blank_line] [3]
Raw output
LogMonitor/src/LogMonitor/EventMonitor.h:11:  Do not leave a blank line after "public:"  [whitespace/blank_line] [3]
static const int WAIT_INTERVAL = 15;

EventMonitor() = delete;

EventMonitor(
Expand Down Expand Up @@ -56,7 +59,14 @@ class EventMonitor final
_In_ const HANDLE& EventHandle
);

static LARGE_INTEGER _ConvertWaitIntervalToLargeInt(
int timeInterval);

static int _GetWaitInterval(
std::double_t waitInSeconds,
int elapsedTime);

void EnableEventLogChannels();

static void EnableEventLogChannel(_In_ LPCWSTR ChannelPath);
static DWORD EnableEventLogChannel(_In_ LPCWSTR ChannelPath);
};

0 comments on commit b061bce

Please sign in to comment.