<commit_before>// Copyright 2000-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.idea;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.RemovalListener;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.objectTree.ThrowableInterner;
import com.intellij.openapi.util.text.StringUtil;
import org.apache.log4j.Level;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

public abstract class MutedLogger extends Logger {

  private static final Cache<String, LoggerWithCounter> myCache = Caffeine.newBuilder()
    .maximumSize(1000)
    .expireAfterAccess(1, TimeUnit.MINUTES)
    .removalListener((RemovalListener<String, LoggerWithCounter>)(key, value, cause) -> {
      if (key != null && value != null) {
        String hash = Objects.requireNonNull(StringUtil.substringBefore(key, ":"));
        value.logger.logRemoved(Integer.parseInt(hash), value.counter.get());
      }
    })
    .build();

  public static void dropCaches() {
    myCache.invalidateAll();
    myCache.cleanUp();
  }

  @SuppressWarnings("NonConstantLogger") protected final Logger myDelegate;

  protected MutedLogger(@NotNull Logger delegate) {
    myDelegate = delegate;
  }

  protected final boolean isAlreadyReported(@Nullable Throwable throwable) {
    if (throwable == null) {
      return false;
    }

    int hash = ThrowableInterner.computeTraceHashCode(throwable);
    String key = hash + ":" + throwable;
    LoggerWithCounter holder = Objects.requireNonNull(myCache.get(key, __ -> new LoggerWithCounter(this, new AtomicInteger())));
    if (holder.counter.compareAndSet(0, 1)) {
      logAdded(hash, throwable);
      return false;
    }

    logOccurrences(hash, throwable, holder.counter.incrementAndGet());
    return true;
  }

  protected abstract void logAdded(int hash, @NotNull Throwable t);

  protected abstract void logOccurrences(int hash, @NotNull Throwable t, int occurrences);

  protected abstract void logRemoved(int hash, int occurrences);

  @Override
  public boolean isDebugEnabled() {
    return myDelegate.isDebugEnabled();
  }
<commit_msg>clean up: change prefix to 'our' for static field in MutedLogger<commit_after>// Copyright 2000-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.idea;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.RemovalListener;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.objectTree.ThrowableInterner;
import com.intellij.openapi.util.text.StringUtil;
import org.apache.log4j.Level;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

public abstract class MutedLogger extends Logger {

  private static final Cache<String, LoggerWithCounter> ourCache = Caffeine.newBuilder()
    .maximumSize(1000)
    .expireAfterAccess(1, TimeUnit.MINUTES)
    .removalListener((RemovalListener<String, LoggerWithCounter>)(key, value, cause) -> {
      if (key != null && value != null) {
        String hash = Objects.requireNonNull(StringUtil.substringBefore(key, ":"));
        value.logger.logRemoved(Integer.parseInt(hash), value.counter.get());
      }
    })
    .build();

  public static void dropCaches() {
    ourCache.invalidateAll();
    ourCache.cleanUp();
  }

  @SuppressWarnings("NonConstantLogger") protected final Logger myDelegate;

  protected MutedLogger(@NotNull Logger delegate) {
    myDelegate = delegate;
  }

  protected final boolean isAlreadyReported(@Nullable Throwable throwable) {
    if (throwable == null) {
      return false;
    }

    int hash = ThrowableInterner.computeTraceHashCode(throwable);
    String key = hash + ":" + throwable;
    LoggerWithCounter holder = Objects.requireNonNull(ourCache.get(key, __ -> new LoggerWithCounter(this, new AtomicInteger())));
    if (holder.counter.compareAndSet(0, 1)) {
      logAdded(hash, throwable);
      return false;
    }

    logOccurrences(hash, throwable, holder.counter.incrementAndGet());
    return true;
  }

  protected abstract void logAdded(int hash, @NotNull Throwable t);

  protected abstract void logOccurrences(int hash, @NotNull Throwable t, int occurrences);

  protected abstract void logRemoved(int hash, int occurrences);

  @Override
  public boolean isDebugEnabled() {
    return myDelegate.isDebugEnabled();
  }

{'fraction_non_alphanumeric': 0.10561797752808989, 'fraction_numerical': 0.007354443309499489, 'mean_word_length': 4.45819397993311, 'pattern_counts': {'":': 4, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5215867', 'n_tokens_mistral': 1490, 'n_tokens_neox': 1427, 'n_words': 344}