2 // Getdown - application installer, patcher and launcher
3 // Copyright (C) 2004-2018 Getdown authors
4 // https://github.com/threerings/getdown/blob/master/LICENSE
6 package com.threerings.getdown;
8 import java.io.PrintWriter;
9 import java.io.StringWriter;
10 import java.text.FieldPosition;
11 import java.text.SimpleDateFormat;
12 import java.util.Date;
13 import java.util.logging.*;
16 * A placeholder class that contains a reference to the log object used by the Getdown code.
20 public static class Shim {
22 * Logs a debug message.
24 * @param message the message to be logged.
25 * @param args a list of key/value pairs and an optional final Throwable.
27 public void debug (Object message, Object... args) { doLog(0, message, args); }
30 * Logs an info message.
32 * @param message the message to be logged.
33 * @param args a list of key/value pairs and an optional final Throwable.
35 public void info (Object message, Object... args) { doLog(1, message, args); }
38 * Logs a warning message.
40 * @param message the message to be logged.
41 * @param args a list of key/value pairs and an optional final Throwable.
43 public void warning (Object message, Object... args) { doLog(2, message, args); }
46 * Logs an error message.
48 * @param message the message to be logged.
49 * @param args a list of key/value pairs and an optional final Throwable.
51 public void error (Object message, Object... args) { doLog(3, message, args); }
53 protected void doLog (int levIdx, Object message, Object[] args) {
54 if (_impl.isLoggable(LEVELS[levIdx])) {
57 if (message instanceof Throwable) {
58 err = (Throwable)message;
59 } else if (nn % 2 == 1 && (args[nn - 1] instanceof Throwable)) {
60 err = (Throwable)args[--nn];
62 _impl.log(LEVELS[levIdx], format(message, args), err);
66 protected final Logger _impl = Logger.getLogger("com.threerings.getdown");
69 /** We dispatch our log messages through this logging shim. */
70 public static final Shim log = new Shim();
72 public static String format (Object message, Object... args) {
73 if (args.length < 2) return String.valueOf(message);
74 StringBuilder buf = new StringBuilder(String.valueOf(message));
75 if (buf.length() > 0) {
79 for (int ii = 0; ii < args.length; ii += 2) {
81 buf.append(',').append(' ');
83 buf.append(args[ii]).append('=');
85 buf.append(args[ii+1]);
86 } catch (Throwable t) {
87 buf.append("<toString() failure: ").append(t).append(">");
90 return buf.append(']').toString();
94 Formatter formatter = new OneLineFormatter();
95 Logger logger = LogManager.getLogManager().getLogger("");
96 for (Handler handler : logger.getHandlers()) {
97 handler.setFormatter(formatter);
101 protected static class OneLineFormatter extends Formatter {
102 @Override public String format (LogRecord record) {
103 StringBuffer buf = new StringBuffer();
105 // append the timestamp
106 _date.setTime(record.getMillis());
107 _format.format(_date, buf, _fpos);
109 // append the log level
111 buf.append(record.getLevel().getLocalizedName());
114 // append the message itself
115 buf.append(formatMessage(record));
116 buf.append(System.lineSeparator());
118 // if an exception was also provided, append that
119 if (record.getThrown() != null) {
121 StringWriter sw = new StringWriter();
122 PrintWriter pw = new PrintWriter(sw);
123 record.getThrown().printStackTrace(pw);
125 buf.append(sw.toString());
126 } catch (Exception ex) {
127 buf.append("Format failure:").append(ex);
131 return buf.toString();
134 protected Date _date = new Date();
135 protected SimpleDateFormat _format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss:SSS");
136 protected FieldPosition _fpos = new FieldPosition(SimpleDateFormat.DATE_FIELD);
139 protected static final String DATE_FORMAT = "{0,date} {0,time}";
140 protected static final Level[] LEVELS = {Level.FINE, Level.INFO, Level.WARNING, Level.SEVERE};