Android defines the global log print log and displays information about the caller

Source: Internet
Author: User

Public classlogutils{private static Final BooleanVERBOSE=true;private static Final BooleanDEBUG=true;private static Final BooleanINFO=true;private static Final BooleanWARN=true;private static Final BooleanERROR=true;private static FinalStringSEPARATOR=",";Public static voidv(String message) {if(VERBOSE) {stacktraceelement stacktraceelement = Thread.CurrentThread(). Getstacktrace () [3];            String tag =Getdefaulttag(stacktraceelement);            Log.v(tag, Getloginfo(stacktraceelement) + message);        }    }Public static voidv(String tag, String message) {if(VERBOSE) {stacktraceelement stacktraceelement = Thread.CurrentThread(). Getstacktrace () [3];if(Textutils.IsEmpty(tag)) {tag =Getdefaulttag(stacktraceelement);            } Log.v(tag, Getloginfo(stacktraceelement) + message);        }    }Public static voidD(String message) {if(DEBUG) {stacktraceelement stacktraceelement = Thread.CurrentThread(). Getstacktrace () [3];            String tag =Getdefaulttag(stacktraceelement);            Log.D(tag, Getloginfo(stacktraceelement) + message);        }    }Public static voidD(String tag, String message) {if(DEBUG) {stacktraceelement stacktraceelement = Thread.CurrentThread(). Getstacktrace () [3];if(Textutils.IsEmpty(tag)) {tag =Getdefaulttag(stacktraceelement);            } Log.D(tag, Getloginfo(stacktraceelement) + message);        }    }Public static voidI(String message) {if(INFO) {stacktraceelement stacktraceelement = Thread.CurrentThread(). Getstacktrace () [3];            String tag =Getdefaulttag(stacktraceelement);            Log.I(tag, Message;        }    }Public static voidI(String tag, String message) {if(INFO) {stacktraceelement stacktraceelement = Thread.CurrentThread(). Getstacktrace () [3];if(Textutils.IsEmpty(tag)) {tag =Getdefaulttag(stacktraceelement);            } Log.I(tag, Getloginfo(stacktraceelement) + message);        }    }Public static voidW(String message) {if(WARN) {stacktraceelement stacktraceelement = Thread.CurrentThread(). Getstacktrace () [3];            String tag =Getdefaulttag(stacktraceelement);            Log.W(tag, Getloginfo(stacktraceelement) + message);        }    }Public static voidW(String tag, String message) {if(WARN) {stacktraceelement stacktraceelement = Thread.CurrentThread(). Getstacktrace () [3];if(Textutils.IsEmpty(tag)) {tag =Getdefaulttag(stacktraceelement);            } Log.W(tag, Getloginfo(stacktraceelement) + message);        }    }Public static voide(String message) {off(ERROR) {stacktraceelement stacktraceelement = Thread.CurrentThread(). Getstacktrace () [3];            String tag =Getdefaulttag(stacktraceelement);            Log.e(tag, Message;        }    }Public static voide(String tag, String message) {if(ERROR) {stacktraceelement stacktraceelement = Thread.CurrentThread(). Getstacktrace () [3];if(Textutils.IsEmpty(tag)) {tag =Getdefaulttag(stacktraceelement);            } Log.e(tag, Getloginfo(stacktraceelement) + message);        }    }/*** Gets the default tag name. For example, the log output is called in Mainactivity.java. The tag is mainactivity     */    Public StaticStringGetdefaulttag(Stacktraceelement stacktraceelement) {String fileName = Stacktraceelement.getfilename ();        String stringarray[] = Filename.split ("\\.");        String tag = stringarray[0];returnTag;    }/*** The information contained in the output log     */    Public StaticStringGetloginfo(Stacktraceelement stacktraceelement) {StringBuilder Loginfostringbuilder =NewStringBuilder ();        //Get thread name        String threadname = Thread.CurrentThread(). GetName ();        //Get thread ID        LongThreadID = Thread.CurrentThread(). GetId ();        //Get the filename. That is Xxx.java        String fileName = Stacktraceelement.getfilename ();        //Gets the class name. package name + class name        String className = Stacktraceelement.getclassname ();        //Get method name        String methodName = Stacktraceelement.getmethodname ();        //Get the number of birthday output lines        intlinenumber = Stacktraceelement.getlinenumber ();        Loginfostringbuilder.append ("[ ");        Loginfostringbuilder.append ("Threadid="+ ThreadID). Append (SEPARATOR);        Loginfostringbuilder.append ("Threadname="+ threadname). Append (SEPARATOR);        Loginfostringbuilder.append ("Filename="+ fileName). Append (SEPARATOR);        Loginfostringbuilder.append ("Classname="+ className). Append (SEPARATOR);        Loginfostringbuilder.append ("Methodname="+ methodName). Append (SEPARATOR);        Loginfostringbuilder.append ("Linenumber="+ linenumber);        Loginfostringbuilder.append (" ] ");returnLoginfostringbuilder.tostring ();    }}

Android defines the global log print log and displays information about the caller

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.