Browse Source

Merge pull request #340 from greboid/dev4

Stop using addline without timestamp.
pull/341/head
Chris Smith 9 years ago
parent
commit
1a5a78ed6e

+ 25
- 0
logging/build.gradle View File

@@ -0,0 +1,25 @@
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
+
23
+dependencies {
24
+    testCompile group: 'com.google.jimfs', name: 'jimfs', version: '1.0'
25
+}

+ 0
- 1
logging/plugin.config View File

@@ -37,7 +37,6 @@ defaults:
37 37
   general.networkfolders=true
38 38
   advanced.filenamehash=false
39 39
   general.addtime=true
40
-  general.timestamp=[dd/MM/yyyy HH:mm:ss]
41 40
   general.stripcodes=true
42 41
   general.channelmodeprefix=true
43 42
   backbuffer.autobackbuffer=true

+ 31
- 9
logging/src/com/dmdirc/addons/logging/HistoryWindow.java View File

@@ -31,9 +31,15 @@ import com.dmdirc.ui.core.components.WindowComponent;
31 31
 import com.dmdirc.ui.messages.BackBufferFactory;
32 32
 import com.dmdirc.util.io.ReverseFileReader;
33 33
 
34
+import com.google.common.annotations.VisibleForTesting;
35
+
34 36
 import java.io.IOException;
35 37
 import java.nio.file.Path;
38
+import java.text.ParsePosition;
39
+import java.text.SimpleDateFormat;
36 40
 import java.util.Collections;
41
+import java.util.Date;
42
+import java.util.List;
37 43
 import java.util.Optional;
38 44
 
39 45
 /**
@@ -41,6 +47,10 @@ import java.util.Optional;
41 47
  */
42 48
 public class HistoryWindow extends FrameContainer {
43 49
 
50
+    private final Path logFile;
51
+    private final DMDircMBassador eventBus;
52
+    private final int numLines;
53
+
44 54
     /**
45 55
      * Creates a new HistoryWindow.
46 56
      */
@@ -54,17 +64,12 @@ public class HistoryWindow extends FrameContainer {
54 64
         super(parent, "raw", title, title, parent.getConfigManager(), backBufferFactory,
55 65
                 eventBus,
56 66
                 Collections.singletonList(WindowComponent.TEXTAREA.getIdentifier()));
67
+        this.logFile = logFile;
68
+        this.eventBus = eventBus;
69
+        this.numLines = numLines;
57 70
 
58 71
         initBackBuffer();
59
-        final int frameBufferSize = parent.getConfigManager().getOptionInt(
60
-                "ui", "frameBufferSize");
61
-        try (final ReverseFileReader reader = new ReverseFileReader(logFile)) {
62
-            addLine(reader.getLinesAsString(Math.min(frameBufferSize, numLines)), false);
63
-        } catch (IOException | SecurityException ex) {
64
-            eventBus.publishAsync(
65
-                    new UserErrorEvent(ErrorLevel.MEDIUM, ex, "Unable to read log file.", ""));
66
-        }
67
-
72
+        outputLoggingBackBuffer(parent.getConfigManager().getOptionInt("ui", "frameBufferSize"));
68 73
     }
69 74
 
70 75
     @Override
@@ -72,4 +77,21 @@ public class HistoryWindow extends FrameContainer {
72 77
         return getParent().flatMap(FrameContainer::getConnection);
73 78
     }
74 79
 
80
+    @VisibleForTesting
81
+    void outputLoggingBackBuffer(final int limit) {
82
+        try (final ReverseFileReader reader = new ReverseFileReader(logFile)) {
83
+            final List<String> lines = reader.getLines(Math.min(limit, numLines));
84
+            Collections.reverse(lines);
85
+            lines.forEach(l -> {
86
+                final ParsePosition pos = new ParsePosition(0);
87
+                final Date date = new SimpleDateFormat("[dd/MM/yyyy HH:mm:ss]").parse(l, pos);
88
+                final String text = l.substring(pos.getIndex()+1);
89
+                addLine(text, date);
90
+            });
91
+        } catch (IOException | SecurityException ex) {
92
+            eventBus.publishAsync(
93
+                    new UserErrorEvent(ErrorLevel.MEDIUM, ex, "Unable to read log file.", ""));
94
+        }
95
+    }
96
+
75 97
 }

