Debian Codegraph @

src/dr/inference/loggers/TabDelimitedFormatter.java

raw symbols SCIP
1 /*
2 * TabDelimitedFormatter.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.inference.loggers;
27
28 import java.io.OutputStream;
29 import java.io.OutputStreamWriter;
30 import java.io.PrintWriter;
31
32 /**
33 * A class that writes a log in tab delimited format.
34 *
35 * @author Andrew Rambaut
36 * @author Alexei Drummond
37 * @version $Id: TabDelimitedFormatter.java,v 1.5 2005/05/24 20:25:59 rambaut Exp $
38 */
39 public class TabDelimitedFormatter implements LogFormatter {
40
41 protected final PrintWriter printWriter;
42 private final boolean outputLabels;
43 private final boolean closeFile;
44
45
46 public TabDelimitedFormatter(PrintWriter printWriter) {
47 this.printWriter = printWriter;
48 outputLabels = true;
49 closeFile = false;
50 }
51
52 public TabDelimitedFormatter(OutputStream stream) {
53 this.printWriter = new PrintWriter(new OutputStreamWriter(stream));
54 outputLabels = true;
55 closeFile = false;
56 }
57
58 public TabDelimitedFormatter(PrintWriter printWriter, boolean labels) {
59
60 this.printWriter = printWriter;
61 outputLabels = labels;
62 closeFile = true;
63 }
64
65 public void startLogging(String title) {
66 // DO NOTHING
67 }
68
69 public void logHeading(String heading) {
70 if (heading != null) {
71 String[] lines = heading.split("[\r\n]");
72 for (String line : lines) {
73 printWriter.println("# " + line);
74 }
75 }
76 printWriter.flush();
77 }
78
79 public void logLine(String line) {
80 printWriter.println(line);
81 printWriter.flush();
82 }
83
84 public void logLabels(String[] labels) {
85 if (outputLabels) {
86 if (labels.length > 0) {
87 printWriter.print(labels[0]);
88 }
89
90 for (int i = 1; i < labels.length; i++) {
91 printWriter.print('\t');
92 printWriter.print(labels[i]);
93 }
94
95 printWriter.println();
96 printWriter.flush();
97 }
98 }
99
100 public void logValues(String[] values) {
101
102 if (values.length > 0) {
103 printWriter.print(values[0]);
104 }
105
106 for (int i = 1; i < values.length; i++) {
107 printWriter.print('\t');
108 printWriter.print(values[i]);
109 }
110
111 printWriter.println();
112 printWriter.flush();
113 }
114
115 public void stopLogging() {
116 printWriter.flush();
117 if (closeFile) {
118 printWriter.close();
119 }
120 }
121
122 }