Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / lint-rules / src / main / java / com / vayunmathur / lint / DirectAndroidLogDetector.kt
DirectAndroidLogDetector.kt73 lines · 3.4 KiBRaw
| 1 | package com.vayunmathur.lint |
| 2 | |
| 3 | import com.android.tools.lint.detector.api.Category |
| 4 | import com.android.tools.lint.detector.api.Detector |
| 5 | import com.android.tools.lint.detector.api.Implementation |
| 6 | import com.android.tools.lint.detector.api.Issue |
| 7 | import com.android.tools.lint.detector.api.JavaContext |
| 8 | import com.android.tools.lint.detector.api.Scope |
| 9 | import com.android.tools.lint.detector.api.Severity |
| 10 | import com.android.tools.lint.detector.api.SourceCodeScanner |
| 11 | import com.intellij.psi.PsiMethod |
| 12 | import org.jetbrains.uast.UCallExpression |
| 13 | |
| 14 | /** |
| 15 | * Bans direct `android.util.Log` use everywhere but the facade itself. |
| 16 | * |
| 17 | * All logging goes through `com.vayunmathur.library.log.Log` |
| 18 | * (`Log.dev` / `Log.debug` / `Log.status` / `Log.error`), so every log carries |
| 19 | * an explicit tag and dev-only logs honour the build's dev gate |
| 20 | * (`Log.init(BuildConfig.DEV_BUILD)` in each app's `Application`). A raw |
| 21 | * `android.util.Log` call bypasses both, so it is an error. |
| 22 | * |
| 23 | * Matches on the resolved method owner, not the import text, so aliased |
| 24 | * imports (`import android.util.Log as AndroidLog`) and fully-qualified calls |
| 25 | * (`android.util.Log.d(...)`) are caught too. |
| 26 | * |
| 27 | * No per-file opt-out: the only legitimate `android.util.Log` reference is the |
| 28 | * facade's own delegation, which is exempt by package |
| 29 | * (`com.vayunmathur.library.log`). The untracked AOSP reference stub at the |
| 30 | * repo root is not part of any module source set, so lint never sees it. |
| 31 | */ |
| 32 | class DirectAndroidLogDetector : Detector(), SourceCodeScanner { |
| 33 | |
| 34 | override fun getApplicableMethodNames(): List<String> = |
| 35 | listOf("d", "e", "i", "w", "v", "wtf", "println", "isLoggable", "getStackTraceString") |
| 36 | |
| 37 | override fun visitMethodCall(context: JavaContext, node: UCallExpression, method: PsiMethod) { |
| 38 | if (!context.evaluator.isMemberInClass(method, ANDROID_LOG)) return |
| 39 | if (context.uastFile?.packageName == LOG_PACKAGE) return |
| 40 | |
| 41 | context.report( |
| 42 | ISSUE, |
| 43 | node, |
| 44 | context.getLocation(node), |
| 45 | "Direct android.util.Log use is not allowed. Use " + |
| 46 | "`com.vayunmathur.library.log.Log` instead: `Log.debug(tag, content)`, " + |
| 47 | "`Log.status(tag, content)`, `Log.error(tag, content)`, or the dev-gated " + |
| 48 | "`Log.dev(tag, content)` (each with a `Throwable` overload).", |
| 49 | ) |
| 50 | } |
| 51 | |
| 52 | companion object { |
| 53 | private const val ANDROID_LOG = "android.util.Log" |
| 54 | private const val LOG_PACKAGE = "com.vayunmathur.library.log" |
| 55 | |
| 56 | val ISSUE: Issue = Issue.create( |
| 57 | id = "DirectAndroidLog", |
| 58 | briefDescription = "Direct android.util.Log use is not allowed", |
| 59 | explanation = """ |
| 60 | All logging must go through the repo facade \ |
| 61 | `com.vayunmathur.library.log.Log` (Log.dev / Log.debug / Log.status / \ |
| 62 | Log.error), so every log carries an explicit tag and dev-only logs honour \ |
| 63 | the build's dev gate. A raw android.util.Log call bypasses both. Replace \ |
| 64 | `Log.d` with `Log.debug`, `Log.i`/`Log.w` with `Log.status`, `Log.e` with \ |
| 65 | `Log.error`, keeping the existing tag as the first argument. |
| 66 | """.trimIndent(), |
| 67 | category = Category.CORRECTNESS, |
| 68 | priority = 8, |
| 69 | severity = Severity.ERROR, |
| 70 | implementation = Implementation(DirectAndroidLogDetector::class.java, Scope.JAVA_FILE_SCOPE), |
| 71 | ) |
| 72 | } |
| 73 | } |