Commit 421b1fcc authored by Daniel Wolf's avatar Daniel Wolf
Browse files

Try writing to log file, do not depend on it

parent b6699f04
...@@ -118,7 +118,7 @@ fun Context.log(e: Throwable, extras: Map<String, String>? = null) { ...@@ -118,7 +118,7 @@ fun Context.log(e: Throwable, extras: Map<String, String>? = null) {
"${Logger.logFileNameTimeStampFormatter.format(System.currentTimeMillis())}.err" "${Logger.logFileNameTimeStampFormatter.format(System.currentTimeMillis())}.err"
) )
if (errorFile.createNewFile()) { if (errorFile.tryCreateNewFile()) {
val writer = BufferedWriter(FileWriter(errorFile, false)) val writer = BufferedWriter(FileWriter(errorFile, false))
writer.write("App version: ${BuildConfig.VERSION_NAME} (${BuildConfig.VERSION_CODE}, Commit: ${BuildConfig.COMMIT_HASH})\n") writer.write("App version: ${BuildConfig.VERSION_NAME} (${BuildConfig.VERSION_CODE}, Commit: ${BuildConfig.COMMIT_HASH})\n")
writer.write("Android SDK version: ${Build.VERSION.SDK_INT} (${Build.VERSION.RELEASE} - ${Build.VERSION.CODENAME})\n") writer.write("Android SDK version: ${Build.VERSION.SDK_INT} (${Build.VERSION.RELEASE} - ${Build.VERSION.CODENAME})\n")
...@@ -178,7 +178,7 @@ class Logger private constructor(context: Context) { ...@@ -178,7 +178,7 @@ class Logger private constructor(context: Context) {
"${id}_${logFileNameTimeStampFormatter.format(System.currentTimeMillis())}.log" "${id}_${logFileNameTimeStampFormatter.format(System.currentTimeMillis())}.log"
) )
logDir.mkdirs() logDir.mkdirs()
logFile.createNewFile() logFile.tryCreateNewFile()
fileWriter = BufferedWriter(FileWriter(logFile, false)) fileWriter = BufferedWriter(FileWriter(logFile, false))
log("App version: ${BuildConfig.VERSION_NAME} (${BuildConfig.VERSION_CODE})") log("App version: ${BuildConfig.VERSION_NAME} (${BuildConfig.VERSION_CODE})")
...@@ -306,8 +306,12 @@ class Logger private constructor(context: Context) { ...@@ -306,8 +306,12 @@ class Logger private constructor(context: Context) {
if (printToConsole) { if (printToConsole) {
(oldSystemOut ?: System.out).println(textBuilder) (oldSystemOut ?: System.out).println(textBuilder)
} }
fileWriter.write(textBuilder.toString()) try {
fileWriter.flush() fileWriter.write(textBuilder.toString())
fileWriter.flush()
} catch (ex:Throwable) {
}
} }
} }
} }
...@@ -322,7 +326,7 @@ class Logger private constructor(context: Context) { ...@@ -322,7 +326,7 @@ class Logger private constructor(context: Context) {
"${id}_${logFileNameTimeStampFormatter.format(System.currentTimeMillis())}.err" "${id}_${logFileNameTimeStampFormatter.format(System.currentTimeMillis())}.err"
) )
if (errorFile.createNewFile()) { if (errorFile.tryCreateNewFile()) {
val writer = BufferedWriter(FileWriter(errorFile, false)) val writer = BufferedWriter(FileWriter(errorFile, false))
writer.write("App version: ${BuildConfig.VERSION_NAME} (${BuildConfig.VERSION_CODE})\n") writer.write("App version: ${BuildConfig.VERSION_NAME} (${BuildConfig.VERSION_CODE})\n")
writer.write("Android SDK version: ${Build.VERSION.SDK_INT} (${Build.VERSION.RELEASE} - ${Build.VERSION.CODENAME})\n") writer.write("Android SDK version: ${Build.VERSION.SDK_INT} (${Build.VERSION.RELEASE} - ${Build.VERSION.CODENAME})\n")
...@@ -340,6 +344,14 @@ class Logger private constructor(context: Context) { ...@@ -340,6 +344,14 @@ class Logger private constructor(context: Context) {
} }
} }
fun File.tryCreateNewFile():Boolean {
return try {
createNewFile()
} catch (ex:Throwable) {
false
}
}
fun Context.zipAllLogFiles(): File? { fun Context.zipAllLogFiles(): File? {
val dir = Logger.getLogDir(this) val dir = Logger.getLogDir(this)
if (!dir.canWrite() || !dir.canRead()) return null if (!dir.canWrite() || !dir.canRead()) return null
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment