|
@@ -36,6 +36,7 @@ import com.dmdirc.config.prefs.PreferencesManager;
|
36
|
36
|
import com.dmdirc.config.prefs.PreferencesSetting;
|
37
|
37
|
import com.dmdirc.config.prefs.PreferencesType;
|
38
|
38
|
import com.dmdirc.interfaces.ActionListener;
|
|
39
|
+import com.dmdirc.interfaces.ConfigChangeListener;
|
39
|
40
|
import com.dmdirc.logger.ErrorLevel;
|
40
|
41
|
import com.dmdirc.logger.Logger;
|
41
|
42
|
import com.dmdirc.parser.interfaces.ChannelClientInfo;
|
|
@@ -71,18 +72,33 @@ import java.util.TimerTask;
|
71
|
72
|
*
|
72
|
73
|
* @author Shane 'Dataforce' McCormack
|
73
|
74
|
*/
|
74
|
|
-public class LoggingPlugin extends Plugin implements ActionListener {
|
|
75
|
+public class LoggingPlugin extends Plugin implements ActionListener,
|
|
76
|
+ ConfigChangeListener {
|
75
|
77
|
|
76
|
78
|
/** The command we registered. */
|
77
|
79
|
private LoggingCommand command;
|
|
80
|
+ /** Cached boolean settings. */
|
|
81
|
+ private boolean networkfolders, filenamehash, addtime, stripcodes,
|
|
82
|
+ channelmodeprefix, autobackbuffer, backbufferTimestamp, usedate;
|
|
83
|
+ /** Cached string settings. */
|
|
84
|
+ private String timestamp, usedateformat, logDirectory, colour;
|
|
85
|
+ /** Cached int settings. */
|
|
86
|
+ private int historyLines, backbufferLines;
|
78
|
87
|
|
79
|
88
|
/** Open File */
|
80
|
89
|
protected class OpenFile {
|
81
|
90
|
|
|
91
|
+ /** Last used time. */
|
82
|
92
|
public long lastUsedTime = System.currentTimeMillis();
|
83
|
93
|
|
|
94
|
+ /** Open file's writer. */
|
84
|
95
|
public BufferedWriter writer = null;
|
85
|
96
|
|
|
97
|
+ /**
|
|
98
|
+ * Creates a new open file.
|
|
99
|
+ *
|
|
100
|
+ * @param writer Writer that has file open
|
|
101
|
+ */
|
86
|
102
|
public OpenFile(final BufferedWriter writer) {
|
87
|
103
|
this.writer = writer;
|
88
|
104
|
}
|
|
@@ -117,7 +133,7 @@ public class LoggingPlugin extends Plugin implements ActionListener {
|
117
|
133
|
*/
|
118
|
134
|
@Override
|
119
|
135
|
public void onLoad() {
|
120
|
|
- final File dir = new File(IdentityManager.getGlobalConfig().getOption(getDomain(), "general.directory"));
|
|
136
|
+ final File dir = new File(logDirectory);
|
121
|
137
|
if (dir.exists()) {
|
122
|
138
|
if (!dir.isDirectory()) {
|
123
|
139
|
Logger.userError(ErrorLevel.LOW, "Unable to create logging dir (file exists instead)");
|
|
@@ -128,6 +144,9 @@ public class LoggingPlugin extends Plugin implements ActionListener {
|
128
|
144
|
}
|
129
|
145
|
}
|
130
|
146
|
|
|
147
|
+ setCachedSettings();
|
|
148
|
+ IdentityManager.getGlobalConfig().addChangeListener(getDomain(), this);
|
|
149
|
+
|
131
|
150
|
command = new LoggingCommand();
|
132
|
151
|
ActionManager.addListener(this,
|
133
|
152
|
CoreActionType.CHANNEL_OPENED,
|
|
@@ -191,8 +210,10 @@ public class LoggingPlugin extends Plugin implements ActionListener {
|
191
|
210
|
*/
|
192
|
211
|
@Override
|
193
|
212
|
public void onUnload() {
|
194
|
|
- idleFileTimer.cancel();
|
195
|
|
- idleFileTimer.purge();
|
|
213
|
+ if (idleFileTimer != null) {
|
|
214
|
+ idleFileTimer.cancel();
|
|
215
|
+ idleFileTimer.purge();
|
|
216
|
+ }
|
196
|
217
|
|
197
|
218
|
CommandManager.unregisterCommand(command);
|
198
|
219
|
ActionManager.removeListener(this);
|
|
@@ -258,7 +279,7 @@ public class LoggingPlugin extends Plugin implements ActionListener {
|
258
|
279
|
|
259
|
280
|
if (parser == null) {
|
260
|
281
|
// Without a parser object, we might not be able to find the file to log this to.
|
261
|
|
- if (IdentityManager.getGlobalConfig().getOptionBool(getDomain(), "general.networkfolders")) {
|
|
282
|
+ if (networkfolders) {
|
262
|
283
|
// We *wont* be able to, so rather than logging to an incorrect file we just won't log.
|
263
|
284
|
return;
|
264
|
285
|
}
|
|
@@ -271,7 +292,7 @@ public class LoggingPlugin extends Plugin implements ActionListener {
|
271
|
292
|
|
272
|
293
|
switch (type) {
|
273
|
294
|
case QUERY_OPENED:
|
274
|
|
- if (IdentityManager.getGlobalConfig().getOptionBool(getDomain(), "backbuffer.autobackbuffer")) {
|
|
295
|
+ if (autobackbuffer) {
|
275
|
296
|
showBackBuffer(query.getFrame(), filename);
|
276
|
297
|
}
|
277
|
298
|
|
|
@@ -326,7 +347,7 @@ public class LoggingPlugin extends Plugin implements ActionListener {
|
326
|
347
|
|
327
|
348
|
switch (type) {
|
328
|
349
|
case CHANNEL_OPENED:
|
329
|
|
- if (IdentityManager.getGlobalConfig().getOptionBool(getDomain(), "backbuffer.autobackbuffer")) {
|
|
350
|
+ if (autobackbuffer) {
|
330
|
351
|
showBackBuffer(chan.getFrame(), filename);
|
331
|
352
|
}
|
332
|
353
|
|
|
@@ -449,6 +470,12 @@ public class LoggingPlugin extends Plugin implements ActionListener {
|
449
|
470
|
}
|
450
|
471
|
}
|
451
|
472
|
|
|
473
|
+ /** {@inheritDoc} */
|
|
474
|
+ @Override
|
|
475
|
+ public void configChanged(String domain, String key) {
|
|
476
|
+ setCachedSettings();
|
|
477
|
+ }
|
|
478
|
+
|
452
|
479
|
/**
|
453
|
480
|
* Add a backbuffer to a frame.
|
454
|
481
|
*
|
|
@@ -456,9 +483,6 @@ public class LoggingPlugin extends Plugin implements ActionListener {
|
456
|
483
|
* @param filename File to get backbuffer from
|
457
|
484
|
*/
|
458
|
485
|
protected void showBackBuffer(final Window frame, final String filename) {
|
459
|
|
- final int numLines = IdentityManager.getGlobalConfig().getOptionInt(getDomain(), "backbuffer.lines");
|
460
|
|
- final String colour = IdentityManager.getGlobalConfig().getOption(getDomain(), "backbuffer.colour");
|
461
|
|
- final boolean showTimestamp = IdentityManager.getGlobalConfig().getOptionBool(getDomain(), "backbuffer.timestamp");
|
462
|
486
|
if (frame == null) {
|
463
|
487
|
Logger.userError(ErrorLevel.LOW, "Given a null frame");
|
464
|
488
|
return;
|
|
@@ -472,12 +496,12 @@ public class LoggingPlugin extends Plugin implements ActionListener {
|
472
|
496
|
// is returned by getLines. To counter this, we call getLines(1) and do
|
473
|
497
|
// nothing with the output.
|
474
|
498
|
file.getLines(1);
|
475
|
|
- final Stack<String> lines = file.getLines(numLines);
|
|
499
|
+ final Stack<String> lines = file.getLines(backbufferLines);
|
476
|
500
|
while (!lines.empty()) {
|
477
|
|
- frame.addLine(getColouredString(colour, lines.pop()), showTimestamp);
|
|
501
|
+ frame.addLine(getColouredString(colour, lines.pop()), backbufferTimestamp);
|
478
|
502
|
}
|
479
|
503
|
file.close();
|
480
|
|
- frame.addLine(getColouredString(colour, "--- End of backbuffer\n"), showTimestamp);
|
|
504
|
+ frame.addLine(getColouredString(colour, "--- End of backbuffer\n"), backbufferTimestamp);
|
481
|
505
|
} catch (FileNotFoundException e) {
|
482
|
506
|
Logger.userError(ErrorLevel.LOW, "Unable to show backbuffer (Filename: " + filename + "): " + e.getMessage());
|
483
|
507
|
} catch (IOException e) {
|
|
@@ -545,24 +569,23 @@ public class LoggingPlugin extends Plugin implements ActionListener {
|
545
|
569
|
protected boolean appendLine(final String filename, final String line) {
|
546
|
570
|
final StringBuffer finalLine = new StringBuffer();
|
547
|
571
|
|
548
|
|
- if (IdentityManager.getGlobalConfig().getOptionBool(getDomain(), "general.addtime")) {
|
|
572
|
+ if (addtime) {
|
549
|
573
|
String dateString;
|
550
|
|
- final String dateFormatString = IdentityManager.getGlobalConfig().getOption(getDomain(), "general.timestamp");
|
551
|
574
|
try {
|
552
|
|
- final DateFormat dateFormat = new SimpleDateFormat(dateFormatString);
|
|
575
|
+ final DateFormat dateFormat = new SimpleDateFormat(timestamp);
|
553
|
576
|
dateString = dateFormat.format(new Date()).trim();
|
554
|
577
|
} catch (IllegalArgumentException iae) {
|
555
|
578
|
// Default to known good format
|
556
|
579
|
final DateFormat dateFormat = new SimpleDateFormat("[dd/MM/yyyy HH:mm:ss]");
|
557
|
580
|
dateString = dateFormat.format(new Date()).trim();
|
558
|
581
|
|
559
|
|
- Logger.userError(ErrorLevel.LOW, "Dateformat String '" + dateFormatString + "' is invalid. For more information: http://java.sun.com/javase/6/docs/api/java/text/SimpleDateFormat.html");
|
|
582
|
+ Logger.userError(ErrorLevel.LOW, "Dateformat String '" + timestamp + "' is invalid. For more information: http://java.sun.com/javase/6/docs/api/java/text/SimpleDateFormat.html");
|
560
|
583
|
}
|
561
|
584
|
finalLine.append(dateString);
|
562
|
585
|
finalLine.append(" ");
|
563
|
586
|
}
|
564
|
587
|
|
565
|
|
- if (IdentityManager.getGlobalConfig().getOptionBool(getDomain(), "general.stripcodes")) {
|
|
588
|
+ if (stripcodes) {
|
566
|
589
|
finalLine.append(Styliser.stipControlCodes(line));
|
567
|
590
|
} else {
|
568
|
591
|
finalLine.append(line);
|
|
@@ -605,7 +628,7 @@ public class LoggingPlugin extends Plugin implements ActionListener {
|
605
|
628
|
final StringBuffer file = new StringBuffer();
|
606
|
629
|
String md5String = "";
|
607
|
630
|
|
608
|
|
- directory.append(IdentityManager.getGlobalConfig().getOption(getDomain(), "general.directory"));
|
|
631
|
+ directory.append(logDirectory);
|
609
|
632
|
if (directory.charAt(directory.length() - 1) != File.separatorChar) {
|
610
|
633
|
directory.append(File.separatorChar);
|
611
|
634
|
}
|
|
@@ -631,8 +654,8 @@ public class LoggingPlugin extends Plugin implements ActionListener {
|
631
|
654
|
md5String = obj.toString();
|
632
|
655
|
}
|
633
|
656
|
|
634
|
|
- if (IdentityManager.getGlobalConfig().getOptionBool(getDomain(), "advanced.usedate")) {
|
635
|
|
- final String dateFormat = IdentityManager.getGlobalConfig().getOption(getDomain(), "advanced.usedateformat");
|
|
657
|
+ if (usedate) {
|
|
658
|
+ final String dateFormat = usedateformat;
|
636
|
659
|
final String dateDir = (new SimpleDateFormat(dateFormat)).format(new Date());
|
637
|
660
|
directory.append(dateDir);
|
638
|
661
|
if (directory.charAt(directory.length() - 1) != File.separatorChar) {
|
|
@@ -644,7 +667,7 @@ public class LoggingPlugin extends Plugin implements ActionListener {
|
644
|
667
|
}
|
645
|
668
|
}
|
646
|
669
|
|
647
|
|
- if (IdentityManager.getGlobalConfig().getOptionBool(getDomain(), "advanced.filenamehash")) {
|
|
670
|
+ if (filenamehash) {
|
648
|
671
|
file.append('.');
|
649
|
672
|
file.append(md5(md5String));
|
650
|
673
|
}
|
|
@@ -663,7 +686,7 @@ public class LoggingPlugin extends Plugin implements ActionListener {
|
663
|
686
|
* @param networkName Name of network
|
664
|
687
|
*/
|
665
|
688
|
protected void addNetworkDir(final StringBuffer directory, final StringBuffer file, final String networkName) {
|
666
|
|
- if (!IdentityManager.getGlobalConfig().getOptionBool(getDomain(), "general.networkfolders")) {
|
|
689
|
+ if (!networkfolders) {
|
667
|
690
|
return;
|
668
|
691
|
}
|
669
|
692
|
|
|
@@ -761,14 +784,12 @@ public class LoggingPlugin extends Plugin implements ActionListener {
|
761
|
784
|
* @return name to display
|
762
|
785
|
*/
|
763
|
786
|
protected String getDisplayName(final ChannelClientInfo channelClient, final String overrideNick) {
|
764
|
|
- final boolean addModePrefix = (IdentityManager.getGlobalConfig().getOptionBool(getDomain(), "general.channelmodeprefix"));
|
765
|
|
-
|
766
|
787
|
if (channelClient == null) {
|
767
|
788
|
return (overrideNick.isEmpty()) ? "Unknown Client" : overrideNick;
|
768
|
789
|
} else if (overrideNick.isEmpty()) {
|
769
|
|
- return (addModePrefix) ? channelClient.toString() : channelClient.getClient().getNickname();
|
|
790
|
+ return channelmodeprefix ? channelClient.toString() : channelClient.getClient().getNickname();
|
770
|
791
|
} else {
|
771
|
|
- return (addModePrefix) ? channelClient.getImportantModePrefix() + overrideNick : overrideNick;
|
|
792
|
+ return channelmodeprefix ? channelClient.getImportantModePrefix() + overrideNick : overrideNick;
|
772
|
793
|
}
|
773
|
794
|
}
|
774
|
795
|
|
|
@@ -812,10 +833,27 @@ public class LoggingPlugin extends Plugin implements ActionListener {
|
812
|
833
|
return false;
|
813
|
834
|
}
|
814
|
835
|
|
815
|
|
- new HistoryWindow("History", reader, target,
|
816
|
|
- IdentityManager.getGlobalConfig().getOptionInt(getDomain(), "history.lines"));
|
|
836
|
+ new HistoryWindow("History", reader, target, historyLines);
|
817
|
837
|
|
818
|
838
|
return true;
|
819
|
839
|
}
|
820
|
840
|
|
|
841
|
+ /** Updates cached settings. */
|
|
842
|
+ public void setCachedSettings() {
|
|
843
|
+ networkfolders = IdentityManager.getGlobalConfig().getOptionBool(getDomain(), "general.networkfolders");
|
|
844
|
+ filenamehash = IdentityManager.getGlobalConfig().getOptionBool(getDomain(), "advanced.filenamehash");
|
|
845
|
+ addtime = IdentityManager.getGlobalConfig().getOptionBool(getDomain(), "general.addtime");
|
|
846
|
+ stripcodes = IdentityManager.getGlobalConfig().getOptionBool(getDomain(), "general.stripcodes");
|
|
847
|
+ channelmodeprefix = IdentityManager.getGlobalConfig().getOptionBool(getDomain(), "general.channelmodeprefix");
|
|
848
|
+ autobackbuffer = IdentityManager.getGlobalConfig().getOptionBool(getDomain(), "backbuffer.autobackbuffer");
|
|
849
|
+ backbufferTimestamp = IdentityManager.getGlobalConfig().getOptionBool(getDomain(), "backbuffer.timestamp");
|
|
850
|
+ usedate = IdentityManager.getGlobalConfig().getOptionBool(getDomain(), "advanced.usedate");
|
|
851
|
+ timestamp = IdentityManager.getGlobalConfig().getOption(getDomain(), "general.timestamp");
|
|
852
|
+ usedateformat = IdentityManager.getGlobalConfig().getOption(getDomain(), "advanced.usedateformat");
|
|
853
|
+ historyLines = IdentityManager.getGlobalConfig().getOptionInt(getDomain(), "history.lines");
|
|
854
|
+ colour = IdentityManager.getGlobalConfig().getOption(getDomain(), "backbuffer.colour");
|
|
855
|
+ backbufferLines = IdentityManager.getGlobalConfig().getOptionInt(getDomain(), "backbuffer.lines");
|
|
856
|
+ logDirectory = IdentityManager.getGlobalConfig().getOption(getDomain(), "general.directory");
|
|
857
|
+ }
|
|
858
|
+
|
821
|
859
|
}
|