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 org.apache.log4j.EnhancedPatternLayout;
    14  import org.apache.log4j.Layout;
    15  import org.apache.log4j.spi.LoggingEvent;
    16
    17  import javax.swing.*;
    18  import java.awt.*;
    19  import java.awt.datatransfer.StringSelection;
    20  import java.awt.event.ActionEvent;
    21  import java.util.List;
    22
    23  public class CopyEventsToClipboardAction extends AbstractAction {
    24
    25      private static final long serialVersionUID = 1L;
    26      private static final int EVENTSIZE_FUDGE_FACTOR = 128; // guestimate 128 chars per event
    27      private final LogUI logUi;
    28
    29      /**
    30       * Layout pattern uses a simple but concise format that reads well and has a fixed size set of
    31       * useful columns before the message. Nice format for pasting into issue trackers.
    32       */
    33      private final Layout layout = new EnhancedPatternLayout(
    34          "[%d{ISO8601} %-5p][%20.20c][%t] %m%n");
    35
    36      public CopyEventsToClipboardAction(LogUI parent) {
    37          super("Copy events to clipboard");
    38          this.logUi = parent;
    39          layout.activateOptions();
    40
    41          putValue(Action.SHORT_DESCRIPTION,
    42              "Copies to the clipboard currently visible events to a human-readable, log-like format");
    43
    44      }
    45
    46
    47      public void actionPerformed(ActionEvent e) {
    48          List filteredEvents = logUi.getCurrentLogPanel().getFilteredEvents();
    49          StringBuilder writer = new StringBuilder(filteredEvents.size() * EVENTSIZE_FUDGE_FACTOR);
    50          for (Object filteredEvent : filteredEvents) {
    51              LoggingEvent event = ((LoggingEventWrapper) filteredEvent).getLoggingEvent();
    52              writer.append(layout.format(event));
    53          }
    54
    55          StringSelection stringSelection = new StringSelection(writer.toString());
    56          Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection,
    57              stringSelection);
    58      }
    59
    60  }