From 49a481dbfba04f8e24543b54a456195c6d0e83b9 Mon Sep 17 00:00:00 2001 From: kmanikanta335 <118070186+kmanikanta335@users.noreply.github.com> Date: Mon, 18 Dec 2023 23:08:46 +0530 Subject: [PATCH 1/2] [BUG]: ConsoleLogger overwrites local log file for each line write~ --- .../android/util/logging/ConsoleLogger.kt | 22 ++++++++----------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/utility/src/main/java/org/oppia/android/util/logging/ConsoleLogger.kt b/utility/src/main/java/org/oppia/android/util/logging/ConsoleLogger.kt index 453f0c550af..ebc9e142d18 100644 --- a/utility/src/main/java/org/oppia/android/util/logging/ConsoleLogger.kt +++ b/utility/src/main/java/org/oppia/android/util/logging/ConsoleLogger.kt @@ -2,12 +2,11 @@ package org.oppia.android.util.logging import android.content.Context import android.util.Log -import kotlinx.coroutines.CoroutineDispatcher -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.launch import org.oppia.android.util.locale.OppiaLocale -import org.oppia.android.util.threading.BlockingDispatcher import java.io.File +import java.io.FileOutputStream +import java.io.OutputStreamWriter +import java.io.PrintWriter import javax.inject.Inject import javax.inject.Singleton @@ -15,14 +14,13 @@ import javax.inject.Singleton @Singleton class ConsoleLogger @Inject constructor( context: Context, - @BlockingDispatcher private val blockingDispatcher: CoroutineDispatcher, @EnableConsoleLog private val enableConsoleLog: Boolean, @EnableFileLog private val enableFileLog: Boolean, @GlobalLogLevel private val globalLogLevel: LogLevel, private val machineLocale: OppiaLocale.MachineLocale ) { - private val blockingScope = CoroutineScope(blockingDispatcher) private val logDirectory = File(context.filesDir, "oppia_app.log") + private val logStream = PrintWriter(OutputStreamWriter(FileOutputStream(logDirectory, true), Charsets.UTF_8)) /** Logs a verbose message with the specified tag. */ fun v(tag: String, msg: String) { @@ -94,17 +92,15 @@ class ConsoleLogger @Inject constructor( Log.println(logLevel.logLevel, tag, fullLog) } if (enableFileLog) { - logToFileInBackground( - "${machineLocale.computeCurrentTimeString()}\t${logLevel.name}/$tag: $fullLog" - ) + logStream.println("${machineLocale.computeCurrentTimeString()}\t${logLevel.name}/$tag: $fullLog") + logStream.flush() } } /** - * Writes the specified text line to file in a background thread to ensure that saving messages don't block the main - * thread. A blocking dispatcher is used to ensure messages are written in order. + * Closes the log stream when the logger is no longer needed. */ - private fun logToFileInBackground(text: String) { - blockingScope.launch { logDirectory.printWriter().use { out -> out.println(text) } } + fun close() { + logStream.close() } } From 3ffe3cb917ded5b3649483bb7682ad12c0873de6 Mon Sep 17 00:00:00 2001 From: kmanikanta335 <118070186+kmanikanta335@users.noreply.github.com> Date: Tue, 19 Dec 2023 20:27:56 +0530 Subject: [PATCH 2/2] Fix #5232: ConsoleLogger overwrites local log file for each line write --- .../java/org/oppia/android/util/logging/ConsoleLogger.kt | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/utility/src/main/java/org/oppia/android/util/logging/ConsoleLogger.kt b/utility/src/main/java/org/oppia/android/util/logging/ConsoleLogger.kt index ebc9e142d18..797035ee31f 100644 --- a/utility/src/main/java/org/oppia/android/util/logging/ConsoleLogger.kt +++ b/utility/src/main/java/org/oppia/android/util/logging/ConsoleLogger.kt @@ -20,7 +20,9 @@ class ConsoleLogger @Inject constructor( private val machineLocale: OppiaLocale.MachineLocale ) { private val logDirectory = File(context.filesDir, "oppia_app.log") - private val logStream = PrintWriter(OutputStreamWriter(FileOutputStream(logDirectory, true), Charsets.UTF_8)) + private val logStream = PrintWriter( + OutputStreamWriter(FileOutputStream(logDirectory, true), Charsets.UTF_8) + ) /** Logs a verbose message with the specified tag. */ fun v(tag: String, msg: String) { @@ -92,7 +94,9 @@ class ConsoleLogger @Inject constructor( Log.println(logLevel.logLevel, tag, fullLog) } if (enableFileLog) { - logStream.println("${machineLocale.computeCurrentTimeString()}\t${logLevel.name}/$tag: $fullLog") + logStream.println( + "${machineLocale.computeCurrentTimeString()}\t${logLevel.name}/$tag: $fullLog" + ) logStream.flush() } }