-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathPersistentLogger.kt
More file actions
307 lines (260 loc) · 10.3 KB
/
PersistentLogger.kt
File metadata and controls
307 lines (260 loc) · 10.3 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package org.thoughtcrime.securesms.logging
import android.content.Context
import android.net.Uri
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.ReceiveChannel
import kotlinx.coroutines.channels.SendChannel
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withTimeoutOrNull
import org.session.libsignal.utilities.Log.Logger
import java.io.File
import java.text.SimpleDateFormat
import java.util.Locale
import java.util.regex.Pattern
import java.util.zip.ZipEntry
import java.util.zip.ZipOutputStream
import javax.inject.Inject
import javax.inject.Singleton
import kotlin.time.Duration.Companion.milliseconds
/**
* A [Logger] that writes logs to encrypted files in the app's cache directory.
*/
@Singleton
class PersistentLogger @Inject constructor(
@ApplicationContext private val context: Context
) : Logger() {
private val freeLogEntryPool = LogEntryPool()
private val logEntryChannel: SendChannel<LogEntry>
private val logChannelIdleSignal = MutableSharedFlow<Unit>()
private val logDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS zzz", Locale.ENGLISH)
private val secret by lazy {
LogSecretProvider.getOrCreateAttachmentSecret(context)
}
private val logFolder by lazy {
File(context.cacheDir, "logs").apply {
mkdirs()
}
}
init {
val channel = Channel<LogEntry>(capacity = MAX_PENDING_LOG_ENTRIES)
logEntryChannel = channel
GlobalScope.launch {
val bulk = ArrayList<LogEntry>()
var logWriter: LogFile.Writer? = null
val entryBuilder = StringBuilder()
try {
while (true) {
channel.receiveBulkLogs(bulk)
if (bulk.isNotEmpty()) {
if (logWriter == null) {
logWriter = LogFile.Writer(secret, File(logFolder, CURRENT_LOG_FILE_NAME))
}
bulkWrite(entryBuilder, logWriter, bulk)
// Release entries back to the pool
freeLogEntryPool.release(bulk)
bulk.clear()
// Rotate the log file if necessary
if (logWriter.logSize > MAX_SINGLE_LOG_FILE_SIZE) {
rotateAndTrimLogFiles(logWriter.file)
logWriter.close()
logWriter = null
}
}
// Notify that the log channel is idle
logChannelIdleSignal.tryEmit(Unit)
}
} catch (e: Exception) {
logWriter?.close()
android.util.Log.e(
TAG,
"Error while processing log entries: ${e.message}",
e
)
}
}
}
private fun rotateAndTrimLogFiles(currentFile: File) {
val permLogFile = File(logFolder, "${System.currentTimeMillis()}$PERM_LOG_FILE_SUFFIX")
if (currentFile.renameTo(permLogFile)) {
android.util.Log.d(TAG, "Rotated log file: $currentFile to $permLogFile")
currentFile.createNewFile()
} else {
android.util.Log.e(TAG, "Failed to rotate log file: $currentFile")
}
val logFilesNewToOld = getLogFilesSorted(includeActiveLogFile = false)
// Keep the last N log files, delete the rest
while (logFilesNewToOld.size > MAX_LOG_FILE_COUNT) {
val last = logFilesNewToOld.removeLastOrNull()!!
if (last.delete()) {
android.util.Log.d(TAG, "Deleted old log file: $last")
} else {
android.util.Log.e(TAG, "Failed to delete log file: $last")
}
}
}
private fun bulkWrite(sb: StringBuilder, writer: LogFile.Writer, bulk: List<LogEntry>) {
for (entry in bulk) {
sb.clear()
sb.append(logDateFormat.format(entry.timestampMills))
.append(' ')
.append(entry.logLevel)
.append(' ')
.append(entry.tag.orEmpty())
.append(": ")
.append(entry.message.orEmpty())
entry.err?.let {
sb.append('\n')
sb.append(it.stackTraceToString())
}
writer.writeEntry(sb.toString(), false)
}
writer.flush()
}
private suspend fun ReceiveChannel<LogEntry>.receiveBulkLogs(out: MutableList<LogEntry>) {
out += receive()
withTimeoutOrNull(500.milliseconds) {
repeat(15) {
out += receiveCatching().getOrNull() ?: return@repeat
}
}
}
private fun sendLogEntry(
level: String,
tag: String?,
message: String?,
t: Throwable? = null
) {
val entry = freeLogEntryPool.createLogEntry(level, tag, message, t)
if (logEntryChannel.trySend(entry).isFailure) {
android.util.Log.e(TAG, "Failed to send log entry, buffer is full")
}
}
override fun v(tag: String?, message: String?, t: Throwable?) =
sendLogEntry(LOG_V, tag, message, t)
override fun d(tag: String?, message: String?, t: Throwable?) =
sendLogEntry(LOG_D, tag, message, t)
override fun i(tag: String?, message: String?, t: Throwable?) =
sendLogEntry(LOG_I, tag, message, t)
override fun w(tag: String?, message: String?, t: Throwable?) =
sendLogEntry(LOG_W, tag, message, t)
override fun e(tag: String?, message: String?, t: Throwable?) =
sendLogEntry(LOG_E, tag, message, t)
override fun wtf(tag: String?, message: String?, t: Throwable?) =
sendLogEntry(LOG_WTF, tag, message, t)
override fun blockUntilAllWritesFinished() {
runBlocking {
withTimeoutOrNull(1000) {
logChannelIdleSignal.first()
}
}
}
private fun getLogFilesSorted(includeActiveLogFile: Boolean): MutableList<File> {
val files = (logFolder.listFiles()?.asSequence() ?: emptySequence())
.mapNotNull {
if (!it.isFile) return@mapNotNull null
PERM_LOG_FILE_PATTERN.matcher(it.name).takeIf { it.matches() }
?.group(1)
?.toLongOrNull()
?.let { timestamp -> it to timestamp }
}
.toMutableList()
.apply { sortByDescending { (_, timestamp) -> timestamp } }
.mapTo(arrayListOf()) { it.first }
if (includeActiveLogFile) {
val currentLogFile = File(logFolder, CURRENT_LOG_FILE_NAME)
if (currentLogFile.exists()) {
files.add(0, currentLogFile)
}
}
return files
}
/**
* Reads all log entries from the log files and writes them as a ZIP file at the specified URI.
*
* This method will block until all log entries are read and written.
*/
fun readAllLogsCompressed(output: Uri) {
val logs = getLogFilesSorted(includeActiveLogFile = true).apply { reverse() }
requireNotNull(context.contentResolver.openOutputStream(output, "w")) {
"Failed to open output stream for URI: $output"
}.use { outStream ->
ZipOutputStream(outStream).use { zipOut ->
zipOut.putNextEntry(ZipEntry("log.txt"))
for (log in logs) {
LogFile.Reader(secret, log).use { reader ->
var entry = reader.readEntryBytes()
while (entry != null) {
zipOut.write(entry)
zipOut.write('\n'.code)
entry = reader.readEntryBytes()
}
}
}
zipOut.closeEntry()
}
}
}
private class LogEntry(
var logLevel: String,
var tag: String?,
var message: String?,
var err: Throwable?,
var timestampMills: Long,
)
/**
* A pool for reusing [LogEntry] objects to reduce memory allocations.
*/
private class LogEntryPool {
private val pool = ArrayList<LogEntry>(MAX_LOG_ENTRIES_POOL_SIZE)
fun createLogEntry(level: String, tag: String?, message: String?, t: Throwable?): LogEntry {
val fromPool = synchronized(pool) { pool.removeLastOrNull() }
val now = System.currentTimeMillis()
if (fromPool != null) {
fromPool.logLevel = level
fromPool.tag = tag
fromPool.message = message
fromPool.err = t
fromPool.timestampMills = now
return fromPool
}
return LogEntry(
logLevel = level,
tag = tag,
message = message,
err = t,
timestampMills = now
)
}
fun release(entry: Iterable<LogEntry>) {
val iterator = entry.iterator()
synchronized(pool) {
while (pool.size < MAX_LOG_ENTRIES_POOL_SIZE && iterator.hasNext()) {
pool.add(iterator.next())
}
}
}
}
companion object {
private const val TAG = "PersistentLoggingV2"
private const val LOG_V: String = "V"
private const val LOG_D: String = "D"
private const val LOG_I: String = "I"
private const val LOG_W: String = "W"
private const val LOG_E: String = "E"
private const val LOG_WTF: String = "A"
private const val PERM_LOG_FILE_SUFFIX = ".permlog"
private const val CURRENT_LOG_FILE_NAME = "current.log"
private val PERM_LOG_FILE_PATTERN by lazy { Pattern.compile("^(\\d+?)\\.permlog$") }
// Maximum size of a single log file
private const val MAX_SINGLE_LOG_FILE_SIZE = 2 * 1024 * 1024
// Maximum number of log files to keep
private const val MAX_LOG_FILE_COUNT = 10
private const val MAX_LOG_ENTRIES_POOL_SIZE = 64
private const val MAX_PENDING_LOG_ENTRIES = 65536
}
}