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

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