This repository was archived by the owner on Dec 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathLogStorage.java
More file actions
123 lines (100 loc) · 3.89 KB
/
LogStorage.java
File metadata and controls
123 lines (100 loc) · 3.89 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.logentries.logger;
import android.content.Context;
import android.util.Log;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.Queue;
public class LogStorage {
private static final String TAG = "LogentriesAndroidLogger";
private static final String STORAGE_FILE_NAME = "LogentriesLogStorage.log";
private static final long MAX_QUEUE_FILE_SIZE = 10 * 1024 * 1024; // 10 MBytes.
private Context context;
private File storageFilePtr = null; // We keep the ptr permanently, because frequently accessing
// the file for retrieving it's size.
public LogStorage(Context context) throws IOException {
this.context = context;
storageFilePtr = create();
}
public void putLogToStorage(String message) throws IOException, RuntimeException {
// Fix line endings for ingesting the log to the local storage.
if (!message.endsWith("\n")) {
message += "\n";
}
FileOutputStream writer = null;
try {
byte[] rawMessage = message.getBytes();
long currSize = getCurrentStorageFileSize() + rawMessage.length;
String sizeStr = Long.toString(currSize);
//Log.d(TAG, "Current size: " + sizeStr);
if (currSize >= MAX_QUEUE_FILE_SIZE) {
Log.d(TAG, "Log storage will be cleared because threshold of " + MAX_QUEUE_FILE_SIZE + " bytes has been reached");
reCreateStorageFile();
}
writer = context.openFileOutput(STORAGE_FILE_NAME, Context.MODE_APPEND);
writer.write(rawMessage);
} finally {
if (writer != null) {
writer.close();
}
}
}
public Queue<String> getAllLogsFromStorage(boolean needToRemoveStorageFile) {
Queue<String> logs = new ArrayDeque<String>();
FileInputStream input = null;
try {
input = context.openFileInput(STORAGE_FILE_NAME);
DataInputStream inputStream = new DataInputStream(input);
BufferedReader bufReader = new BufferedReader(new InputStreamReader(inputStream));
String logLine = bufReader.readLine();
while (logLine != null) {
logs.offer(logLine);
logLine = bufReader.readLine();
}
if (needToRemoveStorageFile) {
removeStorageFile();
}
} catch (IOException ex) {
Log.e(TAG, "Cannot load logs from the local storage: " + ex.getMessage());
// Basically, ignore the exception - if something has gone wrong - just return empty
// logs list.
} finally {
try {
if (input != null) {
input.close();
}
} catch (IOException ex2) {
Log.e(TAG, "Cannot close the local storage file: " + ex2.getMessage());
}
}
return logs;
}
public void removeStorageFile() throws IOException {
if (!storageFilePtr.delete()) {
throw new IOException("Cannot delete " + STORAGE_FILE_NAME);
}
}
public void reCreateStorageFile() throws IOException {
Log.d(TAG, "Log storage has been re-created.");
if (storageFilePtr == null) {
storageFilePtr = create();
} else {
removeStorageFile();
}
storageFilePtr = create();
}
private File create() throws IOException {
return new File(context.getFilesDir(), STORAGE_FILE_NAME);
}
private long getCurrentStorageFileSize() throws IOException {
if (storageFilePtr == null) {
storageFilePtr = create();
}
return storageFilePtr.length();
}
}