// Path: src/main/java/org/zeroturnaround/exec/stream/CallerLoggerUtil.java
// public abstract class CallerLoggerUtil {
// 
//   /**
//    * Returns full name for the caller class' logger.
//    *
//    * @param name name of the logger. In case of full name (it contains dots) same value is just returned.
//    * In case of short names (no dots) the given name is prefixed by caller's class name and a dot.
//    * In case of <code>null</code> the caller's class name is just returned.
//    * @return full name for the caller class' logger.
//    */
//   public static String getName(String name) {
//     return getName(name, 1);
//   }
// 
//   /**
//    * Returns full name for the caller class' logger.
//    *
//    * @param name name of the logger. In case of full name (it contains dots) same value is just returned.
//    * In case of short names (no dots) the given name is prefixed by caller's class name and a dot.
//    * In case of <code>null</code> the caller's class name is just returned.
//    * @param level no of call stack levels to get the caller (0 means the caller of this method).
//    * @return full name for the caller class' logger.
//    */
//   public static String getName(String name, int level) {
//     level++;
//     String fullName;
//     if (name == null)
//       fullName = getCallerClassName(level);
//     else if (name.contains("."))
//       fullName = name;
//     else
//       fullName = getCallerClassName(level) + "." + name;
//     return fullName;
//   }
// 
//   /**
//    * @return caller class name of the given level.
//    */
//   private static String getCallerClassName(int level) {
//     return Thread.currentThread().getStackTrace()[level + 2].getClassName();
//   }
// 
// }


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zeroturnaround.exec.stream.CallerLoggerUtil;
/*
 * Copyright (C) 2014 ZeroTurnaround <support@zeroturnaround.com>
 * Contains fragments of code from Apache Commons Exec, rights owned
 * by Apache Software Foundation (ASF).
 *
 * 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 org.zeroturnaround.exec.stream.slf4j;


/**
 * Creates output streams that write to {@link Logger}s.
 *
 * @author Rein Raudjärv
 */
public class Slf4jStream {

  private final Logger log;

  private Slf4jStream(Logger log) {
    this.log = log;
  }

  /**
   * @param log logger which an output stream redirects to.
   * @return Slf4jStream with the given logger.
   */
  public static Slf4jStream of(Logger log) {
    return new Slf4jStream(log);
  }

  /**
   * @param klass class which is used to get the logger's name.
   * @return Slf4jStream with a logger named after the given class.
   */
  public static Slf4jStream of(Class<?> klass) {
    return of(LoggerFactory.getLogger(klass));
  }

  /**
   * Constructs a logger from a class name and an additional name,
   * appended to the end. So the final logger name will be:
   * <blockquote><code>klass.getName() + "." + name</code></blockquote>
   *
   * @param klass class which is used to get the logger's name.
   * @param name logger's name, appended to the class name.
   * @return Slf4jStream with a logger named after the given class.
   *
   * @since 1.8
   */
  public static Slf4jStream of(Class<?> klass, String name) {
    if (name == null) {
      return of(klass);
    } else {
      return of(LoggerFactory.getLogger(klass.getName() + "." + name));
    }
  }

  /**
   * @param name logger's name (full or short).
   *    In case of short name (no dots) the given name is prefixed by caller's class name and a dot.
   * @return Slf4jStream with the given logger.
   */
  public static Slf4jStream of(String name) {
// Path: src/main/java/org/zeroturnaround/exec/stream/CallerLoggerUtil.java
// public abstract class CallerLoggerUtil {
// 
//   /**
//    * Returns full name for the caller class' logger.
//    *
//    * @param name name of the logger. In case of full name (it contains dots) same value is just returned.
//    * In case of short names (no dots) the given name is prefixed by caller's class name and a dot.
//    * In case of <code>null</code> the caller's class name is just returned.
//    * @return full name for the caller class' logger.
//    */
//   public static String getName(String name) {
//     return getName(name, 1);
//   }
// 
//   /**
//    * Returns full name for the caller class' logger.
//    *
//    * @param name name of the logger. In case of full name (it contains dots) same value is just returned.
//    * In case of short names (no dots) the given name is prefixed by caller's class name and a dot.
//    * In case of <code>null</code> the caller's class name is just returned.
//    * @param level no of call stack levels to get the caller (0 means the caller of this method).
//    * @return full name for the caller class' logger.
//    */
//   public static String getName(String name, int level) {
//     level++;
//     String fullName;
//     if (name == null)
//       fullName = getCallerClassName(level);
//     else if (name.contains("."))
//       fullName = name;
//     else
//       fullName = getCallerClassName(level) + "." + name;
//     return fullName;
//   }
// 
//   /**
//    * @return caller class name of the given level.
//    */
//   private static String getCallerClassName(int level) {
//     return Thread.currentThread().getStackTrace()[level + 2].getClassName();
//   }
// 
// }


// Path: src/main/java/org/zeroturnaround/exec/stream/slf4j/Slf4jStream.java
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zeroturnaround.exec.stream.CallerLoggerUtil;

/*
 * Copyright (C) 2014 ZeroTurnaround <support@zeroturnaround.com>
 * Contains fragments of code from Apache Commons Exec, rights owned
 * by Apache Software Foundation (ASF).
 *
 * 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 org.zeroturnaround.exec.stream.slf4j;


/**
 * Creates output streams that write to {@link Logger}s.
 *
 * @author Rein Raudjärv
 */
public class Slf4jStream {

  private final Logger log;

  private Slf4jStream(Logger log) {
    this.log = log;
  }

  /**
   * @param log logger which an output stream redirects to.
   * @return Slf4jStream with the given logger.
   */
  public static Slf4jStream of(Logger log) {
    return new Slf4jStream(log);
  }

  /**
   * @param klass class which is used to get the logger's name.
   * @return Slf4jStream with a logger named after the given class.
   */
  public static Slf4jStream of(Class<?> klass) {
    return of(LoggerFactory.getLogger(klass));
  }

  /**
   * Constructs a logger from a class name and an additional name,
   * appended to the end. So the final logger name will be:
   * <blockquote><code>klass.getName() + "." + name</code></blockquote>
   *
   * @param klass class which is used to get the logger's name.
   * @param name logger's name, appended to the class name.
   * @return Slf4jStream with a logger named after the given class.
   *
   * @since 1.8
   */
  public static Slf4jStream of(Class<?> klass, String name) {
    if (name == null) {
      return of(klass);
    } else {
      return of(LoggerFactory.getLogger(klass.getName() + "." + name));
    }
  }

  /**
   * @param name logger's name (full or short).
   *    In case of short name (no dots) the given name is prefixed by caller's class name and a dot.
   * @return Slf4jStream with the given logger.
   */
  public static Slf4jStream of(String name) {
    return of(LoggerFactory.getLogger(CallerLoggerUtil.getName(name, 1)));