]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.logging/src/org/simantics/logging/DBAndMetadataLogProvider.java
79e10dbef85b675783e8f68c86077e020772f267
[simantics/platform.git] / bundles / org.simantics.logging / src / org / simantics / logging / DBAndMetadataLogProvider.java
1 package org.simantics.logging;
2
3 import java.lang.reflect.Field;
4 import java.net.URL;
5 import java.nio.file.Path;
6 import java.nio.file.Paths;
7 import java.util.ArrayList;
8 import java.util.List;
9
10 import org.eclipse.core.runtime.Platform;
11 import org.osgi.framework.Bundle;
12 import org.slf4j.LoggerFactory;
13
14 public class DBAndMetadataLogProvider implements LogProvider {
15
16     private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(DBAndMetadataLogProvider.class);
17     
18     @Override
19     public List<Path> get() {
20         List<Path> logs = new ArrayList<>();
21         Path dbClientLog = getDBClientLogLocation();
22         if (dbClientLog != null)
23             logs.add(dbClientLog);
24         Path metadataLogLocation = getMetadataLogLocation();
25         if (metadataLogLocation != null)
26             logs.add(metadataLogLocation);
27         return logs;
28     }
29
30     private static Path getDBClientLogLocation() {
31         Bundle bundle = Platform.getBundle("org.simantics.db.common");
32         try {
33             Class<?> forName = bundle.loadClass("org.simantics.db.common.internal.config.InternalClientConfig");
34             Field field = forName.getField("DB_CLIENT_LOG_FILE");
35             String value = (String) field.get(null);
36             return Paths.get(value);
37         } catch (ClassNotFoundException | NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
38             LOGGER.error("Could not read db-client.log location", e);
39         }
40         return null;
41     }
42
43     private static Path getMetadataLogLocation() {
44         String prop = System.getProperty("osgi.instance.area", null);
45         if (prop != null) {
46             try {
47                 URL url = new URL(prop);
48                 if ("file".equals(url .getProtocol())) {
49                     Path path = Paths.get(url.toURI());
50                     return path.resolve(".metadata").resolve(".log");
51                 } else {
52                     LOGGER.warn("Unsupported protocol {}", url);
53                 }
54             } catch (Throwable t) {
55                 LOGGER.error("Could not get .metadata/.log", t);
56             }
57         }
58         return null;
59     }
60 }