<commit_before>/**
 * @license
 * Copyright 2018 The FOAM Authors. All Rights Reserved.
 * http://www.apache.org/licenses/LICENSE-2.0
 */


foam.CLASS({
  package: 'foam.nanos.logger',
  name: 'DiscardLogger',
  extends: 'foam.nanos.logger.ProxyLogger',
  requires: [
    'foam.nanos.logger.LogLevel'
  ],
  properties: [
    {
      class: 'Object',
      name: 'lastUniqueObject',
      value: null
    },
    {
      class: 'Int',
      name: 'repeatCount',
      value: 0
    },
    {
      class: 'Enum',
      of: 'foam.nanos.logger.LogLevel',
      name: 'lastLogLevel',
      factory: function() { return foam.nanos.logger.LogLevel.INFO; },
      javaFactory: `return foam.nanos.logger.LogLevel.INFO;`
    }
  ],
  methods: [
    {
        name: 'newLogHandler',
        args: [ 
          {
          name: 'logLevelName',
          javaType: 'Enum'
          },
          {
            name: 'setLastLogLevel',
            javaType: 'Boolean'
          },
          {
            name: 'args',
            javaType: 'Object...'
          },
        ],
        javaReturns: 'void',
        javaCode: `
        switch ( LogLevel.values()[logLevelName.ordinal()] ) {
          case DEBUG: {
              setRepeatCount(1);
              setLastUniqueObject(args);
              getDelegate().debug(args);  
              if (setLastLogLevel){
                setLastLogLevel(LogLevel.DEBUG);
              break;
          }
              }
              case INFO:  {
                setRepeatCount(1);
                setLastUniqueObject(args);
                getDelegate().info(args);  
                if (setLastLogLevel){
                  setLastLogLevel(LogLevel.INFO);
                }
                break;
              }
              case WARNING:  {
                setRepeatCount(1);
                setLastUniqueObject(args);
                getDelegate().warning(args);  
                if (setLastLogLevel){
                  setLastLogLevel(LogLevel.WARNING);
                }
                break;
                
                }     
                case ERROR:  {
                  setRepeatCount(1);
                  setLastUniqueObject(args);
                  getDelegate().error(args);  
                  if (setLastLogLevel){
                    setLastLogLevel(LogLevel.ERROR);
                  }
                break;
                }
              }
    `  
    },
    {
      name: 'logLastLogRepeats',
      javaReturns: 'void',
      javaCode: 
      `
      switch ( getLastLogLevel() ) {
        case DEBUG: {
          getDelegate().debug("The last log was repeated " + getRepeatCount() + " times" );
          break;
        }
        case INFO:  {
          getDelegate().info("The last log was repeated "+ getRepeatCount()  + " times" );
          break;
        }
        case WARNING:  {
          getDelegate().warning("The last log was repeated "+ getRepeatCount()  + " times" );
          break;      
        }        
        case ERROR:  {
          getDelegate().error("The last log was repeated " + getRepeatCount() + " times" );
          break;
        }
      }     
      `
    },
    {
      name: 'repeatLogConstructor',
      args: [ 
        {
        name: 'logLevelName',
        javaType: 'Enum'
        },
        {
          name: 'args',
          javaType: 'Object...',
        }
      ],
      synchronized: true,
      javaReturns: 'void',
      javaCode: `
        if ( getLastLogLevel().ordinal() != logLevelName.ordinal() ){
              if (getRepeatCount() > 1){
                logLastLogRepeats();   
              }
              newLogHandler(logLevelName,true,args);
              return;
            };
            
        if ( getLastUniqueObject() == null){
          newLogHandler(logLevelName,false,args);
          }
        else {
          Object[] o = (Object[]) getLastUniqueObject();
          if ( ! (args.length == o.length ) ){
            // Different Message
            if (getRepeatCount() > 1){
              logLastLogRepeats();   
            }
            newLogHandler(logLevelName,false,args);
          }
             
          else{
            for ( int i = 0; i < args.length; i ++) {
              if  ( ! ( args[i].equals(o[i]) && !(args[i] instanceof Exception) )  ) {
                if (getRepeatCount() > 1){
                  logLastLogRepeats();   
                }
                newLogHandler(logLevelName,false,args);
                return;        
              } 
            }
            setRepeatCount(getRepeatCount()+1);
          }
        }
      `
    },
    {
      name: 'debug',
      args:[
        {
          name: 'args',
          javaType: 'Object...',
        }
      ],
      javaReturns: 'void',
      javaCode: `
      Enum logLevelName = LogLevel.DEBUG;
      repeatLogConstructor(logLevelName,args);
      `   
    },
    {
      name: 'info',
      args:[
        {
          name: 'args',
          javaType: 'Object...',
        }
      ],
      javaReturns: 'void',
      javaCode: `
      Enum logLevelName = LogLevel.INFO;
      repeatLogConstructor(logLevelName,args);
      `
    },
    {
      name: 'warning',
      args:[
        {
          name: 'args',
          javaType: 'Object...',
        }
      ],
      javaReturns: 'void',
      javaCode: `
      Enum logLevelName = LogLevel.WARNING;
      repeatLogConstructor(logLevelName,args);
      `
    },
    {
      name: 'error',
      args:[
        {
          name: 'args',
          javaType: 'Object...',
        }
      ],
      javaReturns: 'void',
      javaCode: `
      Enum logLevelName = LogLevel.ERROR;
      repeatLogConstructor(logLevelName,args);
      `
    }
  ]
});
 
// implement hashcode to check instead of equals<commit_msg>move a break; in setLastLogLevel, prettify code in general<commit_after>/**
 * @license
 * Copyright 2018 The FOAM Authors. All Rights Reserved.
 * http://www.apache.org/licenses/LICENSE-2.0
 */


