<commit_before>/*
 * Copyright 2016-present Facebook, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain
 * a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

package com.facebook.buck.log;

import com.facebook.buck.model.BuildId;
import com.facebook.buck.util.DirectoryCleaner;
import com.facebook.buck.util.Verbosity;
import com.facebook.buck.util.environment.Platform;
import com.google.common.collect.Lists;
import java.io.Closeable;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.logging.Level;
import javax.annotation.Nullable;

public class GlobalStateManager {
  private static final Logger LOG = Logger.get(GlobalStateManager.class);

  private static final GlobalStateManager SINGLETON = new GlobalStateManager();
  private static final String DEFAULT_LOG_FILE_WRITER_KEY = "DEFAULT";
  private static final DirectoryCleaner LOG_FILE_DIR_CLEANER = LogFileHandler.newCleaner();

  // Shared global state.
  private final ConcurrentMap<Long, String> threadIdToCommandId;

  // Global state required by the ConsoleHandler.
  private final ConcurrentMap<String, ConsoleHandlerState.Writer> commandIdToConsoleHandlerWriter;
  private final ConcurrentMap<String, Level> commandIdToConsoleHandlerLevel;

  // Global state required by the LogFileHandler.
  private final ConcurrentMap<String, java.io.Writer> commandIdToLogFileHandlerWriter;
  private final ConcurrentMap<String, Boolean> commandIdToIsSuperconsoleEnabled;
  private final ConcurrentMap<String, Boolean> commandIdToIsDaemon;

  public static GlobalStateManager singleton() {
    return SINGLETON;
  }

  public GlobalStateManager() {
    this.threadIdToCommandId = new ConcurrentHashMap<>();
    this.commandIdToConsoleHandlerWriter = new ConcurrentHashMap<>();
    this.commandIdToConsoleHandlerLevel = new ConcurrentHashMap<>();
    this.commandIdToLogFileHandlerWriter = new ConcurrentHashMap<>();
    this.commandIdToIsSuperconsoleEnabled = new ConcurrentHashMap<>();
    this.commandIdToIsDaemon = new ConcurrentHashMap<>();

    ReferenceCountedWriter defaultWriter =
        createReferenceCountedWriter(
            InvocationInfo.of(
                    new BuildId(),
                    false,
                    false,
                    "launch",
                    new String[0],
                    new String[0],
                    LogConfigSetup.DEFAULT_SETUP.getLogDir())
                .getLogFilePath());
    putReferenceCountedWriter(DEFAULT_LOG_FILE_WRITER_KEY, defaultWriter);
  }

  public LoggerIsMappedToThreadScope setupLoggers(
      final InvocationInfo info,
      OutputStream consoleHandlerStream,
      final OutputStream consoleHandlerOriginalStream,
      final Verbosity consoleHandlerVerbosity) {
    final long threadId = Thread.currentThread().getId();
    final String commandId = info.getCommandId();

    ReferenceCountedWriter defaultWriter = createReferenceCountedWriter(info.getLogFilePath());
    ReferenceCountedWriter newWriter = defaultWriter.newReference();
    // Put defaultWriter to map only after newWriter has been created. Otherwise defaultWriter may
    // get closed before newWriter was created due to concurrency.
    putReferenceCountedWriter(DEFAULT_LOG_FILE_WRITER_KEY, defaultWriter);
    putReferenceCountedWriter(commandId, newWriter);
    createUserFriendlySymLink(info);

    // Setup the shared state.
    threadIdToCommandId.putIfAbsent(threadId, commandId);

    // Setup the ConsoleHandler state.
    commandIdToConsoleHandlerWriter.put(
        commandId, ConsoleHandler.utf8OutputStreamWriter(consoleHandlerStream));
    if (Verbosity.ALL.equals(consoleHandlerVerbosity)) {
      commandIdToConsoleHandlerLevel.put(commandId, Level.ALL);
    }
    commandIdToIsSuperconsoleEnabled.put(commandId, info.getSuperConsoleEnabled());
    commandIdToIsDaemon.put(commandId, info.getIsDaemon());

    return new LoggerIsMappedToThreadScope() {
      @Override
      public Closeable setWriter(ConsoleHandlerState.Writer writer) {
        final ConsoleHandlerState.Writer previousWriter =
            commandIdToConsoleHandlerWriter.get(commandId);
        commandIdToConsoleHandlerWriter.put(commandId, writer);
        return () -> commandIdToConsoleHandlerWriter.put(commandId, previousWriter);
      }

      @Override
      public void close() {
        // Tear down the LogFileHandler state.
        removeReferenceCountedWriter(commandId);

        // Tear down the ConsoleHandler state.
        commandIdToConsoleHandlerWriter.put(
            commandId, ConsoleHandler.utf8OutputStreamWriter(consoleHandlerOriginalStream));
        commandIdToConsoleHandlerLevel.remove(commandId);
        commandIdToIsSuperconsoleEnabled.remove(commandId);
        commandIdToIsDaemon.remove(commandId);

        // Tear down the shared state.
        // NOTE: Avoid iterator in case there's a concurrent change to this map.
        List<Long> allKeys = Lists.newArrayList(threadIdToCommandId.keySet());
        for (Long threadId1 : allKeys) {
          if (commandId.equals(threadIdToCommandId.get(threadId1))) {
            threadIdToCommandId.remove(threadId1);
          }
        }

        try {
          LOG_FILE_DIR_CLEANER.clean(info.getLogDirectoryPath().getParent());
        } catch (IOException e) {
          LOG.info(e, "It's possible another concurrent buck command removed the file.");
        }
      }
    };
  }

  private void createUserFriendlySymLink(final InvocationInfo info) {
    try {<commit_msg>Quiet the logging of failed directory cleaning<commit_after>/*
 * Copyright 2016-present Facebook, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain
 * a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

package com.facebook.buck.log;

import com.facebook.buck.model.BuildId;
import com.facebook.buck.util.DirectoryCleaner;
import com.facebook.buck.util.Verbosity;
import com.facebook.buck.util.environment.Platform;
import com.google.common.collect.Lists;
import java.io.Closeable;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.logging.Level;
import javax.annotation.Nullable;

public class GlobalStateManager {
  private static final Logger LOG = Logger.get(GlobalStateManager.class);

  private static final GlobalStateManager SINGLETON = new GlobalStateManager();
  private static final String DEFAULT_LOG_FILE_WRITER_KEY = "DEFAULT";
  private static final DirectoryCleaner LOG_FILE_DIR_CLEANER = LogFileHandler.newCleaner();

  // Shared global state.
  private final ConcurrentMap<Long, String> threadIdToCommandId;

  // Global state required by the ConsoleHandler.
  private final ConcurrentMap<String, ConsoleHandlerState.Writer> commandIdToConsoleHandlerWriter;
  private final ConcurrentMap<String, Level> commandIdToConsoleHandlerLevel;

  // Global state required by the LogFileHandler.
  private final ConcurrentMap<String, java.io.Writer> commandIdToLogFileHandlerWriter;
  private final ConcurrentMap<String, Boolean> commandIdToIsSuperconsoleEnabled;
  private final ConcurrentMap<String, Boolean> commandIdToIsDaemon;

  public static GlobalStateManager singleton() {
    return SINGLETON;
  }

  public GlobalStateManager() {
    this.threadIdToCommandId = new ConcurrentHashMap<>();
    this.commandIdToConsoleHandlerWriter = new ConcurrentHashMap<>();
    this.commandIdToConsoleHandlerLevel = new ConcurrentHashMap<>();
    this.commandIdToLogFileHandlerWriter = new ConcurrentHashMap<>();
    this.commandIdToIsSuperconsoleEnabled = new ConcurrentHashMap<>();
    this.commandIdToIsDaemon = new ConcurrentHashMap<>();

    ReferenceCountedWriter defaultWriter =
        createReferenceCountedWriter(
            InvocationInfo.of(
                    new BuildId(),
                    false,
                    false,
                    "launch",
                    new String[0],
                    new String[0],
                    LogConfigSetup.DEFAULT_SETUP.getLogDir())
                .getLogFilePath());
    putReferenceCountedWriter(DEFAULT_LOG_FILE_WRITER_KEY, defaultWriter);
  }

  public LoggerIsMappedToThreadScope setupLoggers(
      final InvocationInfo info,
      OutputStream consoleHandlerStream,
      final OutputStream consoleHandlerOriginalStream,
      final Verbosity consoleHandlerVerbosity) {
    final long threadId = Thread.currentThread().getId();
    final String commandId = info.getCommandId();

    ReferenceCountedWriter defaultWriter = createReferenceCountedWriter(info.getLogFilePath());
    ReferenceCountedWriter newWriter = defaultWriter.newReference();
    // Put defaultWriter to map only after newWriter has been created. Otherwise defaultWriter may
    // get closed before newWriter was created due to concurrency.
    putReferenceCountedWriter(DEFAULT_LOG_FILE_WRITER_KEY, defaultWriter);
    putReferenceCountedWriter(commandId, newWriter);
    createUserFriendlySymLink(info);

    // Setup the shared state.
    threadIdToCommandId.putIfAbsent(threadId, commandId);

    // Setup the ConsoleHandler state.
    commandIdToConsoleHandlerWriter.put(
        commandId, ConsoleHandler.utf8OutputStreamWriter(consoleHandlerStream));
    if (Verbosity.ALL.equals(consoleHandlerVerbosity)) {
      commandIdToConsoleHandlerLevel.put(commandId, Level.ALL);
    }
    commandIdToIsSuperconsoleEnabled.put(commandId, info.getSuperConsoleEnabled());
    commandIdToIsDaemon.put(commandId, info.getIsDaemon());

    return new LoggerIsMappedToThreadScope() {
      @Override
      public Closeable setWriter(ConsoleHandlerState.Writer writer) {
        final ConsoleHandlerState.Writer previousWriter =
            commandIdToConsoleHandlerWriter.get(commandId);
        commandIdToConsoleHandlerWriter.put(commandId, writer);
        return () -> commandIdToConsoleHandlerWriter.put(commandId, previousWriter);
      }

      @Override
      public void close() {
        // Tear down the LogFileHandler state.
        removeReferenceCountedWriter(commandId);

        // Tear down the ConsoleHandler state.
        commandIdToConsoleHandlerWriter.put(
            commandId, ConsoleHandler.utf8OutputStreamWriter(consoleHandlerOriginalStream));
        commandIdToConsoleHandlerLevel.remove(commandId);
        commandIdToIsSuperconsoleEnabled.remove(commandId);
        commandIdToIsDaemon.remove(commandId);

        // Tear down the shared state.
        // NOTE: Avoid iterator in case there's a concurrent change to this map.
        List<Long> allKeys = Lists.newArrayList(threadIdToCommandId.keySet());
        for (Long threadId1 : allKeys) {
          if (commandId.equals(threadIdToCommandId.get(threadId1))) {
            threadIdToCommandId.remove(threadId1);
          }
        }

        try {
          LOG_FILE_DIR_CLEANER.clean(info.getLogDirectoryPath().getParent());
        } catch (IOException e) {
          LOG.info(
              "Failed cleaning log directory. It's possible another concurrent buck command "
                  + "removed the file. Error: %s",
              e.getMessage());
          LOG.debug(e, "From exception");
        }
      }
    };
  }

  private void createUserFriendlySymLink(final InvocationInfo info) {
    try {
{'fraction_non_alphanumeric': 0.07883326763894363, 'fraction_numerical': 0.002364998029168309, 'mean_word_length': 3.762012012012012, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2396138', 'n_tokens_mistral': 3316, 'n_tokens_neox': 3242, 'n_words': 818}