001/* 002 * (C) Copyright 2009 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Thierry Delprat 018 */ 019package org.nuxeo.ecm.platform.importer.log; 020 021import java.io.File; 022import java.io.FileWriter; 023import java.io.IOException; 024import java.io.Writer; 025 026import org.nuxeo.common.utils.Path; 027import org.nuxeo.runtime.api.Framework; 028 029public class PerfLogger { 030 031 protected String[] headers; 032 033 protected File logFile; 034 035 protected Writer logWriter; 036 037 protected static final String SEP = ";"; 038 039 public PerfLogger(String[] headers) throws IOException { 040 this.headers = headers; 041 File home = Framework.getRuntime().getHome(); 042 String logPath = new Path(home.getAbsolutePath()).append("perfLog_" + System.currentTimeMillis() + ".csv").toString(); 043 logFile = new File(logPath); 044 logWriter = new FileWriter(logFile); 045 log(headers); 046 } 047 048 public void log(String[] data) throws IOException { 049 StringBuffer sb = new StringBuffer(); 050 sb.append(System.currentTimeMillis()); 051 for (String s : data) { 052 sb.append(SEP); 053 sb.append("\""); 054 sb.append(s); 055 sb.append("\""); 056 } 057 sb.append("\n"); 058 logWriter.write(sb.toString()); 059 } 060 061 public void log(Double[] data) throws IOException { 062 StringBuffer sb = new StringBuffer(); 063 sb.append(System.currentTimeMillis()); 064 for (Double d : data) { 065 sb.append(SEP); 066 sb.append(d.toString()); 067 } 068 sb.append("\n"); 069 logWriter.write(sb.toString()); 070 logWriter.flush(); 071 } 072 073 public void release() throws IOException { 074 if (logWriter != null) { 075 logWriter.flush(); 076 logWriter.close(); 077 logWriter = null; 078 } 079 } 080}