Skip to content

Commit 15309f5

Browse files
committed
usage logging
1 parent fcd1016 commit 15309f5

File tree

8 files changed

+256
-13
lines changed

8 files changed

+256
-13
lines changed

bundles/org.codechecker.eclipse.plugin/build.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ bin.includes = plugin.xml,\
55
.,\
66
icons/,\
77
contexts.xml,\
8-
log4j.properties
8+
log4j.properties,\
9+
resources/
910

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
4+
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>org.codechecker.eclipse</groupId>
8+
<artifactId>org.codechecker.eclipse.bundles</artifactId>
9+
<version>1.0.0-SNAPSHOT</version>
10+
</parent>
11+
12+
<artifactId>org.codechecker.eclipse.plugin</artifactId>
13+
<version>0.0.6-SNAPSHOT</version>
14+
<packaging>eclipse-plugin</packaging>
15+
16+
<properties>
17+
<host.log>empty</host.log>
18+
<port.log>0</port.log>
19+
<plugin.version>${project.version}</plugin.version>
20+
</properties>
21+
22+
<build>
23+
<resources>
24+
<resource>
25+
<directory>resources</directory>
26+
<targetPath>resources</targetPath>
27+
<filtering>true</filtering>
28+
<includes>
29+
<include>**/*.properties</include>
30+
</includes>
31+
</resource>
32+
</resources>
33+
</build>
34+
35+
</project>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
host=${host.log}
2+
port=${port.log}
3+
version=${plugin.version}

bundles/org.codechecker.eclipse.plugin/src/org/codechecker/eclipse/plugin/codechecker/CodeChecker.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
import org.codechecker.eclipse.plugin.runtime.LogI;
1313
import org.codechecker.eclipse.plugin.runtime.SLogger;
1414
import org.codechecker.eclipse.plugin.runtime.ShellExecutorHelper;
15+
import org.codechecker.eclipse.plugin.usage.StatisticUploader;
16+
import org.codechecker.eclipse.plugin.usage.UsageInfo;
17+
import org.codechecker.eclipse.plugin.usage.UsageInfo.CommandType;
1518
import org.eclipse.core.runtime.IProgressMonitor;
1619
import org.eclipse.jdt.annotation.NonNull;
1720

@@ -87,6 +90,8 @@ public String analyze(Path logFile, boolean logToConsole, IProgressMonitor monit
8790
+ RESULTS_SUB + " " + LOGFILE_SUB;
8891

8992
SLogger.log(LogI.INFO, "Running analyze Command: " + cmd);
93+
new Thread(new StatisticUploader(new UsageInfo(CommandType.analyze_started, null))).run();
94+
9095
Optional<String> ccOutput = she.progressableWaitReturnOutput(cmd, subMap, logToConsole, monitor, taskCount);
9196

9297
return ccOutput.or("");

bundles/org.codechecker.eclipse.plugin/src/org/codechecker/eclipse/plugin/init/StartupJob.java

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,19 @@
33
import java.util.HashSet;
44
import java.util.Set;
55

6+
import org.codechecker.eclipse.plugin.CodeCheckerNature;
7+
import org.codechecker.eclipse.plugin.ExternalLogger;
8+
import org.codechecker.eclipse.plugin.Logger;
9+
import org.codechecker.eclipse.plugin.config.CodeCheckerContext;
10+
import org.codechecker.eclipse.plugin.config.global.CcGlobalConfiguration;
11+
import org.codechecker.eclipse.plugin.config.project.CodeCheckerProject;
12+
import org.codechecker.eclipse.plugin.report.job.AnalyzeJob;
13+
import org.codechecker.eclipse.plugin.report.job.JobDoneChangeListener;
14+
import org.codechecker.eclipse.plugin.report.job.PlistParseJob;
15+
import org.codechecker.eclipse.plugin.runtime.SLogger;
16+
import org.codechecker.eclipse.plugin.usage.StatisticUploader;
17+
import org.codechecker.eclipse.plugin.usage.UsageInfo;
18+
import org.codechecker.eclipse.plugin.usage.UsageInfo.CommandType;
619
import org.eclipse.cdt.core.model.CoreModel;
720
import org.eclipse.core.resources.IProject;
821
import org.eclipse.core.resources.IResource;
@@ -24,18 +37,6 @@
2437
import org.eclipse.ui.IWorkbenchWindow;
2538
import org.eclipse.ui.PlatformUI;
2639

27-
import org.codechecker.eclipse.plugin.CodeCheckerNature;
28-
import org.codechecker.eclipse.plugin.ExternalLogger;
29-
import org.codechecker.eclipse.plugin.Logger;
30-
import org.codechecker.eclipse.plugin.config.CcConfiguration;
31-
import org.codechecker.eclipse.plugin.config.CodeCheckerContext;
32-
import org.codechecker.eclipse.plugin.config.global.CcGlobalConfiguration;
33-
import org.codechecker.eclipse.plugin.config.project.CodeCheckerProject;
34-
import org.codechecker.eclipse.plugin.report.job.AnalyzeJob;
35-
import org.codechecker.eclipse.plugin.report.job.JobDoneChangeListener;
36-
import org.codechecker.eclipse.plugin.report.job.PlistParseJob;
37-
import org.codechecker.eclipse.plugin.runtime.SLogger;
38-
3940
/**
4041
* This eclipse job is responsible for registering the Build listener.
4142
*
@@ -83,6 +84,8 @@ public IStatus runInUIThread(IProgressMonitor monitor) {
8384
Logger.log(IStatus.INFO, Logger.getStackTrace(e));
8485
}
8586

87+
new Thread(new StatisticUploader(new UsageInfo(CommandType.started, null))).run();
88+
8689
Logger.log(IStatus.INFO, "adding addResourceChangeListener ");
8790
ResourcesPlugin.getWorkspace().addResourceChangeListener(new ResourceChangeListener(),
8891
IResourceChangeEvent.POST_BUILD | IResourceChangeEvent.POST_CHANGE |
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package org.codechecker.eclipse.plugin.usage;
2+
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
import java.net.DatagramPacket;
6+
import java.net.DatagramSocket;
7+
import java.net.Inet4Address;
8+
import java.util.Properties;
9+
10+
import org.eclipse.core.runtime.FileLocator;
11+
import org.eclipse.core.runtime.Path;
12+
import org.osgi.framework.FrameworkUtil;
13+
14+
import com.google.gson.Gson;
15+
16+
/**
17+
* Class for uploading the usage statistics.
18+
*/
19+
public class StatisticUploader implements Runnable {
20+
21+
private Integer port;
22+
private String host;
23+
private UsageInfo info;
24+
25+
/**
26+
* Need to pass an {@link UsageInfo}.
27+
*
28+
* @param info
29+
* This will be uploaded in JSON format.
30+
*/
31+
public StatisticUploader(UsageInfo info) {
32+
this.info = info;
33+
}
34+
35+
/**
36+
* This is the upload logic. The host and port is specified compile time in
37+
* maven.
38+
*/
39+
private void uploadStatistics() {
40+
try (InputStream is = FileLocator.openStream(FrameworkUtil.getBundle(getClass()),
41+
new Path("resources/config.properties"), false)) {
42+
Properties prop = new Properties();
43+
prop.load(is);
44+
host = prop.getProperty("host");
45+
try {
46+
port = Integer.parseInt(prop.getProperty("port"));
47+
} catch (Exception e) {
48+
;
49+
}
50+
} catch (IOException e1) {
51+
;
52+
}
53+
54+
try (DatagramSocket socket = new DatagramSocket()) {
55+
if (port != null && host != null) {
56+
DatagramPacket packet = new DatagramPacket(new Gson().toJson(info).getBytes(),
57+
new Gson().toJson(info).getBytes().length,
58+
Inet4Address.getByName(host), port);
59+
socket.send(packet);
60+
}
61+
} catch (IOException e) {
62+
;
63+
}
64+
}
65+
66+
/**
67+
* Uploads usage statistics.
68+
*/
69+
@Override
70+
public void run() {
71+
uploadStatistics();
72+
}
73+
74+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package org.codechecker.eclipse.plugin.usage;
2+
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
import java.net.InetAddress;
6+
import java.net.UnknownHostException;
7+
import java.util.Properties;
8+
9+
import org.eclipse.core.runtime.FileLocator;
10+
import org.eclipse.core.runtime.IContributor;
11+
import org.eclipse.core.runtime.IExtension;
12+
import org.eclipse.core.runtime.IExtensionPoint;
13+
import org.eclipse.core.runtime.IExtensionRegistry;
14+
import org.eclipse.core.runtime.Path;
15+
import org.eclipse.core.runtime.Platform;
16+
import org.eclipse.jdt.annotation.Nullable;
17+
import org.osgi.framework.Bundle;
18+
import org.osgi.framework.FrameworkUtil;
19+
20+
/**
21+
* Class for Storing usage logging related info.
22+
*/
23+
public class UsageInfo {
24+
private static final String UNKNOWN = "unknown";
25+
private static final String UNKNOWN_VER = "unknown version";
26+
27+
@SuppressWarnings("unused")
28+
private String machine = UNKNOWN;
29+
@SuppressWarnings("unused")
30+
private String hostname = UNKNOWN;
31+
//@SuppressWarnings("unused")
32+
//private String clangsa = UNKNOWN;
33+
@SuppressWarnings("unused")
34+
private String version = UNKNOWN_VER;
35+
// TODO make this parameter dynamic, from build parameters.
36+
@SuppressWarnings("unused")
37+
private String pluginVersion = UNKNOWN_VER;
38+
@SuppressWarnings("unused")
39+
private String user = UNKNOWN;
40+
@SuppressWarnings("unused")
41+
private CommandType command_type;
42+
//@SuppressWarnings("unused")
43+
//private String clang_tidy = UNKNOWN;
44+
45+
/**
46+
* Specify the event type and the CodeChecker version if in context.
47+
*
48+
* @param ct
49+
* The command (event) type to be logged.
50+
* @param ccVersion
51+
* The CodeChecker version to be logged. Not every context has
52+
* CodeChecker.
53+
*/
54+
public UsageInfo(CommandType ct, @Nullable String ccVersion) {
55+
StringBuilder tempos = new StringBuilder(System.getProperty("os.name"));
56+
tempos.append(" ").append(System.getProperty("os.version"));
57+
tempos.append(" / Eclipse ").append(getEclipseVersion());
58+
machine = tempos.toString();
59+
try {
60+
hostname = InetAddress.getLocalHost().getHostName();
61+
} catch (UnknownHostException ex) {
62+
;
63+
}
64+
if (ccVersion != null)
65+
version = ccVersion;
66+
setPluginVersion();
67+
user = System.getProperty("user.name");
68+
command_type = ct;
69+
}
70+
71+
/**
72+
* Used for returning the eclipse version number. From:
73+
* https://stackoverflow.com/a/28855362/8149485
74+
*
75+
* @return The eclipse version number in 1.2.3.v19700101-0000 format.
76+
*/
77+
private String getEclipseVersion() {
78+
String version = UNKNOWN_VER;
79+
String product = System.getProperty("eclipse.product");
80+
IExtensionRegistry registry = Platform.getExtensionRegistry();
81+
IExtensionPoint point = registry.getExtensionPoint("org.eclipse.core.runtime.products");
82+
if (point != null) {
83+
IExtension[] extensions = point.getExtensions();
84+
for (IExtension ext : extensions)
85+
if (product.equals(ext.getUniqueIdentifier())) {
86+
IContributor contributor = ext.getContributor();
87+
if (contributor != null) {
88+
Bundle bundle = Platform.getBundle(contributor.getName());
89+
if (bundle != null)
90+
version = bundle.getVersion().toString();
91+
}
92+
}
93+
}
94+
return version;
95+
}
96+
97+
/**
98+
* Sets the plugin version from a properties file, which gets substituted during
99+
* build.
100+
*/
101+
private void setPluginVersion() {
102+
try (InputStream is = FileLocator.openStream(FrameworkUtil.getBundle(getClass()),
103+
new Path("resources/config.properties"), false)) {
104+
Properties prop = new Properties();
105+
prop.load(is);
106+
pluginVersion = prop.getProperty("version");
107+
} catch (IOException e1) {
108+
e1.printStackTrace();
109+
}
110+
}
111+
112+
/**
113+
* Command (Event) types.
114+
*/
115+
public enum CommandType {
116+
started, analyze_started
117+
}
118+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/**
2+
* Usage logging.
3+
*/
4+
package org.codechecker.eclipse.plugin.usage;

0 commit comments

Comments
 (0)