Commit b3e355df authored by Jonathan Shahen's avatar Jonathan Shahen

Using the global formatters - removing duplicate code

parent 52531b36
...@@ -8,14 +8,14 @@ import org.apache.commons.cli.*; ...@@ -8,14 +8,14 @@ import org.apache.commons.cli.*;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import mohawk.converter.helper.SpecHelper; import mohawk.converter.helper.SpecHelper;
import mohawk.converter.logging.MohawkCSVFileFormatter;
import mohawk.converter.logging.MohawkConsoleFormatter;
import mohawk.converter.to.asaptime.ConvertToASAPTimeNSA; import mohawk.converter.to.asaptime.ConvertToASAPTimeNSA;
import mohawk.converter.to.asaptime.ConvertToASAPTimeSA; import mohawk.converter.to.asaptime.ConvertToASAPTimeSA;
import mohawk.converter.to.mohawk.ConvertToMohawk; import mohawk.converter.to.mohawk.ConvertToMohawk;
import mohawk.converter.to.nusmv.ConvertToNuSMV; import mohawk.converter.to.nusmv.ConvertToNuSMV;
import mohawk.converter.to.tred.ConvertToTRole; import mohawk.converter.to.tred.ConvertToTRole;
import mohawk.global.FileExtensions; import mohawk.global.FileExtensions;
import mohawk.global.formatter.MohawkCSVFileFormatter;
import mohawk.global.formatter.MohawkConsoleFormatter;
import mohawk.global.helper.RoleHelper; import mohawk.global.helper.RoleHelper;
import mohawk.global.parser.mohawkT.MohawkTARBACParser; import mohawk.global.parser.mohawkT.MohawkTARBACParser;
import mohawk.global.pieces.MohawkT; import mohawk.global.pieces.MohawkT;
......
package mohawk.converter.logging;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Formatter;
import java.util.logging.Handler;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import org.apache.commons.lang3.StringEscapeUtils;
public class MohawkCSVFileFormatter extends Formatter {
public final static Logger logger = Logger.getLogger("mohawk");
private static final DateFormat df = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss.SSSZ");
public String format(LogRecord record) {
StringBuilder builder = new StringBuilder(1000);
builder.append(df.format(new Date(record.getMillis()))).append(",");
builder.append(record.getSourceClassName()).append(",");
builder.append(record.getSourceMethodName()).append(",");
builder.append(record.getLevel()).append(",");
builder.append(StringEscapeUtils.escapeCsv(formatMessage(record)));
builder.append("\n");
return builder.toString();
}
public static String csvHeaders() {
StringBuilder builder = new StringBuilder(100);
builder.append("Datetime").append(",");
builder.append("Classname").append(",");
builder.append("Method").append(",");
builder.append("Level").append(",");
builder.append("Message");
builder.append("\n");
return builder.toString();
}
public String getHead(Handler h) {
return super.getHead(h);
}
public String getTail(Handler h) {
return super.getTail(h);
}
}
package mohawk.converter.logging;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Formatter;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import org.apache.commons.lang3.text.WordUtils;
public class MohawkConsoleFormatter extends Formatter {
public final static Logger logger = Logger.getLogger("mohawk");
private static final DateFormat df = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss.SSSZ");
public Integer padLeft = Level.WARNING.toString().length() + 1;
public Integer maxWidth = 0;
public String newLineStr = "\n ";
public String format(LogRecord record) {
StringBuilder builder = new StringBuilder(1000);
// Date Time
builder.append(df.format(new Date(record.getMillis()))).append(" - ");
// Class Name
builder.append("[").append(record.getSourceClassName()).append(".");
// Method Name
builder.append(record.getSourceMethodName()).append(" #");
// Thread ID
builder.append(record.getThreadID()).append("]:\n");
// Logging Level
String signifcantLevel = "";
char padChar = ' ';
if (record.getLevel() == Level.WARNING || record.getLevel() == Level.SEVERE) {
signifcantLevel = "";
padChar = '*';
}
builder.append("[");
builder.append(String.format("%1$" + signifcantLevel + padLeft + "s", record.getLevel()).replace(' ', padChar));
builder.append("] ");
// Message
builder.append(formatMessage(record));
builder.append("\n"); // End of Message
if (maxWidth == 0) {
return builder.toString();
} else {
return WordUtils.wrap(builder.toString(), maxWidth, newLineStr, true);
}
}
/**
* Get the current line number.
*
* @return int - Current line number.
*/
public static int getLineNumber() {
return Thread.currentThread().getStackTrace()[2].getLineNumber();
}
public String getHead(Handler h) {
return super.getHead(h);
}
public String getTail(Handler h) {
return super.getTail(h);
}
}
...@@ -7,9 +7,9 @@ import java.util.logging.*; ...@@ -7,9 +7,9 @@ import java.util.logging.*;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import mohawk.converter.logging.MohawkConsoleFormatter;
import mohawk.converter.to.asaptime.ConvertToASAPTimeNSA; import mohawk.converter.to.asaptime.ConvertToASAPTimeNSA;
import mohawk.converter.to.asaptime.ConvertToASAPTimeSA; import mohawk.converter.to.asaptime.ConvertToASAPTimeSA;
import mohawk.global.formatter.MohawkConsoleFormatter;
import mohawk.global.parser.BooleanErrorListener; import mohawk.global.parser.BooleanErrorListener;
import mohawk.global.parser.mohawkT.MohawkTARBACParser; import mohawk.global.parser.mohawkT.MohawkTARBACParser;
import mohawk.global.pieces.MohawkT; import mohawk.global.pieces.MohawkT;
......
...@@ -7,8 +7,8 @@ import java.util.logging.*; ...@@ -7,8 +7,8 @@ import java.util.logging.*;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import mohawk.converter.logging.MohawkConsoleFormatter;
import mohawk.converter.to.tred.ConvertToTRole; import mohawk.converter.to.tred.ConvertToTRole;
import mohawk.global.formatter.MohawkConsoleFormatter;
import mohawk.global.parser.BooleanErrorListener; import mohawk.global.parser.BooleanErrorListener;
import mohawk.global.parser.mohawkT.MohawkTARBACParser; import mohawk.global.parser.mohawkT.MohawkTARBACParser;
import mohawk.global.pieces.MohawkT; import mohawk.global.pieces.MohawkT;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment