$ show square/logcat
Square Logcat is a lightweight Kotlin multiplatform logging library that provides a simple API for logging on Android and JVM platforms. The library wraps Android's android.util.Log class and offers a similar API for JVM environments. Key architectural decisions include inlined logging functions that avoid string evaluation when logging is disabled, automatic tag extraction from calling class names without stacktrace overhead, and support for multiple concurrent loggers with thread-safe management. The library uses kotlinx.atomicfu for thread-safe collections and follows a clean separation between the logging API (logcat function) and logger installation (LogcatLogger interface).
The LLM identified 4 files worth highlighting for reuse:
The logcat() function demonstrates excellent Kotlin craftsmanship through inlined extension functions that capture the calling context without runtime overhead. The function evaluates the message lambda only once even when multiple loggers are registered, and uses internal helper functions (findFirstLogger, sendToLoggers) to reduce code size while maintaining performance. The try/finally block ensures observer callbacks are always called, demonstrating defensive programming.
AndroidLogcatLogger implements platform-specific optimizations including tag length truncation for older Android versions, automatic message splitting for logs exceeding 4000 characters, and special handling for ASSERT priority (using Log.wtf). The installOnDebuggableApp companion method provides a safe default that prevents logging in release builds. This component is cleanly isolated and could be adapted for other platform-specific loggers.
The outerClassSimpleName() function demonstrates clever Kotlin multiplatform engineering by extracting the calling class name from the full class name string without generating a stacktrace. This avoids the performance cost of stacktrace generation while still providing automatic tag extraction. The implementation handles edge cases like companion objects and Kotlin-generated class names (removing 'Kt' suffix). The expect/actual pattern cleanly separates platform-specific implementations.
LogcatLogger companion object implements thread-safe logger management using synchronized blocks and CopyOnWriteArrayList. The install/uninstall pattern with volatile state provides a clean lifecycle for the logging system. The observer pattern for tracing/logging metrics is well-implemented with proper exception handling to ensure afterLog is always called. The deprecated install(logger) method maintains backward compatibility while guiding users toward the new pattern.