logcat.java

    0

    0

    This code shows a log entry for a certain tag and message. First, the StackTraceElement array is populated with the stack trace of the current thread. Next, the current index is incremented by 1 and the loop is iterated over the stackTraceElement array. For each stackTraceElement, the MethodName and LineNumber fields are extracted. Finally, the Log object is initialized with the tag, msg and the current index. The fullClassName, className and methodName are strings, and the lineNumber is a number.

    
      public static void showLogCat(String tag, String msg) {
    
      StackTraceElement[] stackTraceElement = Thread.currentThread()
        .getStackTrace();
      int currentIndex = -1;
      for (int i = 0; i < stackTraceElement.length; i++) {
      if (stackTraceElement[i].getMethodName().compareTo("showLogCat") == 0) {
        currentIndex = i + 1;
        break;
      }
      }
    
      String fullClassName = stackTraceElement[currentIndex].getClassName();
      String className = fullClassName.substring(fullClassName
        .lastIndexOf(".") + 1);
      String methodName = stackTraceElement[currentIndex].getMethodName();
      String lineNumber = String.valueOf(stackTraceElement[currentIndex]
        .getLineNumber());
    
      Log.i(tag, msg);
      Log.i(tag + " position", "at " + fullClassName + "." + methodName + "("
        + className + ".java:" + lineNumber + ")");
    
    }
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.