JAL-3130 adapted getdown src. attempt 2. first attempt failed due to cp'ed .git files
[jalview.git] / getdown / src / getdown / core / src / main / java / com / threerings / getdown / Log.java
1 //
2 // Getdown - application installer, patcher and launcher
3 // Copyright (C) 2004-2018 Getdown authors
4 // https://github.com/threerings/getdown/blob/master/LICENSE
5
6 package com.threerings.getdown;
7
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.*;
14
15 /**
16  * A placeholder class that contains a reference to the log object used by the Getdown code.
17  */
18 public class Log
19 {
20     public static class Shim {
21         /**
22          * Logs a debug message.
23          *
24          * @param message the message to be logged.
25          * @param args a list of key/value pairs and an optional final Throwable.
26          */
27         public void debug (Object message, Object... args) { doLog(0, message, args); }
28
29         /**
30          * Logs an info message.
31          *
32          * @param message the message to be logged.
33          * @param args a list of key/value pairs and an optional final Throwable.
34          */
35         public void info (Object message, Object... args) { doLog(1, message, args); }
36
37         /**
38          * Logs a warning message.
39          *
40          * @param message the message to be logged.
41          * @param args a list of key/value pairs and an optional final Throwable.
42          */
43         public void warning (Object message, Object... args) { doLog(2, message, args); }
44
45         /**
46          * Logs an error message.
47          *
48          * @param message the message to be logged.
49          * @param args a list of key/value pairs and an optional final Throwable.
50          */
51         public void error (Object message, Object... args) { doLog(3, message, args); }
52
53         protected void doLog (int levIdx, Object message, Object[] args) {
54             if (_impl.isLoggable(LEVELS[levIdx])) {
55                 Throwable err = null;
56                 int nn = args.length;
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];
61                 }
62                 _impl.log(LEVELS[levIdx], format(message, args), err);
63             }
64         }
65
66         protected final Logger _impl = Logger.getLogger("com.threerings.getdown");
67     }
68
69     /** We dispatch our log messages through this logging shim. */
70     public static final Shim log = new Shim();
71
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) {
76             buf.append(' ');
77         }
78         buf.append('[');
79         for (int ii = 0; ii < args.length; ii += 2) {
80             if (ii > 0) {
81                 buf.append(',').append(' ');
82             }
83             buf.append(args[ii]).append('=');
84             try {
85                 buf.append(args[ii+1]);
86             } catch (Throwable t) {
87                 buf.append("<toString() failure: ").append(t).append(">");
88             }
89         }
90         return buf.append(']').toString();
91     }
92
93     static {
94         Formatter formatter = new OneLineFormatter();
95         Logger logger = LogManager.getLogManager().getLogger("");
96         for (Handler handler : logger.getHandlers()) {
97             handler.setFormatter(formatter);
98         }
99     }
100
101     protected static class OneLineFormatter extends Formatter {
102         @Override public String format (LogRecord record) {
103             StringBuffer buf = new StringBuffer();
104
105             // append the timestamp
106             _date.setTime(record.getMillis());
107             _format.format(_date, buf, _fpos);
108
109             // append the log level
110             buf.append(" ");
111             buf.append(record.getLevel().getLocalizedName());
112             buf.append(" ");
113
114             // append the message itself
115             buf.append(formatMessage(record));
116             buf.append(System.lineSeparator());
117
118             // if an exception was also provided, append that
119             if (record.getThrown() != null) {
120                 try {
121                     StringWriter sw = new StringWriter();
122                     PrintWriter pw = new PrintWriter(sw);
123                     record.getThrown().printStackTrace(pw);
124                     pw.close();
125                     buf.append(sw.toString());
126                 } catch (Exception ex) {
127                     buf.append("Format failure:").append(ex);
128                 }
129             }
130
131             return buf.toString();
132         }
133
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);
137     }
138
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};
141 }