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.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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.events.UserErrorEvent;
  26. import com.dmdirc.interfaces.Connection;
  27. import com.dmdirc.logger.ErrorLevel;
  28. import com.dmdirc.ui.core.components.WindowComponent;
  29. import com.dmdirc.ui.messages.BackBufferFactory;
  30. import com.dmdirc.util.io.ReverseFileReader;
  31. import com.google.common.annotations.VisibleForTesting;
  32. import java.io.IOException;
  33. import java.nio.file.Path;
  34. import java.text.ParsePosition;
  35. import java.text.SimpleDateFormat;
  36. import java.util.Collections;
  37. import java.util.Date;
  38. import java.util.Optional;
  39. /**
  40. * Displays an extended history of a window.
  41. */
  42. public class HistoryWindow extends FrameContainer {
  43. private final Path logFile;
  44. private final DMDircMBassador eventBus;
  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 FrameContainer parent,
  53. final DMDircMBassador eventBus,
  54. final BackBufferFactory backBufferFactory,
  55. final int numLines) {
  56. super(parent, "raw", title, title, parent.getConfigManager(), backBufferFactory,
  57. eventBus,
  58. Collections.singletonList(WindowComponent.TEXTAREA.getIdentifier()));
  59. this.logFile = logFile;
  60. this.eventBus = eventBus;
  61. this.numLines = numLines;
  62. initBackBuffer();
  63. outputLoggingBackBuffer(parent.getConfigManager().getOptionInt("ui", "frameBufferSize"));
  64. }
  65. @Override
  66. public Optional<Connection> getConnection() {
  67. return getParent().flatMap(FrameContainer::getConnection);
  68. }
  69. @VisibleForTesting
  70. void outputLoggingBackBuffer(final int limit) {
  71. try (final ReverseFileReader reader = new ReverseFileReader(logFile)) {
  72. final Iterable<String> lines = reader.getLines(Math.min(limit, numLines));
  73. lines.forEach(l -> {
  74. final ParsePosition pos = new ParsePosition(0);
  75. final Date date = new SimpleDateFormat("[dd/MM/yyyy HH:mm:ss]").parse(l, pos);
  76. final String text = l.substring(pos.getIndex()+1);
  77. addLine(text, date);
  78. });
  79. } catch (IOException | SecurityException ex) {
  80. eventBus.publishAsync(
  81. new UserErrorEvent(ErrorLevel.MEDIUM, ex, "Unable to read log file.", ""));
  82. }
  83. }
  84. }