Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package edu.stanford.nlp.util.logging;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Stack;
import java.util.function.Supplier;
import edu.stanford.nlp.math.SloppyMath;
import edu.stanford.nlp.util.logging.Redwood.Record;
import edu.stanford.nlp.util.Generics;
/**
* An abstract handler incorporating the logic of outputing a log message,
* to some source. This class is responsible for printing channel information,
* formatting tracks, and writing the actual log messages.
*
* Classes overriding this class should implement the print() method based
* on their output source.
*
* @author Gabor Angeli (angeli at cs.stanford)
*/
public abstract class OutputHandler extends LogRecordHandler{
/**
* A list of tracks which have been started but not yet printed as no
* log messages are in them yet.
*/
protected LinkedList queuedTracks = new LinkedList<>();
/**
* Information about the current and higher level tracks
*/
protected Stack trackStack = new Stack<>();
/**
* The current track info; used to avoid trackStack.peek() calls
*/
protected TrackInfo info;
/**
* The tab character
*/
protected String tab = " ";
/**
* Character used to join multiple channel names
*/
protected char channelSeparatorChar = ' ';
/**
* The length of the left margin in which to print channel information.
* If this is set to a value < 3, then no channel information is printed.
*/
protected int leftMargin = 0;
/**
* Number of lines above which the closing brace of a track shows the name of the
* track
*/
protected int minLineCountForTrackNameReminder = 50;
/**
* True if we have not printed the opening bracket for a track yet
*/
private boolean missingOpenBracket = false;
/**
* The color to use for track beginning and ends
*/
protected Color trackColor = Color.NONE;
protected Map channelColors = null;
protected boolean addRandomColors = false;
/**
* The style to use for track beginning and ends
*/
protected Style trackStyle = Style.NONE;
protected Map channelStyles = null;
/**
* Print a string to an output without the trailing newline.
* Many output handlers can get by with just implementing this method.
* @param channel The channels this message was printed on; in most cases
* an implementing handler should not have to do anything with
* this. The channels should not be printed here.
* The channels may be null.
* @param line The string to be printed.
*/
public abstract void print(Object[] channel, String line);
/**
* Color the tag for a particular channel this color
* @param channel The channel to color
* @param color The color to use
*/
public void colorChannel(String channel, Color color){
if(this.channelColors == null){
this.channelColors = Generics.newHashMap();
}
this.channelColors.put(channel.toLowerCase(Locale.ENGLISH),color);
}
/**
* Style the tag for a particular channel this style
* @param channel The channel to style
* @param style The style to use
*/
public void styleChannel(String channel, Style style){
if(this.channelStyles == null){
this.channelStyles = Generics.newHashMap();
}
this.channelStyles.put(channel.toLowerCase(Locale.ENGLISH),style);
}
public void setColorChannels(boolean colorChannels){
this.addRandomColors = colorChannels;
if(colorChannels){ this.channelColors = Generics.newHashMap(); }
}
/**
* Style a particular String segment, according to a color and style
* @param b The string builder to append to (for efficiency)
* @param line The String to be wrapped
* @param color The color to color as
* @param style The style to use
* @return The SringBuilder b
*/
protected StringBuilder style(StringBuilder b, String line, Color color, Style style){
if(color != Color.NONE || style != Style.NONE){
if (Redwood.supportsAnsi && this.supportsAnsi()) {
b.append(color.ansiCode);
b.append(style.ansiCode);
}
b.append(line);
if (Redwood.supportsAnsi && this.supportsAnsi()) {
b.append("\033[0m");
}
} else {
b.append(line);
}
return b;
}
/**
* Specify whether this output handler supports ansi output
* @return False by default, unless overwritten.
*/
protected boolean supportsAnsi() {
return false;
}
/**
* Format a channel
* @param b The StringBuilder to append to
* @param channelStr The [possibly truncated and/or modified] string
* to actually print to the StringBuilder
* @param channel The original channel
* @return |true| if the channel was printed (that is, appended to the StringBuilder)
*/
protected boolean formatChannel(StringBuilder b, String channelStr, Object channel){
if(this.channelColors == null && this.channelStyles == null){
//(regular concat)
b.append(channelStr);
} else {
String channelToString = channel.toString().toLowerCase(Locale.ENGLISH);
//(default: no style)
Color color = Color.NONE;
Style style = Style.NONE;
//(get color)
if(this.channelColors != null){
Color candColor = this.channelColors.get(channelToString);
if(candColor != null){
//((case: found a color))
color = candColor;
} else if(addRandomColors){
//((case: random colors))
color = Color.values()[SloppyMath.pythonMod(channelToString.hashCode(), (Color.values().length-3))+3];
if(channelToString.equals(Redwood.ERR.toString().toLowerCase())){
color = Color.RED;
} else if(channelToString.equals(Redwood.WARN.toString().toLowerCase())){
color = Color.YELLOW;
}
this.channelColors.put(channelToString, color);
}
}
//(get style)
if(this.channelStyles != null){
Style candStyle = this.channelStyles.get(channelToString);
if(candStyle != null){ style = candStyle; }
}
//(format)
style(b,channelStr,color,style);
}
return true; // Unless this method is overwritten, channel is always printed
}
private void writeContent(int depth, Object content, StringBuilder b){
if(leftMargin > 2){ b.append(tab); }
//(write tabs)
for(int i=0; i= untilDepth){ queuedTracks.add(signal); return; }
//(begin record message)
StringBuilder b = new StringBuilder();
if(missingOpenBracket){
b.append("{\n");
}
//(write margin)
for(int i=0; i 0){ b.append(" "); }
//(print)
print(null, this.style(new StringBuilder(), b.toString(), trackColor, trackStyle).toString() );
this.missingOpenBracket = true; //only set to false if actually updated track state
//(update lines printed)
if(info != null){
info.numElementsPrinted += 1;
}
}
}
/** {@inheritDoc} */
@Override
public List handle(Record record) {
StringBuilder b = new StringBuilder(1024);
//--Special case for Exceptions
String[] content;
if (record.content instanceof Throwable) {
//(vars)
List lines = new ArrayList<>();
StackTraceElement[] trace = null;
StackTraceElement topTraceElement= null;
//(root message)
Throwable exception = (Throwable) record.content;
lines.add(record.content.toString());
trace = exception.getStackTrace();
topTraceElement = trace.length > 0 ? trace[0] : null;
for(StackTraceElement e : exception.getStackTrace()){
lines.add(tab + e.toString());
}
//(causes)
while(exception.getCause() != null){
System.out.println("TOP ELEMENT: " + topTraceElement);
//((variables))
exception = exception.getCause();
trace = exception.getStackTrace();
lines.add("Caused by: " + exception.getClass()+": " + exception.getMessage());
for(int i=0; i 0 ? trace[0] : null;
}
//(set content array)
content = new String[lines.size()];
content = lines.toArray(content);
} else if(record.content == null){
content = new String[]{"null"};
} else {
String toStr;
if (record.content instanceof Supplier) {
//noinspection unchecked
toStr = ((Supplier