cloudroam
2025-02-14 2fce91b8c0faf1290d8a35ee022dab3cdbc28a54
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
package com.example.firstapp.utils
 
import android.content.Context
import android.os.Build
import com.example.firstapp.App
import java.io.File
import java.io.FileWriter
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale
import android.util.Log as AndroidLog
 
@Suppress("unused", "MemberVisibilityCanBePrivate")
object Log {
    const val ASSERT = 7
    const val DEBUG = 3
    const val ERROR = 6
    const val INFO = 4
    const val VERBOSE = 2
    const val WARN = 5
 
    private const val TAG = "Logger"
    private var logFile: File? = null
    private lateinit var appContext: Context
    private var initDate: String = ""
 
    fun init(context: Context) {
        appContext = context
        createLogFile()
    }
 
    private fun createLogFile() {
        val currentDate = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(Date())
        if (currentDate != initDate || logFile == null || !logFile!!.exists()) {
            initDate = currentDate
            val logPath = appContext.cacheDir.absolutePath + "/logs"
            val logDir = File(logPath)
            if (!logDir.exists()) logDir.mkdirs()
            logFile = File(logPath, "log_$currentDate.txt")
        }
    }
 
    fun logToFile(level: String, tag: String, message: String) {
        if (Build.DEVICE == null) return
        
        if (!::appContext.isInitialized) {
            throw IllegalStateException("Log not initialized. Call init(context) first.")
        }
 
        if (!App.isDebug) return
 
        Thread {
            try {
                createLogFile()
                logFile?.let { file ->
                    try {
                        val logTimeStamp = SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.getDefault()).format(Date())
                        val logWriter = FileWriter(file, true)
                        logWriter.append("$logTimeStamp | $level | $tag | $message\n\n")
                        logWriter.close()
                    } catch (e: Exception) {
                        AndroidLog.e(TAG, "Error writing to file: ${e.message}")
                    }
                }
            } catch (e: Exception) {
                AndroidLog.e(TAG, "Error writing to file: ${e.message}")
            }
        }.start()
    }
 
    fun v(tag: String, message: String) {
        AndroidLog.v(tag, message)
        logToFile("V", tag, message)
    }
 
    fun v(tag: String, message: String, throwable: Throwable) {
        val logMessage = "${message}\n${getStackTraceString(throwable)}"
        AndroidLog.v(tag, logMessage)
        logToFile("V", tag, logMessage)
    }
 
    fun d(tag: String, message: String) {
        AndroidLog.d(tag, message)
        logToFile("D", tag, message)
    }
 
    fun d(tag: String, message: String, throwable: Throwable) {
        val logMessage = "${message}\n${getStackTraceString(throwable)}"
        AndroidLog.d(tag, logMessage)
        logToFile("D", tag, logMessage)
    }
 
    fun i(tag: String, message: String) {
        AndroidLog.d(tag, message)
        logToFile("I", tag, message)
    }
 
    fun i(tag: String, message: String, throwable: Throwable) {
        val logMessage = "${message}\n${getStackTraceString(throwable)}"
        AndroidLog.d(tag, logMessage)
        logToFile("I", tag, logMessage)
    }
 
    fun w(tag: String, message: String) {
        AndroidLog.w(tag, message)
        logToFile("W", tag, message)
    }
 
    fun w(tag: String, throwable: Throwable) {
        val logMessage = getStackTraceString(throwable)
        AndroidLog.w(tag, logMessage)
        logToFile("W", tag, logMessage)
    }
 
    fun w(tag: String, message: String, throwable: Throwable) {
        val logMessage = "${message}\n${getStackTraceString(throwable)}"
        AndroidLog.w(tag, logMessage)
        logToFile("W", tag, logMessage)
    }
 
    fun e(tag: String, message: String) {
        AndroidLog.e(tag, message)
        logToFile("E", tag, message)
    }
 
    fun e(tag: String, message: String, throwable: Throwable) {
        val logMessage = "${message}\n${getStackTraceString(throwable)}"
        AndroidLog.e(tag, logMessage)
        logToFile("E", tag, logMessage)
    }
 
    fun wtf(tag: String, message: String) {
        AndroidLog.wtf(tag, message)
        logToFile("WTF", tag, message)
    }
 
    fun wtf(tag: String, throwable: Throwable) {
        val logMessage = getStackTraceString(throwable)
        AndroidLog.wtf(tag, logMessage)
        logToFile("WTF", tag, logMessage)
    }
 
    fun wtf(tag: String, message: String, throwable: Throwable) {
        val logMessage = "${message}\n${getStackTraceString(throwable)}"
        AndroidLog.wtf(tag, logMessage)
        logToFile("WTF", tag, logMessage)
    }
 
    fun getStackTraceString(throwable: Throwable): String {
        return AndroidLog.getStackTraceString(throwable)
    }
 
    fun isLoggable(tag: String?, level: Int): Boolean {
        return AndroidLog.isLoggable(tag, level)
    }
 
    fun println(priority: Int, tag: String, message: String) {
        AndroidLog.println(priority, tag, message)
        logToFile("P", tag, message)
    }
}