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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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.text.ParsePosition;
  34. import java.text.SimpleDateFormat;
  35. import java.util.Collections;
  36. import java.util.Date;
  37. import java.util.List;
  38. import java.util.Optional;
  39. import org.slf4j.Logger;
  40. import org.slf4j.LoggerFactory;
  41. import static com.dmdirc.util.LogUtils.USER_ERROR;
  42. /**
  43. * Displays an extended history of a window.
  44. */
  45. public class HistoryWindow extends FrameContainer {
  46. private static final Logger LOG = LoggerFactory.getLogger(HistoryWindow.class);
  47. private final Path logFile;
  48. private final int numLines;
  49. /**
  50. * Creates a new HistoryWindow.
  51. */
  52. public HistoryWindow(
  53. final String title,
  54. final Path logFile,
  55. final WindowModel parent,
  56. final DMDircMBassador eventBus,
  57. final BackBufferFactory backBufferFactory,
  58. final int numLines) {
  59. super(parent, "raw", title, title, parent.getConfigManager(), backBufferFactory,
  60. eventBus, Collections.singletonList(WindowComponent.TEXTAREA.getIdentifier()));
  61. this.logFile = logFile;
  62. this.numLines = numLines;
  63. initBackBuffer();
  64. outputLoggingBackBuffer(parent.getConfigManager().getOptionInt("ui", "frameBufferSize"));
  65. }
  66. @Override
  67. public Optional<Connection> getConnection() {
  68. return getParent().flatMap(WindowModel::getConnection);
  69. }
  70. @VisibleForTesting
  71. void outputLoggingBackBuffer(final int limit) {
  72. try (final ReverseFileReader reader = new ReverseFileReader(logFile)) {
  73. final List<String> lines = reader.getLines(Math.min(limit, numLines));
  74. Collections.reverse(lines);
  75. lines.forEach(l -> {
  76. final ParsePosition pos = new ParsePosition(0);
  77. final Date date = new SimpleDateFormat("[dd/MM/yyyy HH:mm:ss]").parse(l, pos);
  78. final String text = l.substring(pos.getIndex()+1);
  79. addLine(text, date);
  80. });
  81. } catch (IOException | SecurityException ex) {
  82. LOG.warn(USER_ERROR, "Unable to read log file.", ex);
  83. }
  84. }
  85. }