Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
huangzhicong
/
SmartCanteen
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
25997146
authored
May 08, 2020
by
pye52
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
增加日志文件夹监测(每4小时进行一次),当检测到空间不足时,删除旧的日志文件
parent
ad95c6e6
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
83 additions
and
4 deletions
+83
-4
app/build.gradle
+2
-0
app/src/main/java/com/bgycc/smartcanteen/RootApp.java
+1
-4
app/src/main/java/com/bgycc/smartcanteen/activity/MainActivity.java
+3
-0
app/src/main/java/com/bgycc/smartcanteen/utils/WorkerUtils.java
+28
-0
app/src/main/java/com/bgycc/smartcanteen/worker/LogFileMonitor.java
+49
-0
No files found.
app/build.gradle
View file @
25997146
...
...
@@ -108,6 +108,8 @@ dependencies {
def
lifecycle_version
=
"2.2.0"
implementation
"androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
implementation
"androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"
def
work_version
=
"2.3.1"
implementation
"androidx.work:work-runtime:$work_version"
implementation
'androidx.appcompat:appcompat:1.1.0'
implementation
'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation
'junit:junit:4.13'
...
...
app/src/main/java/com/bgycc/smartcanteen/RootApp.java
View file @
25997146
...
...
@@ -12,8 +12,6 @@ import java.io.File;
public
class
RootApp
extends
Application
{
private
static
final
String
LOG_PREFIX
=
"app"
;
private
static
final
String
LOG_DIR
=
"log"
;
// 设置日志保存期限,避免日志过多占用内存空间
private
static
final
int
LOG_SAVE_DAYS
=
7
;
@Override
public
void
onCreate
()
{
...
...
@@ -25,7 +23,6 @@ public class RootApp extends Application {
.
setDir
(
logDir
)
.
setLog2FileSwitch
(
true
)
.
setBorderSwitch
(
false
)
.
setFilePrefix
(
LOG_PREFIX
)
.
setSaveDays
(
LOG_SAVE_DAYS
);
.
setFilePrefix
(
LOG_PREFIX
);
}
}
app/src/main/java/com/bgycc/smartcanteen/activity/MainActivity.java
View file @
25997146
...
...
@@ -35,6 +35,7 @@ import com.bgycc.smartcanteen.state.QRCodeState;
import
com.bgycc.smartcanteen.utils.NetworkUtils
;
import
com.bgycc.smartcanteen.utils.SmartCanteenUtils
;
import
com.bgycc.smartcanteen.utils.TTSHelper
;
import
com.bgycc.smartcanteen.utils.WorkerUtils
;
import
com.bgycc.smartcanteen.viewModel.CommandViewModel
;
import
com.bgycc.smartcanteen.viewModel.PayOfflineViewModel
;
import
com.bgycc.smartcanteen.viewModel.PayOnlineViewModel
;
...
...
@@ -269,6 +270,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
commandViewModel
.
initialize
();
handler
.
post
(
updateTimeRunnable
);
WorkerUtils
.
startLogFilesMonitor
(
this
);
SCWebSocketClient
.
getInstance
().
tryConnect
();
}
...
...
@@ -332,6 +334,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
TTSHelper
.
release
();
SCWebSocketClient
.
getInstance
().
realClose
();
SCTaskExecutor
.
getInstance
().
quit
();
WorkerUtils
.
stopLogFilesMonitor
(
this
);
}
private
void
initViews
()
{
...
...
app/src/main/java/com/bgycc/smartcanteen/utils/WorkerUtils.java
0 → 100644
View file @
25997146
package
com
.
bgycc
.
smartcanteen
.
utils
;
import
android.content.Context
;
import
androidx.work.PeriodicWorkRequest
;
import
androidx.work.WorkManager
;
import
com.bgycc.smartcanteen.worker.LogFileMonitor
;
import
java.util.concurrent.TimeUnit
;
public
class
WorkerUtils
{
private
static
final
String
LOG_MONITOR_WORKER
=
"worker_log_monitor"
;
// 每隔一定时间检查日志文件夹并进行清理
public
static
void
startLogFilesMonitor
(
Context
context
)
{
PeriodicWorkRequest
request
=
new
PeriodicWorkRequest
.
Builder
(
LogFileMonitor
.
class
,
4
,
TimeUnit
.
HOURS
)
.
addTag
(
LOG_MONITOR_WORKER
)
.
build
();
WorkManager
.
getInstance
(
context
)
.
enqueue
(
request
);
}
public
static
void
stopLogFilesMonitor
(
Context
context
)
{
WorkManager
.
getInstance
(
context
)
.
cancelAllWorkByTag
(
LOG_MONITOR_WORKER
);
}
}
app/src/main/java/com/bgycc/smartcanteen/worker/LogFileMonitor.java
0 → 100644
View file @
25997146
package
com
.
bgycc
.
smartcanteen
.
worker
;
import
android.content.Context
;
import
androidx.annotation.NonNull
;
import
androidx.work.Worker
;
import
androidx.work.WorkerParameters
;
import
com.blankj.utilcode.util.FileUtils
;
import
com.blankj.utilcode.util.LogUtils
;
import
java.io.File
;
import
java.util.Collections
;
import
java.util.List
;
import
static
com
.
bgycc
.
smartcanteen
.
utils
.
SmartCanteenUtils
.
TAG
;
public
class
LogFileMonitor
extends
Worker
{
public
LogFileMonitor
(
@NonNull
Context
context
,
@NonNull
WorkerParameters
workerParams
)
{
super
(
context
,
workerParams
);
}
@NonNull
@Override
public
Result
doWork
()
{
String
logDirPath
=
LogUtils
.
getConfig
().
getDir
();
long
totalSize
=
FileUtils
.
getFsTotalSize
(
logDirPath
);
long
availableSize
=
FileUtils
.
getFsAvailableSize
(
logDirPath
);
LogUtils
.
d
(
TAG
,
"日志文件系统, 可用大小: "
+
availableSize
+
", 总大小: "
+
totalSize
);
while
(
availableSize
<
(
totalSize
*
0.1f
))
{
// 当可用小于10%时,删除旧日志文件
List
<
File
>
logFiles
=
LogUtils
.
getLogFiles
();
Collections
.
sort
(
logFiles
,
(
f1
,
f2
)
->
{
if
(
f1
.
lastModified
()
==
f2
.
lastModified
())
{
return
0
;
}
return
(
f1
.
lastModified
()
>
f2
.
lastModified
())
?
1
:
-
1
;
});
for
(
File
f
:
logFiles
.
subList
(
0
,
2
))
{
FileUtils
.
delete
(
f
);
}
totalSize
=
FileUtils
.
getFsTotalSize
(
logDirPath
);
availableSize
=
FileUtils
.
getFsAvailableSize
(
logDirPath
);
}
return
Result
.
success
();
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment