mirror of
https://github.com/niushuai233/DevControl.git
synced 2024-10-27 22:43:20 +08:00
fix: 初始化逻辑修改
This commit is contained in:
parent
3a817c0fc5
commit
aef284d685
@ -17,6 +17,8 @@ import cc.niushuai.project.devcontrol.base.util.Global;
|
|||||||
import cc.niushuai.project.devcontrol.base.util.Keys;
|
import cc.niushuai.project.devcontrol.base.util.Keys;
|
||||||
import cc.niushuai.project.devcontrol.base.util.XLog;
|
import cc.niushuai.project.devcontrol.base.util.XLog;
|
||||||
import cc.niushuai.project.devcontrol.db.DB;
|
import cc.niushuai.project.devcontrol.db.DB;
|
||||||
|
import cc.niushuai.project.devcontrol.db.entity.SysConfig;
|
||||||
|
import cc.niushuai.project.devcontrol.db.util.DBHelper;
|
||||||
import cn.hutool.core.io.FileUtil;
|
import cn.hutool.core.io.FileUtil;
|
||||||
import cn.hutool.core.text.StrPool;
|
import cn.hutool.core.text.StrPool;
|
||||||
import cn.hutool.core.util.RuntimeUtil;
|
import cn.hutool.core.util.RuntimeUtil;
|
||||||
@ -30,22 +32,16 @@ public class App {
|
|||||||
|
|
||||||
// 申请权限
|
// 申请权限
|
||||||
requestPermissions(activity);
|
requestPermissions(activity);
|
||||||
|
|
||||||
// 初始化日志相关内容
|
|
||||||
initLog(activity);
|
|
||||||
|
|
||||||
XLog.i(Keys.Tag.APP_INIT, "{}====================================", System.lineSeparator());
|
|
||||||
XLog.i(Keys.Tag.APP_INIT, "应用{}启动", BuildConfig.APP_NAME);
|
|
||||||
XLog.i(Keys.Tag.APP_INIT, "====================================");
|
|
||||||
|
|
||||||
// 检查root权限
|
|
||||||
checkRoot(activity);
|
|
||||||
// 初始化数据库
|
// 初始化数据库
|
||||||
initDb(activity);
|
initDb(activity);
|
||||||
|
// 初始化日志相关内容
|
||||||
|
initLogSupport(activity);
|
||||||
|
// 检查root权限
|
||||||
|
checkRoot(activity);
|
||||||
// 重建设备信息缓存
|
// 重建设备信息缓存
|
||||||
Global.initDeviceInfoMap();
|
// Global.initDeviceInfoMap();
|
||||||
|
|
||||||
XLog.i(Keys.Tag.APP_INIT, "初始化完毕");
|
XLog.i(Keys.Tag.APP_INIT, "应用{}初始化完毕", BuildConfig.APP_NAME);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -115,7 +111,7 @@ public class App {
|
|||||||
* @date: 2022/10/26 14:58
|
* @date: 2022/10/26 14:58
|
||||||
*/
|
*/
|
||||||
private static void initDb(Activity activity) {
|
private static void initDb(Activity activity) {
|
||||||
XLog.v(Keys.Tag.APP_INIT, "加载SQLite");
|
XLog.d(Keys.Tag.APP_INIT, "加载SQLite");
|
||||||
DB.getInstance().init(activity);
|
DB.getInstance().init(activity);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -125,10 +121,27 @@ public class App {
|
|||||||
* @author niushuai
|
* @author niushuai
|
||||||
* @date: 2022/10/26 15:00
|
* @date: 2022/10/26 15:00
|
||||||
*/
|
*/
|
||||||
private static void initLog(Activity activity) {
|
private static void initLogSupport(Activity activity) {
|
||||||
|
|
||||||
|
SysConfig switchConfig = DBHelper.configOneByKey(Keys.SETUP_LOG_SWITCH);
|
||||||
|
if (null != switchConfig) {
|
||||||
|
XLog.SWITCH = Boolean.valueOf(switchConfig.getValue());
|
||||||
|
XLog.SWITCH_FOR_FILE = Boolean.valueOf(switchConfig.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
SysConfig levelConfig = DBHelper.configOneByKey(Keys.SETUP_LOG_LEVEL);
|
||||||
|
if (null != levelConfig) {
|
||||||
|
XLog.ROOT_LEVEL = XLog.Level.transform(levelConfig.getValue());
|
||||||
|
XLog.ROOT_LEVEL_NAME = XLog.Level.transform(XLog.ROOT_LEVEL);
|
||||||
|
}
|
||||||
|
|
||||||
|
SysConfig keepDayConfig = DBHelper.configOneByKey(Keys.SETUP_LOG_KEEP_DAY);
|
||||||
|
if (null != keepDayConfig) {
|
||||||
|
XLog.KEEP_DAY = Integer.valueOf(keepDayConfig.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
// 初始化日志路径
|
||||||
String rootPath = getRootPath();
|
String rootPath = getRootPath();
|
||||||
XLog.v(Keys.Tag.APP_INIT, "获取日志文件根目录: {}", rootPath);
|
|
||||||
|
|
||||||
if (!FileUtil.exist(rootPath)) {
|
if (!FileUtil.exist(rootPath)) {
|
||||||
// 不存在的时候创建文件
|
// 不存在的时候创建文件
|
||||||
@ -136,10 +149,12 @@ public class App {
|
|||||||
XLog.v(Keys.Tag.APP_INIT, "日志根目录不存在, 创建: {}", mkdirs);
|
XLog.v(Keys.Tag.APP_INIT, "日志根目录不存在, 创建: {}", mkdirs);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// 内存存储日志根目录
|
// 内存存储日志根目录
|
||||||
Global.LOG_ROOT_PATH = rootPath;
|
Global.LOG_ROOT_PATH = rootPath;
|
||||||
XLog.v(Keys.Tag.APP_INIT, "日志存储位置: {}", Global.LOG_ROOT_PATH);
|
XLog.d(Keys.Tag.APP_INIT, "日志开关: {}", XLog.SWITCH);
|
||||||
|
XLog.d(Keys.Tag.APP_INIT, "日志存储位置: {}", Global.LOG_ROOT_PATH);
|
||||||
|
XLog.d(Keys.Tag.APP_INIT, "日志级别: {}", XLog.ROOT_LEVEL_NAME);
|
||||||
|
XLog.d(Keys.Tag.APP_INIT, "日志保留天数: {}", XLog.KEEP_DAY);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String getRootPath() {
|
private static String getRootPath() {
|
||||||
@ -149,11 +164,9 @@ public class App {
|
|||||||
if (externalStorageState.equals(Environment.MEDIA_MOUNTED)) {
|
if (externalStorageState.equals(Environment.MEDIA_MOUNTED)) {
|
||||||
// 如果存在外置存储
|
// 如果存在外置存储
|
||||||
rootPath = StrUtil.join(StrPool.SLASH, Environment.getExternalStorageDirectory().getAbsolutePath(), BuildConfig.APP_NAME);
|
rootPath = StrUtil.join(StrPool.SLASH, Environment.getExternalStorageDirectory().getAbsolutePath(), BuildConfig.APP_NAME);
|
||||||
XLog.v(Keys.Tag.APP_INIT, "外置存储已挂载");
|
|
||||||
} else {
|
} else {
|
||||||
// 否则就写到/data/data里
|
// 否则就写到/data/data里
|
||||||
rootPath = StrUtil.join(StrPool.SLASH, Keys.LOG_ROOT_FILE_PATH, BuildConfig.APPLICATION_ID);
|
rootPath = StrUtil.join(StrPool.SLASH, Keys.LOG_ROOT_FILE_PATH, BuildConfig.APPLICATION_ID);
|
||||||
XLog.v(Keys.Tag.APP_INIT, "外置存储未挂载");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return StrUtil.join(StrPool.SLASH, rootPath, Keys.LOG_LOG_FOLDER);
|
return StrUtil.join(StrPool.SLASH, rootPath, Keys.LOG_LOG_FOLDER);
|
||||||
|
@ -25,23 +25,23 @@ public class XLog {
|
|||||||
/**
|
/**
|
||||||
* 日志总开关
|
* 日志总开关
|
||||||
*/
|
*/
|
||||||
public static Boolean LOG_SWITCH = true;
|
public static Boolean SWITCH = true;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 是否写入到文件
|
* 是否写入到文件
|
||||||
*/
|
*/
|
||||||
public static Boolean LOG_SWITCH_TO_FILE = true;
|
public static Boolean SWITCH_FOR_FILE = true;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 日志级别 默认info
|
* 日志级别 默认info
|
||||||
*/
|
*/
|
||||||
public static Integer SET_ROOT_LEVEL = Level.INFO;
|
public static Integer ROOT_LEVEL = Level.INFO;
|
||||||
public static String SET_ROOT_LEVEL_NAME = Level.INFO_NAME;
|
public static String ROOT_LEVEL_NAME = Level.INFO_NAME;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 日志保存最长时间
|
* 日志保存最长时间
|
||||||
*/
|
*/
|
||||||
public static Integer LOG_KEEP_DAY = 7;
|
public static Integer KEEP_DAY = 7;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <pre>
|
* <pre>
|
||||||
@ -49,7 +49,7 @@ public class XLog {
|
|||||||
* 2022-10-26 11:11:11:111 - [DEBUG] - packageName#methodName:lineNumber: message
|
* 2022-10-26 11:11:11:111 - [DEBUG] - packageName#methodName:lineNumber: message
|
||||||
* </pre>
|
* </pre>
|
||||||
*/
|
*/
|
||||||
public static final String LOG_TEMPLATE = "{} - [{}] - {}: {}";
|
public static final String PRINT_TEMPLATE = "{} - [{}] - {}: {}";
|
||||||
|
|
||||||
public static void v(String tag, String message, Object... params) {
|
public static void v(String tag, String message, Object... params) {
|
||||||
log(tag, filterThr(message, params), Level.VERBOSE);
|
log(tag, filterThr(message, params), Level.VERBOSE);
|
||||||
@ -107,7 +107,7 @@ public class XLog {
|
|||||||
* @date: 2022/10/27 9:07
|
* @date: 2022/10/27 9:07
|
||||||
*/
|
*/
|
||||||
private static void log(String tag, String message, int level) {
|
private static void log(String tag, String message, int level) {
|
||||||
if (!LOG_SWITCH) {
|
if (!SWITCH) {
|
||||||
// 不输出日志 直接返回
|
// 不输出日志 直接返回
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -116,7 +116,7 @@ public class XLog {
|
|||||||
// 设置级别为 info 4 打印级别为 debug 3 可打印
|
// 设置级别为 info 4 打印级别为 debug 3 可打印
|
||||||
|
|
||||||
// 要求当前输出的级别大于等于设置的级别
|
// 要求当前输出的级别大于等于设置的级别
|
||||||
if (level >= SET_ROOT_LEVEL) {
|
if (level >= ROOT_LEVEL) {
|
||||||
if (Level.ERROR == level) {
|
if (Level.ERROR == level) {
|
||||||
|
|
||||||
Log.e(tag, message);
|
Log.e(tag, message);
|
||||||
@ -155,7 +155,7 @@ public class XLog {
|
|||||||
*/
|
*/
|
||||||
public static void write(String tag, String message, int level) {
|
public static void write(String tag, String message, int level) {
|
||||||
|
|
||||||
if (LOG_SWITCH_TO_FILE) {
|
if (SWITCH_FOR_FILE) {
|
||||||
String logContent = concatFinalLog(tag, level, message);
|
String logContent = concatFinalLog(tag, level, message);
|
||||||
FileUtil.appendUtf8String(logContent + System.lineSeparator(), Global.logAbsolutePath());
|
FileUtil.appendUtf8String(logContent + System.lineSeparator(), Global.logAbsolutePath());
|
||||||
}
|
}
|
||||||
@ -176,7 +176,7 @@ public class XLog {
|
|||||||
|
|
||||||
// 时间 级别 线程信息 | 日志内容
|
// 时间 级别 线程信息 | 日志内容
|
||||||
// 2022-10-26 11:11:11:111 - [DEBUG] - packageName#methodName:lineNumber: message
|
// 2022-10-26 11:11:11:111 - [DEBUG] - packageName#methodName:lineNumber: message
|
||||||
return StrUtil.format(LOG_TEMPLATE,
|
return StrUtil.format(PRINT_TEMPLATE,
|
||||||
DateUtil.format(new Date(), DatePattern.NORM_DATETIME_MS_FORMAT),
|
DateUtil.format(new Date(), DatePattern.NORM_DATETIME_MS_FORMAT),
|
||||||
Level.transform(level),
|
Level.transform(level),
|
||||||
threadInfo(),
|
threadInfo(),
|
||||||
@ -205,7 +205,7 @@ public class XLog {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
static class Level {
|
public static class Level {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 详细
|
* 详细
|
||||||
|
@ -14,7 +14,6 @@ import cc.niushuai.project.devcontrol.R;
|
|||||||
import cc.niushuai.project.devcontrol.base.util.Keys;
|
import cc.niushuai.project.devcontrol.base.util.Keys;
|
||||||
import cc.niushuai.project.devcontrol.base.util.XLog;
|
import cc.niushuai.project.devcontrol.base.util.XLog;
|
||||||
import cc.niushuai.project.devcontrol.databinding.LayoutLogKeepDayBinding;
|
import cc.niushuai.project.devcontrol.databinding.LayoutLogKeepDayBinding;
|
||||||
import cc.niushuai.project.devcontrol.db.DB;
|
|
||||||
import cc.niushuai.project.devcontrol.db.util.DBHelper;
|
import cc.niushuai.project.devcontrol.db.util.DBHelper;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -45,7 +44,9 @@ public class KeepDayDialogFragment extends DialogFragment {
|
|||||||
|
|
||||||
private void addListener() {
|
private void addListener() {
|
||||||
binding.layoutLogKeepDayConfirm.setOnClickListener(v -> {
|
binding.layoutLogKeepDayConfirm.setOnClickListener(v -> {
|
||||||
String keepDayPickerValue = binding.keepDayPicker.getValue() + "";
|
int value = binding.keepDayPicker.getValue();
|
||||||
|
XLog.KEEP_DAY = value;
|
||||||
|
String keepDayPickerValue = value + "";
|
||||||
DBHelper.configDeal(Keys.SETUP_LOG_KEEP_DAY, keepDayPickerValue);
|
DBHelper.configDeal(Keys.SETUP_LOG_KEEP_DAY, keepDayPickerValue);
|
||||||
XLog.v(Keys.Tag.KEEP_DAY_SELECT, "更新日志存储天数为: {}", keepDayPickerValue);
|
XLog.v(Keys.Tag.KEEP_DAY_SELECT, "更新日志存储天数为: {}", keepDayPickerValue);
|
||||||
|
|
||||||
@ -60,7 +61,7 @@ public class KeepDayDialogFragment extends DialogFragment {
|
|||||||
|
|
||||||
binding.keepDayPicker.setMinValue(1);
|
binding.keepDayPicker.setMinValue(1);
|
||||||
binding.keepDayPicker.setMaxValue(90);
|
binding.keepDayPicker.setMaxValue(90);
|
||||||
binding.keepDayPicker.setValue(XLog.LOG_KEEP_DAY);
|
binding.keepDayPicker.setValue(XLog.KEEP_DAY);
|
||||||
binding.keepDayPicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
|
binding.keepDayPicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
|
||||||
binding.keepDayPicker.setFormatter(value -> value + "");
|
binding.keepDayPicker.setFormatter(value -> value + "");
|
||||||
binding.keepDayPicker.setOnValueChangedListener((picker, oldVal, newVal) -> {
|
binding.keepDayPicker.setOnValueChangedListener((picker, oldVal, newVal) -> {
|
||||||
|
@ -38,14 +38,14 @@ public class NavSetUpFragment extends BaseFragment {
|
|||||||
|
|
||||||
SysConfig switchConfig = DBHelper.configOneByKey(Keys.SETUP_LOG_SWITCH);
|
SysConfig switchConfig = DBHelper.configOneByKey(Keys.SETUP_LOG_SWITCH);
|
||||||
if (null == switchConfig) {
|
if (null == switchConfig) {
|
||||||
DBHelper.configDeal(Keys.SETUP_LOG_SWITCH, XLog.LOG_SWITCH.toString());
|
DBHelper.configDeal(Keys.SETUP_LOG_SWITCH, XLog.SWITCH.toString());
|
||||||
} else {
|
} else {
|
||||||
binding.setupLogSwitchSwitch.setChecked(Boolean.parseBoolean(switchConfig.getValue()));
|
binding.setupLogSwitchSwitch.setChecked(Boolean.parseBoolean(switchConfig.getValue()));
|
||||||
}
|
}
|
||||||
|
|
||||||
SysConfig levelConfig = DBHelper.configOneByKey(Keys.SETUP_LOG_LEVEL);
|
SysConfig levelConfig = DBHelper.configOneByKey(Keys.SETUP_LOG_LEVEL);
|
||||||
if (null == levelConfig) {
|
if (null == levelConfig) {
|
||||||
DBHelper.configDeal(Keys.SETUP_LOG_LEVEL, XLog.SET_ROOT_LEVEL_NAME);
|
DBHelper.configDeal(Keys.SETUP_LOG_LEVEL, XLog.ROOT_LEVEL_NAME);
|
||||||
} else {
|
} else {
|
||||||
String[] logLevel = getResources().getStringArray(R.array.logLevel);
|
String[] logLevel = getResources().getStringArray(R.array.logLevel);
|
||||||
for (int i = 0; i < logLevel.length; i++) {
|
for (int i = 0; i < logLevel.length; i++) {
|
||||||
@ -59,7 +59,7 @@ public class NavSetUpFragment extends BaseFragment {
|
|||||||
|
|
||||||
SysConfig keepDayConfig = DBHelper.configOneByKey(Keys.SETUP_LOG_KEEP_DAY);
|
SysConfig keepDayConfig = DBHelper.configOneByKey(Keys.SETUP_LOG_KEEP_DAY);
|
||||||
if (null == keepDayConfig) {
|
if (null == keepDayConfig) {
|
||||||
DBHelper.configDeal(Keys.SETUP_LOG_KEEP_DAY, XLog.LOG_KEEP_DAY.toString());
|
DBHelper.configDeal(Keys.SETUP_LOG_KEEP_DAY, XLog.KEEP_DAY.toString());
|
||||||
} else {
|
} else {
|
||||||
binding.setupLogKeepDayDisplay.setText(keepDayConfig.getValue());
|
binding.setupLogKeepDayDisplay.setText(keepDayConfig.getValue());
|
||||||
}
|
}
|
||||||
@ -107,7 +107,7 @@ public class NavSetUpFragment extends BaseFragment {
|
|||||||
*/
|
*/
|
||||||
private void setupLogSwitchClickListener(View view) {
|
private void setupLogSwitchClickListener(View view) {
|
||||||
Boolean checked = binding.setupLogSwitchSwitch.isChecked();
|
Boolean checked = binding.setupLogSwitchSwitch.isChecked();
|
||||||
XLog.LOG_SWITCH = checked;
|
XLog.SWITCH = checked;
|
||||||
|
|
||||||
DBHelper.configDeal(Keys.SETUP_LOG_SWITCH, checked.toString());
|
DBHelper.configDeal(Keys.SETUP_LOG_SWITCH, checked.toString());
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user