Skip to content

Commit

Permalink
Cleanup debug logging
Browse files Browse the repository at this point in the history
  • Loading branch information
cb1kenobi committed Jul 14, 2024
1 parent 14f99a0 commit 8fe94ea
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 44 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
"prebuild-arm64": "prebuildify --napi --strip --platform=win32 --arch arm64",
"prebuild-ia32": "prebuildify --napi --strip --platform=win32 --arch ia32",
"prebuild-x64": "prebuildify --napi --strip --platform=win32 --arch x64",
"rebuild:local": "node-gyp -j 20 rebuild",
"rebuild:local-debug": "node-gyp -j 20 rebuild --debug",
"rebuild:local": "node-gyp rebuild",
"rebuild:local-debug": "node-gyp rebuild --debug",
"test": "vitest --allowOnly --pool=forks",
"type-check": "tsc --noEmit"
},
Expand Down
46 changes: 6 additions & 40 deletions src/watchman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,14 @@
using namespace winreglib;

static void execute(napi_env env, void* data) {
LOG_DEBUG("execute", L"Calling run()");
((Watchman*)data)->run();
LOG_DEBUG("execute", L"run() returned!");
}

static void complete(napi_env env, napi_status status, void* data) {
if (status != napi_ok) {
const napi_extended_error_info* error;
::napi_get_last_error_info(env, &error);
char msg[1024];
::snprintf(msg, 1024, "Watchman::complete: Worker thread failed (status=%d) %s", status, error->error_message);
LOG_DEBUG_2("Watchman::complete", L"Watchman::complete: Worker thread failed (status=%d) %s", status, error->error_message)
} else {
LOG_DEBUG_1("Watchman::complete", L"Worker thread exited (status=%d)", status)
}
Expand Down Expand Up @@ -65,13 +62,8 @@ Watchman::Watchman(napi_env env) : env(env) {
notifyChange = new uv_async_t;
notifyChange->data = (void*)this;
::uv_async_init(loop, notifyChange, [](uv_async_t* handle) {
LOG_DEBUG("Watchman::Watchman", L"uv_async_t callback")
if (handle && handle->data) {
LOG_DEBUG("Watchman::Watchman", L"Calling dispatch()")
((Watchman*)handle->data)->dispatch();
LOG_DEBUG("Watchman::Watchman", L"dispatch() success!")
} else {
LOG_DEBUG("Watchman::Watchman", L"No data in handle! Watchman is no more!")
}
});
::uv_unref((uv_handle_t*)notifyChange);
Expand All @@ -81,17 +73,15 @@ Watchman::Watchman(napi_env env) : env(env) {
* Stops the background thread and closes all handles.
*/
Watchman::~Watchman() {
LOG_DEBUG("Watchman::~Watchman", L"Stopping background thread")
::SetEvent(term);
::napi_delete_async_work(env, asyncWork);
::CloseHandle(term);
::CloseHandle(refresh);

::uv_close(reinterpret_cast<uv_handle_t*>(notifyChange), [](uv_handle_t* handle) {
uv_async_t* async = reinterpret_cast<uv_async_t*>(handle);
delete async;
});

::SetEvent(term);
::napi_delete_async_work(env, asyncWork);
::CloseHandle(term);
::CloseHandle(refresh);
}

/**
Expand Down Expand Up @@ -168,25 +158,12 @@ void Watchman::config(const std::wstring& key, napi_value listener, WatchAction
}
}

// if (beforeCount == 0 && afterCount > 0) {
// LOG_DEBUG_THREAD_ID("Watchman::config", L"Starting background thread")
// NAPI_THROW("Watchman::config", "ERR_NAPI_QUEUE_ASYNC_WORK", ::napi_queue_async_work(env, asyncWork))
// } else if (beforeCount > 0 && afterCount == 0) {
// LOG_DEBUG_THREAD_ID("Watchman::config", L"Signaling term event")
// ::SetEvent(term);
// } else if (beforeCount != afterCount) {
// LOG_DEBUG_THREAD_ID("Watchman::config", L"Signaling refresh event")
// ::SetEvent(refresh);
// }

if ((beforeCount > 0 || afterCount > 0) && beforeCount != afterCount) {
if (afterCount > 0 && beforeCount != afterCount) {
LOG_DEBUG_THREAD_ID("Watchman::config", L"Signaling refresh event")
::SetEvent(refresh);
}

LOG_DEBUG("Watchman::config", L"Printing tree")
printTree();
LOG_DEBUG("Watchman::config", L"Done printing tree")
}

/**
Expand All @@ -203,20 +180,15 @@ void Watchman::dispatch() {
// check if there are any changed nodes left...
// the first time we loop, we know there's at least one
{
LOG_DEBUG("Watchman::dispatch", L"Obtaining lock for changed nodes...")
std::lock_guard<std::mutex> lock(changedNodesLock);

if (changedNodes.empty()) {
LOG_DEBUG("Watchman::dispatch", L"No changed nodes to dispatch, returning")
return;
}

remaining = changedNodes.size();
LOG_DEBUG_1("Watchman::dispatch", L"Found %ld changed nodes", remaining)
node = changedNodes.front();
LOG_DEBUG("Watchman::dispatch", L"Popping changed node...")
changedNodes.pop_front();
LOG_DEBUG("Watchman::dispatch", L"Popped changed node!")
}

LOG_DEBUG_2("Watchman::dispatch", L"Dispatching change event for \"%ls\" (%d remaining)", node->name.c_str(), --remaining)
Expand Down Expand Up @@ -260,9 +232,7 @@ void Watchman::run() {

while (1) {
if (handles != NULL) {
LOG_DEBUG("Watchman::run", L"Deleting handles 1")
delete[] handles;
LOG_DEBUG("Watchman::run", L"Deleted handles 1")
}

// WaitForMultipleObjects() wants an array of handles, so we must construct one
Expand Down Expand Up @@ -322,16 +292,12 @@ void Watchman::run() {
}
}

LOG_DEBUG("Watchman::run", L"Sending notification to main thread")
::uv_async_send(notifyChange);
LOG_DEBUG("Watchman::run", L"Done sending notification to main thread")
}
}
}
}
}

LOG_DEBUG("Watchman::run", L"Deleting handles 2")
delete[] handles;
LOG_DEBUG("Watchman::run", L"Deleted handles 2")
}
2 changes: 0 additions & 2 deletions src/winreglib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -382,8 +382,6 @@ NAPI_METHOD(unwatch) {
* Destroys the Watchman instance, log ref handle, and notify handle.
*/
static void cleanup(napi_async_cleanup_hook_handle handle, void* arg) {
LOG_DEBUG("cleanup", L"CLEANING UP!")

if (winreglib::watchman) {
delete winreglib::watchman;
}
Expand Down

0 comments on commit 8fe94ea

Please sign in to comment.