| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package dr.inference.loggers; |
| 27 |
|
| 28 |
import dr.util.Keywordable; |
| 29 |
|
| 30 |
import java.io.FileWriter; |
| 31 |
import java.io.IOException; |
| 32 |
import java.io.PrintWriter; |
| 33 |
import java.text.NumberFormat; |
| 34 |
import java.util.ArrayList; |
| 35 |
import java.util.HashSet; |
| 36 |
import java.util.List; |
| 37 |
import java.util.Set; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public class MCLogger implements Logger { |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
private final boolean performanceReport; |
| 52 |
private final int performanceReportDelay; |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
public MCLogger(LogFormatter formatter, long logEvery, boolean performanceReport, int performanceReportDelay) { |
| 61 |
|
| 62 |
addFormatter(formatter); |
| 63 |
this.logEvery = logEvery; |
| 64 |
this.performanceReport = performanceReport; |
| 65 |
this.performanceReportDelay = performanceReportDelay; |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
public MCLogger(LogFormatter formatter, long logEvery, boolean performanceReport) { |
| 75 |
this(formatter, logEvery, performanceReport, 0); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
public MCLogger(String fileName, long logEvery, boolean performanceReport, int performanceReportDelay) throws IOException { |
| 84 |
this(new TabDelimitedFormatter(new PrintWriter(new FileWriter(fileName))), logEvery, performanceReport, performanceReportDelay); |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
public MCLogger(boolean performanceReport, int performanceReportDelay) { |
| 93 |
this.performanceReport = performanceReport; |
| 94 |
this.performanceReportDelay = performanceReportDelay; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
public MCLogger(long logEvery) { |
| 103 |
this(new TabDelimitedFormatter(System.out), logEvery, true, 0); |
| 104 |
} |
| 105 |
|
| 106 |
public final void setTitle(String title) { |
| 107 |
this.title = title; |
| 108 |
} |
| 109 |
|
| 110 |
public final String getTitle() { |
| 111 |
return title; |
| 112 |
} |
| 113 |
|
| 114 |
public long getLogEvery() { |
| 115 |
return logEvery; |
| 116 |
} |
| 117 |
|
| 118 |
public void setLogEvery(long logEvery) { |
| 119 |
this.logEvery = logEvery; |
| 120 |
} |
| 121 |
|
| 122 |
public final void addFormatter(LogFormatter formatter) { |
| 123 |
|
| 124 |
formatters.add(formatter); |
| 125 |
} |
| 126 |
|
| 127 |
public final void add(Loggable loggable) { |
| 128 |
|
| 129 |
LogColumn[] columns = loggable.getColumns(); |
| 130 |
|
| 131 |
for (LogColumn column : columns) { |
| 132 |
addColumn(column); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
public final void addColumn(LogColumn column) { |
| 137 |
|
| 138 |
if (column instanceof Keywordable) { |
| 139 |
keywords.addAll(((Keywordable)column).getKeywords()); |
| 140 |
} |
| 141 |
|
| 142 |
columns.add(column); |
| 143 |
} |
| 144 |
|
| 145 |
public final void addColumns(LogColumn[] columns) { |
| 146 |
|
| 147 |
for (LogColumn column : columns) { |
| 148 |
addColumn(column); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
public final int getColumnCount() { |
| 153 |
return columns.size(); |
| 154 |
} |
| 155 |
|
| 156 |
public final LogColumn getColumn(int index) { |
| 157 |
|
| 158 |
return columns.get(index); |
| 159 |
} |
| 160 |
|
| 161 |
public final String getColumnLabel(int index) { |
| 162 |
|
| 163 |
return columns.get(index).getLabel(); |
| 164 |
} |
| 165 |
|
| 166 |
public final String getColumnFormatted(int index) { |
| 167 |
|
| 168 |
return columns.get(index).getFormatted(); |
| 169 |
} |
| 170 |
|
| 171 |
protected void logHeading(String heading) { |
| 172 |
for (LogFormatter formatter : formatters) { |
| 173 |
formatter.logHeading(heading); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
protected void logLine(String line) { |
| 178 |
for (LogFormatter formatter : formatters) { |
| 179 |
formatter.logLine(line); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
protected void logLabels(String[] labels) { |
| 184 |
for (LogFormatter formatter : formatters) { |
| 185 |
formatter.logLabels(labels); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
protected void logValues(String[] values) { |
| 190 |
for (LogFormatter formatter : formatters) { |
| 191 |
formatter.logValues(values); |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
public void startLogging() { |
| 196 |
|
| 197 |
for (LogFormatter formatter : formatters) { |
| 198 |
formatter.startLogging(title); |
| 199 |
} |
| 200 |
|
| 201 |
if (title != null) { |
| 202 |
logHeading(title); |
| 203 |
} |
| 204 |
if (keywords.size() > 0) { |
| 205 |
StringBuffer sb = new StringBuffer("keywords:"); |
| 206 |
for (String keyword: keywords) { |
| 207 |
sb.append(" "); |
| 208 |
sb.append(keyword); |
| 209 |
} |
| 210 |
logHeading(sb.toString()); |
| 211 |
} |
| 212 |
|
| 213 |
if (logEvery > 0) { |
| 214 |
final int columnCount = getColumnCount(); |
| 215 |
String[] labels = new String[columnCount + 1]; |
| 216 |
|
| 217 |
labels[0] = "state"; |
| 218 |
|
| 219 |
for (int i = 0; i < columnCount; i++) { |
| 220 |
labels[i + 1] = getColumnLabel(i); |
| 221 |
} |
| 222 |
|
| 223 |
logLabels(labels); |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
public final void log(int state) { |
| 228 |
|
| 229 |
} |
| 230 |
|
| 231 |
@Override |
| 232 |
public void log(long state) { |
| 233 |
|
| 234 |
if (performanceReport && !performanceReportStarted && state >= performanceReportDelay) { |
| 235 |
startTime = System.currentTimeMillis(); |
| 236 |
startState = state; |
| 237 |
formatter.setMaximumFractionDigits(2); |
| 238 |
} |
| 239 |
|
| 240 |
if (logEvery > 0 && (state % logEvery == 0)) { |
| 241 |
|
| 242 |
final int columnCount = getColumnCount(); |
| 243 |
|
| 244 |
String[] values = new String[columnCount + (performanceReport ? 2 : 1)]; |
| 245 |
|
| 246 |
values[0] = Long.toString(state); |
| 247 |
|
| 248 |
for (int i = 0; i < columnCount; i++) { |
| 249 |
values[i + 1] = getColumnFormatted(i); |
| 250 |
} |
| 251 |
|
| 252 |
if (performanceReport) { |
| 253 |
if (performanceReportStarted) { |
| 254 |
|
| 255 |
long time = System.currentTimeMillis(); |
| 256 |
|
| 257 |
double hoursPerMillionStates = (double) (time - startTime) / (3.6 * (double) (state - startState)); |
| 258 |
|
| 259 |
String timePerMillion = formatter.format(hoursPerMillionStates); |
| 260 |
String units = " hours/million states"; |
| 261 |
if (hoursPerMillionStates < 0.1) { |
| 262 |
double minutesPerMillionStates = hoursPerMillionStates * 60; |
| 263 |
timePerMillion = formatter.format(minutesPerMillionStates); |
| 264 |
units = " minutes/million states"; |
| 265 |
if (minutesPerMillionStates < 0.1) { |
| 266 |
double secondsPerMillionStates = minutesPerMillionStates * 60; |
| 267 |
timePerMillion = formatter.format(secondsPerMillionStates); |
| 268 |
units = " seconds/million states"; |
| 269 |
} |
| 270 |
} |
| 271 |
values[columnCount + 1] = timePerMillion + units; |
| 272 |
|
| 273 |
} else { |
| 274 |
values[columnCount + 1] = "-"; |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
logValues(values); |
| 279 |
} |
| 280 |
|
| 281 |
if (performanceReport && !performanceReportStarted && state >= performanceReportDelay) { |
| 282 |
performanceReportStarted = true; |
| 283 |
} |
| 284 |
|
| 285 |
} |
| 286 |
|
| 287 |
public void stopLogging() { |
| 288 |
|
| 289 |
for (LogFormatter formatter : formatters) { |
| 290 |
formatter.stopLogging(); |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
private String title = null; |
| 295 |
|
| 296 |
private Set<String> keywords = new HashSet<String>(); |
| 297 |
|
| 298 |
private List<LogColumn> columns = new ArrayList<LogColumn>(); |
| 299 |
|
| 300 |
protected long logEvery = 0; |
| 301 |
|
| 302 |
public List<LogFormatter> getFormatters() { |
| 303 |
return formatters; |
| 304 |
} |
| 305 |
|
| 306 |
public void setFormatters(List<LogFormatter> formatters) { |
| 307 |
this.formatters = formatters; |
| 308 |
} |
| 309 |
|
| 310 |
protected List<LogFormatter> formatters = new ArrayList<LogFormatter>(); |
| 311 |
|
| 312 |
private boolean performanceReportStarted = false; |
| 313 |
private long startTime; |
| 314 |
private long startState; |
| 315 |
|
| 316 |
private final NumberFormat formatter = NumberFormat.getNumberInstance(); |
| 317 |
|
| 318 |
} |