| 1 | /* |
|---|
| 2 | * $Id$ |
|---|
| 3 | * |
|---|
| 4 | * Copyright 2006, The jCoderZ.org Project. All rights reserved. |
|---|
| 5 | * |
|---|
| 6 | * Redistribution and use in source and binary forms, with or without |
|---|
| 7 | * modification, are permitted provided that the following conditions are |
|---|
| 8 | * met: |
|---|
| 9 | * |
|---|
| 10 | * * Redistributions of source code must retain the above copyright |
|---|
| 11 | * notice, this list of conditions and the following disclaimer. |
|---|
| 12 | * * Redistributions in binary form must reproduce the above |
|---|
| 13 | * copyright notice, this list of conditions and the following |
|---|
| 14 | * disclaimer in the documentation and/or other materials |
|---|
| 15 | * provided with the distribution. |
|---|
| 16 | * * Neither the name of the jCoderZ.org Project nor the names of |
|---|
| 17 | * its contributors may be used to endorse or promote products |
|---|
| 18 | * derived from this software without specific prior written |
|---|
| 19 | * permission. |
|---|
| 20 | * |
|---|
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND |
|---|
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|---|
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|---|
| 24 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS |
|---|
| 25 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|---|
| 26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|---|
| 27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
|---|
| 28 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
|---|
| 29 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
|---|
| 30 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
|---|
| 31 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 32 | */ |
|---|
| 33 | package org.jcoderz.commons.logging; |
|---|
| 34 | |
|---|
| 35 | import java.io.PrintWriter; |
|---|
| 36 | |
|---|
| 37 | /** |
|---|
| 38 | * An instance of this formats and prints the output of the log viewer. |
|---|
| 39 | * Implementations of this differ in the format they use for printing the data. |
|---|
| 40 | * |
|---|
| 41 | */ |
|---|
| 42 | public abstract class LogPrinter |
|---|
| 43 | { |
|---|
| 44 | private DisplayOptions mDisplayOptions = new DisplayOptions(); |
|---|
| 45 | |
|---|
| 46 | /** |
|---|
| 47 | * Sets the options to use for formatting. |
|---|
| 48 | * |
|---|
| 49 | * @param options The display options to set. |
|---|
| 50 | * |
|---|
| 51 | * @see DisplayOptions |
|---|
| 52 | */ |
|---|
| 53 | public void setDisplayOptions (DisplayOptions options) |
|---|
| 54 | { |
|---|
| 55 | try |
|---|
| 56 | { |
|---|
| 57 | mDisplayOptions = (DisplayOptions) options.clone(); |
|---|
| 58 | } |
|---|
| 59 | catch (CloneNotSupportedException e) |
|---|
| 60 | { |
|---|
| 61 | throw new RuntimeException("Unexpected exception caught.", e); |
|---|
| 62 | } |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | /** |
|---|
| 66 | * Gets the display options. |
|---|
| 67 | * |
|---|
| 68 | * @return The configured display options. |
|---|
| 69 | */ |
|---|
| 70 | public final DisplayOptions getDisplayOptions () |
|---|
| 71 | { |
|---|
| 72 | return mDisplayOptions; |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | /** |
|---|
| 76 | * Formats the log data and prints the formatted data using the supplied |
|---|
| 77 | * print writer. A line feed is printed after the log data has been printed. |
|---|
| 78 | * |
|---|
| 79 | * @param printer The PrintWriter to use for printing the data. |
|---|
| 80 | * @param entry The log data to format and print using <code>printer</code>. |
|---|
| 81 | */ |
|---|
| 82 | public abstract void print (PrintWriter printer, LogItem entry); |
|---|
| 83 | |
|---|
| 84 | /** |
|---|
| 85 | * Gets information whether the stack trace of the supplied log file entry |
|---|
| 86 | * should be displayed. |
|---|
| 87 | * |
|---|
| 88 | * @param entry The entry to check. |
|---|
| 89 | * |
|---|
| 90 | * @return true if the stack trace of <code>entry</code> should be displayed; |
|---|
| 91 | * false, else. |
|---|
| 92 | * |
|---|
| 93 | */ |
|---|
| 94 | protected boolean displayStackTrace (final LogItem entry) |
|---|
| 95 | { |
|---|
| 96 | return ((entry.isExceptionItem() && mDisplayOptions.displayStackTrace()) |
|---|
| 97 | || mDisplayOptions.displayMessageStackTrace()); |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | /** |
|---|
| 102 | * Get the LogItem needed to complete the stack trace when a |
|---|
| 103 | * '...nnn more' line has been encountered. This depends on the display |
|---|
| 104 | * settings, since we do not want to display the complete stacktrace again |
|---|
| 105 | * when it has been displayed already. |
|---|
| 106 | * |
|---|
| 107 | * @param entry The entry with the '...nnn more' stack trace line. |
|---|
| 108 | * @param info The '...nnn more' stack trace line. |
|---|
| 109 | * |
|---|
| 110 | * @return <code>entry</code> if the complete stack trace has already been |
|---|
| 111 | * displayed; the entry with the complete stacktrace, else. |
|---|
| 112 | */ |
|---|
| 113 | protected LogItem getEntryForMoreStackTrace ( |
|---|
| 114 | final LogItem entry, |
|---|
| 115 | final StackTraceInfo info) |
|---|
| 116 | { |
|---|
| 117 | LogItem currentEntry = entry.getParentItem(); |
|---|
| 118 | LogItem rc = null; |
|---|
| 119 | while ((currentEntry != null) && (rc == null)) |
|---|
| 120 | { |
|---|
| 121 | if (displayStackTrace(currentEntry)) |
|---|
| 122 | { |
|---|
| 123 | rc = entry; |
|---|
| 124 | } |
|---|
| 125 | else if (currentEntry.getStackTraceLines().size() |
|---|
| 126 | >= info.getMoreLines()) |
|---|
| 127 | { |
|---|
| 128 | rc = currentEntry; |
|---|
| 129 | } |
|---|
| 130 | else |
|---|
| 131 | { |
|---|
| 132 | currentEntry = currentEntry.getParentItem(); |
|---|
| 133 | } |
|---|
| 134 | } |
|---|
| 135 | return rc; |
|---|
| 136 | } |
|---|
| 137 | } |
|---|