diff options
author | Sven Gothel <[email protected]> | 2013-01-19 21:24:22 +0100 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2013-01-19 21:24:22 +0100 |
commit | 821d52e3694fac0b907ec240e264f4d52d352966 (patch) | |
tree | ac79120fb5550e0e4e6bc7688cb3597b36096698 /src/org/jogamp/jabot/irc/CatOut.java | |
parent | 60ccfc5ff7dee58740ba1302626e3ccea35259b9 (diff) |
Allow 'joinMessage' to include actual log URL, notify @ each logrotate
- joinMessage configurable
- Add config: urlprefix (same as logprefix, but in URL space)
- Set bot's joinMessage: joinMessage + urlprefix + current logfile
- Notify new logfile via logged joinMessage in old and new logfile and channel itself
Diffstat (limited to 'src/org/jogamp/jabot/irc/CatOut.java')
-rw-r--r-- | src/org/jogamp/jabot/irc/CatOut.java | 112 |
1 files changed, 70 insertions, 42 deletions
diff --git a/src/org/jogamp/jabot/irc/CatOut.java b/src/org/jogamp/jabot/irc/CatOut.java index f6812af..3e494de 100644 --- a/src/org/jogamp/jabot/irc/CatOut.java +++ b/src/org/jogamp/jabot/irc/CatOut.java @@ -36,23 +36,27 @@ public class CatOut extends PircBot { } public static void main(String[] args) throws Exception { - final String joinMessage = "This channel is logged"; + final String joinMessage; final boolean showHostname = false; final String login, name, server, channelNoHash; final boolean verbose; final long logrotate, logrotateStart; - final String logprefix; + final String logprefix, urlprefix; final boolean htmlOut; final File htmlHeader, htmlFooter; { + String _joinMessage = "This channel is logged"; String _login=null, _name=null, _server=null, _channelNoHash=null; boolean _verbose=false; long _logrotate = 0, _logrotateStart=System.currentTimeMillis(); - String _logprefix=""; + String _logprefix="", _urlprefix=""; boolean _htmlOut = false; String _htmlHeader = null, _htmlFooter=null; for(int i=0; i<args.length; i++) { - if(args[i].equals("-login")) { + if(args[i].equals("-joinMessage")) { + i++; + _joinMessage = args[i]; + } else if(args[i].equals("-login")) { i++; _login = args[i]; } else if(args[i].equals("-name")) { @@ -83,6 +87,9 @@ public class CatOut extends PircBot { } else if(args[i].equals("-logprefix")) { i++; _logprefix = args[i]; + } else if(args[i].equals("-urlprefix")) { + i++; + _urlprefix = args[i]; } else if(args[i].equals("-logrotateStart")) { i++; final String hhmmS = args[i]; @@ -103,15 +110,17 @@ public class CatOut extends PircBot { null == _server || null == _channelNoHash || ( _htmlOut && null == _htmlHeader || null == _htmlFooter ) ) { - System.err.println("Incomplete commandline, use "+CatOut.class.getName()+" -login VAL -name VAL -server VAL -channel VAL [-verbose] [-htmlHeader VAL -htmlFooter VAL] [-logrotate millis [-logrotateStart hhmm] [-logprefix VAL]"); + System.err.println("Incomplete commandline, use "+CatOut.class.getName()+" -login VAL -name VAL -server VAL -channel VAL [-verbose] [-joinMessage VAL] [-htmlHeader VAL -htmlFooter VAL] [-logrotate millis [-logrotateStart hhmm] [-logprefix VAL] [-urlprefix VAL]]"); return; } + joinMessage = _joinMessage; login=_login; name=_name; server=_server; channelNoHash=_channelNoHash; verbose=_verbose; logrotate = _logrotate; logrotateStart = _logrotateStart; logprefix = _logprefix; + urlprefix = _urlprefix; htmlOut = _htmlOut; htmlHeader = new File(_htmlHeader); htmlFooter= new File(_htmlFooter); @@ -136,12 +145,14 @@ public class CatOut extends PircBot { final LogBot bot = new LogBot(showHostname, joinMessage, htmlOut); bot.setVerbose(verbose); bot.setLoginAndName(login, name); - PrintStream fout; + LogStream logOut; if ( 0 < logrotate ) { - fout = createLogStream(bot, logprefix, server, channelNoHash, htmlHeader); - bot.setOut(fout, true); + logOut = new LogStream(bot, logprefix, urlprefix, server, channelNoHash, htmlHeader); + bot.setOut(logOut.out, true); + bot.setJoinMessage(joinMessage+" @ "+logOut.urlString); + bot.logNotice(bot.getJoinMessage()); } else { - fout = null; + logOut = null; } bot.connect(server); bot.joinChannel(HASH+channelNoHash); @@ -151,13 +162,16 @@ public class CatOut extends PircBot { if( now >= nextLogrotate ) { // Swap Logfiles: Open and set new logfile, then close old one. - final PrintStream _fout = createLogStream(bot, logprefix, server, channelNoHash, htmlHeader); - bot.setOut(_fout, true); + final LogStream _logOut = new LogStream(bot, logprefix, urlprefix, server, channelNoHash, htmlHeader); + bot.logNotice("Continue @ "+_logOut.urlString); + bot.setOut(_logOut.out, true); + bot.setJoinMessage(joinMessage+" @ "+_logOut.urlString); + bot.sendLoggedNotice(HASH+channelNoHash, bot.getJoinMessage()); if(htmlOut) { - cat(htmlFooter, fout); + logOut.cat(htmlFooter); } - fout.close(); // implies flush - fout = _fout; + logOut.close(); // implies flush + logOut = _logOut; nextLogrotate += logrotate; logCal.setTimeInMillis(now); System.err.println("Now : "+logCal.getTime()); @@ -167,41 +181,55 @@ public class CatOut extends PircBot { try { Thread.sleep(FLUSH_INTERVAL); } catch (Exception e) { } - if(null != fout) { - fout.flush(); + if(null != logOut) { + logOut.flush(); } } } } public static final long FLUSH_INTERVAL = 60*1000; // flush every minute + + static class LogStream { + public final PrintStream out; + public final String fileName; + public final String urlString; + + public LogStream(final LogBot bot, String logprefix, String urlprefix, String serverName, String channelName, File htmlHeader) throws Exception { + final Calendar calendar = bot.tick(); + final String suffix = null != htmlHeader ? ".html" : ".txt" ; + final String baseName = channelName+"_"+TimeTool.getTimeStamp(calendar, true, false)+suffix; + fileName = logprefix+baseName; + urlString = urlprefix+baseName; + final File _file = new File(fileName); + out = new PrintStream(new FileOutputStream(_file)); + if(null != htmlHeader) { + cat(htmlHeader); + htmlHead(HASH+channelName+" @ "+serverName+" - "+bot.getTimeStamp()+" ("+calendar.getTimeZone().getID()+")"); + } + } + public void flush() { + out.flush(); + } + public void close() { + out.close(); + } - private static PrintStream createLogStream(final LogBot bot, String logprefix, String serverName, String channelName, File htmlHeader) throws Exception { - final Calendar calendar = bot.tick(); - final String suffix = null != htmlHeader ? ".html" : ".txt" ; - final String logfilename = logprefix+channelName+"_"+TimeTool.getTimeStamp(calendar, true, false)+suffix; - final File _file = new File(logfilename); - final PrintStream _fout = new PrintStream(new FileOutputStream(_file)); - if(null != htmlHeader) { - cat(htmlHeader, _fout); - htmlHead(HASH+channelName+" @ "+serverName+" - "+bot.getTimeStamp()+" ("+calendar.getTimeZone().getID()+")", - _fout); + public void cat(File source) throws IOException { + final BufferedInputStream input = new BufferedInputStream(new FileInputStream(source)); + int bytesRead = 0; + byte[] buffer = new byte[1024]; + while ((bytesRead = input.read(buffer, 0, buffer.length)) != -1) { + out.write(buffer, 0, bytesRead); + } + out.flush(); + input.close(); } - return _fout; - } - - public static void cat(File source, PrintStream out) throws IOException { - final BufferedInputStream input = new BufferedInputStream(new FileInputStream(source)); - int bytesRead = 0; - byte[] buffer = new byte[1024]; - while ((bytesRead = input.read(buffer, 0, buffer.length)) != -1) { - out.write(buffer, 0, bytesRead); + + private void htmlHead(String head) { + out.println("<h2>"+head+"</h2>"); + out.println("<br/>"); + out.flush(); } - out.flush(); - input.close(); - } - public static void htmlHead(String head, PrintStream out) { - out.println("<h2>"+head+"</h2>"); - out.println("<br/>"); - out.flush(); } + } |