Skip to content

Commit 9672795

Browse files
committed
1、增加日志解析适配器,完善日志打印
2、增加定时删除日志功能 3、增加日志保存到本地功能
1 parent 3866d1e commit 9672795

File tree

14 files changed

+366
-57
lines changed

14 files changed

+366
-57
lines changed

app/src/main/java/com/liubuyao/androidlogutils/MainActivity.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.liubuyao.androidlogutils
22

33
import androidx.appcompat.app.AppCompatActivity
44
import android.os.Bundle
5+
import android.util.Log
56
import androidx.appcompat.widget.AppCompatButton
67
import com.liubuyao.utils.CrashUtils
78
import com.liubuyao.utils.LogUtils
@@ -18,12 +19,14 @@ class MainActivity : AppCompatActivity() {
1819
super.onCreate(savedInstanceState)
1920
setContentView(R.layout.activity_main)
2021

22+
23+
2124
findViewById<AppCompatButton>(R.id.btnTest).setOnClickListener {
2225
// findViewById<TextView>(R.id.tvText).text = list[2]
23-
LogUtils.d("btnTest")
26+
// LogUtils.d("这都没超过-btnTest".repeat(2000))
2427
val list = mutableListOf<User>()
2528
list.add(User("test",2,false))
26-
list.add(User("test2",22,true))
29+
list.add(User("这都没超过-test2".repeat(2000),22,true))
2730
LogUtils.d(list)
2831

2932
val map = mutableMapOf<String,User>()

utils/src/main/java/com/liubuyao/utils/DateUtils.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.liubuyao.utils
22

3+
import java.text.ParseException
34
import java.text.SimpleDateFormat
45
import java.util.*
56

@@ -54,6 +55,11 @@ object DateUtils {
5455
*/
5556
fun getNowDate() = Date()
5657

58+
/**
59+
* @return Long
60+
*/
61+
fun getNowDateTime() = Date().time
62+
5763
/**
5864
* 格式化日期
5965
* @return string
@@ -62,4 +68,16 @@ object DateUtils {
6268
return SimpleDateFormat(format, Locale.getDefault()).format(date)
6369
}
6470

71+
/**
72+
* 获取日期
73+
*/
74+
fun getDataByStr(dateStr: String?, format: String): Date? {
75+
if (dateStr==null) return null
76+
return try {
77+
SimpleDateFormat(format, Locale.getDefault()).parse(dateStr)
78+
} catch (e: ParseException) {
79+
null
80+
}
81+
}
82+
6583
}

utils/src/main/java/com/liubuyao/utils/FileUtils.kt

Lines changed: 66 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,32 @@ package com.liubuyao.utils
22

33
import android.os.Build
44
import java.io.File
5+
import java.io.FilenameFilter
56
import java.io.IOException
67
import java.nio.file.Files
78
import java.nio.file.StandardOpenOption
89

910
object FileUtils {
1011

12+
/**
13+
* 文件存在
14+
* @return 是否存在
15+
*/
16+
fun fileExists(path: String?): Boolean {
17+
if (path == null) return false
18+
val file = getFile(path) ?: return false
19+
return file.exists()
20+
}
21+
22+
/**
23+
* 文件存在
24+
* @return 是否存在
25+
*/
26+
fun fileExists(file: File?): Boolean {
27+
if (file == null) return false
28+
return file.exists()
29+
}
30+
1131
/**
1232
* 通过路径获取文件
1333
* @return 文件
@@ -62,12 +82,10 @@ object FileUtils {
6282
Files.write(file.toPath(), content.toByteArray(), StandardOpenOption.CREATE_NEW)
6383
}
6484
} else {
65-
file.bufferedWriter().use {
66-
if (append) {
67-
it.append(content)
68-
} else {
69-
it.write(content)
70-
}
85+
if (append) {
86+
file.appendText(content)
87+
} else {
88+
file.writeText(content)
7189
}
7290
}
7391
return true
@@ -76,5 +94,47 @@ object FileUtils {
7694
}
7795
}
7896

97+
/**
98+
* 删除文件
99+
*/
100+
fun deleteFile(path: String?): Boolean {
101+
return getFile(path)?.delete() ?: false
102+
}
103+
104+
/**
105+
* 获取文件列表
106+
*/
107+
fun getFileListByFile(path: String?, filterFilename: String, suffix: String): Array<String>? {
108+
val file = getFile(path)
109+
if (file != null) {
110+
val list = if (file.isFile) {
111+
val parentFile = file.parentFile
112+
parentFile?.list(object : FilenameFilter {
113+
override fun accept(dir: File?, name: String?): Boolean {
114+
if (name != null) {
115+
if (name.contains(filterFilename) && name.endsWith(".$suffix")) {
116+
return true
117+
}
118+
}
119+
return false
120+
}
121+
})
122+
} else {
123+
file.list(object : FilenameFilter {
124+
override fun accept(dir: File?, name: String?): Boolean {
125+
if (name != null) {
126+
if (name.contains(filterFilename) && name.endsWith(".$suffix")) {
127+
return true
128+
}
129+
}
130+
return false
131+
}
132+
133+
})
134+
}
135+
return list
136+
}
137+
return null
138+
}
79139

80140
}

0 commit comments

Comments
 (0)