log exceptions

Signed-off-by: pengzhile <pengzhile@gmail.com>
This commit is contained in:
pengzhile 2021-12-28 15:02:34 +08:00
parent 7ba3c94e53
commit 14978ba176
7 changed files with 27 additions and 13 deletions

View File

@ -1,4 +1,4 @@
# ja-netfilter v2.0.0
# ja-netfilter v2.0.1
### A javaagent framework

View File

@ -6,7 +6,7 @@
<groupId>com.ja-netfilter</groupId>
<artifactId>ja-netfilter</artifactId>
<version>2.0.0</version>
<version>2.0.1</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

View File

@ -60,7 +60,7 @@ public final class Dispatcher implements ClassFileTransformer {
classFileBuffer = transformer.transform(className, classFileBuffer, order++);
}
} catch (Throwable e) {
DebugInfo.output("Transform class failed: " + e.getMessage());
DebugInfo.output("Transform class failed: " + className, e);
}
} while (false);

View File

@ -23,7 +23,7 @@ public class Initializer {
try {
inst.retransformClasses(c);
} catch (Throwable e) {
DebugInfo.output("Retransform class failed: " + name);
DebugInfo.output("Retransform class failed: " + name, e);
}
}
}

View File

@ -9,7 +9,7 @@ import java.net.URL;
import java.util.jar.JarFile;
public class Launcher {
private static final String VERSION = "v2.0.0";
private static final String VERSION = "v2.0.1";
public static void main(String[] args) {
printUsage();
@ -22,7 +22,7 @@ public class Launcher {
try {
jarURI = getJarURI();
} catch (Throwable e) {
DebugInfo.output("ERROR: Can not locate ja-netfilter jar file.");
DebugInfo.output("ERROR: Can not locate ja-netfilter jar file.", e);
return;
}
@ -30,7 +30,7 @@ public class Launcher {
try {
inst.appendToBootstrapClassLoaderSearch(new JarFile(agentFile));
} catch (Throwable e) {
DebugInfo.output("ERROR: Can not access ja-netfilter jar file.");
DebugInfo.output("ERROR: Can not access ja-netfilter jar file.", e);
return;
}

View File

@ -5,7 +5,11 @@ import com.janetfilter.core.utils.DateUtils;
public class DebugInfo {
private static final boolean DEBUG = "1".equals(System.getenv("JANF_DEBUG")) || "1".equals(System.getProperty("janf.debug"));
public static void output(String content) { // No logger lib required
public static void output(String content) {
output(content, null);
}
public static void output(String content, Throwable e) { // No logger lib required
if (!DEBUG) {
return;
}
@ -15,7 +19,15 @@ public class DebugInfo {
StackTraceElement[] traces = new Throwable().getStackTrace();
String caller = traces.length < 2 ? "UNKNOWN" : traces[1].toString();
System.out.printf(template, DateUtils.formatNow(), caller, content);
System.out.flush();
String outContent = String.format(template, DateUtils.formatNow(), caller, content);
if (null == e) {
System.out.print(outContent);
return;
}
synchronized (DebugInfo.class) {
System.out.print(outContent);
e.printStackTrace(System.err);
}
}
}

View File

@ -29,6 +29,8 @@ public final class PluginManager {
}
public void loadPlugins() {
long startTime = System.currentTimeMillis();
File pluginsDirectory = environment.getPluginsDir();
if (!pluginsDirectory.exists() || !pluginsDirectory.isDirectory()) {
return;
@ -50,9 +52,9 @@ public final class PluginManager {
throw new RuntimeException("Load plugin timeout");
}
DebugInfo.output("============ All plugins loaded ============");
DebugInfo.output(String.format("============ All plugins loaded, %.2fs elapsed ============", (System.currentTimeMillis() - startTime) / 1000D));
} catch (Throwable e) {
DebugInfo.output("Load plugin failed: " + e.getMessage());
DebugInfo.output("Load plugin failed", e);
}
}
@ -93,7 +95,7 @@ public final class PluginManager {
DebugInfo.output("Plugin loaded: {name=" + pluginEntry.getName() + ", version=" + pluginEntry.getVersion() + ", author=" + pluginEntry.getAuthor() + "}");
} catch (Throwable e) {
DebugInfo.output("Parse plugin info failed: " + e.getMessage());
DebugInfo.output("Parse plugin info failed", e);
}
}
}