]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/GraphPrinter.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / GraphPrinter.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.db.impl;
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 GraphPrinter {
21
22     public static String LOG_FILE = "d:\\graph.dot";
23     
24     public static boolean LOG = true;
25
26     static Object loggerCreationLock = new Object();
27     static GraphPrinter logger = null; 
28
29     DataOutput log;
30
31     public GraphPrinter() {
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 GraphPrinter getInstance() {
43         if(logger == null) {
44             synchronized (loggerCreationLock) {
45                 if(logger == null)
46                     logger = new GraphPrinter();        
47             }                    
48         }
49         return logger;
50     }
51
52     public void begin(String filename) {
53         if(LOG) {
54             try {
55                 FileOutputStream stream = new FileOutputStream(LOG_FILE);
56                 log = new DataOutputStream(stream);
57                 try {
58                     synchronized(log) {
59                         log.writeBytes("digraph test {\n");
60                     }
61                 } catch(IOException e) {
62                     e.printStackTrace();
63                 }
64             } catch (FileNotFoundException e) {
65                 e.printStackTrace();
66             }
67         }
68     }
69
70     public void finish() {
71         if(LOG) {
72             try {
73                 synchronized(log) {
74                     log.writeBytes("}\n");
75                 }
76             } catch(IOException e) {
77                 e.printStackTrace();
78             }
79             log = null;
80         }
81     }
82
83     private String escape(String input) {
84         return input.replace("[", "_").replace("]", "_").replace(":", "_").replace("/", "_").replace("@", "_").replace(".", "_").replace(" ", "_").replace("#", "_").replace("-", "_");
85     }
86     
87     public void log(String start, String end) {
88
89         try {
90             synchronized(log) {
91                 log.writeBytes(escape(start.toString()));
92                 log.writeBytes(" -> ");
93                 log.writeBytes(escape(end.toString()));
94                 log.writeBytes("\n");
95             }
96         } catch(IOException e) {
97             e.printStackTrace();
98         }
99         
100     }
101     
102 }