You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HistoryWindow.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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.FrameContainer;
  24. import com.dmdirc.interfaces.Connection;
  25. import com.dmdirc.interfaces.EventBus;
  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 Optional<Connection> connection;
  45. private final Path logFile;
  46. private final int numLines;
  47. /**
  48. * Creates a new HistoryWindow.
  49. */
  50. public HistoryWindow(
  51. final String title,
  52. final Path logFile,
  53. final WindowModel parent,
  54. final EventBus eventBus,
  55. final BackBufferFactory backBufferFactory,
  56. final int numLines) {
  57. super("raw", title, title, parent.getConfigManager(), backBufferFactory,
  58. eventBus, Collections.singletonList(WindowComponent.TEXTAREA.getIdentifier()));
  59. this.logFile = logFile;
  60. this.numLines = numLines;
  61. this.connection = parent.getConnection();
  62. initBackBuffer();
  63. outputLoggingBackBuffer(parent.getConfigManager().getOptionInt("ui", "frameBufferSize"));
  64. }
  65. @Override
  66. public Optional<Connection> getConnection() {
  67. return connection;
  68. }
  69. @VisibleForTesting
  70. void outputLoggingBackBuffer(final int limit) {
  71. try (final ReverseFileReader reader = new ReverseFileReader(logFile)) {
  72. final List<String> lines = reader.getLines(Math.min(limit, numLines));
  73. Collections.reverse(lines);
  74. lines.forEach(l ->
  75. getEventBus().publishAsync(new HistoricalLineRestoredEvent(this, l)));
  76. } catch (IOException | SecurityException ex) {
  77. LOG.warn(USER_ERROR, "Unable to read log file.", ex);
  78. }
  79. }
  80. }