]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.thread/src/org/simantics/utils/threads/logger/ThreadLogger.java
d569d4aad2a3c4b4c70fd7d95a3d0be9963c7b85
[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     public static String LOG_FILE = "d:\\threads.log";
23     
24     public static boolean LOG = false;
25
26     static Object loggerCreationLock = new Object();
27     static ThreadLogger logger = null; 
28
29     DataOutput log;
30
31     public ThreadLogger() {
32         if(LOG) {
33             try {
34                 FileOutputStream stream = new FileOutputStream(LOG_FILE);
35                 log = new DataOutputStream(stream);
36             } catch (FileNotFoundException e) {
37                 e.printStackTrace();
38             }
39         }
40     }
41
42     public static ThreadLogger getInstance() {
43         if(logger == null) {
44             synchronized (loggerCreationLock) {
45                 if(logger == null)
46                     logger = new ThreadLogger();        
47             }                    
48         }
49         return logger;
50     }
51
52     public class Task implements ITask {
53
54         String name;
55         long beginTime;
56         long endTime;
57         long threadId;
58
59         public Task(String name) {                      
60             this.name = name;
61             this.threadId = Thread.currentThread().getId();
62             this.beginTime = System.nanoTime();
63         }
64
65         @Override
66         public void finish() {
67             this.endTime = System.nanoTime();
68             if(LOG && log != null) {
69                 
70                 try {
71                     synchronized(log) {
72                         log.writeUTF(name);
73                         log.writeLong(threadId);
74                         log.writeLong(beginTime);
75                         log.writeLong(endTime);
76                     }
77                 } catch(IOException e) {
78                     e.printStackTrace();
79                 }
80
81             }
82         }
83
84     }
85
86     @Override
87     public ITask begin(String taskName) {
88         return new Task(taskName);
89     }
90
91 }