DiskLogStrategy.java
3.79 KB
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
115
116
117
118
119
120
121
122
123
package com.wd.base.log;
import static com.wd.base.log.Utils.checkNotNull;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
/**
* Abstract class that takes care of background threading the file log operation on Android.
* implementing classes are free to directly perform I/O operations there.
* Writes all logs to the disk with CSV format.
*/
public class DiskLogStrategy implements LogStrategy {
@NonNull
private final Handler handler;
public DiskLogStrategy(@NonNull Handler handler) {
this.handler = checkNotNull(handler);
}
@Override
public void log(int level, @Nullable String tag, @NonNull String message) {
checkNotNull(message);
// do nothing on the calling thread, simply pass the tag/msg to the background thread
handler.sendMessage(handler.obtainMessage(level, message));
}
static class WriteHandler extends Handler {
@NonNull
private final String folder;
private final int maxFileSize;
WriteHandler(@NonNull Looper looper, @NonNull String folder, int maxFileSize) {
super(checkNotNull(looper));
this.folder = checkNotNull(folder);
this.maxFileSize = maxFileSize;
}
@SuppressWarnings("checkstyle:emptyblock")
@Override
public void handleMessage(@NonNull Message msg) {
String content = (String) msg.obj;
FileWriter fileWriter = null;
File logFile = getLogFile(folder, "logs");
try {
fileWriter = new FileWriter(logFile, true);
writeLog(fileWriter, content);
fileWriter.flush();
fileWriter.close();
} catch (IOException e) {
if (fileWriter != null) {
try {
fileWriter.flush();
fileWriter.close();
} catch (IOException e1) {
/* fail silently */ }
}
}
}
/**
* This is always called on a single background thread.
* Implementing classes must ONLY write to the fileWriter and nothing more.
* The abstract class takes care of everything else including close the stream and catching IOException
*
* @param fileWriter an instance of FileWriter already initialised to the correct file
*/
private void writeLog(@NonNull FileWriter fileWriter, @NonNull String content) throws IOException {
checkNotNull(fileWriter);
checkNotNull(content);
fileWriter.append(content);
}
private File getLogFile(@NonNull String folderName, @NonNull String fileName) {
checkNotNull(folderName);
checkNotNull(fileName);
File folder = new File(folderName);
if (!folder.exists()) {
// TODO: What if folder is not created, what happens then?
folder.mkdirs();
}
int newFileCount = 0;
File newFile;
File existingFile = null;
newFile = new File(folder, String.format("%s_%s.csv", fileName, newFileCount));
while (newFile.exists()) {
existingFile = newFile;
newFileCount++;
newFile = new File(folder, String.format("%s_%s.csv", fileName, newFileCount));
}
if (existingFile != null) {
if (existingFile.length() >= maxFileSize) {
return newFile;
}
return existingFile;
}
return newFile;
}
}
}