package com.jraska.console.timber;

import android.util.Log;
import com.jraska.console.Console;
import timber.log.Timber;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public final class ConsoleTree extends Timber.Tree {

  //region Constants

  private static final int CALL_STACK_INDEX = 6;
  private static final Pattern ANONYMOUS_CLASS = Pattern.compile("(\\$\\d+)+$");

  //endregion

  //region Tree impl

  @Override
  protected boolean isLoggable(int priority) {
    return Console.consoleViewsCount() >= 1 && super.isLoggable(priority);
  }

  @Override
  protected final void log(int priority, String tag, String message, Throwable t) {
    if (tag == null) {
      tag = getTag();
    }

    String consoleMessage;
    if (tag == null) {
      consoleMessage = String.format("%s: %s", toPriorityString(priority), message);
    } else {
      consoleMessage = String.format("%s/%s: %s", toPriorityString(priority), tag, message);
    }

    writeToConsole(consoleMessage);
  }

  //endregion

  //region Methods

  protected void writeToConsole(String consoleMessage) {
    Console.writeLine(consoleMessage);
  }

  String createStackElementTag(StackTraceElement element) {
    String tag = element.getClassName();
    Matcher matcher = ANONYMOUS_CLASS.matcher(tag);
    if (matcher.find()) {
      tag = matcher.replaceAll("");
    }

    return tag.substring(tag.lastIndexOf('.') + 1);
  }

  String getTag() {
    StackTraceElement[] stackTrace = new Throwable().getStackTrace();
    if (stackTrace.length <= CALL_STACK_INDEX) {
      return null;
    }
    return createStackElementTag(stackTrace[CALL_STACK_INDEX]);
  }

  protected String toPriorityString(int priority) {
    switch (priority) {
      case Log.ASSERT:
        return "WTF";
      case Log.ERROR:
        return "E";
      case Log.WARN:
        return "W";
      case Log.INFO:
        return "I";
      case Log.DEBUG:
        return "D";
      case Log.VERBOSE:
        return "V";

      default:
        throw new IllegalArgumentException();
    }
  }

  //endregion

}

@@ -395,32 +395,271 @@
 %0A  //endregion%0A%0A
+  //region Fields%0A%0A  private final int _minPriority;%0A%0A  //endregion%0A%0A  //region Constructors%0A%0A  public ConsoleTree() %7B%0A    this(Log.VERBOSE);%0A  %7D%0A%0A  public ConsoleTree(int minPriority) %7B%0A    _minPriority = minPriority;%0A  %7D%0A%0A  //endregion%0A%0A
   //region Tree 
@@ -733,16 +733,44 @@
   return
+ priority %3E= _minPriority &&
  Console
@@ -798,38 +798,8 @@
 %3E= 1
- && super.isLoggable(priority)
 ;%0A  

Add possibility to log only some priority for ConsoleTree