Share an Android sorting print, and Logging tool

Source: Internet
Author: User
Tags system log
Printing Tools
public class Logi {/** global output/private static Boolean is_show = true; Private static final String project_name = project_name;//project name/** picture file rack Path */public static final string Pic_folder
    = Environment.getexternalstoragedirectory (). GetAbsolutePath () + "/" +project_name;
            /** Voice File Rack path */public static final String Voice_folder = Environment.getexternalstoragedirectory (). GetAbsolutePath ()

    + "/" +project_name;
            /** Error Output file path */private static final String Error_path = Environment.getexternalstoragedirectory (). GetAbsolutePath ()
    + "/" +project_name+ "/error.txt";
            /** Output Record File path * * Private static final String Log_path = Environment.getexternalstoragedirectory (). GetAbsolutePath ()
    + "/" +project_name+ "/log.txt"; /** the file path for output debugging information * * Private static final String Debug_path = Environment.getexternalstoragedirectory (). Getabsolutepat
    H () + "/" +project_name+ "/debug.txt"; /** time Format: 2015-12-16 13:15:20 999,999 for millisecondsValue */private static final String Time_sytle = "Yyyy-mm-dd HH:mm:ss SSS";
    private static final String Time_sytle_short = "dd Day HH:mm:ss SSS";
    /** format string */private static final SimpleDateFormat formate = new SimpleDateFormat (time_sytle);

    private static final SimpleDateFormat Formateshort = new SimpleDateFormat (time_sytle_short);
    private static String Errorpath = Error_path;
    private static String LogPath = Log_path;

    private static String Debugpath = Debug_path;
    Private static final String i = "I";
    Private static final String e = "E";
    private static final String W = "W";
    private static final String v = "V";

