001/* [{ 002Copyright 2007, 2008, 2009 Nicolas Carranza <nicarran at gmail.com> 003 004This file is part of jpen. 005 006jpen is free software: you can redistribute it and/or modify 007it under the terms of the GNU Lesser General Public License as published by 008the Free Software Foundation, either version 3 of the License, 009or (at your option) any later version. 010 011jpen is distributed in the hope that it will be useful, 012but WITHOUT ANY WARRANTY; without even the implied warranty of 013MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014GNU Lesser General Public License for more details. 015 016You should have received a copy of the GNU Lesser General Public License 017along with jpen. If not, see <http://www.gnu.org/licenses/>. 018}] */ 019package jpen.demo.inspect; 020 021import java.io.IOException; 022import java.text.SimpleDateFormat; 023import java.util.Collection; 024import java.util.Date; 025import java.util.logging.FileHandler; 026import java.util.logging.Level; 027import java.util.logging.Logger; 028import java.util.logging.SimpleFormatter; 029import jpen.PenManager; 030 031public class Inspector{ 032 static final Logger L=Logger.getLogger(Inspector.class.getName()); 033 //static { L.setLevel(Level.ALL); } 034 035 final PenManager penManager; 036 final FileHandler fileHandler; 037 final InspectorThread inspectorThread; 038 039 public Inspector(PenManager penManager, String loggerName, int periodInSec) throws IOException{ 040 this.penManager=penManager; 041 042 String fileHandlerName=evalFileHandlerName(loggerName); 043 fileHandler=new FileHandler(fileHandlerName); 044 fileHandler.setFormatter(new SimpleFormatter()); 045 fileHandler.setLevel(Level.ALL); 046 L.info("logging to file:"+fileHandlerName); 047 048 Logger logger=Logger.getLogger(loggerName); 049 logger.setLevel(Level.ALL); 050 logger.addHandler(fileHandler); 051 052 inspectorThread=new InspectorThread(this, periodInSec*1000/2, 2); 053 } 054 055 private String evalFileHandlerName(String loggerName){ 056 StringBuilder sb=new StringBuilder(loggerName); 057 sb.append("-inspect-"); 058 sb.append(new SimpleDateFormat("yyMMddHHmmss").format(new Date())); 059 sb.append(".txt"); 060 return sb.toString(); 061 } 062}