r34547 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r34546‎ | r34547 | r34548 >
Date:01:14, 10 May 2008
Author:river
Status:old
Tags:
Comment:
use log4j for logging
Modified paths:
  • /trunk/imgserv/imgserv-server/lib/log4j.jar (added) (history)
  • /trunk/imgserv/imgserv-server/log4j.properties (added) (history)
  • /trunk/imgserv/imgserv-server/mkdist.sh (modified) (history)
  • /trunk/imgserv/imgserv-server/nbproject/project.properties (modified) (history)
  • /trunk/imgserv/imgserv-server/src/imgservserver/ImageClient.java (modified) (history)
  • /trunk/imgserv/imgserv-server/src/imgservserver/Main.java (modified) (history)

Diff [purge]

Index: trunk/imgserv/imgserv-server/mkdist.sh
@@ -20,6 +20,7 @@
2121 mkdir bin
2222 cp -r ../dist/lib/* lib/
2323 cp -r ../dist/*.jar .
 24+cp log4j.properties dist/
2425
2526 chmod 644 lib/*
2627 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
2728 + application/octet-stream
Index: trunk/imgserv/imgserv-server/nbproject/project.properties
@@ -32,6 +32,7 @@
3333 file.reference.batik-transcoder.jar=H:\\imgserv\\imgserv-server\\lib\\batik-transcoder.jar
3434 file.reference.batik-util.jar=H:\\imgserv\\imgserv-server\\lib\\batik-util.jar
3535 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
3637 file.reference.pngds.jar=H:\\imgserv\\imgserv-server\\lib\\pngds.jar
3738 file.reference.xml-apis-ext.jar=H:\\imgserv\\imgserv-server\\lib\\xml-apis-ext.jar
3839 file.reference.xml-apis.jar=H:\\imgserv\\imgserv-server\\lib\\xml-apis.jar
@@ -54,7 +55,8 @@
5556 ${file.reference.batik-awt-util.jar}:\
5657 ${file.reference.batik-codec.jar}:\
5758 ${file.reference.pngds.jar}:\
58 - ${file.reference.xml-apis.jar}
 59+ ${file.reference.xml-apis.jar}:\
 60+ ${file.reference.log4j.jar}
5961 # Space-separated list of extra javac options
6062 javac.compilerargs=
6163 javac.deprecation=false
Index: trunk/imgserv/imgserv-server/src/imgservserver/ImageClient.java
@@ -16,24 +16,40 @@
1717 import java.io.IOException;
1818 import java.io.OutputStream;
1919 import java.net.Socket;
 20+import org.apache.log4j.Logger;
2021 import pngds.PNGResizer;
2122
2223 public class ImageClient extends Thread {
 24+ private static Logger logger = Logger.getLogger(ImageClient.class);
 25+
2326 Socket client;
2427 ImageClientInputStream reader;
2528 ImageClientOutputStream writer;
2629 Configuration config;
2730 int pngcount = 0;
2831
 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+
2944 public ImageClient(Socket cl, Configuration c) {
3045 client = cl;
3146 config = c;
32 -
 47+ }
 48+
 49+ public void run() {
3350 try {
3451 handleRequest();
3552 } 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());
3854 return;
3955 } finally {
4056 try {
@@ -72,8 +88,13 @@
7389 String line = reader.readLine();
7490 String[] args = line.split(" ");
7591
 92+ if (args.length == 0) {
 93+ error("Invalid command from client: empty line.");
 94+ return;
 95+ }
 96+
7697 if (args.length < 2) {
77 - System.out.printf("%% Invalid command from client.\n");
 98+ error("Invalid command from client: not enough arguments.");
7899 return;
79100 }
80101
@@ -93,18 +114,19 @@
94115 len = Integer.parseInt(args[1]);
95116 break;
96117 } else {
97 - System.out.printf("%% Invalid command from client.\n");
 118+ error("Invalid command from client: \"%s\" unrecognised.",
 119+ args[0]);
98120 return;
99121 }
100122 }
101123
102124 if (informat == null) {
103 - System.out.printf("%% No input format received.\n");
 125+ error("No input format received.");
104126 return;
105127 }
106128
107129 if (outformat == null) {
108 - System.out.printf("%% No output format received.\n");
 130+ error("No output format received.");
109131 return;
110132 }
111133
@@ -124,7 +146,7 @@
125147 for (;;) {
126148 n = reader.read(data, offs, len - offs);
127149 if (n == -1) {
128 - System.out.printf("%% Unexpected EOF reading from client.\n");
 150+ error("Unexpected EOF reading from client.");
129151 return;
130152 }
131153
@@ -148,18 +170,17 @@
149171 */
150172 tr.transcode(informat, outformat, width, height, data, writer);
151173 } catch (ImageTranscoderException e) {
152 - String error = e.getMessage();
 174+ String errorstr = e.getMessage();
153175 Throwable cause = e;
154176 while ((cause = cause.getCause()) != null) {
155 - error = error + ": " + cause.getMessage();
 177+ errorstr = errorstr + ": " + cause.getMessage();
156178 }
157179
158180 writer.cancel();
159 - String status = "ERROR " + error + "\r\n";
 181+ String status = "ERROR " + errorstr + "\r\n";
160182 writer.write(status.getBytes());
161183
162 - System.err.printf("%% [client: %s] %s\n", client.getRemoteSocketAddress().toString(),
163 - error);
 184+ error("%s", errorstr);
164185 }
165186
166187 writer.close();
@@ -186,7 +207,7 @@
187208 for (;;) {
188209 n = reader.read(buf, 0, 8192);
189210 if (n == -1) {
190 - System.out.printf("%% Unexpected EOF reading from client.\n");
 211+ error("Unexpected EOF reading from client.");
191212 return -1;
192213 }
193214
@@ -201,7 +222,7 @@
202223 int ret = PNGResizer.resize(inp, outp, height, height);
203224
204225 if (ret == -1) {
205 - System.out.printf("%% pngds resizing failed.\n");
 226+ error("pngds resizing failed.");
206227 }
207228
208229 /*
Index: trunk/imgserv/imgserv-server/src/imgservserver/Main.java
@@ -14,23 +14,29 @@
1515 import java.io.FileInputStream;
1616 import java.io.IOException;
1717 import java.util.Properties;
 18+import org.apache.log4j.Logger;
 19+import org.apache.log4j.PropertyConfigurator;
1820
1921 public class Main {
 22+ private static Logger logger = Logger.getLogger(ImageClient.class);
 23+
2024 public static void main(String[] args) {
2125 int i = 0;
2226 String configfile = null;
2327
 28+ PropertyConfigurator.configure("log4j.properties");
 29+
2430 while (i < args.length) {
2531 if (args[i].equals("-c")) {
2632 if (i + 1 >= args.length) {
27 - System.err.println("% Option '-c' requires an argument.");
 33+ logger.fatal("Option '-c' requires an argument.");
2834 System.exit(1);
2935 }
3036
3137 configfile = args[i + 1];
3238 i += 2;
3339 } else {
34 - System.err.printf("%% Option '%s' not recognised.\n", args[i]);
 40+ logger.fatal("Option \""+args[i]+"\" not recognised.");
3541 System.exit(1);
3642 }
3743 }
@@ -41,19 +47,18 @@
4248 if (configfile != null)
4349 config.load(new FileInputStream(new File(configfile)));
4450 } 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());
4752 System.exit(1);
4853 }
4954
5055 Configuration c = new Configuration(config);
5156
5257 try {
 58+ logger.info("Startup successful.");
5359 RequestListener listener = new RequestListener(c);
5460 listener.run();
5561 } 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());
5863 }
5964 }
6065
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

Status & tagging log