11package ktee
22
3- import ktee.KTee.Companion.debug
43import org.slf4j.Logger
54
6- class KTee {
7- companion object {
8- var debugChanged = false
9- private set
10-
11- /* *
12- * If debug is set to false, all tee
13- * functions won't output anything.
14- *
15- * Variable can be set only once!
16- */
17- var debug = true
18- @Synchronized set(value) {
19- if (! debugChanged) { debugChanged = true ; field = value }
20- else throw IllegalStateException (" Variable debug has already been set once." )
21- }
22- } }
23-
245/* *
256 * Prints the value to the stdout and returns the same value. Useful when chaining
267 * methods. For example:
@@ -30,53 +11,53 @@ class KTee {
3011 * myList.map(fn).tee(">>> ").reduce(fn)
3112 *
3213 */
33- fun <T > T.tee (marker : String = "") = apply { if (debug) println (marker + this ) }
14+ fun <T > T.tee (marker : String = "") = apply { println (marker + this ) }
3415
3516/* *
3617 *
3718 * executes the lambda with the value of the chain and writes the
3819 *
3920 */
40- inline fun <T > T.tee (fn : (T ) -> String ) = apply { if (debug) println (fn(this )) }
21+ inline fun <T > T.tee (fn : (T ) -> String ) = apply { println (fn(this )) }
4122
4223/* *
4324 * logs the value to the given logger at info level. Message can be customized using message parameter
4425 */
4526fun <T > T.teeToInfo (logger : Logger , message : String = "{}")
46- = apply { if (debug) logger.info(message, this ) }
27+ = apply { logger.info(message, this ) }
4728
4829/* *
4930 * Evaluates the lambda and logs the result (of evaluation) to the given logger at info level
5031 *
5132 */
5233inline fun <T > T.teeToInfo (logger : Logger , fn : (T ) -> String )
53- = apply { if (debug) logger.info(fn(this ), this ) }
34+ = apply { logger.info(fn(this ), this ) }
5435
5536/* *
5637 * logs the value to the given logger at info level. Message can be customized using message parameter
5738 */
5839fun <T > T.teeToDebug (logger : Logger , message : String = "{}")
59- = apply { if (debug) logger.debug(message, this ) }
40+ = apply { logger.debug(message, this ) }
6041
6142/* *
6243 * Evaluates the lambda and logs the result (of evaluation) to the given logger at debug level
6344 *
6445 */
6546inline fun <T > T.teeToDebug (logger : Logger , fn : (T ) -> String )
66- = apply { if (debug) logger.debug(fn(this ), this ) }
47+ = apply { logger.debug(fn(this ), this ) }
6748
6849/* *
6950 * logs the value to the given logger at trace level. Message can be customized using message parameter
7051 */
7152fun <T > T.teeToTrace (logger : Logger , message : String = "{}")
72- = apply { if (debug) logger.trace(message, this ) }
53+ = apply { logger.trace(message, this ) }
7354
7455/* *
7556 * Evaluates the lambda and logs the result (of evaluation) to the given logger at trace level
7657 *
7758 */
7859inline fun <T > T.teeToTrace (logger : Logger , fn : (T ) -> String )
79- = apply { if (debug) logger.trace(fn(this ), this ) }
60+ = apply { logger.trace(fn(this ), this ) }
8061
8162
8263
0 commit comments