Skip to content

Commit

Permalink
Merge pull request #21 from gradle/lptr/use-slf4j
Browse files Browse the repository at this point in the history
Switch to SLF4J for logging
  • Loading branch information
lptr authored Oct 28, 2024
2 parents 4904ecb + 0ef1782 commit 417e98b
Show file tree
Hide file tree
Showing 17 changed files with 190 additions and 198 deletions.
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ group = "org.gradle.fileevents"
dependencies {
compileOnly("com.google.code.findbugs:jsr305:3.0.2")
api("net.rubygrapefruit:native-platform:0.22-milestone-26")
implementation("org.slf4j:slf4j-api:1.7.36")
testImplementation(platform("org.junit:junit-bom:5.11.0"))
testImplementation("org.junit.jupiter:junit-jupiter")
testImplementation("ch.qos.logback:logback-classic:1.5.11")
}

java {
Expand Down
8 changes: 4 additions & 4 deletions src/main/cpp/apple_fsnotifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,12 @@ static constexpr FSEventStreamEventFlags IGNORED_FLAGS = kFSEventStreamCreateFla
| kFSEventStreamEventFlagItemCloned;

void Server::handleEvent(JNIEnv* env, const char* path, FSEventStreamEventFlags flags, FSEventStreamEventId eventId) {
logToJava(LogLevel::FINE, "Event flags: 0x%x (ID %d) for '%s'", flags, eventId, path);
logToJava(LogLevel::TRACE_LEVEL, "Event flags: 0x%x (ID %d) for '%s'", flags, eventId, path);

u16string pathStr = utf8ToUtf16String(path);

if ((flags & ~IGNORED_FLAGS) == kFSEventStreamCreateFlagNone) {
logToJava(LogLevel::FINE, "Ignoring event 0x%x (ID %d) for '%s'", flags, eventId, path);
logToJava(LogLevel::TRACE_LEVEL, "Ignoring event 0x%x (ID %d) for '%s'", flags, eventId, path);
return;
}

Expand Down Expand Up @@ -216,7 +216,7 @@ void Server::handleEvent(JNIEnv* env, const char* path, FSEventStreamEventFlags
} else if (IS_SET(flags, kFSEventStreamEventFlagItemCreated)) {
type = ChangeType::CREATED;
} else {
logToJava(LogLevel::WARNING, "Unknown event 0x%x (ID %d) for '%s'", flags, eventId, path);
logToJava(LogLevel::WARN_LEVEL, "Unknown event 0x%x (ID %d) for '%s'", flags, eventId, path);
reportUnknownEvent(env, pathStr);
return;
}
Expand All @@ -241,7 +241,7 @@ bool Server::unregisterPaths(const vector<u16string>& paths) {
bool success = true;
for (auto& path : paths) {
if (watchPoints.erase(path) == 0) {
logToJava(LogLevel::INFO, "Path is not watched: %s", utf16ToUtf8String(path).c_str());
logToJava(LogLevel::INFO_LEVEL, "Path is not watched: %s", utf16ToUtf8String(path).c_str());
success = false;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/cpp/generic_fsnotifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void AbstractServer::reportUnknownEvent(JNIEnv* env, const u16string& path) {
}

void AbstractServer::reportOverflow(JNIEnv* env, const u16string& path) {
logToJava(LogLevel::INFO, "Detected overflow for %s", utf16ToUtf8String(path).c_str());
logToJava(LogLevel::INFO_LEVEL, "Detected overflow for %s", utf16ToUtf8String(path).c_str());
jstring javaPath = env->NewString((jchar*) path.c_str(), (jsize) path.length());
env->CallVoidMethod(watcherCallback.get(), watcherReportOverflowMethod, javaPath);
env->DeleteLocalRef(javaPath);
Expand Down Expand Up @@ -65,7 +65,7 @@ AbstractServer* getServer(JNIEnv* env, jobject javaServer) {
}

jobject rethrowAsJavaException(JNIEnv* env, const exception& e) {
logToJava(LogLevel::SEVERE, "Caught exception: %s", e.what());
logToJava(LogLevel::ERROR_LEVEL, "Caught exception: %s", e.what());
return rethrowAsJavaException(env, e, nativePlatformJniConstants->nativeExceptionClass.get());
}

Expand Down
24 changes: 12 additions & 12 deletions src/main/cpp/linux_fsnotifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ CancelResult WatchPoint::cancel() {
if (inotify_rm_watch(inotify->fd, watchDescriptor) != 0) {
switch (errno) {
case EINVAL:
logToJava(LogLevel::INFO, "Couldn't stop watching %s (probably because the directory was removed)", utf16ToUtf8String(path).c_str());
logToJava(LogLevel::INFO_LEVEL, "Couldn't stop watching %s (probably because the directory was removed)", utf16ToUtf8String(path).c_str());
return CancelResult::NOT_CANCELLED;
break;
default:
Expand Down Expand Up @@ -162,7 +162,7 @@ void Server::handleEvents() {
// Handle events
unique_lock<recursive_mutex> lock(mutationMutex);
JNIEnv* env = getThreadEnv();
logToJava(LogLevel::FINE, "Processing %d bytes worth of events", bytesRead);
logToJava(LogLevel::TRACE_LEVEL, "Processing %d bytes worth of events", bytesRead);
int index = 0;
int count = 0;
while (index < bytesRead) {
Expand All @@ -171,7 +171,7 @@ void Server::handleEvents() {
index += sizeof(struct inotify_event) + event->len;
count++;
}
logToJava(LogLevel::FINE, "Processed %d events", count);
logToJava(LogLevel::TRACE_LEVEL, "Processed %d events", count);
break;
}
available -= bytesRead;
Expand All @@ -183,7 +183,7 @@ void Server::handleEvent(JNIEnv* env, const inotify_event* event) {
const char* eventName = (event->len == 0)
? ""
: event->name;
logToJava(LogLevel::FINE, "Event mask: 0x%x for %s (wd = %d, cookie = 0x%x, len = %d)", mask, eventName, event->wd, event->cookie, event->len);
logToJava(LogLevel::TRACE_LEVEL, "Event mask: 0x%x for %s (wd = %d, cookie = 0x%x, len = %d)", mask, eventName, event->wd, event->cookie, event->len);
if (IS_SET(mask, IN_UNMOUNT)) {
return;
}
Expand All @@ -201,16 +201,16 @@ void Server::handleEvent(JNIEnv* env, const inotify_event* event) {
if (iWatchRoot == watchRoots.end()) {
auto iRecentlyUnregisteredWatchPoint = recentlyUnregisteredWatchRoots.find(event->wd);
if (iRecentlyUnregisteredWatchPoint == recentlyUnregisteredWatchRoots.end()) {
logToJava(LogLevel::INFO, "Received event for unknown watch descriptor %d", event->wd);
logToJava(LogLevel::INFO_LEVEL, "Received event for unknown watch descriptor %d", event->wd);
} else {
// We've removed this via unregisterPath() not long ago
auto& path = iRecentlyUnregisteredWatchPoint->second;
if (IS_SET(mask, IN_IGNORED)) {
logToJava(LogLevel::FINE, "Finished watching recently unregistered watch point '%s' (wd = %d)",
logToJava(LogLevel::TRACE_LEVEL, "Finished watching recently unregistered watch point '%s' (wd = %d)",
utf16ToUtf8String(path).c_str(), event->wd);
recentlyUnregisteredWatchRoots.erase(iRecentlyUnregisteredWatchPoint);
} else {
logToJava(LogLevel::FINE, "Ignoring incoming events for recently removed watch descriptor for '%s' (wd = %d)",
logToJava(LogLevel::TRACE_LEVEL, "Ignoring incoming events for recently removed watch descriptor for '%s' (wd = %d)",
utf16ToUtf8String(path).c_str(), event->wd);
}
}
Expand All @@ -222,21 +222,21 @@ void Server::handleEvent(JNIEnv* env, const inotify_event* event) {

if (IS_SET(mask, IN_IGNORED)) {
// Finished with watch point
logToJava(LogLevel::FINE, "Finished watching still registered '%s' (wd = %d)",
logToJava(LogLevel::TRACE_LEVEL, "Finished watching still registered '%s' (wd = %d)",
utf16ToUtf8String(path).c_str(), event->wd);
watchRoots.erase(event->wd);
watchPoints.erase(path);
return;
}

if (watchPoint.status != WatchPointStatus::LISTENING) {
logToJava(LogLevel::FINE, "Ignoring incoming events for %s as watch-point is not listening (status = %d)",
logToJava(LogLevel::TRACE_LEVEL, "Ignoring incoming events for %s as watch-point is not listening (status = %d)",
utf16ToUtf8String(path).c_str(), watchPoint.status);
return;
}

if (shouldTerminate) {
logToJava(LogLevel::FINE, "Ignoring incoming events for %s because server is terminating (status = %d)",
logToJava(LogLevel::TRACE_LEVEL, "Ignoring incoming events for %s because server is terminating (status = %d)",
utf16ToUtf8String(path).c_str(), watchPoint.status);
return;
}
Expand All @@ -256,7 +256,7 @@ void Server::handleEvent(JNIEnv* env, const inotify_event* event) {
} else if (IS_SET(mask, IN_MODIFY)) {
type = ChangeType::MODIFIED;
} else {
logToJava(LogLevel::WARNING, "Unknown event 0x%x for %s", mask, utf16ToUtf8String(path).c_str());
logToJava(LogLevel::WARN_LEVEL, "Unknown event 0x%x for %s", mask, utf16ToUtf8String(path).c_str());
reportUnknownEvent(env, path);
return;
}
Expand Down Expand Up @@ -312,7 +312,7 @@ void Server::registerPath(const u16string& path) {
bool Server::unregisterPath(const u16string& path) {
auto it = watchPoints.find(path);
if (it == watchPoints.end()) {
logToJava(LogLevel::INFO, "Path is not watched: %s", utf16ToUtf8String(path).c_str());
logToJava(LogLevel::INFO_LEVEL, "Path is not watched: %s", utf16ToUtf8String(path).c_str());
return false;
}
auto& watchPoint = it->second;
Expand Down
36 changes: 18 additions & 18 deletions src/main/cpp/win_fsnotifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ bool resolveFinalPath(HANDLE handle, wstring& path) {
PATH_BUFFER_SIZE,
FILE_NAME_OPENED);
if (pathLength == 0 || pathLength > PATH_BUFFER_SIZE) {
logToJava(LogLevel::WARNING, "Couldn't get final path for handle 0x%x, error code: %d", handle, GetLastError());
logToJava(LogLevel::WARN_LEVEL, "Couldn't get final path for handle 0x%x, error code: %d", handle, GetLastError());
return false;
}
path.clear();
Expand Down Expand Up @@ -126,7 +126,7 @@ WatchPoint::WatchPoint(Server* server, size_t eventBufferSize, const wstring& pa

bool WatchPoint::cancel() {
if (status == WatchPointStatus::LISTENING) {
logToJava(LogLevel::FINE, "Cancelling %s", wideToUtf8String(registeredPath).c_str());
logToJava(LogLevel::TRACE_LEVEL, "Cancelling %s", wideToUtf8String(registeredPath).c_str());
bool cancelled = (bool) CancelIoEx(directoryHandle, &overlapped);
if (cancelled) {
status = WatchPointStatus::CANCELLED;
Expand All @@ -135,7 +135,7 @@ bool WatchPoint::cancel() {
close();
if (cancelError == ERROR_NOT_FOUND) {
// Do nothing, looks like this is a typical scenario
logToJava(LogLevel::FINE, "Watch point already finished %s", wideToUtf8String(registeredPath).c_str());
logToJava(LogLevel::TRACE_LEVEL, "Watch point already finished %s", wideToUtf8String(registeredPath).c_str());
} else {
throw FileWatcherException("Couldn't cancel watch point", wideToUtf16String(registeredPath), cancelError);
}
Expand All @@ -151,7 +151,7 @@ WatchPoint::~WatchPoint() {
SleepEx(0, true);
close();
} catch (const exception& ex) {
logToJava(LogLevel::WARNING, "Couldn't cancel watch point %s: %s", wideToUtf8String(registeredPath).c_str(), ex.what());
logToJava(LogLevel::WARN_LEVEL, "Couldn't cancel watch point %s: %s", wideToUtf8String(registeredPath).c_str(), ex.what());
}
}

Expand Down Expand Up @@ -197,26 +197,26 @@ void WatchPoint::close() {
try {
BOOL ret = CloseHandle(directoryHandle);
if (!ret) {
logToJava(LogLevel::SEVERE, "Couldn't close handle %p for '%ls': %d", directoryHandle, wideToUtf8String(registeredPath).c_str(), GetLastError());
logToJava(LogLevel::ERROR_LEVEL, "Couldn't close handle %p for '%ls': %d", directoryHandle, wideToUtf8String(registeredPath).c_str(), GetLastError());
}
} catch (const exception& ex) {
// Apparently with debugging enabled CloseHandle() can also throw, see:
// https://docs.microsoft.com/en-us/windows/win32/api/handleapi/nf-handleapi-closehandle#return-value
logToJava(LogLevel::SEVERE, "Couldn't close handle %p for '%ls': %s", directoryHandle, wideToUtf8String(registeredPath).c_str(), ex.what());
logToJava(LogLevel::ERROR_LEVEL, "Couldn't close handle %p for '%ls': %s", directoryHandle, wideToUtf8String(registeredPath).c_str(), ex.what());
}
status = WatchPointStatus::FINISHED;
}
}

void WatchPoint::handleEventsInBuffer(DWORD errorCode, DWORD bytesTransferred) {
if (errorCode == ERROR_OPERATION_ABORTED) {
logToJava(LogLevel::FINE, "Finished watching '%s', status = %d", wideToUtf8String(registeredPath).c_str(), status);
logToJava(LogLevel::TRACE_LEVEL, "Finished watching '%s', status = %d", wideToUtf8String(registeredPath).c_str(), status);
close();
return;
}

if (status != WatchPointStatus::LISTENING) {
logToJava(LogLevel::FINE, "Ignoring incoming events for %s as watch-point is not listening (%d bytes, errorCode = %d, status = %d)",
logToJava(LogLevel::TRACE_LEVEL, "Ignoring incoming events for %s as watch-point is not listening (%d bytes, errorCode = %d, status = %d)",
wideToUtf8String(registeredPath).c_str(), bytesTransferred, errorCode, status);
return;
}
Expand Down Expand Up @@ -251,7 +251,7 @@ void Server::handleEvents(WatchPoint* watchPoint, DWORD errorCode, const vector<

const wstring& path = watchPoint->registeredPath;
if (shouldTerminate) {
logToJava(LogLevel::FINE, "Ignoring incoming events for %s because server is terminating (%d bytes, status = %d)",
logToJava(LogLevel::TRACE_LEVEL, "Ignoring incoming events for %s because server is terminating (%d bytes, status = %d)",
wideToUtf8String(path).c_str(), bytesTransferred, watchPoint->status);
return;
}
Expand Down Expand Up @@ -284,7 +284,7 @@ void Server::handleEvents(WatchPoint* watchPoint, DWORD errorCode, const vector<
case ListenResult::SUCCESS:
break;
case ListenResult::DELETED:
logToJava(LogLevel::FINE, "Watched directory removed for %s", wideToUtf8String(path).c_str());
logToJava(LogLevel::TRACE_LEVEL, "Watched directory removed for %s", wideToUtf8String(path).c_str());
reportChangeEvent(env, ChangeType::REMOVED, wideToUtf16String(path));
break;
}
Expand All @@ -300,7 +300,7 @@ void Server::handleEvent(JNIEnv* env, const wstring& watchedPathW, FILE_NOTIFY_E
}
changedPathW.insert(0, watchedPathW);

logToJava(LogLevel::FINE, "Change detected: 0x%x '%s'", info->Action, wideToUtf8String(changedPathW).c_str());
logToJava(LogLevel::TRACE_LEVEL, "Change detected: 0x%x '%s'", info->Action, wideToUtf8String(changedPathW).c_str());

ChangeType type;
if (info->Action == FILE_ACTION_ADDED || info->Action == FILE_ACTION_RENAMED_NEW_NAME) {
Expand All @@ -310,12 +310,12 @@ void Server::handleEvent(JNIEnv* env, const wstring& watchedPathW, FILE_NOTIFY_E
} else if (info->Action == FILE_ACTION_MODIFIED) {
if (info->FileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
// Ignore MODIFIED events on directories
logToJava(LogLevel::FINE, "Ignored MODIFIED event on directory", nullptr);
logToJava(LogLevel::TRACE_LEVEL, "Ignored MODIFIED event on directory", nullptr);
return;
}
type = ChangeType::MODIFIED;
} else {
logToJava(LogLevel::WARNING, "Unknown event 0x%x for %s", info->Action, wideToUtf8String(changedPathW).c_str());
logToJava(LogLevel::WARN_LEVEL, "Unknown event 0x%x for %s", info->Action, wideToUtf8String(changedPathW).c_str());
reportUnknownEvent(env, wideToUtf16String(changedPathW));
return;
}
Expand Down Expand Up @@ -362,19 +362,19 @@ void Server::runLoop() {
}

// We have received termination, cancel all watchers
logToJava(LogLevel::FINE, "Finished with run loop, now cancelling remaining watch points", NULL);
logToJava(LogLevel::TRACE_LEVEL, "Finished with run loop, now cancelling remaining watch points", NULL);
for (auto& it : watchPoints) {
auto& watchPoint = it.second;
if (watchPoint.status == WatchPointStatus::LISTENING) {
try {
watchPoint.cancel();
} catch (const exception& ex) {
logToJava(LogLevel::SEVERE, "%s", ex.what());
logToJava(LogLevel::ERROR_LEVEL, "%s", ex.what());
}
}
}

logToJava(LogLevel::FINE, "Waiting for any pending watch points to abort completely", NULL);
logToJava(LogLevel::TRACE_LEVEL, "Waiting for any pending watch points to abort completely", NULL);
SleepEx(0, true);

// Warn about any unfinished watchpoints
Expand All @@ -385,7 +385,7 @@ void Server::runLoop() {
case WatchPointStatus::FINISHED:
break;
default:
logToJava(LogLevel::WARNING, "Watch point %s did not finish before termination timeout (status = %d)",
logToJava(LogLevel::WARN_LEVEL, "Watch point %s did not finish before termination timeout (status = %d)",
wideToUtf8String(watchPoint.registeredPath).c_str(), watchPoint.status);
break;
}
Expand Down Expand Up @@ -446,7 +446,7 @@ void Server::registerPath(const u16string& path) {
bool Server::unregisterPath(const u16string& path) {
wstring registeredPath(path.begin(), path.end());
if (watchPoints.erase(registeredPath) == 0) {
logToJava(LogLevel::INFO, "Path is not watched: %s", wideToUtf8String(registeredPath).c_str());
logToJava(LogLevel::INFO_LEVEL, "Path is not watched: %s", wideToUtf8String(registeredPath).c_str());
return false;
}
return true;
Expand Down
14 changes: 5 additions & 9 deletions src/main/headers/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,11 @@
#define LOG_LEVEL_CHECK_INTERVAL_IN_MS 1000

enum class LogLevel : int {
ALL,
FINEST,
FINER,
FINE,
CONFIG,
INFO,
WARNING,
SEVERE,
OFF
TRACE_LEVEL,
DEBUG_LEVEL,
INFO_LEVEL,
WARN_LEVEL,
ERROR_LEVEL,
};

class Logging : public JniSupport {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ private void signalOverflow(OverflowType type, @Nullable String path) {
private void forceQueueEvent(FileWatchEvent event) {
boolean eventPublished = eventQueue.offer(event);
if (!eventPublished) {
NativeLogger.LOGGER.severe("Couldn't queue event: " + event);
NativeLogger.LOGGER.error("Couldn't queue event: " + event);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static String getVersion() {
private static native String getVersion0();

/**
* Forces the native backend to drop the cached JUL log level and thus
* Forces the native backend to drop the cached SLF4J log level and thus
* re-query it the next time it tries to log something to the Java side.
*/
public void invalidateLogLevelCache() {
Expand Down
Loading

0 comments on commit 417e98b

Please sign in to comment.