All Downloads are FREE. Search and download functionalities are using the official Maven repository.

fr.ms.log4jdbc.writer.MessageWriterImpl Maven / Gradle / Ivy

The newest version!
/*
 * This file is part of Log4Jdbc.
 *
 * Log4Jdbc is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Log4Jdbc is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Log4Jdbc.  If not, see .
 *
 */
package fr.ms.log4jdbc.writer;

import java.util.Iterator;

import fr.ms.log4jdbc.message.MessageHandler;
import fr.ms.log4jdbc.message.resultset.ResultSetCollector;
import fr.ms.log4jdbc.message.resultset.print.ResultSetPrinter;
import fr.ms.log4jdbc.sql.Query;
import fr.ms.log4jdbc.utils.Trace;

/**
 * 
 * @see Marco4J
 * 
 * 
 * @author Marco Semiao
 * 
 */
public class MessageWriterImpl implements MessageWriter {

  private final String threadName = Thread.currentThread().getName();

  private final MessageHandler message;

  private ResultSetCollector resultSetCollector;

  private final static int MAX = 10000;

  private final static String nl = System.getProperty("line.separator");

  public MessageWriterImpl(final MessageHandler message) {
    this.message = message;
  }

  public void traceMessage(final String str) {
    if (resultSetCollector != null && resultSetCollector.getRows() != null && MAX < resultSetCollector.getRows().length) {
      Trace.print(traceHeader());
      Trace.print(str);
      traceResultSet();
      Trace.print(traceFooter());
    } else {
      final StringBuffer sb = new StringBuffer();
      sb.append(traceHeader());
      sb.append(nl);
      sb.append(str);
      sb.append(nl);
      final Iterator printResultSet = ResultSetPrinter.printResultSet(resultSetCollector, MAX);
      if (printResultSet.hasNext()) {
        sb.append(printResultSet.next());
        sb.append(nl);
      }
      sb.append(traceFooter());
      Trace.print(sb.toString());
    }
  }

  public String traceHeader() {
    final StringBuffer sb = new StringBuffer();

    sb.append(message.getDate());
    sb.append(" - ");
    sb.append(threadName);
    sb.append(nl);
    sb.append(message.getConnectionNumber());
    sb.append(". Total ");
    sb.append(message.getOpenConnection());
    sb.append(nl);

    return sb.toString();
  }

  public void traceResultSet() {
    final Iterator iterator = ResultSetPrinter.printResultSet(resultSetCollector, MAX);

    while (iterator.hasNext()) {
      final String next = (String) iterator.next();
      Trace.print(next);
    }
  }

  public String traceFooter() {
    final long execTime;

    final Query query = message.getQuery();
    if (query != null) {
      execTime = query.getExecTime();
    } else {
      execTime = message.getExecTime();
    }

    final StringBuffer sb = new StringBuffer();

    sb.append(" {executed in ");
    sb.append(execTime);
    sb.append(" ms} ");
    sb.append(nl);
    sb.append("****************************************************************");

    return sb.toString();
  }

  public void setResultSetCollector(final ResultSetCollector resultSetCollector) {
    this.resultSetCollector = resultSetCollector;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy