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