foam.CLASS({
  package: 'foam.nanos.logger',
  name: 'DiscardLogger',
  extends: 'foam.nanos.logger.ProxyLogger',
  requires: [
    'foam.nanos.logger.LogLevel'
  ],

  properties: [
    {
      class: 'Object',
      name: 'lastUniqueObject',
      value: null
    },
    {
      class: 'Int',
      name: 'repeatCount',
      value: 0
    },
    {
      class: 'Enum',
      of: 'foam.nanos.logger.LogLevel',
      name: 'lastLogLevel',
      factory: function() { return foam.nanos.logger.LogLevel.INFO; },
      // javaFactory: `return foam.nanos.logger.LogLevel.INFO;`
    }
  ],

  methods: [
    {
      name: 'newLogHandler',
      args: [ 
        {
          name: 'logLevelName',
          javaType: 'Enum'
        },
        {
          name: 'setLastLogLevel',
          javaType: 'Boolean'
        },
        {
          name: 'args',
          javaType: 'Object...'
        },
      ],
      javaReturns: 'void',
      javaCode: `
  switch ( LogLevel.values()[logLevelName.ordinal()] ) {
    case DEBUG: {
      setRepeatCount(1);
      setLastUniqueObject(args);
      getDelegate().debug(args);  
      if (setLastLogLevel){
        setLastLogLevel(LogLevel.DEBUG);
      }
      break;
    }
    case INFO: {
      setRepeatCount(1);
      setLastUniqueObject(args);
      getDelegate().info(args);  
      if (setLastLogLevel){
        setLastLogLevel(LogLevel.INFO);
      }
      break;
    }
    case WARNING: {
      setRepeatCount(1);
      setLastUniqueObject(args);
      getDelegate().warning(args);  
      if (setLastLogLevel){
        setLastLogLevel(LogLevel.WARNING);
      }
      break;
      
    }     
    case ERROR: {
      setRepeatCount(1);
      setLastUniqueObject(args);
      getDelegate().error(args);  
      if (setLastLogLevel){
        setLastLogLevel(LogLevel.ERROR);
      }
      break;
    }
  }
`  
    },
    {
      name: 'logLastLogRepeats',
      javaReturns: 'void',
      javaCode: 
      `
  switch ( getLastLogLevel() ) {
    case DEBUG: {
      getDelegate().debug("The last log was repeated " + getRepeatCount() + " times" );
      break;
    }
    case INFO: {
      getDelegate().info("The last log was repeated "+ getRepeatCount()  + " times" );
      break;
    }
    case WARNING: {
      getDelegate().warning("The last log was repeated "+ getRepeatCount()  + " times" );
      break;      
    }        
    case ERROR: {
      getDelegate().error("The last log was repeated " + getRepeatCount() + " times" );
      break;
    }
  }     
`
    },
    {
      name: 'repeatLogConstructor',
      args: [ 
        {
          name: 'logLevelName',
          javaType: 'Enum'
        },
        {
          name: 'args',
          javaType: 'Object...'
        }
      ],
      synchronized: true,
      javaReturns: 'void',
      javaCode: `
  if ( getLastLogLevel().ordinal() != logLevelName.ordinal() ) {
    if ( getRepeatCount() > 1 ) {
      logLastLogRepeats();   
    }
    newLogHandler(logLevelName, true, args);
    return;
  };
  
  if ( getLastUniqueObject() == null ) {
    newLogHandler(logLevelName,false,args);
  } else {
    Object[] o = (Object[]) getLastUniqueObject();
    if ( ! (args.length == o.length ) ) {
      // Different Message
      if ( getRepeatCount() > 1 ) {
        logLastLogRepeats();   
      }
      newLogHandler(logLevelName,false,args);
    } else {
      for ( int i = 0 ; i < args.length ; i++ ) {
        if ( ! ( args[i].equals(o[i]) && !(args[i] instanceof Exception) ) ) {
          // simplify above logic
          if ( getRepeatCount() > 1 ) {
            logLastLogRepeats();   
          }
          newLogHandler(logLevelName, false, args);
          return;        
        } 
      }
      setRepeatCount( getRepeatCount() + 1 );
    }
  }
`
    },
    {
      name: 'debug',
      args:[
        {
          name: 'args',
          javaType: 'Object...'
        }
      ],
      javaReturns: 'void',
      javaCode: `
  Enum logLevelName = LogLevel.DEBUG;
  repeatLogConstructor(logLevelName, args);
  `   
    },
    {
      name: 'info',
      args:[
        {
          name: 'args',
          javaType: 'Object...'
        }
      ],
      javaReturns: 'void',
      javaCode: `
  Enum logLevelName = LogLevel.INFO;
  repeatLogConstructor(logLevelName,args);
`
    },
    {
      name: 'warning',
      args:[
        {
          name: 'args',
          javaType: 'Object...'
        }
      ],
      javaReturns: 'void',
      javaCode: `
  Enum logLevelName = LogLevel.WARNING;
  repeatLogConstructor(logLevelName,args);
`
    },
    {
      name: 'error',
      args:[
        {
          name: 'args',
          javaType: 'Object...',
        }
      ],
      javaReturns: 'void',
      javaCode: `
  Enum logLevelName = LogLevel.ERROR;
  repeatLogConstructor(logLevelName,args);
`
    }
  ]
});
 
// implement hashcode to check instead of equals
{'fraction_non_alphanumeric': 0.13074302550409722, 'fraction_numerical': 0.002946321701500783, 'mean_word_length': 1.4557992312909789, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3816106', 'n_tokens_mistral': 3286, 'n_tokens_neox': 3222, 'n_words': 571}