Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

HistoryWindow.java 3.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright (c) 2006-2015 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.addons.logging;
  23. import com.dmdirc.DMDircMBassador;
  24. import com.dmdirc.FrameContainer;
  25. import com.dmdirc.interfaces.Connection;
  26. import com.dmdirc.interfaces.WindowModel;
  27. import com.dmdirc.ui.core.components.WindowComponent;
  28. import com.dmdirc.ui.messages.BackBufferFactory;
  29. import com.dmdirc.util.io.ReverseFileReader;
  30. import com.google.common.annotations.VisibleForTesting;
  31. import java.io.IOException;
  32. import java.nio.file.Path;
  33. import java.util.Collections;
  34. import java.util.List;
  35. import java.util.Optional;
  36. import org.slf4j.Logger;
  37. import org.slf4j.LoggerFactory;
  38. import static com.dmdirc.util.LogUtils.USER_ERROR;
  39. /**
  40. * Displays an extended history of a window.
  41. */
  42. public class HistoryWindow extends FrameContainer {
  43. private static final Logger LOG = LoggerFactory.getLogger(HistoryWindow.class);
  44. private final Path logFile;
  45. private final int numLines;
  46. /**
  47. * Creates a new HistoryWindow.
  48. */
  49. public HistoryWindow(
  50. final String title,
  51. final Path logFile,
  52. final WindowModel parent,
  53. final DMDircMBassador eventBus,
  54. final BackBufferFactory backBufferFactory,
  55. final int numLines) {
  56. super(parent, "raw", title, title, parent.getConfigManager(), backBufferFactory,
  57. eventBus, Collections.singletonList(WindowComponent.TEXTAREA.getIdentifier()));
  58. this.logFile = logFile;
  59. this.numLines = numLines;
  60. initBackBuffer();
  61. outputLoggingBackBuffer(parent.getConfigManager().getOptionInt("ui", "frameBufferSize"));
  62. }
  63. @Override
  64. public Optional<Connection> getConnection() {
  65. return getParent().flatMap(WindowModel::getConnection);
  66. }
  67. @VisibleForTesting
  68. void outputLoggingBackBuffer(final int limit) {
  69. try (final ReverseFileReader reader = new ReverseFileReader(logFile)) {
  70. final List<String> lines = reader.getLines(Math.min(limit, numLines));
  71. Collections.reverse(lines);
  72. lines.forEach(l ->
  73. getEventBus().publishAsync(new HistoricalLineRestoredEvent(this, l)));
  74. } catch (IOException | SecurityException ex) {
  75. LOG.warn(USER_ERROR, "Unable to read log file.", ex);
  76. }
  77. }
  78. }