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