How to exclude debug code

后端 未结 2 933
庸人自扰
庸人自扰 2021-01-20 11:23

Let\'s say I have simple logger:

void main() {
  var logger = new MyLogger();
  logger.log(\"hello Dart\");
}
         


        
2条回答
  •  清酒与你
    2021-01-20 11:49

    I put @GünterZöchbauer "assert trick" inside the factory constructor:

    class _ProductionPlug implements DebugClass{
      const _ProductionPlug();
      noSuchMethod(_) {} //do nothing
    }
    
    class DebugClass{
      static final DebugClass _plug = const _ProductionPlug();
      log(msg){print(msg);}
      DebugClass._(){}
      factory DebugClass(){
        DebugClass instance;
        assert((){
        instance = new DebugClass._();
        return true;
            });
        return instance != null ?  instance :  _plug;
      }
    }
    void main() {
      print("hello");
      new DebugClass()
        ..log("debugging");
    }
    

    This way nothing sticks out.

提交回复
热议问题