+ 2
- 21
logging/src/com/dmdirc/addons/logging/LoggingManager.java View File

@@ -102,6 +102,7 @@ public class LoggingManager implements ConfigChangeListener {
102 102
             "EEEE MMMM dd, yyyy - HH:mm:ss");
103 103
     /** Object for synchronising access to the date forma.t */
104 104
     private static final Object FORMAT_LOCK = new Object();
105
+    private static final DateFormat LOG_FORMAT = new SimpleDateFormat("[dd/MM/yyyy HH:mm:ss]");
105 106
     /** This plugin's plugin info. */
106 107
     private final String domain;
107 108
     private final PluginInfo pluginInfo;
@@ -124,8 +125,6 @@ public class LoggingManager implements ConfigChangeListener {
124 125
     private boolean channelmodeprefix;
125 126
     private boolean autobackbuffer;
126 127
     private boolean backbufferTimestamp;
127
-    /** Cached string settings. */
128
-    private String timestamp;
129 128
     private String colour;
130 129
     /** Cached int settings. */
131 130
     private int historyLines;
@@ -489,20 +488,7 @@ public class LoggingManager implements ConfigChangeListener {
489 488
         final StringBuilder finalLine = new StringBuilder();
490 489
 
491 490
         if (addtime) {
492
-            String dateString;
493
-            try {
494
-                final DateFormat dateFormat = new SimpleDateFormat(timestamp);
495
-                dateString = dateFormat.format(new Date()).trim();
496
-            } catch (IllegalArgumentException iae) {
497
-                // Default to known good format
498
-                final DateFormat dateFormat = new SimpleDateFormat("[dd/MM/yyyy HH:mm:ss]");
499
-                dateString = dateFormat.format(new Date()).trim();
500
-
501
-                eventBus.publishAsync(new UserErrorEvent(ErrorLevel.LOW, iae,
502
-                        "Dateformat String '" + timestamp + "' is invalid. For more information: "
503
-                        + "http://java.sun.com/javase/6/docs/api/java/text/SimpleDateFormat.html",
504
-                        ""));
505
-            }
491
+            final String dateString = LOG_FORMAT.format(new Date()).trim();
506 492
             finalLine.append(dateString);
507 493
             finalLine.append(' ');
508 494
         }
@@ -608,7 +594,6 @@ public class LoggingManager implements ConfigChangeListener {
608 594
         channelmodeprefix = config.getOptionBool(domain, "general.channelmodeprefix");
609 595
         autobackbuffer = config.getOptionBool(domain, "backbuffer.autobackbuffer");
610 596
         backbufferTimestamp = config.getOptionBool(domain, "backbuffer.timestamp");
611
-        timestamp = config.getOption(domain, "general.timestamp");
612 597
         historyLines = config.getOptionInt(domain, "history.lines");
613 598
         colour = config.getOption(domain, "backbuffer.colour");
614 599
         backbufferLines = config.getOptionInt(domain, "backbuffer.lines");
@@ -638,10 +623,6 @@ public class LoggingManager implements ConfigChangeListener {
638 623
                 pluginInfo.getDomain(), "general.addtime", "Timestamp logs",
639 624
                 "Should a timestamp be added to the log files?",
640 625
                 manager.getConfigManager(), manager.getIdentity()));
641
-        general.addSetting(new PreferencesSetting(PreferencesType.TEXT,
642
-                pluginInfo.getDomain(), "general.timestamp", "Timestamp format",
643
-                "The String to pass to 'SimpleDateFormat' to format the timestamp",
644
-                manager.getConfigManager(), manager.getIdentity()));
645 626
         general.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
646 627
                 pluginInfo.getDomain(), "general.stripcodes", "Strip Control Codes",
647 628
                 "Remove known irc control codes from lines before saving?",

+ 5
- 0
logging/test-res/com/dmdirc/addons/logging/logfile.txt View File

@@ -0,0 +1,5 @@
1
+[21/12/2015 12:57:01] RAR
2
+[21/12/2015 12:58:02] RAAR
3
+[21/12/2015 12:59:03] RAAAR
4
+[21/12/2015 13:00:04] RAAAAR
5
+[21/12/2015 13:01:05] RAAAAAR

+ 99
- 0
logging/test/com/dmdirc/addons/logging/HistoryWindowTest.java View File

@@ -0,0 +1,99 @@
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
+
23
+package com.dmdirc.addons.logging;
24
+
25
+import com.dmdirc.DMDircMBassador;
26
+import com.dmdirc.FrameContainer;
27
+import com.dmdirc.config.ConfigBinder;
28
+import com.dmdirc.events.DisplayPropertyMap;
29
+import com.dmdirc.interfaces.Connection;
30
+import com.dmdirc.interfaces.config.AggregateConfigProvider;
31
+import com.dmdirc.ui.messages.BackBuffer;
32
+import com.dmdirc.ui.messages.BackBufferFactory;
33
+import com.dmdirc.ui.messages.IRCDocument;
34
+
35
+import java.nio.file.Paths;
36
+import java.text.SimpleDateFormat;
37
+import java.util.Optional;
38
+
39
+import org.junit.Before;
40
+import org.junit.Test;
41
+import org.junit.runner.RunWith;
42
+import org.mockito.InOrder;
43
+import org.mockito.Mock;
44
+import org.mockito.runners.MockitoJUnitRunner;
45
+
46
+import static junit.framework.TestCase.assertEquals;
47
+import static org.mockito.Matchers.any;
48
+import static org.mockito.Matchers.eq;
49
+import static org.mockito.Mockito.inOrder;
50
+import static org.mockito.Mockito.when;
51
+@RunWith(MockitoJUnitRunner.class)
52
+public class HistoryWindowTest {
53
+
54
+    @Mock private IRCDocument document;
55
+    @Mock private BackBuffer backBuffer;
56
+    @Mock private AggregateConfigProvider config;
57
+    @Mock private ConfigBinder configBinder;
58
+    @Mock private Connection connection;
59
+    @Mock private FrameContainer frameContainer;
60
+    @Mock private DMDircMBassador eventBus;
61
+    @Mock private BackBufferFactory backBufferFactory;
62
+    private HistoryWindow instance;
63
+
64
+    @Before
65
+    public void setUp() throws Exception {
66
+        when(backBufferFactory.getBackBuffer(any())).thenReturn(backBuffer);
67
+        when(backBuffer.getDocument()).thenReturn(document);
68
+        when(config.getBinder()).thenReturn(configBinder);
69
+        when(frameContainer.getConfigManager()).thenReturn(config);
70
+        when(frameContainer.getConnection()).thenReturn(Optional.of(connection));
71
+        instance = new HistoryWindow("Awesome",
72
+                Paths.get(getClass().getResource("logfile.txt").toURI()),
73
+                frameContainer, eventBus, backBufferFactory, 4);
74
+    }
75
+
76
+    @Test
77
+    public void testGetConnection() throws Exception {
78
+        assertEquals(Optional.of(connection), instance.getConnection());
79
+    }
80
+
81
+    @Test
82
+    public void testOutputLoggingBackBuffer() throws Exception {
83
+        instance.outputLoggingBackBuffer(4);
84
+        final InOrder inOrder = inOrder(document);
85
+        inOrder.verify(document).addText(eq(new SimpleDateFormat("[dd/MM/yyyy HH:mm:ss]")
86
+                .parse("[21/12/2015 12:58:02]").getTime()), eq
87
+                (DisplayPropertyMap.EMPTY), eq("RAAR"));
88
+        inOrder.verify(document).addText(eq(new SimpleDateFormat("[dd/MM/yyyy HH:mm:ss]")
89
+                .parse("[21/12/2015 12:59:03]").getTime()), eq
90
+                (DisplayPropertyMap.EMPTY), eq("RAAAR"));
91
+        inOrder.verify(document).addText(eq(new SimpleDateFormat("[dd/MM/yyyy HH:mm:ss]")
92
+                .parse("[21/12/2015 13:00:04]").getTime()), eq
93
+                (DisplayPropertyMap.EMPTY), eq("RAAAAR"));
94
+        inOrder.verify(document).addText(eq(new SimpleDateFormat("[dd/MM/yyyy HH:mm:ss]")
95
+                .parse("[21/12/2015 13:01:05]").getTime()), eq
96
+                (DisplayPropertyMap.EMPTY), eq("RAAAAAR"));
97
+        inOrder.verifyNoMoreInteractions();
98
+    }
99
+}

+ 3
- 2
parserdebug/src/com/dmdirc/addons/parserdebug/DebugWindow.java View File

@@ -31,6 +31,7 @@ import com.dmdirc.ui.core.components.WindowComponent;
31 31
 import com.dmdirc.ui.messages.BackBufferFactory;
32 32
 
33 33
 import java.util.Arrays;
34
+import java.util.Date;
34 35
 import java.util.Optional;
35 36
 
36 37
 /**
@@ -73,8 +74,8 @@ public class DebugWindow extends FrameContainer {
73 74
      * Set the parser to null to stop us holding onto parsers when the server connection is closed.
74 75
      */
75 76
     public void unsetParser() {
76
-        addLine("======================", true);
77
-        addLine("Unset parser: " + parser, true);
77
+        addLine("======================", new Date());
78
+        addLine("Unset parser: " + parser, new Date());
78 79
         parser = null;
79 80
     }
80 81
 

+ 7
- 7
parserdebug/src/com/dmdirc/addons/parserdebug/ParserDebugManager.java View File

@@ -102,9 +102,9 @@ public class ParserDebugManager implements DebugInfoListener {
102 102
                     connection, eventBus, backBufferFactory);
103 103
             windowManager.addWindow(connection.getWindowModel(), window);
104 104
             registeredParsers.put(parser, window);
105
-            window.addLine("======================", true);
106
-            window.addLine("Started Monitoring: " + parser, true);
107
-            window.addLine("======================", true);
105
+            window.addLine("======================", new Date());
106
+            window.addLine("Started Monitoring: " + parser, new Date());
107
+            window.addLine("======================", new Date());
108 108
             return true;
109 109
         } catch (CallbackNotFoundException ex) {
110 110
             return false;
@@ -123,9 +123,9 @@ public class ParserDebugManager implements DebugInfoListener {
123 123
         try {
124 124
             parser.getCallbackManager().delCallback(DebugInfoListener.class, this);
125 125
             final DebugWindow window = registeredParsers.get(parser);
126
-            window.addLine("======================", true);
127
-            window.addLine("No Longer Monitoring: " + parser + " (User Requested)", true);
128
-            window.addLine("======================", true);
126
+            window.addLine("======================", new Date());
127
+            window.addLine("No Longer Monitoring: " + parser + " (User Requested)", new Date());
128
+            window.addLine("======================", new Date());
129 129
             if (close) {
130 130
                 window.close();
131 131
             }
@@ -159,7 +159,7 @@ public class ParserDebugManager implements DebugInfoListener {
159 159
     public void onDebugInfo(final Parser parser, final Date date, final int level, final String data) {
160 160
         final DebugWindow window = registeredParsers.get(parser);
161 161
         if (window != null) {
162
-            window.addLine(String.format("[%d] %s%n", level, data), true);
162
+            window.addLine(String.format("[%d] %s%n", level, data), new Date());
163 163
         }
164 164
     }
165 165
 

Loading…
Cancel
Save