client_Hale
2018-09-04 fff1f19a381e6ad295cb3e7bd41cce48e74fbeea
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package com.secondworld.univeralsdk;
 
import android.app.Activity;
import android.util.Log;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
 
/**
 * Created by Administrator on 2018/7/25 0025.
 */
 
public class LogUtil
{
    private static final String Global_TAG = "h2EngineSdk";
    private static final String TAG = "LogUtil";
    public static boolean UPLOAD = false;
    private static boolean SAVE = false;
    private static boolean SHOW = true;
 
    private final static SimpleDateFormat m_DateFormatMS = new SimpleDateFormat(
            "yyyy-MM-dd HH:mm:ss.SSS");
    private final static SimpleDateFormat m_DateFormatDay = new SimpleDateFormat("yyyy-MM-dd");
 
    private static Activity m_Activity;
 
    public static void init(Activity activity, boolean showLog)
    {
        m_Activity = activity;
        SHOW = showLog;
        File _file = new File(getPath());
        if (_file.exists())
        {
            SAVE = true;
            i(TAG, "启动本地log存储功能");
        }
        else
        {
            SAVE = false;
            i(TAG, "未启动本地log存储功能");
        }
    }
 
    public static void i(String tag, String content)
    {
        content = ("[" + m_DateFormatMS.format(new Date()) + "]") + "[" + tag + "] " + content;
        Log.i(Global_TAG, content);
        if (SAVE)
        {
            write(content);
        }
    }
 
    public static void w(String tag, String content)
    {
        content = ("[" + m_DateFormatMS.format(new Date()) + "]") + "[" + tag + "] " + content;
        Log.w(Global_TAG, content);
        if (SAVE)
        {
            write(content);
        }
    }
 
    public static void e(String tag, String content)
    {
        content = ("[" + m_DateFormatMS.format(new Date()) + "]") + "[" + tag + "] " + content;
        Log.e(Global_TAG, content);
        if (SAVE)
        {
            write(content);
        }
    }
 
    private static String getPath()
    {
        if (m_Activity.getExternalFilesDir("") == null)
        {
            Log.i(TAG,"m_Activity.getExternalFilesDir(\"\") == null");
            return "";
        }
 
        return m_Activity.getExternalFilesDir("").getAbsolutePath()
                + File.separator
                + "debugLog"
                + File.separator;
    }
 
    private static void write(String content)
    {
        try
        {
            String time = m_DateFormatDay.format(new Date());
            String fileName = "log_" + time + ".log";
            FileOutputStream fos = new FileOutputStream(getPath() + fileName, true);
            fos.write(content.getBytes());
            fos.flush();
            fos.close();
        } catch (IOException e)
        {
            e.printStackTrace();
        }
    }
 
    public static void upLoad()
    {
        if (!UPLOAD)
        {
            return;
        }
    }
}