hadoop DurationInfo 源码

  • 2022-10-20
  • 浏览 (262)

haddop DurationInfo 代码

文件路径:/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/DurationInfo.java

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.apache.hadoop.util;

import java.util.function.Supplier;

import org.slf4j.Logger;

import org.apache.hadoop.classification.InterfaceAudience.Public;
import org.apache.hadoop.classification.InterfaceStability.Unstable;

/**
 * A duration with logging of final state at info or debug
 * in the {@code close()} call.
 * This allows it to be used in a try-with-resources clause, and have the
 * duration automatically logged.
 */
@Public
@Unstable
public class DurationInfo extends OperationDuration
    implements AutoCloseable {

  private final Supplier<String> text;

  private String textStr;

  private final Logger log;

  /**
   * Should the log be at INFO rather than DEBUG.
   */
  private final boolean logAtInfo;

  /**
   * Create the duration text from a {@code String.format()} code call;
   * log output at info level.
   * @param log log to write to
   * @param format format string
   * @param args list of arguments
   */
  public DurationInfo(Logger log, String format, Object... args) {
    this(log, true, format, args);
  }

  /**
   * Create the duration text from a {@code String.format()} code call
   * and log either at info or debug.
   * @param log log to write to
   * @param logAtInfo should the log be at info, rather than debug
   * @param format format string
   * @param args list of arguments
   */
  public DurationInfo(Logger log,
      boolean logAtInfo,
      String format,
      Object... args) {
    this.text = () -> String.format(format, args);
    this.log = log;
    this.logAtInfo = logAtInfo;
    if (logAtInfo) {
      log.info("Starting: {}", getFormattedText());
    } else {
      if (log.isDebugEnabled()) {
        log.debug("Starting: {}", getFormattedText());
      }
    }
  }

  private String getFormattedText() {
    return (textStr == null) ? (textStr = text.get()) : textStr;
  }

  @Override
  public String toString() {
    return getFormattedText() + ": duration " + super.toString();
  }

  @Override
  public void close() {
    finished();
    if (logAtInfo) {
      log.info("{}", this);
    } else {
      if (log.isDebugEnabled()) {
        log.debug("{}", this);
      }
    }
  }
}

相关信息

hadoop 源码目录

相关文章

hadoop ApplicationClassLoader 源码

hadoop AsyncDiskService 源码

hadoop AutoCloseableLock 源码

hadoop BasicDiskValidator 源码

hadoop BlockingThreadPoolExecutorService 源码

hadoop CacheableIPList 源码

hadoop ChunkedArrayList 源码

hadoop ClassUtil 源码

hadoop Classpath 源码

hadoop CleanerUtil 源码

0  赞