Loading ...
Sorry, an error occurred while loading the content.

DEBUG && window.console.debug('some data', obj1.prop1, ...);

Expand Messages
  • Alexandre Morgaut
    Hi, In older times, to disable easily I have used code like this: var logger; function doNothing() {} function setDebugMode(mode) { if (mode === on ) {
    Message 1 of 3 , Apr 5 9:46 AM
    • 0 Attachment
      Hi,

      In older times, to disable easily I have used code like this:

      var logger;

      function doNothing() {}

      function setDebugMode(mode) {
      if (mode === 'on') {
      logger.log = logger.command.log;
      logger.warn = logger.command.warn;
      /****/
      } else {
      logger.log = doNothing;
      logger.warn = doNothing;
      /****/
      }
      }

      logger = {};
      logger.commands = {};
      logger.commands.log = function (message) {
      /*****/
      };
      logger.commands.warn = function (message) {
      /*****/
      };

      setDebugMode('on');


      I wasn't very satisfied because:
      - a function was still invocated (not a big deal)
      - each parameter sent to the function are evaluated expression...
      -> this can be really time consuming in some cases


      A production process could remove all these log invocations but being able to active them at any time is often interesting (like for remote support)

      Another approach has been to define DEBUG booleans at application and component levels

      ex:
      function myComponentScope() {
      var
      DEBUG,
      log,
      warn;

      DEBUG = myApp.DEBUG && true;
      log = myApp.logger.log;
      warn = myApp.logger.warn;

      /****/

      DEBUG && warn(exp1, exp2, ....);

      /****/
      }


      unfortunately JSLint compliance would require an "if" block

      if (DEBUG) {
      warn(exp1, exp2, ....);
      }


      Would you still consider the use of "&&" as dangerous in this case ?

      This approach is just a try... How do you consider it ?
      Other suggestions ?


      Any comment welcome ;-)

      Alexandre.
    • Luke Page
      If you use an intelligent compiler, (like closure compiler - not sure what others do it?) then the compile process can strip out debug code and code that is
      Message 2 of 3 , Apr 5 10:07 AM
      • 0 Attachment
        If you use an intelligent compiler, (like closure compiler - not sure what
        others do it?) then the compile process can strip out debug code and code
        that is unused.

        There must be some minifiers that also allow you to strip out a particular
        global function call? Using this approach your logging can be as elegant as
        you like and still not effect production.

        On 5 April 2011 17:46, Alexandre Morgaut <morgaut@...> wrote:

        >
        >
        > Hi,
        >
        > In older times, to disable easily I have used code like this:
        >
        > var logger;
        >
        > function doNothing() {}
        >
        > function setDebugMode(mode) {
        > if (mode === 'on') {
        > logger.log = logger.command.log;
        > logger.warn = logger.command.warn;
        > /****/
        > } else {
        > logger.log = doNothing;
        > logger.warn = doNothing;
        > /****/
        > }
        > }
        >
        > logger = {};
        > logger.commands = {};
        > logger.commands.log = function (message) {
        > /*****/
        > };
        > logger.commands.warn = function (message) {
        > /*****/
        > };
        >
        > setDebugMode('on');
        >
        > I wasn't very satisfied because:
        > - a function was still invocated (not a big deal)
        > - each parameter sent to the function are evaluated expression...
        > -> this can be really time consuming in some cases
        >
        > A production process could remove all these log invocations but being able
        > to active them at any time is often interesting (like for remote support)
        >
        > Another approach has been to define DEBUG booleans at application and
        > component levels
        >
        > ex:
        > function myComponentScope() {
        > var
        > DEBUG,
        > log,
        > warn;
        >
        > DEBUG = myApp.DEBUG && true;
        > log = myApp.logger.log;
        > warn = myApp.logger.warn;
        >
        > /****/
        >
        > DEBUG && warn(exp1, exp2, ....);
        >
        > /****/
        > }
        >
        > unfortunately JSLint compliance would require an "if" block
        >
        > if (DEBUG) {
        > warn(exp1, exp2, ....);
        > }
        >
        > Would you still consider the use of "&&" as dangerous in this case ?
        >
        > This approach is just a try... How do you consider it ?
        > Other suggestions ?
        >
        > Any comment welcome ;-)
        >
        > Alexandre.
        >
        >
        >


        [Non-text portions of this message have been removed]
      • Morgaut Alexandre Louis Marc
        ... Well, what I liked with this approach was that even in development, it is quite easy to active and inactive log commands of any component You re right that
        Message 3 of 3 , Apr 5 10:31 AM
        • 0 Attachment
          On 5 avr. 2011, at 19:07, Luke Page wrote:

          > If you use an intelligent compiler, (like closure compiler - not sure what
          > others do it?) then the compile process can strip out debug code and code
          > that is unused.

          Well, what I liked with this approach was that even in development, it is quite easy to active and inactive log commands of any component

          You're right that a loader could could as well choose to load the either the minified or the debug version of the component

          The other thing is that, in production, the application would still require to be reloaded to change the log behavior (overriding a component while in use is more complicated).
          If a user is having a problem and is afraid to loose its data it is not always the best thing to do ;-)

          But I'll try to look this way too

          Thanks you for the suggestion
        Your message has been successfully submitted and would be delivered to recipients shortly.