diff options
Diffstat (limited to 'src/org/jogamp/jabot/irc/CatOut.java')
-rw-r--r-- | src/org/jogamp/jabot/irc/CatOut.java | 115 |
1 files changed, 75 insertions, 40 deletions
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("<h2>"+head+"</h2>"); out.println("<br/>"); + out.flush(); } } |