1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.utils.threads.logger;
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;
20 public class ThreadLogger implements IThreadLogger {
22 public static String LOG_FILE = "d:\\threads.log";
24 public static boolean LOG = false;
26 static Object loggerCreationLock = new Object();
27 static ThreadLogger logger = null;
31 public ThreadLogger() {
34 FileOutputStream stream = new FileOutputStream(LOG_FILE);
35 log = new DataOutputStream(stream);
36 } catch (FileNotFoundException e) {
42 public static ThreadLogger getInstance() {
44 synchronized (loggerCreationLock) {
46 logger = new ThreadLogger();
52 public class Task implements ITask {
59 public Task(String name) {
61 this.threadId = Thread.currentThread().getId();
62 this.beginTime = System.nanoTime();
66 public void finish() {
67 this.endTime = System.nanoTime();
68 if(LOG && log != null) {
73 log.writeLong(threadId);
74 log.writeLong(beginTime);
75 log.writeLong(endTime);
77 } catch(IOException e) {
87 public ITask begin(String taskName) {
88 return new Task(taskName);