]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.utils.thread/src/org/simantics/utils/threads/logger/ThreadLogger.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.utils.thread / src / org / simantics / utils / threads / logger / ThreadLogger.java
diff --git a/bundles/org.simantics.utils.thread/src/org/simantics/utils/threads/logger/ThreadLogger.java b/bundles/org.simantics.utils.thread/src/org/simantics/utils/threads/logger/ThreadLogger.java
new file mode 100644 (file)
index 0000000..99ac51f
--- /dev/null
@@ -0,0 +1,91 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.utils.threads.logger;\r
+\r
+import java.io.DataOutput;\r
+import java.io.DataOutputStream;\r
+import java.io.FileNotFoundException;\r
+import java.io.FileOutputStream;\r
+import java.io.IOException;\r
+\r
+public class ThreadLogger implements IThreadLogger {\r
+\r
+    public static String LOG_FILE = "d:\\threads.log";\r
+    \r
+    public static boolean LOG = false;\r
+\r
+    static Object loggerCreationLock = new Object();\r
+    static ThreadLogger logger = null; \r
+\r
+    DataOutput log;\r
+\r
+    public ThreadLogger() {\r
+        if(LOG) {\r
+            try {\r
+                FileOutputStream stream = new FileOutputStream(LOG_FILE);\r
+                log = new DataOutputStream(stream);\r
+            } catch (FileNotFoundException e) {\r
+                e.printStackTrace();\r
+            }\r
+        }\r
+    }\r
+\r
+    public static ThreadLogger getInstance() {\r
+        if(logger == null) {\r
+            synchronized (loggerCreationLock) {\r
+                if(logger == null)\r
+                    logger = new ThreadLogger();       \r
+            }                   \r
+        }\r
+        return logger;\r
+    }\r
+\r
+    public class Task implements ITask {\r
+\r
+        String name;\r
+        long beginTime;\r
+        long endTime;\r
+        long threadId;\r
+\r
+        public Task(String name) {                     \r
+            this.name = name;\r
+            this.threadId = Thread.currentThread().getId();\r
+            this.beginTime = System.nanoTime();\r
+        }\r
+\r
+        @Override\r
+        public void finish() {\r
+            this.endTime = System.nanoTime();\r
+            if(LOG && log != null) {\r
+                \r
+                try {\r
+                    synchronized(log) {\r
+                        log.writeUTF(name);\r
+                        log.writeLong(threadId);\r
+                        log.writeLong(beginTime);\r
+                        log.writeLong(endTime);\r
+                    }\r
+                } catch(IOException e) {\r
+                    e.printStackTrace();\r
+                }\r
+\r
+            }\r
+        }\r
+\r
+    }\r
+\r
+    @Override\r
+    public ITask begin(String taskName) {\r
+        return new Task(taskName);\r
+    }\r
+\r
+}\r