package com.secondworld.universalsdk;
|
|
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;
|
}
|
}
|
}
|