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 debug(Throwable t)
55 {
56 if (INSTANCE.debug)
57 {
58 t.printStackTrace(out);
59 }
60 }
61
62 public static StartLog getInstance()
63 {
64 return INSTANCE;
65 }
66
67 public static void info(String format, Object... args)
68 {
69 err.printf("INFO: " + format + "%n",args);
70 }
71
72 public static void warn(String format, Object... args)
73 {
74 err.printf("WARNING: " + format + "%n",args);
75 }
76
77 public static void warn(Throwable t)
78 {
79 t.printStackTrace(err);
80 }
81
82 public static boolean isDebugEnabled()
83 {
84 return INSTANCE.debug;
85 }
86
87 private boolean debug = false;
88
89 public void initialize(BaseHome baseHome, CommandLineConfigSource cmdLineSource) throws IOException
90 {
91 String dbgProp = cmdLineSource.getProperty("debug");
92 if (dbgProp != null)
93 {
94 debug = Boolean.parseBoolean(dbgProp);
95 }
96
97 String logFileName = cmdLineSource.getProperty("start-log-file");
98
99 for (RawArgs.Entry arg : cmdLineSource.getArgs())
100 {
101 if ("--debug".equals(arg.getLine()))
102 {
103 debug = true;
104 continue;
105 }
106
107 if (arg.startsWith("--start-log-file"))
108 {
109 logFileName = Props.getValue(arg.getLine());
110 continue;
111 }
112 }
113
114 if (logFileName != null)
115 {
116 Path logfile = baseHome.getPath(logFileName);
117 initLogFile(logfile);
118 }
119 }
120
121 public void initLogFile(Path logfile) throws IOException
122 {
123 if (logfile != null)
124 {
125 try
126 {
127 Path logDir = logfile.getParent();
128 FS.ensureDirectoryWritable(logDir);
129
130 Path startLog = logfile;
131
132 if (!FS.exists(startLog) && !FS.createNewFile(startLog))
133 {
134
135 throw new UsageException(UsageException.ERR_LOGGING,new IOException("Unable to create: " + startLog.toAbsolutePath()));
136 }
137
138 if (!FS.canWrite(startLog))
139 {
140
141 throw new UsageException(UsageException.ERR_LOGGING,new IOException("Unable to write to: " + startLog.toAbsolutePath()));
142 }
143
144 err.println("StartLog to " + logfile);
145 OutputStream fileout = Files.newOutputStream(startLog,StandardOpenOption.CREATE,StandardOpenOption.APPEND);
146 PrintStream logger = new PrintStream(fileout);
147 out=logger;
148 err=logger;
149 System.setErr(logger);
150 System.setOut(logger);
151 err.println("StartLog Establishing " + logfile + " on " + new Date());
152 }
153 catch (IOException e)
154 {
155 throw new UsageException(UsageException.ERR_LOGGING,e);
156 }
157 }
158 }
159
160 public static void enableDebug()
161 {
162 getInstance().debug = true;
163 }
164
165 public static void endStartLog()
166 {
167 if (stderr!=err && getInstance().debug)
168 {
169 err.println("StartLog ended");
170 stderr.println("StartLog ended");
171 }
172 System.setErr(stderr);
173 System.setOut(stdout);
174 }
175 }