001/* ---------------------------------------------------------------------------- 002 The Kiwi Toolkit - A Java Class Library 003 Copyright (C) 1998-2004 Mark A. Lindner 004 005 This library is free software; you can redistribute it and/or 006 modify it under the terms of the GNU General Public License as 007 published by the Free Software Foundation; either version 2 of the 008 License, or (at your option) any later version. 009 010 This library is distributed in the hope that it will be useful, 011 but WITHOUT ANY WARRANTY; without even the implied warranty of 012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 General Public License for more details. 014 015 You should have received a copy of the GNU General Public License 016 along with this library; if not, write to the Free Software 017 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 018 02111-1307, USA. 019 020 The author may be contacted at: mark_a_lindner@yahoo.com 021 ---------------------------------------------------------------------------- 022 $Log: ConsoleAdapter.java,v $ 023 Revision 1.5 2004/05/12 19:16:28 markl 024 comment block updates 025 026 Revision 1.4 2003/01/19 09:50:23 markl 027 Replaced deprecated OutputLoop calls. 028 029 Revision 1.3 2001/03/12 09:27:53 markl 030 Source code and Javadoc cleanup. 031 032 Revision 1.2 1999/01/10 01:00:58 markl 033 added GPL header & RCS tag 034 ---------------------------------------------------------------------------- 035*/ 036 037package kiwi.ui; 038 039import java.io.*; 040 041import kiwi.io.*; 042import kiwi.util.*; 043 044/** Adapter for using a logging endpoint with the standard output stream. 045 * 046 * This class allows an arbitrary logging endpoint to be connected (via an 047 * <code>OutputLoop</code>) to the standard output stream, 048 * <code>System.out</code>. The class starts a separate thread which reads 049 * messages from the pipe and writes them to the logging endpoint. Message 050 * severity is specified using a message prefix; "warning:", "status:", 051 * "info:", or "error:". The default severity is <code>STATUS</code>. For 052 * example: 053 * <p> 054 * <code>System.out.println("info:Program started.");</code> 055 * <p> 056 * will log the message "Program started." as an <code>INFO</code> message. 057 * 058 * @see kiwi.ui.ConsoleFrame 059 * @see kiwi.io.OutputLoop 060 * 061 * @author Mark Lindner 062 */ 063 064public class ConsoleAdapter 065 { 066 private LoggingEndpoint log; 067 private OutputLoop pipe; 068 private BufferedReader reader; 069 private Thread thread; 070 071 /** Construct a new <code>ConsoleAdapter</code> for the specified logging 072 * endpoint. 073 * 074 * @param log The <code>LoggingEndpoint</code> to use. 075 * @exception java.io.IOException If the output loop could not be created. 076 */ 077 078 public ConsoleAdapter(LoggingEndpoint log) throws IOException 079 { 080 this.log = log; 081 082 pipe = new OutputLoop(); 083 pipe.setActive(true); 084 085 reader = new BufferedReader(new InputStreamReader(pipe.getInputStream())); 086 087 Runnable r = new Runnable() 088 { 089 public void run() 090 { 091 _run(); 092 } 093 }; 094 095 thread = new Thread(r); 096 thread.start(); 097 } 098 099 /* thread body */ 100 101 private void _run() 102 { 103 String s; 104 int type; 105 106 try 107 { 108 while((s = reader.readLine()) != null) 109 { 110 type = log.STATUS; 111 112 if(s.startsWith("status:")) 113 { 114 s = s.substring(7); 115 type = log.STATUS; 116 } 117 else if(s.startsWith("error:")) 118 { 119 s = s.substring(6); 120 type = log.ERROR; 121 } 122 else if(s.startsWith("info:")) 123 { 124 s = s.substring(5); 125 type = log.INFO; 126 } 127 else if(s.startsWith("warning:")) 128 { 129 s = s.substring(8); 130 type = log.WARNING; 131 } 132 133 log.logMessage(type, s); 134 } 135 } 136 catch(IOException ex) 137 { 138 log.close(); 139 } 140 } 141 142 } 143 144/* end of source file */