Skip to content

Instantly share code, notes, and snippets.

@Aidanvii7
Last active February 25, 2017 18:11
Show Gist options
  • Save Aidanvii7/c805448b077b4ff71769f218ce81df2e to your computer and use it in GitHub Desktop.
Save Aidanvii7/c805448b077b4ff71769f218ce81df2e to your computer and use it in GitHub Desktop.
Simple Log delegation in Kotlin with extension function.
package com.aidanvii.extensions
fun Any.logD(message: String) = Logger.d(javaClass.simpleName, message)
interface LoggerDelegate {
fun d(tag: String, message: String)
}
interface CompositeLoggerDelegate : LoggerDelegate {
fun attachDelegate(delegate: LoggerDelegate)
fun detachDelegate(delegate: LoggerDelegate)
}
object Logger : CompositeLoggerDelegate {
// not synchronised!
private val delegates: MutableList<LoggerDelegate> = mutableListOf()
override fun d(tag: String, message: String) = delegates.forEach { it.d(tag, message) }
override fun attachDelegate(delegate: LoggerDelegate) {
delegates.add(delegate)
}
override fun detachDelegate(delegate: LoggerDelegate) {
delegates.remove(delegate)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment