WIP charging behavior
[aquarium] / src / main / scala / gr / grnet / aquarium / util / LogHelpers.scala
1 /*
2  * Copyright 2011-2012 GRNET S.A. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or
5  * without modification, are permitted provided that the following
6  * conditions are met:
7  *
8  *   1. Redistributions of source code must retain the above
9  *      copyright notice, this list of conditions and the following
10  *      disclaimer.
11  *
12  *   2. Redistributions in binary form must reproduce the above
13  *      copyright notice, this list of conditions and the following
14  *      disclaimer in the documentation and/or other materials
15  *      provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  *
30  * The views and conclusions contained in the software and
31  * documentation are those of the authors and should not be
32  * interpreted as representing official policies, either expressed
33  * or implied, of GRNET S.A.
34  */
35
36 package gr.grnet.aquarium.util
37
38 import date.TimeHelpers
39 import org.slf4j.Logger
40
41 /**
42  *
43  * @author Christos KK Loverdos <loverdos@gmail.com>
44  */
45
46 object LogHelpers {
47   def logStarting(logger: Logger): Unit = {
48     logger.debug("Starting ...")
49   }
50
51   def logStarting(logger: Logger, fmt: String, args: Any*): Unit = {
52     logger.debug("Starting %s ...".format(fmt.format(args: _*)))
53   }
54
55   def logStarted(logger: Logger, ms0: Long, ms1: Long): Unit = {
56     logger.info("Started in %.3f sec".format(TimeHelpers.secDiffOfMillis(ms0, ms1)))
57   }
58
59   def logStarted(logger: Logger, ms0: Long, ms1: Long, fmt: String, args: Any*): Unit = {
60     logger.info("Started %s in %.3f sec".format(fmt.format(args: _*), TimeHelpers.secDiffOfMillis(ms0, ms1)))
61   }
62
63   def logStopping(logger: Logger): Unit = {
64     logger.debug("Stopping ...")
65   }
66
67   def logStopping(logger: Logger, fmt: String, args: Any*): Unit = {
68     logger.debug("Stopping %s ...".format(fmt.format(args: _*)))
69   }
70
71   def logStopped(logger: Logger, ms0: Long, ms1: Long): Unit = {
72     logger.info("Stopped in %.3f sec".format(TimeHelpers.secDiffOfMillis(ms0, ms1)))
73   }
74
75   def logStopped(logger: Logger, ms0: Long, ms1: Long, fmt: String, args: Any*): Unit = {
76     logger.info("Stopped %s in %.3f sec".format(fmt.format(args: _*), TimeHelpers.secDiffOfMillis(ms0, ms1)))
77   }
78
79   def logChainOfCauses(logger: Logger, t: Throwable, message: String = "Oops!"): Unit = {
80     logger.error(message + "\n{}", chainOfCausesForLogging(t))
81   }
82
83   def logChainOfCausesAndException(logger: Logger, t: Throwable, message: String = "Oops!"): Unit = {
84     logger.error(message + "\n{}", chainOfCausesForLogging(t))
85     logger.error(message, t)
86   }
87
88   @inline
89   final def Debug(logger: Logger, fmt: String, args: Any*): Unit = {
90     if(logger.isDebugEnabled) {
91       logger.debug(fmt.format(args:_*))
92     }
93   }
94
95   @inline
96   final def Info(logger: Logger, fmt: String, args: Any*): Unit = {
97     if(logger.isInfoEnabled) {
98       logger.info(fmt.format(args:_*))
99     }
100   }
101
102   @inline
103   final def Warn(logger: Logger, fmt: String, args: Any*): Unit = {
104     if(logger.isWarnEnabled) {
105       logger.warn(fmt.format(args:_*))
106     }
107   }
108
109   @inline
110   final def Error(logger: Logger, fmt: String, args: Any*): Unit = {
111     if(logger.isErrorEnabled) {
112       logger.error(fmt.format(args:_*))
113     }
114   }
115
116   @inline
117   final def Error(logger: Logger, t: Throwable, fmt: String, args: Any*): Unit = {
118     if(logger.isErrorEnabled) {
119       logger.error(fmt.format(args:_*), t)
120     }
121   }
122
123   final def DebugMap[K, V](
124       logger: Logger,
125       name: String,
126       map: scala.collection.Map[K, V],
127       oneLineLimit: Int = 3
128   ): Unit = {
129
130     if(logger.isDebugEnabled) {
131       val mapSize = map.size
132       if(mapSize <= oneLineLimit) {
133         Debug(logger, "%s [#=%s] = %s", name, mapSize, map)
134       } else {
135         logger.debug("%s [#=%s]:", name, mapSize)
136         val maxKeySize = maxStringSize(map.keySet)
137         for((k, v) <- map) {
138           this.Debug(logger, "%s -> %s", rpad(k.toString, maxKeySize), v)
139         }
140       }
141     }
142   }
143
144   final def DebugSeq[T](logger: Logger, name: String, seq: scala.collection.Seq[T], oneLineLimit: Int = 3): Unit = {
145     if(logger.isDebugEnabled) {
146       val seqSize = seq.size
147       if(seqSize <= oneLineLimit) {
148         Debug(logger, "%s [#=%s] = %s", name, seqSize, seq)
149       } else {
150         Debug(logger, "%s [#=%s]: ", name, seqSize)
151         seq.foreach(Debug(logger, "%s", _))
152       }
153     }
154   }
155
156   final def DebugSet[T](logger: Logger, name: String, set: scala.collection.Set[T], oneLineLimit: Int = 3): Unit = {
157     if(logger.isDebugEnabled) {
158       val setSize = set.size
159       if(setSize <= oneLineLimit) {
160         Debug(logger, "%s [#=%s] = %s", name, setSize, set)
161       } else {
162         Debug(logger, "%s [#=%s]: ", name, setSize)
163         set.foreach(Debug(logger, "%s", _))
164       }
165     }
166   }
167 }