Skip to content

Commit

Permalink
Improve logging messages
Browse files Browse the repository at this point in the history
  • Loading branch information
jjlauer committed Oct 19, 2023
1 parent 1f19d7a commit 4275058
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 29 deletions.
50 changes: 25 additions & 25 deletions src/main/java/com/fizzed/jne/PlatformInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ static OperatingSystem doDetectOperatingSystem() {
final OperatingSystem operatingSystem = detectOperatingSystemFromValues(osName);

if (operatingSystem != OperatingSystem.UNKNOWN) {
log.debug("Detected operating system {} in {} ms", operatingSystem, (System.currentTimeMillis() - now));
log.debug("Detected operating system {} (in {} ms)", operatingSystem, (System.currentTimeMillis() - now));
} else {
log.warn("Unable to detect operating system in {} ms", (System.currentTimeMillis() - now));
log.warn("Unable to detect operating system (in {} ms)", (System.currentTimeMillis() - now));
}

return operatingSystem;
Expand Down Expand Up @@ -113,9 +113,9 @@ static HardwareArchitecture doDetectHardwareArchitecture() {
osArch, abiType, bootLibPath, linuxMappedFilesResult);

if (hardwareArchitecture != HardwareArchitecture.UNKNOWN) {
log.debug("Detected operating system {} in {} ms", hardwareArchitecture, (System.currentTimeMillis() - now));
log.debug("Detected hardware architecture {} (in {} ms)", hardwareArchitecture, (System.currentTimeMillis() - now));
} else {
log.warn("Unable to detect operating system in {} ms", (System.currentTimeMillis() - now));
log.warn("Unable to detect hardware architecture (in {} ms)", (System.currentTimeMillis() - now));
}

return hardwareArchitecture;
Expand Down Expand Up @@ -190,23 +190,23 @@ static public LinuxLibC detectLinuxLibC() {
return linuxLibCRef.once(new MemoizedInitializer.Initializer<LinuxLibC>() {
@Override
public LinuxLibC init() {
// step 1: use /proc/self/mapped_files available in newer/some kernels to see what libs are loaded
LinuxDetectedFilesResult detectedFilesResult = detectLinuxMappedFiles();
// step 1: use /proc/self/mapped_files available in newer/some kernels to see what libs are loaded
LinuxDetectedFilesResult detectedFilesResult = detectLinuxMappedFiles();

if (detectedFilesResult.getLibc() != null && detectedFilesResult.getLibc() != LinuxLibC.UNKNOWN) {
return detectedFilesResult.getLibc();
}
if (detectedFilesResult.getLibc() != null && detectedFilesResult.getLibc() != LinuxLibC.UNKNOWN) {
return detectedFilesResult.getLibc();
}

// step 2: search /lib/ directory for MUSL and/or architecture
detectedFilesResult = detectLinuxLibFiles();
// step 2: search /lib/ directory for MUSL and/or architecture
detectedFilesResult = detectLinuxLibFiles();

if (detectedFilesResult.getLibc() != null && detectedFilesResult.getLibc() != LinuxLibC.UNKNOWN) {
return detectedFilesResult.getLibc();
}
if (detectedFilesResult.getLibc() != null && detectedFilesResult.getLibc() != LinuxLibC.UNKNOWN) {
return detectedFilesResult.getLibc();
}

// fallback: we will assume this is GLIBC
log.debug("Will assume we are running on GLIBC");
return LinuxLibC.GLIBC;
// fallback: we will assume this is GLIBC
log.debug("Will assume we are running on GLIBC");
return LinuxLibC.GLIBC;
}
});
}
Expand Down Expand Up @@ -263,21 +263,21 @@ static LinuxDetectedFilesResult doDetectLinuxLibFiles() {
if (name.contains("musl")) {
// only try detecting this once
if (result.getLibc() == null) {
log.debug("Detected MUSL libc via /lib dir in {} ms", (System.currentTimeMillis() - now));
log.debug("Detected libc MUSL via /lib dir strategy (in {} ms)", (System.currentTimeMillis() - now));
result.setLibc(LinuxLibC.MUSL);
}
}

if (name.contains("armhf") || name.contains("arm-linux-gnueabihf")) {
// only try detecting this once
if (result.getArch() != HardwareArchitecture.ARMHF) {
log.debug("Detected ARMHF via /lib dir in {} ms", (System.currentTimeMillis() - now));
log.debug("Detected hardware architecture ARMHF via /lib dir strategy (in {} ms)", (System.currentTimeMillis() - now));
result.setArch(HardwareArchitecture.ARMHF);
}
} else if (name.contains("armel") || name.contains("arm-linux-gnueabi")) {
// only try detecting this once
if (result.getArch() != HardwareArchitecture.ARMEL) {
log.debug("Detected ARMEL via /lib dir in {} ms", (System.currentTimeMillis() - now));
log.debug("Detected hardware architecture ARMEL via /lib dir strategy (in {} ms)", (System.currentTimeMillis() - now));
result.setArch(HardwareArchitecture.ARMEL);
}
}
Expand Down Expand Up @@ -326,7 +326,7 @@ static LinuxDetectedFilesResult doDetectLinuxMappedFiles() {
if (realMapFilePath.contains("musl")) {
// only try detecting this once
if (result.getLibc() == null) {
log.debug("Detected MUSL libc via mapped files in {} ms", (System.currentTimeMillis() - now));
log.debug("Detected libc MUSL via mapped files strategy (in {} ms)", (System.currentTimeMillis() - now));
result.setLibc(LinuxLibC.MUSL);
}
} else if (realMapFilePath.contains("/libc")) {
Expand All @@ -336,13 +336,13 @@ static LinuxDetectedFilesResult doDetectLinuxMappedFiles() {
if (realMapFilePath.contains("armhf") || realMapFilePath.contains("arm-linux-gnueabihf")) {
// only try detecting this once
if (result.getArch() != HardwareArchitecture.ARMHF) {
log.debug("Detected ARMHF via mapped files in {} ms", (System.currentTimeMillis() - now));
log.debug("Detected hardware architecture ARMHF via mapped files strategy (in {} ms)", (System.currentTimeMillis() - now));
result.setArch(HardwareArchitecture.ARMHF);
}
} else if (realMapFilePath.contains("armel") || realMapFilePath.contains("arm-linux-gnueabi")) {
// only try detecting this once
if (result.getArch() != HardwareArchitecture.ARMEL) {
log.debug("Detected ARMEL via mapped files in {} ms", (System.currentTimeMillis() - now));
log.debug("Detected hardware architecture ARMEL via mapped files strategy (in {} ms)", (System.currentTimeMillis() - now));
result.setArch(HardwareArchitecture.ARMEL);
}
}
Expand All @@ -357,12 +357,12 @@ static LinuxDetectedFilesResult doDetectLinuxMappedFiles() {
}

if (possiblyFoundGlibc) {
log.debug("Detected GLIBC libc via mapped files in {} ms", (System.currentTimeMillis() - now));
log.debug("Detected libc GLIBC via mapped files strategy (in {} ms)", (System.currentTimeMillis() - now));
result.setLibc(LinuxLibC.GLIBC);
}

if (result.getLibc() == null) {
log.warn("Unable to detect libc via mapped files in {} ms", (System.currentTimeMillis() - now));
log.warn("Unable to detect libc via mapped files strategy (in {} ms)", (System.currentTimeMillis() - now));
result.setLibc(LinuxLibC.UNKNOWN);
}

Expand Down
6 changes: 2 additions & 4 deletions src/test/java/com/fizzed/jne/PlatformInfoDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,16 @@ static public void main(String[] args) throws Exception {
PlatformInfo.detectOperatingSystem();
OperatingSystem os = PlatformInfo.detectOperatingSystem();

log.info("OS: {}", os);

// do this twice to verify its only done once
PlatformInfo.detectHardwareArchitecture();
HardwareArchitecture arch = PlatformInfo.detectHardwareArchitecture();

log.info("Arch: {}", arch);

// do this twice to verify its only done once
PlatformInfo.detectLinuxLibC();
LinuxLibC libc = PlatformInfo.detectLinuxLibC();

log.info("OS: {}", os);
log.info("Arch: {}", arch);
log.info("LibC: {}", libc);
}

Expand Down

0 comments on commit 4275058

Please sign in to comment.