001package gudusoft.gsqlparser;
002
003
004import gudusoft.gsqlparser.nodes.TParseTreeNode;
005
006import java.text.SimpleDateFormat;
007
008public class TLog {
009    public static final SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
010    protected String message = null;
011
012    protected long line;
013    protected long column;
014
015    protected TParseTreeNode node;
016    protected String timestamp;
017
018
019    // log的类型,包括信息、警告、错误。
020    protected int type = INFO;
021
022    public final static int OUTPUT_ATTRIBUTES_MAX = 20;
023
024
025    public final static int ERROR =   0b00000001;
026    public final static int WARNING = 0b00000010;
027    public final static int DEBUG =   0b00000100;
028    public final static int INFO =    0b00001000;
029    public final static int INTERPRETER_ERROR =   0b00010000;
030    public final static int INTERPRETER_WARNING = 0b00100000;
031    public final static int INTERPRETER_DEBUG =   0b01000000;
032    public final static int INTERPRETER_INFO =    0b10000000;
033    public final static int INTERPRETER_MSG =    0b000100000000;
034
035    public static int REPORT_LEVEL = 0|ERROR|WARNING|DEBUG|INFO|INTERPRETER_ERROR|INTERPRETER_WARNING|INTERPRETER_DEBUG|INTERPRETER_INFO|INTERPRETER_MSG;
036
037    public static void disableLog(){
038        REPORT_LEVEL = 0;
039    }
040
041    public static void enableAllLevelLog(){
042        REPORT_LEVEL = 0|ERROR|WARNING|DEBUG|INFO|INTERPRETER_ERROR|INTERPRETER_WARNING|INTERPRETER_DEBUG|INTERPRETER_INFO|INTERPRETER_MSG;
043    }
044
045    public static void enableInterpreterLogOnly(){
046        REPORT_LEVEL = 0|INTERPRETER_ERROR|INTERPRETER_WARNING|INTERPRETER_DEBUG|INTERPRETER_INFO|INTERPRETER_MSG;
047    }
048
049    public static void enableResolverLog(){
050        REPORT_LEVEL = 0|ERROR|WARNING|DEBUG|INFO;
051    }
052
053    private String[] typeStr = {"ERROR","WARNING","DEBUG","INFO","INTERPRETER_ERROR","INTERPRETER_WARNING","INTERPRETER_DEBUG","INTERPRETER_INFO","INTERPRETER_MSG"};
054
055    public static boolean isEnabled(int reportLevel){
056        return  ((reportLevel & REPORT_LEVEL) != 0);
057    }
058
059    private static boolean outputSimpleMode = false;
060
061    public static void setOutputSimpleMode(boolean pShowMessageOnly) {
062        outputSimpleMode = pShowMessageOnly;
063    }
064
065    public static boolean isOutputSimpleMode() {
066        return outputSimpleMode;
067    }
068
069    public static void clearLogs(){
070        TBaseType.logs.clear();
071    }
072
073    @Override
074    public String toString() {
075        int b = (int)(Math.log(type) / Math.log(2));
076        if (outputSimpleMode) return message;
077
078        String retMsg = "["+timestamp+" "+typeStr[b]+"] "+message;
079
080        if (line != -1){
081            retMsg = retMsg + " [Triggered by token at: (" + line + ":" + column+")]";
082        }
083        return  retMsg;
084    }
085}