KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > websphinx > EventLog


1 /*
2  * WebSphinx web-crawling toolkit
3  *
4  * Copyright (c) 1998-2002 Carnegie Mellon University. All rights
5  * reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in
16  * the documentation and/or other materials provided with the
17  * distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
20  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
23  * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  *
31  */

32
33 package websphinx;
34
35 import java.io.File JavaDoc;
36 import java.io.OutputStream JavaDoc;
37 import java.io.IOException JavaDoc;
38 import java.util.Date JavaDoc;
39 //#ifdef JDK1.1
40
import java.io.PrintWriter JavaDoc;
41 //#endif JDK1.1
42
/*#ifdef JDK1.0
43 import java.io.PrintStream;
44 #endif JDK1.0*/

45
46 /**
47  * Crawling monitor that writes messages to standard output or a file.
48  * Acts as both a CrawlListener (monitoring start and end of the crawl)
49  * and as a LinkListener (monitoring page retrieval).
50  */

51 public class EventLog implements CrawlListener, LinkListener {
52
53 //#ifdef JDK1.1
54
PrintWriter JavaDoc stream;
55 //#endif JDK1.1
56
/*#ifdef JDK1.0
57     PrintStream stream;
58 #endif JDK1.0*/

59     boolean onlyNetworkEvents = true;
60
61     /**
62      * Make a EventLog that writes to standard output.
63      */

64     public EventLog () {
65         this (System.out);
66     }
67
68     /**
69      * Make a EventLog that writes to a stream.
70      */

71     public EventLog (OutputStream JavaDoc out) {
72 /*#ifdef JDK1.0
73         stream = new PrintStream (out, true);
74 #endif JDK1.0*/

75 //#ifdef JDK1.1
76
stream = new PrintWriter JavaDoc (out, true);
77 //#endif JDK1.1
78
}
79
80     /**
81      * Make a EventLog that writes to a file. The file is overwritten.
82      * @param filename File to which crawling event messages are written
83      */

84     public EventLog (String JavaDoc filename) throws IOException JavaDoc {
85 /*#ifdef JDK1.0
86         stream = new PrintStream (Access.getAccess ().writeFile (new File(filename), false));
87 #endif JDK1.0*/

88 //#ifdef JDK1.1
89
stream = new PrintWriter JavaDoc (Access.getAccess ().writeFile (new File JavaDoc(filename), false));
90 //#endif JDK1.1
91
}
92
93     /**
94      * Set whether logger prints only network-related LinkEvents.
95      * If true, then the logger only prints LinkEvents where
96      * LinkEvent.isNetworkEvent() returns true. If false,
97      * then the logger prints all LinkEvents. Default is true.
98      * @param flag true iff only network LinkEvents should be logged
99      */

100     public void setOnlyNetworkEvents (boolean flag) {
101         onlyNetworkEvents = flag;
102     }
103     /**
104      * Test whether logger prints only network-related LinkEvents.
105      * If true, then the logger only prints LinkEvents where
106      * LinkEvent.isNetworkEvent() returns true. If false,
107      * then the logger prints all LinkEvents. Default is true.
108      * @return true iff only network LinkEvents are logged
109      */

110     public boolean getOnlyNetworkEvents () {
111         return onlyNetworkEvents;
112     }
113
114     /**
115      * Notify that the crawler started.
116      */

117     public void started (CrawlEvent event) {
118         stream.println (new Date JavaDoc() + ": *** started " + event.getCrawler());
119     }
120
121     /**
122      * Notify that the crawler has stopped.
123      */

124     public void stopped (CrawlEvent event) {
125         stream.println (new Date JavaDoc() + ": *** finished " + event.getCrawler());
126     }
127
128     /**
129      * Notify that the crawler's state was cleared.
130      */

131     public void cleared (CrawlEvent event) {
132         stream.println (new Date JavaDoc() + ": *** cleared " + event.getCrawler());
133     }
134
135     /**
136      * Notify that the crawler timed out.
137      */

138     public void timedOut (CrawlEvent event) {
139         stream.println (new Date JavaDoc() + ": *** timed out " + event.getCrawler());
140     }
141
142     /**
143      * Notify that the crawler paused.
144      */

145     public void paused (CrawlEvent event) {
146         stream.println (new Date JavaDoc() + ": *** paused " + event.getCrawler());
147     }
148
149     /**
150      * Notify that a link event occured.
151      */

152     public void crawled (LinkEvent event) {
153         switch (event.getID()) {
154           case LinkEvent.RETRIEVING:
155           case LinkEvent.DOWNLOADED:
156           case LinkEvent.VISITED:
157           case LinkEvent.ERROR:
158             break;
159           default:
160             if (onlyNetworkEvents)
161                 return;
162             break;
163         }
164         stream.println (new Date JavaDoc () + ": "
165                         + event);
166
167         Throwable JavaDoc exc = event.getException();
168         if (exc != null && ! (exc instanceof IOException JavaDoc))
169             exc.printStackTrace (stream);
170     }
171
172     /**
173      * Create a EventLog that prints to standard error and attach it to a crawler.
174      * This is a convenience method.
175      * @param crawler Crawler to be monitored
176      */

177     public static EventLog monitor (Crawler crawler) {
178         EventLog logger = new EventLog (System.err);
179         crawler.addCrawlListener (logger);
180         crawler.addLinkListener (logger);
181         return logger;
182     }
183 }
184
Popular Tags