Skip to content

Commit

Permalink
Add "show loggging" command to show the most recent log lines. (Also …
Browse files Browse the repository at this point in the history
…added logging output to "show system")
  • Loading branch information
ShaneMcC committed Aug 24, 2015
1 parent c9cde7e commit 20fc532
Show file tree
Hide file tree
Showing 8 changed files with 352 additions and 10 deletions.
35 changes: 32 additions & 3 deletions src/com/dfbnc/DFBnc.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@
import com.dfbnc.servers.ServerTypeManager;
import com.dfbnc.sockets.ListenSocket;
import com.dfbnc.sockets.UserSocket;
import com.dfbnc.util.MultiWriter;
import com.dfbnc.util.RollingWriter;
import java.io.Writer;
import uk.org.dataforce.libs.cliparser.BooleanParam;
import uk.org.dataforce.libs.cliparser.CLIParam;
import uk.org.dataforce.libs.cliparser.CLIParser;
Expand Down Expand Up @@ -100,6 +103,15 @@ public class DFBnc {
/** PID File name. */
static String pidFile = "";

/** MultiWriter for logger. */
final MultiWriter multiWriter = new MultiWriter();

/** Store log entries here to let the show logging command work. */
final RollingWriter rollingWriter = new RollingWriter(1000);

/** Log File Writer. */
Writer logFileWriter;

/**
* Create the BNC.
*/
Expand All @@ -112,7 +124,10 @@ private DFBnc() { }
* @param args CLI Arguments passed to application
*/
private void init(final String[] args) {
Logger.setWriter(multiWriter);
multiWriter.addWriter(rollingWriter);
Logger.setLevel(LogLevel.INFO);

loadVersionInfo();

if (DFBncDaemon.canFork() && daemon.isDaemonized()) {
Expand Down Expand Up @@ -233,6 +248,9 @@ private void init(final String[] args) {
System.exit(1);
}

// Now that we have a config file, set the log buffer capacity correctly.
rollingWriter.setCapacity(getConfig().getOptionInt("general", "logBuffer"));

Logger.info("Setting up Default User Command Manager");
userCommandManager.addCommand(new ServerTypeCommand(userCommandManager));
userCommandManager.addCommand(new ShowCommand(userCommandManager));
Expand Down Expand Up @@ -328,7 +346,8 @@ public void setupLogging() {
bw.append("\n");
bw.flush();
// We will never get to setting it here if it failed to write above!
Logger.setWriter(bw);
logFileWriter = bw;
multiWriter.addWriter(logFileWriter);
Logger.info("Using log file: " + file);
} catch (final IOException ex) {
Logger.error("Unable to write to log file: " + ex);
Expand Down Expand Up @@ -415,6 +434,15 @@ public static long getStartTime() {
return startTime;
}

/**
* Get the rolling log.
*
* @return the rolling writer used for log files.
*/
public RollingWriter getRollingWriter() {
return rollingWriter;
}

/**
* Handle shutdown
*/
Expand Down Expand Up @@ -456,10 +484,10 @@ public void shutdown(final boolean shuttingDown) {
}
}

final BufferedWriter bw = Logger.getWriter();
final Writer bw = logFileWriter;
if (bw != null) {
Logger.info("Closing log file");
Logger.setWriter(null);
multiWriter.removeWriter(logFileWriter);
try {
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
if (!Logger.getTag().isEmpty()) {
Expand All @@ -471,6 +499,7 @@ public void shutdown(final boolean shuttingDown) {
bw.append("\n");
bw.flush();
bw.close();
logFileWriter = null;
} catch (final IOException ioe) { /** Oh well. */ }
}

Expand Down
83 changes: 83 additions & 0 deletions src/com/dfbnc/commands/show/LoggingCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright (c) 2006-2013 Shane Mc Cormack
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.dfbnc.commands.show;

import java.text.SimpleDateFormat;

import com.dfbnc.DFBnc;
import com.dfbnc.commands.AdminCommand;
import com.dfbnc.commands.CommandManager;
import com.dfbnc.commands.CommandOutput;
import com.dfbnc.sockets.UserSocket;
import com.dfbnc.util.RollingWriter;

/**
* This file represents the 'logging' show command
*/
public class LoggingCommand extends AdminCommand {
/**
* Handle a Logging command.
*
* @param user the UserSocket that performed this command
* @param params Params for command (param 0 is the command name)
* @param output CommandOutput where output from this command should go.
*/
@Override
public void handle(final UserSocket user, final String[] params, final CommandOutput output) {
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
final RollingWriter rw = DFBnc.getBNC().getRollingWriter();
output.sendBotMessage("Most recent " + rw.size() + " (Max: " + rw.getCapacity() + ") log entries: ");
output.sendBotMessage("");
for (final String line : rw) {
output.sendBotMessage(" " + line);
}
}

/**
* What does this Command handle.
*
* @return String[] with the names of the tokens we handle.
*/
@Override
public String[] handles() {
return new String[]{"logging", "*logs"};
}

/**
* Create a new instance of the Command Object
*
* @param manager CommandManager that is in charge of this Command
*/
public LoggingCommand (final CommandManager manager) { super(manager); }

/**
* Get a description of what this command does
*
* @param command The command to describe (incase one Command does multiple
* things under different names)
* @return A description of what this command does
*/
@Override
public String getDescription(final String command) {
return "This command shows you the last few lines of system logging output";
}
}
4 changes: 4 additions & 0 deletions src/com/dfbnc/commands/show/SystemCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ public void handle(final UserSocket user, final String[] params, final CommandOu
output.sendBotMessage("--------------------");
try{ myManager.getCommand("connections").handle(user, new String[]{"connections", "full", "all"}, output); } catch (final Exception e) { }
output.sendBotMessage("----------------------------------------");
output.sendBotMessage("Logging:");
output.sendBotMessage("--------------------");
try{ myManager.getCommand("logging").handle(user, new String[]{"connections", "full", "all"}, output); } catch (final Exception e) { }
output.sendBotMessage("----------------------------------------");

}

Expand Down
1 change: 1 addition & 0 deletions src/com/dfbnc/commands/user/ShowCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ public ShowCommand(final CommandManager manager) {
showManager.addCommand(new FirstTimeCommand(showManager));
showManager.addCommand(new ConnectionsCommand(showManager));
showManager.addCommand(new SystemCommand(showManager));
showManager.addCommand(new LoggingCommand(showManager));
showManager.addCommand(new Command(showManager){

@Override
Expand Down
1 change: 1 addition & 0 deletions src/com/dfbnc/defaults.config
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ general:
ServerName=DFBnc.Server
allowshortcommands=true
BotName=-BNC
logBuffer=1000

irc:
perform.connect=
Expand Down
63 changes: 63 additions & 0 deletions src/com/dfbnc/util/MultiWriter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2015 Shane Mc Cormack <shanemcc@gmail.com>.
* See LICENSE.txt for licensing details.
*/

package com.dfbnc.util;

import java.io.IOException;
import java.io.Writer;
import java.util.LinkedList;
import java.util.List;

/**
* Writer that adds lines to multiple writers.
*
* @author Shane Mc Cormack <shanemcc@gmail.com>
*/
public class MultiWriter extends Writer {

/** List of lines that have been written to this BufferedWriter. */
private final List<Writer> writers = new LinkedList<>();

/**
* Create a new MultiWriter.
*/
public MultiWriter() { }

/**
* Add the given writer to the list of writers.
*
* @param writer Writer to add.
*/
public void addWriter(final Writer writer) {
writers.add(writer);
}

/**
* Remove the given writer from the list of writers.
* NOTE: This will not close the writer.
*
* @param writer Writer to add.
*/
public void removeWriter(final Writer writer) {
writers.remove(writer);
}

@Override
public void write(char[] cbuf, int off, int len) throws IOException {
for (final Writer w : writers) { w.write(cbuf, off, len); }
}

@Override
public void flush() throws IOException {
for (final Writer w : writers) { w.flush(); }
}

@Override
public void close() throws IOException {
for (final Writer w : writers) { w.close(); }
}


}

0 comments on commit 20fc532

Please sign in to comment.