Last active
February 25, 2017 18:11
-
-
Save Aidanvii7/c805448b077b4ff71769f218ce81df2e to your computer and use it in GitHub Desktop.
Simple Log delegation in Kotlin with extension function.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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