Package limpidlog.log

Source Code of limpidlog.log.FileLog

//Copyright 2006-2007 Acelet Corporation. All rights reserved.

package limpidlog.log;

import java.io.*;
import java.text.*;
import java.util.*;

import limpidlog.lib.Constants;
import limpidlog.lib.Options;
import limpidlog.lib.Util;

/**
* @author Wei Jiang
*/
public class FileLog extends ConsoleLog implements Constants, LogProxy {
  private static final String LIMPIDLOG_FILE_FORMAT = "LimpidLog_fileFormat";
  private static final String LOG_EXTENSION = ".log";
  private static String fileFormatVersion = "2.0";
  private static String startSymbol = "```";
  private static String delimiter = " ` ";
  private static String fileBaseName = "limpidlog";
  private static long maxSize = 10 * 1048576//10M

  private String fileName;
  private File theFile;
  private FileWriter writer;

  private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");

  public FileLog() throws Exception {
    super();

    createNewFile();
  }

  protected void createNewFile() throws Exception {
    Options options = new Options();

    fileName = getFileName();
    if (Options.debug)
      System.out.println("FileLog.createNewFile: fileName="+fileName);

    theFile = new File(fileName);
    writer = new FileWriter(fileName);

    String threadName = Thread.currentThread().getName();
    log(threadName, "", Options.LIMPIDLOG_VERSION, dataVersion + "\t" + version);
    log(threadName, "", Options.LIMPIDLOG_OPTIONS, options.getBytecoderOptions());
    log(threadName, "", LIMPIDLOG_FILE_FORMAT, fileFormatVersion);
  }

  protected synchronized String getDateTimeString() {
    Date date = new Date();
    StringBuffer buffer = new StringBuffer("");
    simpleDateFormat.format(date, buffer, new FieldPosition(0));
    return buffer.toString();
  }

  protected String getFileName() {
    Options options = new Options();
    String fixedName = options.getOption(Options.FILE_LOG_FIXED_NAME, null);
    if (fixedName != null) {
      maxSize = Long.MAX_VALUE;
      return fixedName;
    }
         
    fileBaseName = options.getOption(Options.FILE_LOG_BASE_NAME, fileBaseName);
    maxSize = options.getOptionInt(Options.FILE_LOG_MAX_SIZE, 100) * 1048576//unit: Meg

    Date date = new Date();
    StringBuffer buffer = new StringBuffer("");
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
    simpleDateFormat.format(date, buffer, new FieldPosition(0));
    String timestamp = buffer.toString();

    return fileBaseName + timestamp + LOG_EXTENSION;
  }

  public void log(String threadName, String whereabouts, String why, String message) {
    synchronized (theFile) {
      StringBuffer outputBuffer = new StringBuffer();
     
      message = (message==null)? NULL: message;

      outputBuffer.append(startSymbol);
      outputBuffer.append(getDateTimeString());
      outputBuffer.append(delimiter);
      outputBuffer.append(hostname);
      outputBuffer.append(delimiter);
      outputBuffer.append(threadName);
      outputBuffer.append(delimiter);
      outputBuffer.append(whereabouts);
      outputBuffer.append(delimiter);
      outputBuffer.append(why);
      outputBuffer.append(delimiter);
      outputBuffer.append(message);
      outputBuffer.append(delimiter);

      out(outputBuffer.toString());
    }
  }

  protected void out(String text) {
    try {
      if (theFile.length() > maxSize) {
        FileWriter old = writer;
        createNewFile();
        old.flush();
        old.close();
      }
      writer.write(text);
      writer.write("\n");

      writer.flush();
    } catch (Exception ex) {
      if (Options.debug)
        ex.printStackTrace();
    }
  }
}
TOP

Related Classes of limpidlog.log.FileLog

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.