    private static final String d = "D";
        /** print map*/public static String maptostring (map map) {StringBuilder sb = new StringBuilder ();
        Iterator iter = Map.entryset (). iterator ();
            while (Iter.hasnext ()) {Map.entry Entry = (map.entry) iter.next ();
   String key = (string) entry.getkey ();         Object val = (object) entry.getvalue ();
        Sb.append (key+ "=" +val+ ");
    return sb.tostring ();
        /** Set GLOBAL display */public static void Setglobalshow (Boolean isshow) {is_show = Isshow;
    I ("Logi", "Setglobalshow", False, "set global log Print:" +is_show);
    /** get global Display/public static Boolean isglobalshow () {return is_show;
        /** Set Error Output file path */public static void Seterrorpath (String _errorpath) {errorpath = _errorpath;
    I ("Logi", "Seterrorpath", False, "set error log Output path:" +errorpath);
    /** the file path to get the error output/public static String Geterrorpath () {return errorpath;
        /** sets the file path of the output/public static void Setlogpath (String _logpath) {logPath = _logpath;
    I ("Logi", "Setlogpath", False, "set the normal log output path:" +logpath);
    /** Get output file path */public static String Getlogpath () {return logPath; /** * Normal log output to file * @param msgs */public static void WriteToFile (String ... msgs) {if ("". Equals (LogPath)) {e ("Logi", "WriteToFile", True, "log road strength is empty.")
            ");
        Return
        //write file try {WriteFile (logpath,getcurrenttime () + ": \ n" +connectstringbysign (msgs, ","), true);
        catch (IOException E1) {e1.printstacktrace (); }/** * LOG.I output * isshow:true output * False output/public Stati when global output is_show true c void I (String classname,string methodname,boolean isshow,string ... msgs) {if (isshow| |
        Is_show) {log.i (i+ "_" +classname+ "_" +methodname, Connectstringbysign (msgs, ",")); }/** * LOG.E output * isshow:true output * False output/public Stati when global output is_show true c void E (String classname,string methodname,boolean isshow,string ... msgs) {if (isshow| |
        Is_show) {LOG.E (e+ "_" +classname+ "_" +methodname, Connectstringbysign (msgs, ","));
   }
    }

    /**  * LOG.E Output * Isshow:true a certain output * False when the global output is_show is true there is output * Iswritefile:true writes information to a file. public static void E (String classname,string methodname,boolean isshow,boolean iswritefile,string ... msgs) {Strin
        G Tag = e+ "_" +classname+ "_" +methodname;
        String msg = connectstringbysign (msgs, ","); if (isshow| |
        Is_show) {LOG.E (tag, msg); } if (Iswritefile) {if ("". Equals (Errorpath)) {e (ClassName, MethodName, isshow, "error log Road strength is Empty.
                ");
            Return
            //write file try {WriteFile (errorpath,tag+ ": \ n" +msg,true);
            catch (IOException E1) {e1.printstacktrace (); /** * LOG.E Output * Isshow:true a certain output * false output * When the global output is_show is true * iswrite File:true writes information to a file * Exception: Output error message/public static void Writeerrorinfotofile (String classname,string Me ThodName,exception ex) {String tag = e+ "_" +classname+ "_" +methodname;
        Add crash time String currentdate = GetCurrentTime ();  Organization text String text = "\ n" + "Mobile Model:" +android.os.build.model + ", SDK version:" + Android.os.Build.VERSION.SDK  + ", Android version:" + Android.os.Build.VERSION.RELEASE + "time:" + currentdate + ", error message:" + "\ n"
        + Ex.getmessage ();
        Write file try {WriteFile (error_path,tag+ ": \ n" +text,true);
        catch (IOException E1) {e1.printstacktrace ();
     }/** * Writes content to the specified path * @param className * @param methodname * @param tag * @param path * @param msgs */public static void E (String classname,string methodname,string tag,string path,string ... msg s) {if (!
        Configvalue.isdebug) {return;
        Date date = new Date ();
    Tag = Formateshort.format (date) + "" +classname + "" + methodname + "" + tag;    String msg = connectstringbysign (msgs, ","); if ("". Equals (Path)) {e (className, methodname,true, error log Road strength is empty.)
            ");
        Return
        //write file try {WriteFile (path,tag+ ": \ n" +msg,true);
        catch (IOException E1) {e1.printstacktrace ();
        /** Debug information to debug path/public static void WriteDebugInfo (String classname,string methodname,string ... msgs) { if (!
        Configvalue.isdebug) {return;
        Date date = new Date ();
        String tag = Formate.format (date) + "" +classname + "" + methodname;
        String msg = connectstringbysign (msgs, ","); if ("" ". Equals (Debugpath)) {e (className, methodname,true," error log Road strength is empty.)
            ");
        Return
        //write file try {WriteFile (debugpath,tag+ ":" +msg,true);
        catch (IOException E1) {e1.printstacktrace (); }/** * LOG.W output * isshow:true certain output *        False has output/public static void W (String classname,string Methodname,boolean isshow,str when the global output Is_show is true) ing ... msgs) {if (isshow| |
        Is_show) {LOG.W (w+ "_" +classname+ "_" +methodname, Connectstringbysign (msgs, ",")); }/** * LOG.V output * isshow:true output * False output/public Stati when global output is_show true c void V (String classname,string methodname,boolean isshow,string ... msgs) {if (isshow| |
        Is_show) {LOG.V (v+ "_" +classname+ "_" +methodname, Connectstringbysign (msgs, ",")); }/** * LOG.D output * isshow:true output * False output/public Stati when global output is_show true c void D (String classname,string methodname,boolean isshow,string ... msgs) {if (isshow| |
        Is_show) {LOG.D (d+ "_" +classname+ "_" +methodname, Connectstringbysign (msgs, ","));
   }/** connect STRs with sign/private static String connectstringbysign (string[] strs,string sign) {     if (strs.length<=0) {return "";
        StringBuilder sb = new StringBuilder ();
            for (int i=0;i<strs.length-1;i++) {sb.append (strs[i]);
        Sb.append (sign);
        } sb.append (Strs[strs.length-1]);
    return sb.tostring (); /** * Writes a string to the specified file * * @param path * @param text * @param isappend/Private stat IC void WriteFile (String path,string text, Boolean isappend) throws IOException {File File = new file
        (path);
        E ("Logi", "WriteFile", false, Path);
        if (!file.getparentfile (). exists ()) {File.getparentfile (). Mkdirs ();
        } if (!file.exists ()) {file.createnewfile ();
        } FileWriter fw = null;
            try {fw = new FileWriter (file, isappend);
            Fw.write ("\ n" +text);
        Fw.flush ();
        catch (FileNotFoundException e) {throw e; } finally {if (fw!= null) {fw.close (); /** * Get Current time * @return/public static String GetCurrentTime () {Simpleda
        Teformat SDF = new SimpleDateFormat (time_sytle);
    Return Sdf.format (New Date ()); /** put the corresponding path file under the specified file/public static void Putfiletopath (String filepath,string newfilepath) {log.i ("ico"
        + "FilePath", FilePath);
        LOG.I ("ico" + "Newfilepath", Newfilepath);
        FileInputStream is = null;
        FileOutputStream OS = null;
            try {File File = new file (FilePath);
            LOG.I ("ico" + "file.exists ()", "File.exists ():" +file.exists ());
            LOG.I ("ico" + "file.isdirectory ()", "File.isdirectory ():" +file.isdirectory ());
                if (!file.exists ()) {LOG.E ("ico", "file does not exist");
            return;
            } File NewFile = new file (Newfilepath);
         if (!newfile.getparentfile (). exists ()) {       Newfile.getparentfile (). Mkdirs ();

            } if (!newfile.exists ()) {newfile.createnewfile ();
            is = new FileInputStream (file);
            Open File OS = new FileOutputStream (newFile);
            byte[] buffer = new byte[1024];
            int count = 0;
                Copy the file to the destination while (count = is.read (buffer) > 0) {//write as Copy
            Os.write (buffer, 0, count);
        The catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();
                }finally{try {if (is!=null) is.close ();
            if (os!=null) os.close ();
            catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();
 }
        }

    }
}
 the tool has a slightly larger number of parameters than the system log, so it is slightly cumbersome to use, but it can be printed in a global control, improving the filtering of information for the debugging process, and the tool also provides output for various output logs.
For better advice, please leave a message, thank you. 

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.