LOG.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. Licensed to the Apache Software Foundation (ASF) under one
  3. or more contributor license agreements. See the NOTICE file
  4. distributed with this work for additional information
  5. regarding copyright ownership. The ASF licenses this file
  6. to you under the Apache License, Version 2.0 (the
  7. "License"); you may not use this file except in compliance
  8. with the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing,
  11. software distributed under the License is distributed on an
  12. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. KIND, either express or implied. See the License for the
  14. specific language governing permissions and limitations
  15. under the License.
  16. */
  17. package org.apache.cordova;
  18. import android.util.Log;
  19. /**
  20. * Log to Android logging system.
  21. *
  22. * Log message can be a string or a printf formatted string with arguments.
  23. * See http://developer.android.com/reference/java/util/Formatter.html
  24. */
  25. public class LOG {
  26. public static final int VERBOSE = Log.VERBOSE;
  27. public static final int DEBUG = Log.DEBUG;
  28. public static final int INFO = Log.INFO;
  29. public static final int WARN = Log.WARN;
  30. public static final int ERROR = Log.ERROR;
  31. // Current log level
  32. public static int LOGLEVEL = Log.ERROR;
  33. /**
  34. * Set the current log level.
  35. *
  36. * @param logLevel
  37. */
  38. public static void setLogLevel(int logLevel) {
  39. LOGLEVEL = logLevel;
  40. Log.i("CordovaLog", "Changing log level to " + logLevel);
  41. }
  42. /**
  43. * Set the current log level.
  44. *
  45. * @param logLevel
  46. */
  47. public static void setLogLevel(String logLevel) {
  48. if ("VERBOSE".equals(logLevel)) LOGLEVEL = VERBOSE;
  49. else if ("DEBUG".equals(logLevel)) LOGLEVEL = DEBUG;
  50. else if ("INFO".equals(logLevel)) LOGLEVEL = INFO;
  51. else if ("WARN".equals(logLevel)) LOGLEVEL = WARN;
  52. else if ("ERROR".equals(logLevel)) LOGLEVEL = ERROR;
  53. Log.i("CordovaLog", "Changing log level to " + logLevel + "(" + LOGLEVEL + ")");
  54. }
  55. /**
  56. * Determine if log level will be logged
  57. *
  58. * @param logLevel
  59. * @return true if the parameter passed in is greater than or equal to the current log level
  60. */
  61. public static boolean isLoggable(int logLevel) {
  62. return (logLevel >= LOGLEVEL);
  63. }
  64. /**
  65. * Verbose log message.
  66. *
  67. * @param tag
  68. * @param s
  69. */
  70. public static void v(String tag, String s) {
  71. if (LOG.VERBOSE >= LOGLEVEL) Log.v(tag, s);
  72. }
  73. /**
  74. * Debug log message.
  75. *
  76. * @param tag
  77. * @param s
  78. */
  79. public static void d(String tag, String s) {
  80. if (LOG.DEBUG >= LOGLEVEL) Log.d(tag, s);
  81. }
  82. /**
  83. * Info log message.
  84. *
  85. * @param tag
  86. * @param s
  87. */
  88. public static void i(String tag, String s) {
  89. if (LOG.INFO >= LOGLEVEL) Log.i(tag, s);
  90. }
  91. /**
  92. * Warning log message.
  93. *
  94. * @param tag
  95. * @param s
  96. */
  97. public static void w(String tag, String s) {
  98. if (LOG.WARN >= LOGLEVEL) Log.w(tag, s);
  99. }
  100. /**
  101. * Error log message.
  102. *
  103. * @param tag
  104. * @param s
  105. */
  106. public static void e(String tag, String s) {
  107. if (LOG.ERROR >= LOGLEVEL) Log.e(tag, s);
  108. }
  109. /**
  110. * Verbose log message.
  111. *
  112. * @param tag
  113. * @param s
  114. * @param e
  115. */
  116. public static void v(String tag, String s, Throwable e) {
  117. if (LOG.VERBOSE >= LOGLEVEL) Log.v(tag, s, e);
  118. }
  119. /**
  120. * Debug log message.
  121. *
  122. * @param tag
  123. * @param s
  124. * @param e
  125. */
  126. public static void d(String tag, String s, Throwable e) {
  127. if (LOG.DEBUG >= LOGLEVEL) Log.d(tag, s, e);
  128. }
  129. /**
  130. * Info log message.
  131. *
  132. * @param tag
  133. * @param s
  134. * @param e
  135. */
  136. public static void i(String tag, String s, Throwable e) {
  137. if (LOG.INFO >= LOGLEVEL) Log.i(tag, s, e);
  138. }
  139. /**
  140. * Warning log message.
  141. *
  142. * @param tag
  143. * @param e
  144. */
  145. public static void w(String tag, Throwable e) {
  146. if (LOG.WARN >= LOGLEVEL) Log.w(tag, e);
  147. }
  148. /**
  149. * Warning log message.
  150. *
  151. * @param tag
  152. * @param s
  153. * @param e
  154. */
  155. public static void w(String tag, String s, Throwable e) {
  156. if (LOG.WARN >= LOGLEVEL) Log.w(tag, s, e);
  157. }
  158. /**
  159. * Error log message.
  160. *
  161. * @param tag
  162. * @param s
  163. * @param e
  164. */
  165. public static void e(String tag, String s, Throwable e) {
  166. if (LOG.ERROR >= LOGLEVEL) Log.e(tag, s, e);
  167. }
  168. /**
  169. * Verbose log message with printf formatting.
  170. *
  171. * @param tag
  172. * @param s
  173. * @param args
  174. */
  175. public static void v(String tag, String s, Object... args) {
  176. if (LOG.VERBOSE >= LOGLEVEL) Log.v(tag, String.format(s, args));
  177. }
  178. /**
  179. * Debug log message with printf formatting.
  180. *
  181. * @param tag
  182. * @param s
  183. * @param args
  184. */
  185. public static void d(String tag, String s, Object... args) {
  186. if (LOG.DEBUG >= LOGLEVEL) Log.d(tag, String.format(s, args));
  187. }
  188. /**
  189. * Info log message with printf formatting.
  190. *
  191. * @param tag
  192. * @param s
  193. * @param args
  194. */
  195. public static void i(String tag, String s, Object... args) {
  196. if (LOG.INFO >= LOGLEVEL) Log.i(tag, String.format(s, args));
  197. }
  198. /**
  199. * Warning log message with printf formatting.
  200. *
  201. * @param tag
  202. * @param s
  203. * @param args
  204. */
  205. public static void w(String tag, String s, Object... args) {
  206. if (LOG.WARN >= LOGLEVEL) Log.w(tag, String.format(s, args));
  207. }
  208. /**
  209. * Error log message with printf formatting.
  210. *
  211. * @param tag
  212. * @param s
  213. * @param args
  214. */
  215. public static void e(String tag, String s, Object... args) {
  216. if (LOG.ERROR >= LOGLEVEL) Log.e(tag, String.format(s, args));
  217. }
  218. }