Skip to content

Commit

Permalink
Mwasplund/log init (#54)
Browse files Browse the repository at this point in the history
* Update file checks

* Update version

* Log initialize
  • Loading branch information
mwasplund authored Aug 3, 2020
1 parent 545fb04 commit ad5aaa3
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 36 deletions.
8 changes: 7 additions & 1 deletion Source/Build/Execute/SystemAccessTracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,19 @@ namespace Soup::Build
return m_output;
}

void OnInitialize() override final
{
Log::Diag("SystemAccessTracker::OnInitialize");
}

void OnShutdown() override final
{
Log::Diag("SystemAccessTracker::OnShutdown");
}

void OnError(std::string_view message) override final
{
Log::Error("SystemAccessTracker Error: " + std::string(message));
Log::Error("SystemAccessTracker::Error - " + std::string(message));
}

// FileApi
Expand Down
2 changes: 1 addition & 1 deletion Source/Client/CLI/Commands/VersionCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace Soup::Client

// TODO var version = Assembly.GetExecutingAssembly().GetName().Version;
// Log::Message($"{version.Major}.{version.Minor}.{version.Build}");
Log::HighPriority("0.8.1");
Log::HighPriority("0.8.2");
}

private:
Expand Down
2 changes: 1 addition & 1 deletion Source/Client/CLI/Recipe.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Name = "Soup"
Version = "0.8.0"
Version = "0.8.2"
Type = "Executable"

# Ensure the core build extensions are runtime dependencies
Expand Down
77 changes: 44 additions & 33 deletions Source/Monitor/Detours/EventLogger.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,13 @@ class EventLogger
public:
static void Initialize()
{
std::stringstream pipeNameBuilder;
pipeNameBuilder << TBLOG_PIPE_NAMEA << "." << s_nTraceProcessId;
auto pipeName = pipeNameBuilder.str();

auto lock = std::lock_guard<std::mutex>(s_pipeMutex);
for (int retries = 0; retries < 10; retries++)
{
// Wait up to 1 seconds for a pipe to appear.
auto timoutMilliseconds = 1000;
if (WaitNamedPipeA(pipeName.c_str(), timoutMilliseconds) != 0)
{
// Attempt to open the pipe
s_pipeHandle = Functions::FileApi::Cache::CreateFileA(
pipeName.c_str(),
GENERIC_WRITE,
0,
nullptr,
OPEN_EXISTING,
0,
nullptr);
if (s_pipeHandle != INVALID_HANDLE_VALUE)
{
DWORD pipeMode = PIPE_READMODE_MESSAGE;
if (SetNamedPipeHandleState(s_pipeHandle, &pipeMode, nullptr, nullptr))
{
// All good!
return;
}
}
}
}
Connect();

// Couldn't open pipe.
throw std::runtime_error("Failed to open pipe for event logger.");
// Notify that we are connected
Monitor::DetourMessage message;
message.Type = Monitor::DetourMessageType::Info_Initialize;
message.ContentSize = 0;
WriteMessage(message);
}

static void Shutdown()
Expand Down Expand Up @@ -163,6 +136,44 @@ class EventLogger
}

private:
static void Connect()
{
std::stringstream pipeNameBuilder;
pipeNameBuilder << TBLOG_PIPE_NAMEA << "." << s_nTraceProcessId;
auto pipeName = pipeNameBuilder.str();

auto lock = std::lock_guard<std::mutex>(s_pipeMutex);
for (int retries = 0; retries < 10; retries++)
{
// Wait up to 1 seconds for a pipe to appear.
auto timoutMilliseconds = 1000;
if (WaitNamedPipeA(pipeName.c_str(), timoutMilliseconds) != 0)
{
// Attempt to open the pipe
s_pipeHandle = Functions::FileApi::Cache::CreateFileA(
pipeName.c_str(),
GENERIC_WRITE,
0,
nullptr,
OPEN_EXISTING,
0,
nullptr);
if (s_pipeHandle != INVALID_HANDLE_VALUE)
{
DWORD pipeMode = PIPE_READMODE_MESSAGE;
if (SetNamedPipeHandleState(s_pipeHandle, &pipeMode, nullptr, nullptr))
{
// All good!
return;
}
}
}
}

// Couldn't open pipe.
throw std::runtime_error("Failed to open pipe for event logger.");
}

static void UnsafeWriteMessage(const Monitor::DetourMessage& message)
{
if (s_pipeHandle == INVALID_HANDLE_VALUE)
Expand Down
5 changes: 5 additions & 0 deletions Source/Monitor/Shared/EventListener.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ namespace Monitor
switch (message.Type)
{
// Info
case DetourMessageType::Info_Initialize:
{
m_callback->OnInitialize();
break;
}
case DetourMessageType::Info_Shutdown:
{
m_callback->OnShutdown();
Expand Down
1 change: 1 addition & 0 deletions Source/Monitor/Shared/IDetourCallback.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace Monitor
export class IDetourCallback
{
public:
virtual void OnInitialize() = 0;
virtual void OnShutdown() = 0;
virtual void OnError(std::string_view message) = 0;

Expand Down
1 change: 1 addition & 0 deletions Source/Monitor/Shared/Module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ namespace Monitor
{
export enum class DetourMessageType : uint32_t
{
Info_Initialize,
Info_Shutdown,
Info_Error,

Expand Down

0 comments on commit ad5aaa3

Please sign in to comment.