LogListenerImpl.java
001 /* This file is part of the project "Hilbert II" - http://www.qedeq.org
002  *
003  * Copyright 2000-2013,  Michael Meyling <mime@qedeq.org>.
004  *
005  * "Hilbert II" is free software; you can redistribute
006  * it and/or modify it under the terms of the GNU General Public
007  * License as published by the Free Software Foundation; either
008  * version 2 of the License, or (at your option) any later version.
009  *
010  * This program 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
013  * GNU General Public License for more details.
014  */
015 
016 package org.qedeq.kernel.bo.log;
017 
018 import java.io.PrintStream;
019 
020 import org.qedeq.base.utility.DateUtility;
021 
022 /**
023  * Listener that writes events to a stream.
024  *
025  @author  Michael Meyling
026  */
027 public final class LogListenerImpl implements LogListener {
028 
029     /** Stream for output. */
030     private PrintStream out = System.out;
031 
032     /** For this module we logged the last event. */
033     private String lastModuleUrl = "";
034 
035     /**
036      * Constructor.
037      */
038     public LogListenerImpl() {
039         this(System.out);
040     }
041 
042     /**
043      * Constructor.
044      *
045      @param   stream  Print to this stream.
046      */
047     public LogListenerImpl(final PrintStream stream) {
048         this.out = stream;
049     }
050 
051     /**
052      * Set output stream.
053      *
054      @param   stream  Output stream.
055      */
056     public final void setPrintStream(final PrintStream stream) {
057         this.out = stream;
058     }
059 
060     public final void logMessageState(final String text, final String url) {
061         if (!lastModuleUrl.equals(url)) {
062             out.println(url);
063             lastModuleUrl = (url != null ? url : "");
064         }
065         out.println(DateUtility.getTimestamp() " state:   " + text);
066     }
067 
068     public final void logFailureState(final String text, final String url,
069             final String description) {
070         if (!lastModuleUrl.equals(url)) {
071             out.println(url);
072             lastModuleUrl = (url != null ? url : "");
073         }
074         out.println(DateUtility.getTimestamp() " failure: " + text + "\n\t"
075             + description);
076     }
077 
078     public final void logSuccessfulState(final String text, final String url) {
079         if (!lastModuleUrl.equals(url)) {
080             out.println(url);
081             lastModuleUrl = (url != null ? url : "");
082         }
083         out.println(DateUtility.getTimestamp() " success: " + text);
084     }
085 
086     public void logRequest(final String text, final String url) {
087         if (!lastModuleUrl.equals(url)) {
088             out.println(url);
089             lastModuleUrl = (url != null ? url : "");
090         }
091         out.println(DateUtility.getTimestamp() " request: " + text);
092     }
093 
094     public final void logMessage(final String text) {
095         lastModuleUrl = "";
096         out.println(DateUtility.getTimestamp() " " + text);
097     }
098 
099 
100     public void logSuccessfulReply(final String text, final String url) {
101         if (!lastModuleUrl.equals(url)) {
102             out.println(url);
103             lastModuleUrl = (url != null ? url : "");
104         }
105         out.println(DateUtility.getTimestamp() " reply:   " + text);
106     }
107 
108     public void logFailureReply(final String text, final String url, final String description) {
109         if (!lastModuleUrl.equals(url)) {
110             out.println(url);
111             lastModuleUrl = (url != null ? url : "");
112         }
113         out.println(DateUtility.getTimestamp() " reply:   " + text + "\n\t" + description);
114     }
115 
116 }