001
002package gudusoft.gsqlparser.pp.logger;
003
004import java.text.SimpleDateFormat;
005import java.util.Date;
006
007public class PPLogger
008{
009
010        public static final int OFF = 0;
011
012        public static final int INFO = 300;
013
014        public static final int ERROR = 100;
015
016        public static int currLogLevel = OFF;
017
018        private static PPLogger logger = new PPLogger( );
019
020        /**
021         * Whether an info record would be written. Callers build their message only then: building it, and the
022         * record's stack trace, dominated the formatter's time while logging was off.
023         */
024        public static boolean isInfoEnabled( )
025        {
026                return currLogLevel >= INFO;
027        }
028
029        public static void error( Throwable e )
030        {
031                if ( currLogLevel < ERROR )
032                {
033                        return;
034                }
035                logger.doLog( new PPLoggerRecord( ERROR, null, e ) );
036        }
037
038        public static void info( Throwable e )
039        {
040                info( null, e );
041        }
042
043        public static void info( String msg )
044        {
045                info( msg, null );
046        }
047
048        public static void info( String msg, Throwable e )
049        {
050                info( 0, msg, null );
051        }
052
053        public static void info( int stackLevel, String msg, Throwable e )
054        {
055                // A record finds its caller from a stack trace: skip it when doLog would drop it anyway.
056                if ( !isInfoEnabled( ) )
057                {
058                        return;
059                }
060                logger.doLog( new PPLoggerRecord( INFO, stackLevel, msg, e ) );
061        }
062
063        private void doLog( PPLoggerRecord record )
064        {
065                if ( currLogLevel == OFF )
066                {
067                        return;
068                }
069
070                SimpleDateFormat fmt = new SimpleDateFormat( "yyyy-MM-dd hh:mm:ss" );
071                if ( currLogLevel >= record.getLogLevel( ) )
072                {
073                        StringBuilder buf = new StringBuilder( );
074                        buf.append( fmt.format( new Date( ) ) );
075                        switch ( record.getLogLevel( ) )
076                        {
077                                case ERROR :
078                                        buf.append( " ERROR: " );
079                                        break;
080                                case INFO :
081                                        buf.append( " INFO: " );
082                                default :
083                                        break;
084                        }
085                        buf.append( record.getClassName( ) )
086                                        .append( "." )
087                                        .append( record.getMethodName( ) )
088                                        .append( "(" )
089                                        .append( record.getFileName( ) )
090                                        .append( ":" )
091                                        .append( record.getLineNum( ) )
092                                        .append( ")" );
093
094                        if ( record.getMsg( ) != null )
095                        {
096                                buf.append( "\n" ).append( record.getMsg( ) );
097                        }
098
099                        System.err.println( buf );
100
101                        if ( record.getE( ) != null )
102                        {
103                                record.getE( ).printStackTrace( );
104                        }
105
106                        System.err.print( "\n" );
107                }
108        }
109
110}