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 final private static ITask DUMMY = () -> {};
24 public static String LOG_FILE = "threads.log";
26 public static boolean LOG = false;
28 static Object loggerCreationLock = new Object();
29 static ThreadLogger logger = null;
33 public ThreadLogger() {
36 FileOutputStream stream = new FileOutputStream(LOG_FILE);
37 log = new DataOutputStream(stream);
38 } catch (FileNotFoundException e) {
44 public static ThreadLogger getInstance() {
46 synchronized (loggerCreationLock) {
48 logger = new ThreadLogger();
54 public class Task implements ITask {
61 public Task(String name) {
63 this.threadId = Thread.currentThread().getId();
64 this.beginTime = System.nanoTime();
68 public void finish() {
69 this.endTime = System.nanoTime();
70 if(LOG && log != null) {
75 log.writeLong(threadId);
76 log.writeLong(beginTime);
77 log.writeLong(endTime);
79 } catch(IOException e) {
89 public ITask begin(String taskName) {
90 return new Task(taskName);
93 final public static ITask task(Object taskName) {
95 return getInstance().begin(taskName.toString());