From: <le...@us...> - 2007-10-16 13:26:33
|
Revision: 9 http://rochat.svn.sourceforge.net/rochat/?rev=9&view=rev Author: levia Date: 2007-10-16 06:26:31 -0700 (Tue, 16 Oct 2007) Log Message: ----------- - Added logging, still working on redirecting error stream. - Changed look and feel to be system native. Modified Paths: -------------- trunk/src/net/sensiva/rochat/ui/main/MainFrame.java Added Paths: ----------- trunk/src/net/sensiva/rochat/core/ trunk/src/net/sensiva/rochat/core/logging/ trunk/src/net/sensiva/rochat/core/logging/ErrorThread.java trunk/src/net/sensiva/rochat/core/logging/LogListener.java trunk/src/net/sensiva/rochat/core/logging/Logger.java Added: trunk/src/net/sensiva/rochat/core/logging/ErrorThread.java =================================================================== --- trunk/src/net/sensiva/rochat/core/logging/ErrorThread.java (rev 0) +++ trunk/src/net/sensiva/rochat/core/logging/ErrorThread.java 2007-10-16 13:26:31 UTC (rev 9) @@ -0,0 +1,60 @@ +/* + ROChat + Copyright (C) 2007 The ROChat team + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + ---------------------------------------------------------------------------- +*/ +package net.sensiva.rochat.ui.core.logging; + +import java.util.*; +import java.io.*; + +/** + * This class is a thread that runs to listen for errors (exceptions, etc). +*/ +public class ErrorThread extends Thread +{ + private PipedInputStream m_ErrInput; + + public ErrorThread(PipedInputStream errorInput) + { + m_ErrInput = errorInput; + } + + public void run() + { + final byte[] buf = new byte[1024]; + try + { + while (true) + { + final int len = m_ErrInput.read(buf); + if (len == -1) + { + break; + } + String message = new String(buf, 0, len); + System.out.println(message); + Logger.getInstance().log(Logger.LOG_ERROR, message); + } + } + catch (IOException e) + { + } + } + +} + Added: trunk/src/net/sensiva/rochat/core/logging/LogListener.java =================================================================== --- trunk/src/net/sensiva/rochat/core/logging/LogListener.java (rev 0) +++ trunk/src/net/sensiva/rochat/core/logging/LogListener.java 2007-10-16 13:26:31 UTC (rev 9) @@ -0,0 +1,37 @@ +/* + ROChat + Copyright (C) 2007 The ROChat team + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + ---------------------------------------------------------------------------- +*/ +package net.sensiva.rochat.ui.core.logging; + +import java.util.Date; + +/** + * The listener interface. +*/ +public interface LogListener +{ + /** + * The logEvent function handles the logged events. + * + * @param severity The severity of this message. + * @param message The actual message. + * @param timeStamp The timestamp of the event. + */ + public void logEvent(String severity, String message, Date timeStamp); +} \ No newline at end of file Added: trunk/src/net/sensiva/rochat/core/logging/Logger.java =================================================================== --- trunk/src/net/sensiva/rochat/core/logging/Logger.java (rev 0) +++ trunk/src/net/sensiva/rochat/core/logging/Logger.java 2007-10-16 13:26:31 UTC (rev 9) @@ -0,0 +1,139 @@ +/* + ROChat + Copyright (C) 2007 The ROChat team + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + ---------------------------------------------------------------------------- +*/ +package net.sensiva.rochat.ui.core.logging; + +import java.util.*; +import java.io.*; + +/** + * The Logger class, here you can listen for log messages, and log them + * yourself, or you can let the standard logger take over. +*/ +public class Logger +{ + /** Severity used for information - nothings wrong, just info. */ + public static final String LOG_INFORMATION = "INFORMATION"; + + /** Severity used for warnings - there was something wrong, but we can + continue without hassle. */ + public static final String LOG_WARNING = "WARNING"; + + /** Severity used for error - there was an error, and we needed to stop our + current execution. */ + public static final String LOG_ERROR = "ERROR"; + + private List<LogListener> m_LogListeners = new ArrayList<LogListener>(); + private static Logger m_Instance = new Logger(); + + private PipedInputStream m_ErrInput; + private PipedOutputStream m_ErrOutput; + + /** + * The Logger constructor. + */ + public Logger() + { + try + { + m_ErrInput = new PipedInputStream(); + m_ErrOutput = new PipedOutputStream(m_ErrInput); + System.setErr(new PrintStream(m_ErrOutput, true)); + } + catch (Exception e) + { + e.printStackTrace(); + } + System.out.println("Blaat"); + new ErrorThread(m_ErrInput).start(); + throw new RuntimeException(); + } + + /** + * This functions logs a message. + * + * @param severity The severity of the event. + * @param message The message. + */ + public void log(String severity, String message) + { + if (m_LogListeners.isEmpty()) + { + // error + System.out.println(severity + " : " + message); + } + Date timeStamp = new Date(); + for (int i = 0; i < m_LogListeners.size(); i++) + { + LogListener listener = (LogListener)m_LogListeners.get(i); + listener.logEvent(severity, message, timeStamp); + } + + } + + /** + * This function logs a message. + * + * @param message The message. + */ + public void log(String message) + { + if (m_LogListeners.isEmpty()) + { + // error + } + Date timeStamp = new Date(); + for (int i = 0; i < m_LogListeners.size(); i++) + { + LogListener listener = (LogListener)m_LogListeners.get(i); + listener.logEvent(LOG_INFORMATION, message, timeStamp); + } + } + + /** + * Method to register a listener for the log. + * + * @param listener The listener. + */ + public void addListener(LogListener listener) + { + m_LogListeners.add(listener); + } + + /** + * Method to unregister a listener for the log. + * + * @param listener The listener to remove. + */ + public void removeListener(LogListener listener) + { + m_LogListeners.remove(listener); + } + + /** + * Get the instance (singleton) + * + * @return This class. + */ + public static Logger getInstance() + { + return m_Instance; + } + +} Modified: trunk/src/net/sensiva/rochat/ui/main/MainFrame.java =================================================================== --- trunk/src/net/sensiva/rochat/ui/main/MainFrame.java 2007-10-16 13:21:24 UTC (rev 8) +++ trunk/src/net/sensiva/rochat/ui/main/MainFrame.java 2007-10-16 13:26:31 UTC (rev 9) @@ -37,6 +37,16 @@ */ public void initialize() { + try + { + UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); + } + catch (Exception e) + { + e.printStackTrace(); + } + + m_MainFrame = new JFrame("ROChat"); m_Tabs = new JTabbedPane(); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |