Index: trunk/imgserv/imgserv-server/mkdist.sh |
— | — | @@ -20,6 +20,7 @@ |
21 | 21 | mkdir bin |
22 | 22 | cp -r ../dist/lib/* lib/ |
23 | 23 | cp -r ../dist/*.jar . |
| 24 | +cp log4j.properties dist/ |
24 | 25 | |
25 | 26 | chmod 644 lib/* |
26 | 27 | chmod 644 *.jar |
Index: trunk/imgserv/imgserv-server/lib/log4j.jar |
Cannot display: file marked as a binary type. |
svn:mime-type = application/octet-stream |
Property changes on: trunk/imgserv/imgserv-server/lib/log4j.jar |
___________________________________________________________________ |
Added: svn:mime-type |
27 | 28 | + application/octet-stream |
Index: trunk/imgserv/imgserv-server/nbproject/project.properties |
— | — | @@ -32,6 +32,7 @@ |
33 | 33 | file.reference.batik-transcoder.jar=H:\\imgserv\\imgserv-server\\lib\\batik-transcoder.jar
|
34 | 34 | file.reference.batik-util.jar=H:\\imgserv\\imgserv-server\\lib\\batik-util.jar
|
35 | 35 | file.reference.batik-xml.jar=H:\\imgserv\\imgserv-server\\lib\\batik-xml.jar
|
| 36 | +file.reference.log4j.jar=H:\\imgserv\\imgserv-server\\lib\\log4j.jar
|
36 | 37 | file.reference.pngds.jar=H:\\imgserv\\imgserv-server\\lib\\pngds.jar
|
37 | 38 | file.reference.xml-apis-ext.jar=H:\\imgserv\\imgserv-server\\lib\\xml-apis-ext.jar
|
38 | 39 | file.reference.xml-apis.jar=H:\\imgserv\\imgserv-server\\lib\\xml-apis.jar
|
— | — | @@ -54,7 +55,8 @@ |
55 | 56 | ${file.reference.batik-awt-util.jar}:\
|
56 | 57 | ${file.reference.batik-codec.jar}:\
|
57 | 58 | ${file.reference.pngds.jar}:\
|
58 | | - ${file.reference.xml-apis.jar}
|
| 59 | + ${file.reference.xml-apis.jar}:\
|
| 60 | + ${file.reference.log4j.jar}
|
59 | 61 | # Space-separated list of extra javac options
|
60 | 62 | javac.compilerargs=
|
61 | 63 | javac.deprecation=false
|
Index: trunk/imgserv/imgserv-server/src/imgservserver/ImageClient.java |
— | — | @@ -16,24 +16,40 @@ |
17 | 17 | import java.io.IOException; |
18 | 18 | import java.io.OutputStream; |
19 | 19 | import java.net.Socket; |
| 20 | +import org.apache.log4j.Logger; |
20 | 21 | import pngds.PNGResizer; |
21 | 22 | |
22 | 23 | public class ImageClient extends Thread { |
| 24 | + private static Logger logger = Logger.getLogger(ImageClient.class); |
| 25 | + |
23 | 26 | Socket client; |
24 | 27 | ImageClientInputStream reader; |
25 | 28 | ImageClientOutputStream writer; |
26 | 29 | Configuration config; |
27 | 30 | int pngcount = 0; |
28 | 31 | |
| 32 | + void error(String fmt, Object... args) { |
| 33 | + logger.error(String.format("[%s] %s", |
| 34 | + client.getRemoteSocketAddress().toString(), |
| 35 | + String.format(fmt, args))); |
| 36 | + } |
| 37 | + |
| 38 | + void info(String fmt, Object... args) { |
| 39 | + logger.error(String.format("[%s] %s", |
| 40 | + client.getRemoteSocketAddress().toString(), |
| 41 | + String.format(fmt, args))); |
| 42 | + } |
| 43 | + |
29 | 44 | public ImageClient(Socket cl, Configuration c) { |
30 | 45 | client = cl; |
31 | 46 | config = c; |
32 | | - |
| 47 | + } |
| 48 | + |
| 49 | + public void run() { |
33 | 50 | try { |
34 | 51 | handleRequest(); |
35 | 52 | } catch (Exception e) { |
36 | | - System.out.printf("%% Error occurred handling client request: %s\n", |
37 | | - e.toString()); |
| 53 | + error("Error occurred handling client request: %s", e.toString()); |
38 | 54 | return; |
39 | 55 | } finally { |
40 | 56 | try { |
— | — | @@ -72,8 +88,13 @@ |
73 | 89 | String line = reader.readLine(); |
74 | 90 | String[] args = line.split(" "); |
75 | 91 | |
| 92 | + if (args.length == 0) { |
| 93 | + error("Invalid command from client: empty line."); |
| 94 | + return; |
| 95 | + } |
| 96 | + |
76 | 97 | if (args.length < 2) { |
77 | | - System.out.printf("%% Invalid command from client.\n"); |
| 98 | + error("Invalid command from client: not enough arguments."); |
78 | 99 | return; |
79 | 100 | } |
80 | 101 | |
— | — | @@ -93,18 +114,19 @@ |
94 | 115 | len = Integer.parseInt(args[1]); |
95 | 116 | break; |
96 | 117 | } else { |
97 | | - System.out.printf("%% Invalid command from client.\n"); |
| 118 | + error("Invalid command from client: \"%s\" unrecognised.", |
| 119 | + args[0]); |
98 | 120 | return; |
99 | 121 | } |
100 | 122 | } |
101 | 123 | |
102 | 124 | if (informat == null) { |
103 | | - System.out.printf("%% No input format received.\n"); |
| 125 | + error("No input format received."); |
104 | 126 | return; |
105 | 127 | } |
106 | 128 | |
107 | 129 | if (outformat == null) { |
108 | | - System.out.printf("%% No output format received.\n"); |
| 130 | + error("No output format received."); |
109 | 131 | return; |
110 | 132 | } |
111 | 133 | |
— | — | @@ -124,7 +146,7 @@ |
125 | 147 | for (;;) { |
126 | 148 | n = reader.read(data, offs, len - offs); |
127 | 149 | if (n == -1) { |
128 | | - System.out.printf("%% Unexpected EOF reading from client.\n"); |
| 150 | + error("Unexpected EOF reading from client."); |
129 | 151 | return; |
130 | 152 | } |
131 | 153 | |
— | — | @@ -148,18 +170,17 @@ |
149 | 171 | */ |
150 | 172 | tr.transcode(informat, outformat, width, height, data, writer); |
151 | 173 | } catch (ImageTranscoderException e) { |
152 | | - String error = e.getMessage(); |
| 174 | + String errorstr = e.getMessage(); |
153 | 175 | Throwable cause = e; |
154 | 176 | while ((cause = cause.getCause()) != null) { |
155 | | - error = error + ": " + cause.getMessage(); |
| 177 | + errorstr = errorstr + ": " + cause.getMessage(); |
156 | 178 | } |
157 | 179 | |
158 | 180 | writer.cancel(); |
159 | | - String status = "ERROR " + error + "\r\n"; |
| 181 | + String status = "ERROR " + errorstr + "\r\n"; |
160 | 182 | writer.write(status.getBytes()); |
161 | 183 | |
162 | | - System.err.printf("%% [client: %s] %s\n", client.getRemoteSocketAddress().toString(), |
163 | | - error); |
| 184 | + error("%s", errorstr); |
164 | 185 | } |
165 | 186 | |
166 | 187 | writer.close(); |
— | — | @@ -186,7 +207,7 @@ |
187 | 208 | for (;;) { |
188 | 209 | n = reader.read(buf, 0, 8192); |
189 | 210 | if (n == -1) { |
190 | | - System.out.printf("%% Unexpected EOF reading from client.\n"); |
| 211 | + error("Unexpected EOF reading from client."); |
191 | 212 | return -1; |
192 | 213 | } |
193 | 214 | |
— | — | @@ -201,7 +222,7 @@ |
202 | 223 | int ret = PNGResizer.resize(inp, outp, height, height); |
203 | 224 | |
204 | 225 | if (ret == -1) { |
205 | | - System.out.printf("%% pngds resizing failed.\n"); |
| 226 | + error("pngds resizing failed."); |
206 | 227 | } |
207 | 228 | |
208 | 229 | /* |
Index: trunk/imgserv/imgserv-server/src/imgservserver/Main.java |
— | — | @@ -14,23 +14,29 @@ |
15 | 15 | import java.io.FileInputStream; |
16 | 16 | import java.io.IOException; |
17 | 17 | import java.util.Properties; |
| 18 | +import org.apache.log4j.Logger; |
| 19 | +import org.apache.log4j.PropertyConfigurator; |
18 | 20 | |
19 | 21 | public class Main { |
| 22 | + private static Logger logger = Logger.getLogger(ImageClient.class); |
| 23 | + |
20 | 24 | public static void main(String[] args) { |
21 | 25 | int i = 0; |
22 | 26 | String configfile = null; |
23 | 27 | |
| 28 | + PropertyConfigurator.configure("log4j.properties"); |
| 29 | + |
24 | 30 | while (i < args.length) { |
25 | 31 | if (args[i].equals("-c")) { |
26 | 32 | if (i + 1 >= args.length) { |
27 | | - System.err.println("% Option '-c' requires an argument."); |
| 33 | + logger.fatal("Option '-c' requires an argument."); |
28 | 34 | System.exit(1); |
29 | 35 | } |
30 | 36 | |
31 | 37 | configfile = args[i + 1]; |
32 | 38 | i += 2; |
33 | 39 | } else { |
34 | | - System.err.printf("%% Option '%s' not recognised.\n", args[i]); |
| 40 | + logger.fatal("Option \""+args[i]+"\" not recognised."); |
35 | 41 | System.exit(1); |
36 | 42 | } |
37 | 43 | } |
— | — | @@ -41,19 +47,18 @@ |
42 | 48 | if (configfile != null) |
43 | 49 | config.load(new FileInputStream(new File(configfile))); |
44 | 50 | } catch (Exception e) { |
45 | | - System.err.printf("%% Cannot load configuration file \"%s\": %s\n", |
46 | | - configfile, e.getMessage()); |
| 51 | + logger.fatal("Cannot load configuration file \""+configfile+"\": "+e.getMessage()); |
47 | 52 | System.exit(1); |
48 | 53 | } |
49 | 54 | |
50 | 55 | Configuration c = new Configuration(config); |
51 | 56 | |
52 | 57 | try { |
| 58 | + logger.info("Startup successful."); |
53 | 59 | RequestListener listener = new RequestListener(c); |
54 | 60 | listener.run(); |
55 | 61 | } catch (IOException e) { |
56 | | - System.out.printf("% Error occurred in request loop: %s", |
57 | | - e.getMessage()); |
| 62 | + logger.fatal("Error occurred in request loop: " + e.getMessage()); |
58 | 63 | } |
59 | 64 | } |
60 | 65 | |
Index: trunk/imgserv/imgserv-server/log4j.properties |
— | — | @@ -0,0 +1,7 @@ |
| 2 | +log4j.rootLogger=INFO, CONS |
| 3 | + |
| 4 | +log4j.appender.CONS=org.apache.log4j.ConsoleAppender |
| 5 | + |
| 6 | +log4j.appender.CONS.layout=org.apache.log4j.PatternLayout |
| 7 | +#log4j.appender.CONF.layout.ConversionPattern=%d (T+%r) [%t] %-5p %c{1} %x - %m%n |
| 8 | +log4j.appender.CONS.layout.ConversionPattern=%%%p[%t]%m%n |