diff options
Diffstat (limited to 'app/src/main/kotlin/org/pacien/tincapp/context/AppLogger.kt')
-rw-r--r-- | app/src/main/kotlin/org/pacien/tincapp/context/AppLogger.kt | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/app/src/main/kotlin/org/pacien/tincapp/context/AppLogger.kt b/app/src/main/kotlin/org/pacien/tincapp/context/AppLogger.kt new file mode 100644 index 0000000..3c1be44 --- /dev/null +++ b/app/src/main/kotlin/org/pacien/tincapp/context/AppLogger.kt | |||
@@ -0,0 +1,63 @@ | |||
1 | /* | ||
2 | * Tinc App, an Android binding and user interface for the tinc mesh VPN daemon | ||
3 | * Copyright (C) 2017-2018 Pacien TRAN-GIRARD | ||
4 | * | ||
5 | * This program is free software: you can redistribute it and/or modify | ||
6 | * it under the terms of the GNU General Public License as published by | ||
7 | * the Free Software Foundation, either version 3 of the License, or | ||
8 | * (at your option) any later version. | ||
9 | * | ||
10 | * This program is distributed in the hope that it will be useful, | ||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | * GNU General Public License for more details. | ||
14 | * | ||
15 | * You should have received a copy of the GNU General Public License | ||
16 | * along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
17 | */ | ||
18 | |||
19 | package org.pacien.tincapp.context | ||
20 | |||
21 | import ch.qos.logback.classic.Logger | ||
22 | import ch.qos.logback.classic.LoggerContext | ||
23 | import ch.qos.logback.classic.android.LogcatAppender | ||
24 | import ch.qos.logback.classic.encoder.PatternLayoutEncoder | ||
25 | import ch.qos.logback.classic.spi.ILoggingEvent | ||
26 | import ch.qos.logback.core.Context | ||
27 | import ch.qos.logback.core.FileAppender | ||
28 | import org.slf4j.LoggerFactory | ||
29 | |||
30 | /** | ||
31 | * @author pacien | ||
32 | */ | ||
33 | object AppLogger { | ||
34 | private const val LOGCAT_PATTERN = "[%thread] %msg%n%rEx" | ||
35 | private const val LOGFILE_PATTERN = "%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n%rEx" | ||
36 | |||
37 | fun configure() { | ||
38 | (LoggerFactory.getILoggerFactory() as LoggerContext) | ||
39 | .apply { reset() } | ||
40 | .let { loggerContext -> | ||
41 | (LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME) as Logger) | ||
42 | .apply { | ||
43 | addAppender(LogcatAppender() | ||
44 | .apply { context = loggerContext } | ||
45 | .apply { encoder = patternEncoder(loggerContext, LOGCAT_PATTERN) } | ||
46 | .apply { start() }) | ||
47 | } | ||
48 | .apply { | ||
49 | addAppender(FileAppender<ILoggingEvent>() | ||
50 | .apply { context = loggerContext } | ||
51 | .apply { encoder = patternEncoder(loggerContext, LOGFILE_PATTERN) } | ||
52 | .apply { file = AppPaths.appLogFile().absolutePath } | ||
53 | .apply { start() }) | ||
54 | } | ||
55 | } | ||
56 | } | ||
57 | |||
58 | private fun patternEncoder(ctx: Context, pat: String) = | ||
59 | PatternLayoutEncoder() | ||
60 | .apply { context = ctx } | ||
61 | .apply { pattern = pat } | ||
62 | .apply { start() } | ||
63 | } | ||