1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.start;
20
21 import java.io.IOException;
22 import java.io.OutputStream;
23 import java.io.PrintStream;
24 import java.nio.file.Files;
25 import java.nio.file.Path;
26 import java.nio.file.StandardOpenOption;
27 import java.util.Date;
28
29 import org.eclipse.jetty.start.config.CommandLineConfigSource;
30
31
32
33
34
35
36
37
38 public class StartLog
39 {
40 private final static PrintStream stdout = System.out;
41 private final static PrintStream stderr = System.err;
42 private static volatile PrintStream out = System.out;
43 private static volatile PrintStream err = System.err;
44 private final static StartLog INSTANCE = new StartLog();
45
46 public static void debug(String format, Object... args)
47 {
48 if (INSTANCE.debug)
49 {
50 out.printf(format + "%n",args);
51 }
52 }
53
54 public static void trace(String format, Object... args)
55 {
56 if (INSTANCE.trace)
57 {
58 out.printf("TRACE: " + format + "%n",args);
59 }
60 }
61
62 public static void debug(Throwable t)
63 {
64 if (INSTANCE.debug)
65 {
66 t.printStackTrace(out);
67 }
68 }
69
70 public static StartLog getInstance()
71 {
72 return INSTANCE;
73 }
74
75 public static void log(String type, String msg)
76 {
77 err.println(type + ": " + msg);
78 }
79
80 public static void log(String type, String format, Object... args)
81 {
82 err.printf(type + ": " + format + "%n",args);
83 }
84
85 public static void info(String format, Object... args)
86 {
87 log("INFO",format,args);
88 }
89
90 public static void warn(String format, Object... args)
91 {
92 log("WARNING",format,args);
93 }
94
95 public static void warn(Throwable t)
96 {
97 t.printStackTrace(err);
98 }
99
100 public static boolean isDebugEnabled()
101 {
102 return INSTANCE.debug;
103 }
104
105 private boolean trace = false;
106 private boolean debug = false;
107
108 public void initialize(BaseHome baseHome, CommandLineConfigSource cmdLineSource) throws IOException
109 {
110 String dbgProp = cmdLineSource.getProperty("debug");
111 if (dbgProp != null)
112 {
113 debug = Boolean.parseBoolean(dbgProp);
114 }
115
116 String logFileName = cmdLineSource.getProperty("start-log-file");
117
118 for (RawArgs.Entry arg : cmdLineSource.getArgs())
119 {
120 if ("--debug".equals(arg.getLine()))
121 {
122 debug = true;
123 continue;
124 }
125
126 if (arg.startsWith("--start-log-file"))
127 {
128 logFileName = Props.getValue(arg.getLine());
129 continue;
130 }
131 }
132
133 if (logFileName != null)
134 {
135 Path logfile = baseHome.getPath(logFileName);
136 logfile = logfile.toAbsolutePath();
137 initLogFile(logfile);
138 }
139 }
140
141 public void initLogFile(Path logfile) throws IOException
142 {
143 if (logfile != null)
144 {
145 try
146 {
147 Path logDir = logfile.getParent();
148 FS.ensureDirectoryWritable(logDir);
149
150 Path startLog = logfile;
151
152 if (!FS.exists(startLog) && !FS.createNewFile(startLog))
153 {
154
155 throw new UsageException(UsageException.ERR_LOGGING,new IOException("Unable to create: " + startLog.toAbsolutePath()));
156 }
157
158 if (!FS.canWrite(startLog))
159 {
160
161 throw new UsageException(UsageException.ERR_LOGGING,new IOException("Unable to write to: " + startLog.toAbsolutePath()));
162 }
163
164 err.println("StartLog to " + logfile);
165 OutputStream fileout = Files.newOutputStream(startLog,StandardOpenOption.CREATE,StandardOpenOption.APPEND);
166 PrintStream logger = new PrintStream(fileout);
167 out=logger;
168 err=logger;
169 System.setErr(logger);
170 System.setOut(logger);
171 err.println("StartLog Establishing " + logfile + " on " + new Date());
172 }
173 catch (IOException e)
174 {
175 throw new UsageException(UsageException.ERR_LOGGING,e);
176 }
177 }
178 }
179
180 public static void enableDebug()
181 {
182 getInstance().debug = true;
183 }
184
185 public static void endStartLog()
186 {
187 if (stderr!=err && getInstance().debug)
188 {
189 err.println("StartLog ended");
190 stderr.println("StartLog ended");
191 }
192 System.setErr(stderr);
193 System.setOut(stdout);
194 }
195 }