source: branches/2.2/jabberit_messenger/java_source/src/nu/fw/jeti/util/Log.java @ 3102

Revision 3102, 5.5 KB checked in by amuller, 14 years ago (diff)

Ticket #986 - Efetuado merge para o Branch 2.2( atualizacao do modulo)

  • Property svn:executable set to *
Line 
1/*
2 *      Jeti, a Java Jabber client, Copyright (C) 2001 E.S. de Boer 
3 *
4 *  This program is free software; you can redistribute it and/or modify
5 *  it under the terms of the GNU General Public License as published by
6 *  the Free Software Foundation; either version 2 of the License, or
7 *  (at your option) any later version.
8 *
9 *  This program is distributed in the hope that it will be useful,
10 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
11 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 *      GNU General Public License for more details.
13 *
14 *  You should have received a copy of the GNU General Public License
15 *  along with this program; if not, write to the Free Software
16 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17 *
18 *      For questions, comments etc,
19 *      use the website at http://jeti.jabberstudio.org
20 *  or mail me at eric@jeti.tk
21 */
22
23package nu.fw.jeti.util;
24import java.io.*;
25import java.text.DateFormat;
26import java.util.Date;
27import java.util.Iterator;
28import java.util.LinkedList;
29import java.util.List;
30
31import nu.fw.jeti.events.JavaErrorListener;
32import nu.fw.jeti.jabber.Backend;
33import nu.fw.jeti.jabber.elements.Packet;
34
35/**
36 * @author E.S. de Boer
37 */
38
39public class Log
40{
41        private static Date date = new Date();
42        private static DateFormat dateFormat = DateFormat.getTimeInstance();
43        private static LinkedList xml = new LinkedList();
44        private static LinkedList xmlErrors = new LinkedList();
45        private static volatile LinkedList errors = new LinkedList();
46        private static Backend backend;
47        private static PrintStream diskLog = null;
48       
49        public Log(Backend backend)
50        {
51                Log.backend= backend;
52                //pipe err stream to log screen
53
54                catchExceptions();
55                 if (Preferences.getBoolean("jeti","debugToFile",false)) {
56            String name = Preferences.getString("jeti","debugFile", "out.log");
57            try {
58                diskLog = new PrintStream(new FileOutputStream(name, true));
59            } catch (IOException e) {
60                System.err.println("Failed to open debug log '" + name + "': " + e);
61            }
62        }
63        }
64
65        private void catchExceptions()
66        {
67                try
68                {
69                        Thread t = new Thread()
70                        {
71                                PipedInputStream piNormal = new PipedInputStream();
72                                PipedOutputStream poNormal = new PipedOutputStream(piNormal);
73                                public void run()
74                                {
75                    try {
76                        System.setErr(new PrintStream(poNormal, true));
77                    } catch (Exception e) {System.out.println("Error logging setup failed");}
78                                        try
79                                        {
80                                                BufferedReader bin = new BufferedReader(new InputStreamReader(piNormal));
81                                                String output = null;
82                                                while ((output = bin.readLine()) != null)
83                                                {
84                                                        System.out.println(output);
85                                                        synchronized(errors)
86                                                        {
87                                                                errors.add(gettime() + " " + output);
88                                                        }
89//                                                      //do only once? ipv every line
90                                                        try
91                                                        {//try catch because exceptions thrown here cause deadlock
92                                                                for(Iterator j = backend.getListeners(JavaErrorListener.class);j.hasNext();)
93                                                                {//show error in ui
94                                                                        ((JavaErrorListener)j.next()).error();
95                                                                }
96                                                        }
97                                                        catch(Exception e)
98                                                        {
99                                                                System.out.println(e.getMessage());     
100                                                        }
101                                                }
102                                                bin.close();
103                                                piNormal.close();
104                                        }
105                                        catch (IOException ioe)
106                                        {
107                                                //write end dead then set system err again
108                                                try
109                                                {
110                                                        piNormal.close();
111                                                        poNormal.close();
112                                                }
113                                                catch (IOException e)
114                                                {
115                                                        System.out.println("foutje");
116                                                }
117                                                catchExceptions();   
118                                        }
119                                }
120                        };
121                        t.setDaemon(true);
122                        t.start();
123                }catch (IOException i){System.out.println("foutje");}
124        }
125       
126        private static String gettime()
127        {
128                date.setTime(System.currentTimeMillis());
129                return dateFormat.format(date);
130        }
131
132    private static void loggedAdd(LinkedList list, String line) {
133        list.add(line);
134        if (diskLog != null) {
135            diskLog.println(line);
136        }
137    }
138   
139    public static void clear()
140        {
141        synchronized(xml)
142                {
143                xml.clear();
144                }
145        }
146
147        //***********xml log*******************\\
148        public static void xmlPacket(Packet packet)
149        {
150                synchronized(xml)
151                {
152                        loggedAdd(xml, gettime() + " - p: " + packet);
153                }
154        }
155
156        public static void completeXML(StringBuffer message)
157        {
158                synchronized(xml)
159                {
160                        loggedAdd(xml, gettime() + " - c: " + message);
161                }
162        }
163
164        public static void sendXML(String message)
165        {
166                synchronized(xml)
167                {
168                        loggedAdd(xml, gettime() + " +  : " + message);
169                }
170        }
171
172        public static List getXML()
173        {
174                synchronized(xml)
175                {
176                        return (List) xml.clone();
177                }
178        }
179
180        //*********xml errors***********\\
181        public static void notParsedXML(String message)
182        {
183                synchronized(xmlErrors)
184                {
185                        xmlErrors.add(gettime() + " not parsed: " + message);
186                }
187        }
188
189        public static void xmlParseException(Exception e)
190        {
191                synchronized(xmlErrors)
192                {
193                        xmlErrors.add(gettime() + " " + e.getMessage());
194                }
195        }
196
197        public static void xmlReceivedError(String message)
198        {
199                synchronized(xmlErrors)
200                {
201                        xmlErrors.add(gettime() + " " + message);
202                }
203        }
204
205        public static List getXMLErrors()
206        {
207                synchronized(xmlErrors)
208                {
209                        return (List) xmlErrors.clone();
210                }
211        }
212
213        //***********errors***************\\
214        public static void notSend(String message)
215        {
216                synchronized(errors)
217                {
218                        errors.add(gettime() + " not Send " + message);
219                }
220        }
221
222        public static void error(String message)
223        {
224                synchronized(errors)
225                {
226                        errors.add(gettime() + " error " + message);
227                }
228        }
229
230        public static List getErrors()
231        {
232                synchronized(errors)
233                {
234                        return (List) errors.clone();
235                }
236        }
237}
238/*
239 * Overrides for emacs
240 * Local variables:
241 * tab-width: 4
242 * End:
243 */
Note: See TracBrowser for help on using the repository browser.