From 60ccfc5ff7dee58740ba1302626e3ccea35259b9 Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Sat, 19 Jan 2013 08:49:32 +0100 Subject: Add id/href to each line enabling linking to each message; Simplify loop; Set logstream before start; Add logrotateStart time. --- src/org/jogamp/jabot/irc/CatOut.java | 115 +++++++++++++++++++++++------------ src/org/jogamp/jabot/irc/LogBot.java | 19 +++++- 2 files changed, 92 insertions(+), 42 deletions(-) (limited to 'src/org') diff --git a/src/org/jogamp/jabot/irc/CatOut.java b/src/org/jogamp/jabot/irc/CatOut.java index bb53211..f6812af 100644 --- a/src/org/jogamp/jabot/irc/CatOut.java +++ b/src/org/jogamp/jabot/irc/CatOut.java @@ -16,6 +16,8 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintStream; import java.util.Calendar; +import java.util.GregorianCalendar; +import java.util.Locale; import org.jibble.pircbot.PircBot; import org.jogamp.jabot.util.TimeTool; @@ -38,14 +40,14 @@ public class CatOut extends PircBot { final boolean showHostname = false; final String login, name, server, channelNoHash; final boolean verbose; - final long logrotate; + final long logrotate, logrotateStart; final String logprefix; final boolean htmlOut; final File htmlHeader, htmlFooter; { String _login=null, _name=null, _server=null, _channelNoHash=null; boolean _verbose=false; - long _logrotate = 0; + long _logrotate = 0, _logrotateStart=System.currentTimeMillis(); String _logprefix=""; boolean _htmlOut = false; String _htmlHeader = null, _htmlFooter=null; @@ -81,6 +83,19 @@ public class CatOut extends PircBot { } else if(args[i].equals("-logprefix")) { i++; _logprefix = args[i]; + } else if(args[i].equals("-logrotateStart")) { + i++; + final String hhmmS = args[i]; + if( 4 == hhmmS.length() ) { + final String hhS = hhmmS.substring(0, 2); + final String mmS = hhmmS.substring(2, 4); + final int hh = (int)atol(hhS, 0); + final int mm = (int)atol(mmS, 0); + final Calendar cal = new GregorianCalendar(TimeTool.getNearZuluTimeZone(), Locale.getDefault()); + cal.set(Calendar.HOUR, hh); + cal.set(Calendar.MINUTE, mm); + _logrotateStart = cal.getTimeInMillis(); + } } } if( null == _login || @@ -88,72 +103,91 @@ 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 [-logprefix VAL]]"); + 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]"); return; } login=_login; name=_name; server=_server; channelNoHash=_channelNoHash; verbose=_verbose; logrotate = _logrotate; + logrotateStart = _logrotateStart; logprefix = _logprefix; htmlOut = _htmlOut; htmlHeader = new File(_htmlHeader); htmlFooter= new File(_htmlFooter); } + long nextLogrotate = logrotateStart + logrotate; + final Calendar logCal = new GregorianCalendar(TimeTool.getNearZuluTimeZone(), Locale.getDefault()); + if(0 < logrotate) { + if( System.currentTimeMillis() > nextLogrotate ) { + nextLogrotate += logrotate; + } + System.err.println("Now : "+logCal.getTime()); + logCal.setTimeInMillis(logrotateStart); + System.err.println("Logrotate Start : "+logCal.getTime()); + logCal.setTimeInMillis(logrotate); + System.err.println("Logrotate Period: "+logCal.getTime()+", "+logrotate+" ms"); + logCal.setTimeInMillis(nextLogrotate); + System.err.println("Next Logrotate-1: "+logCal.getTime()); + logCal.setTimeInMillis(nextLogrotate+logrotate); + System.err.println("Next Logrotate-2: "+logCal.getTime()); + } final LogBot bot = new LogBot(showHostname, joinMessage, htmlOut); bot.setVerbose(verbose); bot.setLoginAndName(login, name); + PrintStream fout; + if ( 0 < logrotate ) { + fout = createLogStream(bot, logprefix, server, channelNoHash, htmlHeader); + bot.setOut(fout, true); + } else { + fout = null; + } bot.connect(server); bot.joinChannel(HASH+channelNoHash); - if( 0 >= logrotate ) { - // bot.sendRawLine("NAMES "+HASH+channelNoHash); // FIXME ? - } else { - PrintStream fout = null; - long t0 = System.currentTimeMillis(); - int sendNamesCount = 0; + if( 0 < logrotate ) { while(true) { // forever ! - // Swap Logfiles: Open and set new logfile, then close old one. - { - final Calendar calendar = bot.tick(); - final String suffix = htmlOut ? ".html" : ".txt" ; - final String logfilename = logprefix+channelNoHash+"_"+TimeTool.getTimeStamp(calendar, true, false)+suffix; - final File _file = new File(logfilename); - final PrintStream _fout = new PrintStream(new FileOutputStream(_file)); + final long now = System.currentTimeMillis(); + + 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); if(htmlOut) { - cat(htmlHeader, _fout); - htmlHead(HASH+channelNoHash+" @ "+server+" - "+bot.getTimeStamp()+" ("+calendar.getTimeZone().getID()+")", - _fout); - } - bot.setOut(_fout); - // bot.sendRawLine("NAMES "+HASH+channelNoHash); // FIXME ? - if(null != fout) { - if(htmlOut) { - cat(htmlFooter, fout); - } - fout.close(); // implies flush + cat(htmlFooter, fout); } + fout.close(); // implies flush fout = _fout; + nextLogrotate += logrotate; + logCal.setTimeInMillis(now); + System.err.println("Now : "+logCal.getTime()); + logCal.setTimeInMillis(nextLogrotate); + System.err.println("Next Logrotate: "+logCal.getTime()); } - final long t1 = System.currentTimeMillis(); - long td = 0; - while ( td < logrotate ) { - try { - Thread.sleep(FLUSH_INTERVAL); - } catch (Exception e) { } + try { + Thread.sleep(FLUSH_INTERVAL); + } catch (Exception e) { } + if(null != fout) { fout.flush(); - final long t2 = System.currentTimeMillis(); - td = t2 - t1; - if ( sendNamesCount < ( t2 - t0 ) / SEND_NAMES ) { - // bot.sendRawLine("NAMES "+HASH+channelNoHash); // FIXME ? - sendNamesCount++; - } } } } } public static final long FLUSH_INTERVAL = 60*1000; // flush every minute - public static final long SEND_NAMES = 10*60*1000; // every 10 minutes + + 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); + } + return _fout; + } public static void cat(File source, PrintStream out) throws IOException { final BufferedInputStream input = new BufferedInputStream(new FileInputStream(source)); @@ -168,5 +202,6 @@ public class CatOut extends PircBot { public static void htmlHead(String head, PrintStream out) { out.println("

"+head+"

"); out.println("
"); + out.flush(); } } diff --git a/src/org/jogamp/jabot/irc/LogBot.java b/src/org/jogamp/jabot/irc/LogBot.java index e334ae8..9c085a2 100644 --- a/src/org/jogamp/jabot/irc/LogBot.java +++ b/src/org/jogamp/jabot/irc/LogBot.java @@ -43,6 +43,7 @@ public class LogBot extends PircBot { private final boolean xmlOut; private final Object outSync = new Object(); private PrintStream out; + private int lineCount; public LogBot(boolean showHostname, String joinMessage, boolean xmlOut) { this(showHostname, joinMessage, TimeTool.getNearZuluTimeZone(), Locale.getDefault(), System.out, xmlOut); @@ -54,6 +55,7 @@ public class LogBot extends PircBot { this.xmlOut = xmlOut; this.out = out; this.joinMessage = joinMessage; + this.lineCount = 0; } public final void setLoginAndName(String login, String nick) { @@ -93,16 +95,28 @@ public class LogBot extends PircBot { return out; } } - public PrintStream setOut(PrintStream newOut) { + public PrintStream setOut(PrintStream newOut, boolean resetLineCount) { synchronized(outSync) { final PrintStream old = out; out = newOut; + if( resetLineCount ) { + lineCount = 0; + } return old; } } + public int getLineCount() { + return lineCount; + } + public void resetLineCount() { + synchronized(outSync) { + lineCount = 0; + } + } public void append(String color, String line) { tick(); + lineCount++; if(xmlOut) { line = Colors.removeFormattingAndColors(line); @@ -117,7 +131,8 @@ public class LogBot extends PircBot { synchronized(outSync) { if(xmlOut) { - out.println("[" + getTimeStamp() + "] " + line + "
"); + // 20130117 21:06:04 + out.println("" + getTimeStamp() + " " + line + "
"); } else { out.println(getTimeStamp()+" "+line); } -- cgit v1.2.3