    1   /* SinkHandlerLogRecord
    2    *
    3    * Created Aug 9, 2005
    4    *
    5    * Copyright (C) 2005 Internet Archive.
    6    *
    7    * This file is part of the Heritrix web crawler (crawler.archive.org).
    8    *
    9    * Heritrix is free software; you can redistribute it and/or modify
    10   * it under the terms of the GNU Lesser Public License as published by
    11   * the Free Software Foundation; either version 2.1 of the License, or
    12   * any later version.
    13   *
    14   * Heritrix is distributed in the hope that it will be useful,
    15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    17   * GNU Lesser Public License for more details.
    18   *
    19   * You should have received a copy of the GNU Lesser Public License
    20   * along with Heritrix; if not, write to the Free Software
    21   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    22   */
    23
    24  package org.archive.io;
    25
    26  import java.io.StringWriter;
    27  import java.util.Date;
    28  import java.util.logging.Level;
    29  import java.util.logging.LogRecord;
    30
    31  import org.archive.crawler.framework.ToeThread;
    32
    33  /***
    34   * Version of LogRecord used by SinkHandler.
    35   * Adds being able to mark the LogRecord as already-read and timestamping time
    36   * of creation. Also adds a different {@link #toString()} implementation.
    37   * Delegates all other calls to the passed LogRecord.
    38   * @author stack
    39   * @version $Date: 2006-08-15 04:39:00 +0000 (Tue, 15 Aug 2006) $ $Version$
    40   */
    41  public class SinkHandlerLogRecord extends LogRecord {
    42      private static final long serialVersionUID = -7782942650334713560L;
    43      boolean read = false;
    44      private final LogRecord delegatee;
    45      private final Date creationTime = new Date();
    46      private static final int SHORT_MSG_LENGTH = 80;
    47
    48      protected SinkHandlerLogRecord() {
    49          this(null);
    50      }
    51
    52      public SinkHandlerLogRecord(final LogRecord record) {
    53          super(record.getLevel(), record.getMessage());
    54          // if available, append current processor name to message
    55          // [ 1108006 ] alerts should show current processor
    56          // http://sourceforge.net/tracker/index.php?func=detail&aid=1108006&group_id=73833&atid=539102
    57          if(Thread.currentThread() instanceof ToeThread) {
    58              String newMessage = this.getMessage();
    59              ToeThread tt = (ToeThread) Thread.currentThread();
    60              newMessage = newMessage + " (in thread '"+tt.getName()+"'";
    61              if(tt.getCurrentProcessorName().length()>0) {
    62                  newMessage = newMessage + "; in processor '"
    63                      +tt.getCurrentProcessorName() + "'";
    64              }
    65              newMessage = newMessage + ")";
    66              this.setMessage(newMessage);
    67          }
    68          this.delegatee = record;
    69      }
    70
    71      public boolean equals(final long id) {
    72          return id == getSequenceNumber();
    73      }
    74
    75      public boolean equals(final SinkHandlerLogRecord compare) {
    76          return equals(compare.getSequenceNumber());
    77      }
    78
    79      public boolean isRead() {
    80          return this.read;
    81      }
    82
    83      /***
    84       * Mark alert as seen (That is, isNew() no longer returns true).
    85       */
    86      public void setRead() {
    87          this.read = true;
    88      }
    89
    90      /***
    91       * @return Time of creation
    92       */
    93      public Date getCreationTime() {
    94          return this.creationTime;
    95      }
    96
    97      public Level getLevel() {
    98          return this.delegatee.getLevel();
    99      }
    100
    101     public String getLoggerName() {
    102         return this.delegatee.getLoggerName();
    103     }
    104
    105     public String getShortMessage() {
    106         String msg = getMessage();
    107         return msg == null || msg.length() < SHORT_MSG_LENGTH?
    108                 msg: msg.substring(0, SHORT_MSG_LENGTH) + "...";
    109     }
    110
    111     public Throwable getThrown() {
    112         return this.delegatee.getThrown();
    113     }
    114
    115     public String getThrownToString() {
    116         StringWriter sw = new StringWriter();
    117         Throwable t = getThrown();
    118         if (t == null) {
    119             sw.write("No associated exception.");
    120         } else {
    121             String tStr = t.toString();
    122             sw.write(tStr);
    123             if (t.getMessage() != null && t.getMessage().length() > 0 &&
    124                     !tStr.endsWith(t.getMessage())) {
    125                 sw.write("\nMessage: ");
    126                 sw.write(t.getMessage());
    127             }
    128             if (t.getCause() != null) {
    129                 sw.write("\nCause: ");
    130                 t.getCause().printStackTrace(new java.io.PrintWriter(sw));
    131             }
    132             sw.write("\nStacktrace: ");
    133             t.printStackTrace(new java.io.PrintWriter(sw));
    134         }
    135         return sw.toString();
    136     }
    137
    138     public String toString() {
    139         StringWriter sw = new StringWriter();
    140         sw.write(getLevel().toString());
    141         sw.write(" ");
    142         sw.write(getMessage());
    143         sw.write(getThrownToString());
    144         return sw.toString();
    145     }
    146 }