]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.utils.threads.logger;\r
13 \r
14 import java.io.DataOutput;\r
15 import java.io.DataOutputStream;\r
16 import java.io.FileNotFoundException;\r
17 import java.io.FileOutputStream;\r
18 import java.io.IOException;\r
19 \r
20 public class ThreadLogger implements IThreadLogger {\r
21 \r
22     public static String LOG_FILE = "d:\\threads.log";\r
23     \r
24     public static boolean LOG = false;\r
25 \r
26     static Object loggerCreationLock = new Object();\r
27     static ThreadLogger logger = null; \r
28 \r
29     DataOutput log;\r
30 \r
31     public ThreadLogger() {\r
32         if(LOG) {\r
33             try {\r
34                 FileOutputStream stream = new FileOutputStream(LOG_FILE);\r
35                 log = new DataOutputStream(stream);\r
36             } catch (FileNotFoundException e) {\r
37                 e.printStackTrace();\r
38             }\r
39         }\r
40     }\r
41 \r
42     public static ThreadLogger getInstance() {\r
43         if(logger == null) {\r
44             synchronized (loggerCreationLock) {\r
45                 if(logger == null)\r
46                     logger = new ThreadLogger();        \r
47             }                    \r
48         }\r
49         return logger;\r
50     }\r
51 \r
52     public class Task implements ITask {\r
53 \r
54         String name;\r
55         long beginTime;\r
56         long endTime;\r
57         long threadId;\r
58 \r
59         public Task(String name) {                      \r
60             this.name = name;\r
61             this.threadId = Thread.currentThread().getId();\r
62             this.beginTime = System.nanoTime();\r
63         }\r
64 \r
65         @Override\r
66         public void finish() {\r
67             this.endTime = System.nanoTime();\r
68             if(LOG && log != null) {\r
69                 \r
70                 try {\r
71                     synchronized(log) {\r
72                         log.writeUTF(name);\r
73                         log.writeLong(threadId);\r
74                         log.writeLong(beginTime);\r
75                         log.writeLong(endTime);\r
76                     }\r
77                 } catch(IOException e) {\r
78                     e.printStackTrace();\r
79                 }\r
80 \r
81             }\r
82         }\r
83 \r
84     }\r
85 \r
86     @Override\r
87     public ITask begin(String taskName) {\r
88         return new Task(taskName);\r
89     }\r
90 \r
91 }\r