How to print to the console in GWT

前端 未结 10 1107
广开言路
广开言路 2020-12-23 14:29

I am debugging a GWT application and I need to print some stuff to the console for testing purposes. System.out.println and GWT.log don\'t work. Do

10条回答
  •  失恋的感觉
    2020-12-23 14:41

    Yet another variation using the native console...

    Add this class:

    package XXX.XXX.XXX.XXX;
    
    public class Debug {
        private static boolean isEnabled_ = false;
        public static void enable() { isEnabled_ = true; }
        public static void setEnabled( final boolean isEnabled ) 
        { isEnabled_ = isEnabled; }
    
        public static void log( final String s ) 
        { if( isEnabled_ ) nativeConsoleLog( s ); }
    
        private static native void nativeConsoleLog( String s ) 
        /*-{ console.log( s ); }-*/;
    }
    

    Then, enable debugging with it at some point, like upon starting the app:

    public class XXXXXX implements EntryPoint {
        @Override
        public void onModuleLoad() {
            Debug.enable();
            ...
        }
    }
    

    Then just use it like so:

    Debug.log("Hello World!");
    

提交回复
热议问题