package com.sw.inbound.utils import com.sw.inbound.MyApp import timber.log.Timber import java.io.File import java.io.FileOutputStream import java.text.SimpleDateFormat import java.util.Date import java.util.Locale object LogSaveUtil { private const val TAG = "LogSaveUtil" private const val CRASH_REPORTS_DIR = "crash_reports" public fun saveLogFile(msg: String) { saveLogFile("yyyy-MM-dd", msg) } public fun saveLogFile(pattern: String, msg: String) { try { saveLog(pattern, msg) } catch (e: Exception) { Timber.tag(TAG).e(e, "Error saving crash info to file") try { saveLog(pattern, "Error saving crash info to file:${e.message}") } catch (e: Exception) { Timber.tag(TAG).e(e, "save exception") } } } fun saveLog(pattern: String, logInfo: String) { val time = SimpleDateFormat(pattern, Locale.getDefault()).format(Date()) val fileName = "crash_$time.log" val crashDir = getCrashDir() val crashFile = File(crashDir, fileName) val saveTime = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).format(Date()) FileOutputStream(crashFile, true).use { it.write("$saveTime||$logInfo\n".toByteArray()) } Timber.tag(TAG).d("Crash info saved to: ${crashFile.absolutePath}") } fun getCrashDir(): File { //val crashDir = File(context.getExternalFilesDir(null), CRASH_REPORTS_DIR) var crashDir = File(MyApp.instance!!.filesDir, CRASH_REPORTS_DIR) if (!crashDir.exists()) { crashDir.mkdirs() } if (!(crashDir.exists())) { crashDir = File(MyApp.instance!!.cacheDir, CRASH_REPORTS_DIR) } return crashDir } }