# HG changeset patch # User cli # Date 1250786481 -7200 # Node ID f2293e8566f52509c147cc204c8fce5c703706c7 # Parent efce4ec25564c9602c2c333d58f9ebef71ce99c2 Started refactoring the Log system to use java.util.logging classes. diff -r efce4ec25564 -r f2293e8566f5 org/sonews/Main.java --- a/org/sonews/Main.java Thu Aug 20 16:57:38 2009 +0200 +++ b/org/sonews/Main.java Thu Aug 20 18:41:21 2009 +0200 @@ -48,7 +48,7 @@ } /** Version information of the sonews daemon */ - public static final String VERSION = "sonews/1.0.0"; + public static final String VERSION = "sonews/1.1.0"; public static final Date STARTDATE = new Date(); /** @@ -120,7 +120,7 @@ if(!StorageManager.current().isGroupExisting("control")) { StorageManager.current().addGroup("control", 0); - Log.msg("Group 'control' created.", true); + Log.get().info("Group 'control' created."); } } catch(StorageBackendException ex) diff -r efce4ec25564 -r f2293e8566f5 org/sonews/config/BackendConfig.java --- a/org/sonews/config/BackendConfig.java Thu Aug 20 16:57:38 2009 +0200 +++ b/org/sonews/config/BackendConfig.java Thu Aug 20 18:41:21 2009 +0200 @@ -18,6 +18,7 @@ package org.sonews.config; +import java.util.logging.Level; import org.sonews.util.Log; import org.sonews.storage.StorageBackendException; import org.sonews.storage.StorageManager; @@ -64,7 +65,7 @@ { if(StorageManager.current() == null) { - Log.msg("Warning: BackendConfig not available, using default.", false); + Log.get().warning("BackendConfig not available, using default."); return defaultValue; } @@ -86,7 +87,7 @@ } catch(StorageBackendException ex) { - Log.msg(ex.getMessage(), false); + Log.get().log(Level.SEVERE, "Storage backend problem", ex); return defaultValue; } } diff -r efce4ec25564 -r f2293e8566f5 org/sonews/config/Config.java --- a/org/sonews/config/Config.java Thu Aug 20 16:57:38 2009 +0200 +++ b/org/sonews/config/Config.java Thu Aug 20 18:41:21 2009 +0200 @@ -42,6 +42,7 @@ public static final String HOSTNAME = "sonews.hostname"; public static final String PORT = "sonews.port"; public static final String TIMEOUT = "sonews.timeout"; + public static final String LOGLEVEL = "sonews.loglevel"; public static final String MLPOLL_DELETEUNKNOWN = "sonews.mlpoll.deleteunknown"; public static final String MLPOLL_HOST = "sonews.mlpoll.host"; public static final String MLPOLL_PASSWORD = "sonews.mlpoll.password"; diff -r efce4ec25564 -r f2293e8566f5 org/sonews/daemon/AbstractDaemon.java --- a/org/sonews/daemon/AbstractDaemon.java Thu Aug 20 16:57:38 2009 +0200 +++ b/org/sonews/daemon/AbstractDaemon.java Thu Aug 20 18:41:21 2009 +0200 @@ -81,7 +81,7 @@ } catch(SQLException ex) { - Log.msg(ex, true); + Log.get().warning(ex.toString()); } } diff -r efce4ec25564 -r f2293e8566f5 org/sonews/daemon/ChannelReader.java --- a/org/sonews/daemon/ChannelReader.java Thu Aug 20 16:57:38 2009 +0200 +++ b/org/sonews/daemon/ChannelReader.java Thu Aug 20 18:41:21 2009 +0200 @@ -26,6 +26,7 @@ import java.nio.channels.SocketChannel; import java.util.Iterator; import java.util.Set; +import java.util.logging.Level; import org.sonews.util.Log; /** @@ -130,11 +131,8 @@ } catch(CancelledKeyException ex) { - Log.msg("ChannelReader.run(): " + ex, false); - if(Log.isDebug()) - { - ex.printStackTrace(); - } + Log.get().warning("ChannelReader.run(): " + ex); + Log.get().log(Level.INFO, "", ex); } catch(Exception ex) { @@ -177,15 +175,11 @@ { // The connection was probably closed by the remote host // in a non-clean fashion - Log.msg("ChannelReader.processSelectionKey(): " + ex, true); + Log.get().info("ChannelReader.processSelectionKey(): " + ex); } catch(Exception ex) { - Log.msg("ChannelReader.processSelectionKey(): " + ex, false); - if(Log.isDebug()) - { - ex.printStackTrace(); - } + Log.get().warning("ChannelReader.processSelectionKey(): " + ex); } if(read == -1) // End of stream @@ -201,7 +195,7 @@ else { // Should not happen - Log.msg(selKey, false); + Log.get().severe("Should not happen: " + selKey.toString()); } } diff -r efce4ec25564 -r f2293e8566f5 org/sonews/daemon/ChannelWriter.java --- a/org/sonews/daemon/ChannelWriter.java Thu Aug 20 16:57:38 2009 +0200 +++ b/org/sonews/daemon/ChannelWriter.java Thu Aug 20 18:41:21 2009 +0200 @@ -123,7 +123,7 @@ } catch (IOException ex) { - Log.msg("Error writing to channel: " + ex, false); + Log.get().warning("Error writing to channel: " + ex); // Cancel write events for this channel selKey.cancel(); @@ -137,7 +137,7 @@ } catch(CancelledKeyException ex) { - Log.msg("ChannelWriter.run(): " + ex, true); + Log.get().info("ChannelWriter.run(): " + ex); } catch(Exception ex) { @@ -192,14 +192,14 @@ } else { - Log.msg("Invalid OP_WRITE key: " + selKey, false); + Log.get().warning("Invalid OP_WRITE key: " + selKey); - if (socketChannel.socket().isClosed()) + if(socketChannel.socket().isClosed()) { connection.shutdownInput(); connection.shutdownOutput(); socketChannel.close(); - Log.msg("Connection closed.", true); + Log.get().info("Connection closed."); } } } diff -r efce4ec25564 -r f2293e8566f5 org/sonews/daemon/CommandSelector.java --- a/org/sonews/daemon/CommandSelector.java Thu Aug 20 16:57:38 2009 +0200 +++ b/org/sonews/daemon/CommandSelector.java Thu Aug 20 18:41:21 2009 +0200 @@ -73,15 +73,15 @@ } catch(ClassNotFoundException ex) { - Log.msg("Could not load command class: " + ex, false); + Log.get().warning("Could not load command class: " + ex); } catch(InstantiationException ex) { - Log.msg("Could not instantiate command class: " + ex, false); + Log.get().severe("Could not instantiate command class: " + ex); } catch(IllegalAccessException ex) { - Log.msg("Could not access command class: " + ex, false); + Log.get().severe("Could not access command class: " + ex); } } } diff -r efce4ec25564 -r f2293e8566f5 org/sonews/daemon/ConnectionWorker.java --- a/org/sonews/daemon/ConnectionWorker.java Thu Aug 20 16:57:38 2009 +0200 +++ b/org/sonews/daemon/ConnectionWorker.java Thu Aug 20 18:41:21 2009 +0200 @@ -89,11 +89,11 @@ } catch(InterruptedException ex) { - Log.msg("ConnectionWorker interrupted: " + ex, true); + Log.get().info("ConnectionWorker interrupted: " + ex); } catch(Exception ex) { - Log.msg("Exception in ConnectionWorker: " + ex, false); + Log.get().severe("Exception in ConnectionWorker: " + ex); ex.printStackTrace(); } } // end while(isRunning()) diff -r efce4ec25564 -r f2293e8566f5 org/sonews/daemon/Connections.java --- a/org/sonews/daemon/Connections.java Thu Aug 20 16:57:38 2009 +0200 +++ b/org/sonews/daemon/Connections.java Thu Aug 20 18:41:21 2009 +0200 @@ -147,12 +147,12 @@ { // Close the channel; implicitely cancels all selectionkeys channel.close(); - Log.msg("Disconnected: " + channel.socket().getRemoteSocketAddress() + - " (timeout)", true); + Log.get().info("Disconnected: " + channel.socket().getRemoteSocketAddress() + + " (timeout)"); } catch(IOException ex) { - Log.msg("Connections.run(): " + ex, false); + Log.get().warning("Connections.run(): " + ex); } // Recycle the used buffers @@ -169,7 +169,7 @@ } catch(InterruptedException ex) { - Log.msg("Connections Thread was interrupted: " + ex.getMessage(), false); + Log.get().warning("Connections Thread was interrupted: " + ex.getMessage()); } } } diff -r efce4ec25564 -r f2293e8566f5 org/sonews/daemon/NNTPConnection.java --- a/org/sonews/daemon/NNTPConnection.java Thu Aug 20 16:57:38 2009 +0200 +++ b/org/sonews/daemon/NNTPConnection.java Thu Aug 20 18:41:21 2009 +0200 @@ -166,11 +166,7 @@ } catch(IOException ex) { - Log.msg("Exception in NNTPConnection.shutdownInput(): " + ex, false); - if(Log.isDebug()) - { - ex.printStackTrace(); - } + Log.get().warning("Exception in NNTPConnection.shutdownInput(): " + ex); } } @@ -190,15 +186,11 @@ catch(SocketException ex) { // Socket was already disconnected - Log.msg("NNTPConnection.shutdownOutput(): " + ex, true); + Log.get().info("NNTPConnection.shutdownOutput(): " + ex); } catch(Exception ex) { - Log.msg("NNTPConnection.shutdownOutput(): " + ex, false); - if(Log.isDebug()) - { - ex.printStackTrace(); - } + Log.get().warning("NNTPConnection.shutdownOutput(): " + ex); } } }, 3000); @@ -272,7 +264,7 @@ raw = Arrays.copyOf(raw, raw.length - 1); } - Log.msg("<< " + line, true); + Log.get().fine("<< " + line); if(command == null) { @@ -289,8 +281,8 @@ { try { - Log.msg("Connection to " + channel.socket().getRemoteSocketAddress() - + " closed: " + ex0, true); + Log.get().info("Connection to " + channel.socket().getRemoteSocketAddress() + + " closed: " + ex0); } catch(Exception ex0a) { @@ -367,7 +359,7 @@ { if(!charset.canEncode()) { - Log.msg("FATAL: Charset " + charset + " cannot encode!", false); + Log.get().severe("FATAL: Charset " + charset + " cannot encode!"); return; } @@ -386,9 +378,9 @@ this.writeSelKey.interestOps(SelectionKey.OP_WRITE); ChannelWriter.getInstance().getSelector().wakeup(); } - catch (Exception ex) // CancelledKeyException and ChannelCloseException + catch(Exception ex) // CancelledKeyException and ChannelCloseException { - Log.msg("NNTPConnection.writeToChannel(): " + ex, false); + Log.get().warning("NNTPConnection.writeToChannel(): " + ex); return; } @@ -396,7 +388,7 @@ this.lastActivity = System.currentTimeMillis(); if(debugLine != null) { - Log.msg(">> " + debugLine, true); + Log.get().fine(">> " + debugLine); } } diff -r efce4ec25564 -r f2293e8566f5 org/sonews/daemon/NNTPDaemon.java --- a/org/sonews/daemon/NNTPDaemon.java Thu Aug 20 16:57:38 2009 +0200 +++ b/org/sonews/daemon/NNTPDaemon.java Thu Aug 20 18:41:21 2009 +0200 @@ -61,7 +61,7 @@ private NNTPDaemon(final int port) { - Log.msg("Server listening on port " + port, false); + Log.get().info("Server listening on port " + port); this.port = port; } @@ -113,8 +113,8 @@ // Under heavy load an IOException "Too many open files may // be thrown. It most cases we should slow down the connection // accepting, to give the worker threads some time to process work. - Log.msg("IOException while accepting connection: " + ex.getMessage(), false); - Log.msg("Connection accepting sleeping for seconds...", true); + Log.get().severe("IOException while accepting connection: " + ex.getMessage()); + Log.get().info("Connection accepting sleeping for seconds..."); Thread.sleep(5000); // 5 seconds continue; } @@ -127,7 +127,7 @@ } catch(IOException ex) { - Log.msg(ex.getLocalizedMessage(), false); + Log.get().warning(ex.toString()); socketChannel.close(); continue; } @@ -138,7 +138,7 @@ registerSelector(writeSelector, socketChannel, SelectionKey.OP_WRITE); registerSelector(readSelector, socketChannel, SelectionKey.OP_READ); - Log.msg("Connected: " + socketChannel.socket().getRemoteSocketAddress(), true); + Log.get().info("Connected: " + socketChannel.socket().getRemoteSocketAddress()); // Set write selection key and send hello to client conn.setWriteSelectionKey(selKeyWrite); @@ -147,13 +147,13 @@ } catch(CancelledKeyException cke) { - Log.msg("CancelledKeyException " + cke.getMessage() + " was thrown: " - + socketChannel.socket(), false); + Log.get().warning("CancelledKeyException " + cke.getMessage() + " was thrown: " + + socketChannel.socket()); } catch(ClosedChannelException cce) { - Log.msg("ClosedChannelException " + cce.getMessage() + " was thrown: " - + socketChannel.socket(), false); + Log.get().warning("ClosedChannelException " + cce.getMessage() + " was thrown: " + + socketChannel.socket()); } } } diff -r efce4ec25564 -r f2293e8566f5 org/sonews/daemon/command/OverCommand.java --- a/org/sonews/daemon/command/OverCommand.java Thu Aug 20 16:57:38 2009 +0200 +++ b/org/sonews/daemon/command/OverCommand.java Thu Aug 20 18:41:21 2009 +0200 @@ -168,7 +168,7 @@ } catch(NumberFormatException e) { - Log.msg(e.getMessage(), true); + Log.get().info(e.getMessage()); artStart = Integer.parseInt(command[1]); } } diff -r efce4ec25564 -r f2293e8566f5 org/sonews/daemon/command/PostCommand.java --- a/org/sonews/daemon/command/PostCommand.java Thu Aug 20 16:57:38 2009 +0200 +++ b/org/sonews/daemon/command/PostCommand.java Thu Aug 20 18:41:21 2009 +0200 @@ -226,7 +226,7 @@ default: { // Should never happen - Log.msg("PostCommand::processLine(): already finished...", false); + Log.get().severe("PostCommand::processLine(): already finished..."); } } } @@ -252,7 +252,7 @@ } catch(StorageBackendException ex) { - Log.msg(ex, false); + Log.get().severe(ex.toString()); conn.println("500 internal server error"); } } @@ -274,7 +274,7 @@ } catch(StorageBackendException ex) { - Log.msg(ex, false); + Log.get().severe(ex.toString()); conn.println("500 internal server error"); } } @@ -338,7 +338,7 @@ } catch(AddressException ex) { - Log.msg(ex.getMessage(), true); + Log.get().warning(ex.getMessage()); conn.println("441 invalid sender address"); } catch(MessagingException ex) diff -r efce4ec25564 -r f2293e8566f5 org/sonews/feed/PullFeeder.java --- a/org/sonews/feed/PullFeeder.java Thu Aug 20 16:57:38 2009 +0200 +++ b/org/sonews/feed/PullFeeder.java Thu Aug 20 18:41:21 2009 +0200 @@ -163,7 +163,7 @@ String host = "localhost"; int port = 119; - Log.msg("Start PullFeeder run...", true); + Log.get().info("Start PullFeeder run..."); try { @@ -174,14 +174,14 @@ try { - Log.msg("Feeding " + sub.getGroup() + " from " + sub.getHost(), true); + Log.get().info("Feeding " + sub.getGroup() + " from " + sub.getHost()); try { connectTo(host, port); } catch(SocketException ex) { - Log.msg("Skipping " + sub.getHost() + ": " + ex, false); + Log.get().info("Skipping " + sub.getHost() + ": " + ex); continue; } @@ -202,13 +202,14 @@ ArticleReader aread = new ArticleReader(sub.getHost(), sub.getPort(), messageID); byte[] abuf = aread.getArticleData(); - if (abuf == null) + if(abuf == null) { - Log.msg("Could not feed " + messageID + " from " + sub.getHost(), true); + Log.get().warning("Could not feed " + messageID + + " from " + sub.getHost()); } else { - Log.msg("Feeding " + messageID, true); + Log.get().info("Feeding " + messageID); ArticleWriter awrite = new ArticleWriter( "localhost", Config.inst().get(Config.PORT, 119)); awrite.writeArticle(abuf); @@ -220,7 +221,7 @@ { // There may be a temporary network failure ex.printStackTrace(); - Log.msg("Skipping mail " + messageID + " due to exception.", false); + Log.get().warning("Skipping mail " + messageID + " due to exception."); } } } // for(;;) @@ -236,16 +237,16 @@ catch(IOException ex) { ex.printStackTrace(); - Log.msg("PullFeeder run stopped due to exception.", false); + Log.get().severe("PullFeeder run stopped due to exception."); } } // for(Subscription sub : subscriptions) - Log.msg("PullFeeder run ended. Waiting " + pullInterval / 1000 + "s", true); + Log.get().info("PullFeeder run ended. Waiting " + pullInterval / 1000 + "s"); Thread.sleep(pullInterval); } catch(InterruptedException ex) { - Log.msg(ex.getMessage(), false); + Log.get().warning(ex.getMessage()); } } } diff -r efce4ec25564 -r f2293e8566f5 org/sonews/feed/PushFeeder.java --- a/org/sonews/feed/PushFeeder.java Thu Aug 20 16:57:38 2009 +0200 +++ b/org/sonews/feed/PushFeeder.java Thu Aug 20 18:41:21 2009 +0200 @@ -51,14 +51,14 @@ Article article = this.articleQueue.poll(); String[] groups = article.getHeader(Headers.NEWSGROUPS)[0].split(","); - Log.msg("PushFeed: " + article.getMessageID(), true); + Log.get().info("PushFeed: " + article.getMessageID()); for(Subscription sub : this.subscriptions) { // Circle check if(article.getHeader(Headers.PATH)[0].contains(sub.getHost())) { - Log.msg(article.getMessageID() + " skipped for host " - + sub.getHost(), true); + Log.get().info(article.getMessageID() + " skipped for host " + + sub.getHost()); continue; } @@ -84,13 +84,13 @@ } catch(IOException ex) { - Log.msg(ex, false); + Log.get().warning(ex.toString()); } } } catch(InterruptedException ex) { - Log.msg("PushFeeder interrupted: " + ex, true); + Log.get().warning("PushFeeder interrupted: " + ex); } } } diff -r efce4ec25564 -r f2293e8566f5 org/sonews/util/Log.java --- a/org/sonews/util/Log.java Thu Aug 20 16:57:38 2009 +0200 +++ b/org/sonews/util/Log.java Thu Aug 20 18:41:21 2009 +0200 @@ -18,7 +18,11 @@ package org.sonews.util; -import java.util.Date; +import java.util.logging.Level; +import java.util.logging.LogManager; +import java.util.logging.Logger; +import java.util.logging.SimpleFormatter; +import java.util.logging.StreamHandler; import org.sonews.config.Config; /** @@ -28,32 +32,26 @@ */ public class Log { - - public static boolean isDebug() + + public static final String MAIN = "main"; + + static { - // We must use FileConfig here otherwise we come - // into hell's kittchen when using the Logger within the - // Database class. - return Config.inst().get(Config.DEBUG, false); + Logger mainLogger = Logger.getLogger(MAIN); + StreamHandler handler = new StreamHandler(System.out, new SimpleFormatter()); + handler.setLevel(Level.parse(Config.inst().get(Config.LOGLEVEL, "INFO"))); + mainLogger.addHandler(handler); + LogManager.getLogManager().addLogger(mainLogger); } - - /** - * Writes the given message to the debug output. - * @param msg A String message or an object. - * @param If true this message is only shown if debug mode is enabled. - */ - public static void msg(final Object msg, boolean debug) + + public static Logger get() { - if(isDebug() || !debug) - { - synchronized(System.out) - { - System.out.print(new Date().toString()); - System.out.print(": "); - System.out.println(msg); - System.out.flush(); - } - } + return get(MAIN); + } + + public static Logger get(String name) + { + return LogManager.getLogManager().getLogger(name); } }