]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.logging/src/org/simantics/logging/DBAndMetadataLogProvider.java
Logging configuration via SCL and UI & saving to ZIP-archive
[simantics/platform.git] / bundles / org.simantics.logging / src / org / simantics / logging / DBAndMetadataLogProvider.java
diff --git a/bundles/org.simantics.logging/src/org/simantics/logging/DBAndMetadataLogProvider.java b/bundles/org.simantics.logging/src/org/simantics/logging/DBAndMetadataLogProvider.java
new file mode 100644 (file)
index 0000000..79e10db
--- /dev/null
@@ -0,0 +1,60 @@
+package org.simantics.logging;
+
+import java.lang.reflect.Field;
+import java.net.URL;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.eclipse.core.runtime.Platform;
+import org.osgi.framework.Bundle;
+import org.slf4j.LoggerFactory;
+
+public class DBAndMetadataLogProvider implements LogProvider {
+
+    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(DBAndMetadataLogProvider.class);
+    
+    @Override
+    public List<Path> get() {
+        List<Path> logs = new ArrayList<>();
+        Path dbClientLog = getDBClientLogLocation();
+        if (dbClientLog != null)
+            logs.add(dbClientLog);
+        Path metadataLogLocation = getMetadataLogLocation();
+        if (metadataLogLocation != null)
+            logs.add(metadataLogLocation);
+        return logs;
+    }
+
+    private static Path getDBClientLogLocation() {
+        Bundle bundle = Platform.getBundle("org.simantics.db.common");
+        try {
+            Class<?> forName = bundle.loadClass("org.simantics.db.common.internal.config.InternalClientConfig");
+            Field field = forName.getField("DB_CLIENT_LOG_FILE");
+            String value = (String) field.get(null);
+            return Paths.get(value);
+        } catch (ClassNotFoundException | NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
+            LOGGER.error("Could not read db-client.log location", e);
+        }
+        return null;
+    }
+
+    private static Path getMetadataLogLocation() {
+        String prop = System.getProperty("osgi.instance.area", null);
+        if (prop != null) {
+            try {
+                URL url = new URL(prop);
+                if ("file".equals(url .getProtocol())) {
+                    Path path = Paths.get(url.toURI());
+                    return path.resolve(".metadata").resolve(".log");
+                } else {
+                    LOGGER.warn("Unsupported protocol {}", url);
+                }
+            } catch (Throwable t) {
+                LOGGER.error("Could not get .metadata/.log", t);
+            }
+        }
+        return null;
+    }
+}