View Javadoc

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
    3    * agreements. See the NOTICE file distributed with this work for additional information regarding
    4    * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the
    5    * "License"); you may not use this file except in compliance with the License. You may obtain a
    6    * copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable
    7    * law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
    8    * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
    9    * for the specific language governing permissions and limitations under the License.
    10   */
    11  package org.apache.log4j.chainsaw;
    12
    13  import java.awt.Toolkit;
    14  import java.awt.datatransfer.StringSelection;
    15  import java.awt.event.ActionEvent;
    16  import java.util.Iterator;
    17  import java.util.List;
    18
    19  import javax.swing.AbstractAction;
    20  import javax.swing.Action;
    21
    22  import org.apache.log4j.EnhancedPatternLayout;
    23  import org.apache.log4j.Layout;
    24  import org.apache.log4j.spi.LoggingEvent;
    25
    26  public class CopyEventsToClipboardAction extends AbstractAction {
    27
    28      private static final long serialVersionUID = 1L;
    29      private static final int EVENTSIZE_FUDGE_FACTOR = 128; // guestimate 128 chars per event
    30      private final LogUI logUi;
    31
    32      /**
    33       * Layout pattern uses a simple but concise format that reads well and has a fixed size set of
    34       * useful columns before the message. Nice format for pasting into issue trackers.
    35       */
    36      private final Layout layout = new EnhancedPatternLayout(
    37              "[%d{ISO8601} %-5p][%20.20c][%t] %m%n");
    38
    39      public CopyEventsToClipboardAction(LogUI parent) {
    40          super("Copy events to clipboard");
    41          this.logUi = parent;
    42          layout.activateOptions();
    43
    44          putValue(Action.SHORT_DESCRIPTION,
    45                  "Copies to the clipboard currently visible events to a human-readable, log-like format");
    46
    47      }
    48
    49
    50      public void actionPerformed(ActionEvent e) {
    51          List filteredEvents = logUi.getCurrentLogPanel().getFilteredEvents();
    52          StringBuffer writer = new StringBuffer(filteredEvents.size() * EVENTSIZE_FUDGE_FACTOR);
    53          for (Iterator iterator = filteredEvents.iterator(); iterator.hasNext();) {
    54              LoggingEvent event = ((LoggingEventWrapper) iterator.next()).getLoggingEvent();
    55              writer.append(layout.format(event));
    56          }
    57
    58          StringSelection stringSelection = new StringSelection(writer.toString());
    59          Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection,
    60                  stringSelection);
    61      }
    62
    63  }