PageRenderTime 50ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/src/mpv5/logging/Log.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 360 lines | 213 code | 34 blank | 113 comment | 34 complexity | 59bc4e403f82e6a6e3389a6276a1c8f3 MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0, GPL-3.0, GPL-2.0, AGPL-3.0, JSON, BSD-3-Clause
  1. /*
  2. * This file is part of YaBS.
  3. *
  4. * YaBS is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * YaBS is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with YaBS. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. package mpv5.logging;
  18. import de.frame4j.io.LogHandler;
  19. import java.awt.Color;
  20. import java.io.File;
  21. import java.io.IOException;
  22. import java.io.PrintWriter;
  23. import java.io.StringWriter;
  24. import java.util.ArrayList;
  25. import java.util.Arrays;
  26. import java.util.Collections;
  27. import java.util.Enumeration;
  28. import java.util.List;
  29. import java.util.logging.Formatter;
  30. import java.util.logging.Handler;
  31. import java.util.logging.Level;
  32. import java.util.logging.LogManager;
  33. import java.util.logging.LogRecord;
  34. import java.util.logging.Logger;
  35. import javax.swing.table.TableModel;
  36. import mpv5.Main;
  37. import mpv5.bugtracker.ExceptionHandler;
  38. import mpv5.globals.Messages;
  39. import mpv5.utils.files.FileReaderWriter;
  40. /**
  41. *
  42. *
  43. */
  44. public class Log {
  45. static {
  46. Logger globalLogger = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
  47. globalLogger.addHandler(new LogHandler(new Formatter() {
  48. @Override
  49. public String format(LogRecord record) {
  50. if (record != null) {
  51. return record.getMessage();
  52. } else {
  53. return "null";
  54. }
  55. }
  56. }) {
  57. @Override
  58. public void publish(LogRecord record) {
  59. Log.Debug(this, record);
  60. }
  61. @Override
  62. public void flush() {
  63. }
  64. @Override
  65. public void close() throws SecurityException {
  66. }
  67. });
  68. Logger.getLogger(Log.class.getName()).log(Level.INFO, "Yabs Logger set!");
  69. }
  70. /**
  71. *
  72. * Set the logging to NONE
  73. */
  74. public static final int LOGLEVEL_NONE = 0;
  75. /**
  76. *
  77. * Gives basic messages
  78. */
  79. public static final int LOGLEVEL_NORMAL = 1;
  80. /**
  81. *
  82. * Produces a huge amount of messages, and error stack traces
  83. */
  84. public static final int LOGLEVEL_DEBUG = 2;
  85. private static int loglevel = 1;
  86. private static List<LogConsole> loggers = new ArrayList<LogConsole>(Arrays.asList(new LogConsole[]{(LogConsole) new YConsole()}));
  87. /**
  88. * Print out a text file
  89. * @param file
  90. */
  91. public static void Debug(File file) {
  92. PrintArray(new FileReaderWriter(file).readLines());
  93. }
  94. /**
  95. *
  96. * @param source
  97. * @param message
  98. * @param alwaysToKonsole
  99. * @deprecated Replaced with <code>Debug(Object source, Object message)</code>
  100. */
  101. public static void Debug(Object source, Object message, boolean alwaysToKonsole) {
  102. Debug(source, message);
  103. }
  104. /**
  105. * The main debug method. Logs the given message depending on the current Log level.<br/><br/>
  106. * <li>LOGLEVEL_NONE = no logging</li>
  107. * <li>LOGLEVEL_HIGH = basic logging</li>
  108. * <li>LOGLEVEL_DEBUG = verbose logging</li>
  109. *
  110. * @param source
  111. * @param message
  112. */
  113. public static synchronized void Debug(Object source, Object message) {
  114. String sourcen;
  115. if (source instanceof Class) {
  116. sourcen = ((Class) source).getName();
  117. } else {
  118. sourcen = source.getClass().getName();
  119. }
  120. if (message instanceof Throwable) {
  121. try {
  122. ExceptionHandler.add((Exception) message);
  123. } catch (Exception e) {
  124. Print(e);
  125. }
  126. }
  127. switch (loglevel) {
  128. case LOGLEVEL_DEBUG:
  129. writeDirect(sourcen + ": " + message);
  130. if (message != null && message instanceof Throwable) {
  131. ((Throwable) message).printStackTrace();
  132. writeDirect(getStackTrace(((Throwable) message)));
  133. writeDirect("\nCaused by:\n");
  134. try {
  135. ((Exception) message).getCause().printStackTrace();
  136. writeDirect(getStackTrace(((Throwable) message).getCause()));
  137. mpv5.YabsViewProxy.instance().addMessage(Messages.ERROR_OCCURED + ". " + Messages.SEE_LOG, Color.RED);
  138. } catch (Exception e) {
  139. }
  140. }
  141. break;
  142. case LOGLEVEL_NORMAL:
  143. if (message != null && (message.toString().contains("Exception") || message.toString().contains("Error"))) {
  144. write(sourcen + ": " + message);
  145. mpv5.YabsViewProxy.instance().addMessage(Messages.ERROR_OCCURED + ". " + Messages.SEE_LOG, Color.RED);
  146. }
  147. break;
  148. case LOGLEVEL_NONE:
  149. break;
  150. default:
  151. write(sourcen + ": " + message);
  152. }
  153. }
  154. /**
  155. * Prints messages, regardless the log level
  156. * @param message
  157. */
  158. public static void Print(Object... message) {
  159. for (int i = 0; i < message.length; i++) {
  160. Object object = message[i];
  161. write(object);
  162. if (!YConsole.CONSOLE_LOG_ENABLED) {
  163. System.out.println(object);
  164. }
  165. }
  166. }
  167. /**
  168. * Print an array
  169. * @param array
  170. */
  171. public static void PrintArray(Object[][][] array) {
  172. write("Print array: {");
  173. if (loglevel != LOGLEVEL_NONE) {
  174. for (int i = 0; i < array.length; i++) {
  175. write("");
  176. for (int k = 0; k < array[i].length; k++) {
  177. write("");
  178. for (int f = 0; f < array[i][k].length; f++) {
  179. write(array[i][k][f] + " ");
  180. }
  181. }
  182. }
  183. }
  184. write("}//End Print array");
  185. }
  186. /**
  187. * Print an array
  188. * @param array
  189. */
  190. public static void PrintArray(Object[][] array) {
  191. write("Print array: {");
  192. for (int i = 0; i < array.length; i++) {
  193. for (int k = 0; k < array[i].length; k++) {
  194. if (loglevel != LOGLEVEL_NONE) {
  195. write("[" + i + "]" + " [" + k + "] " + array[i][k]);
  196. }
  197. }
  198. }
  199. write("}//End Print array");
  200. }
  201. /**
  202. * Print an array
  203. * @param string
  204. */
  205. public static void PrintArray(Object[] string) {
  206. write("Print array: {");
  207. if (loglevel != LOGLEVEL_NONE) {
  208. write(Arrays.asList(string));
  209. }
  210. write("}//End Print array");
  211. }
  212. /**
  213. * Print a list
  214. * @param lst
  215. */
  216. public static void PrintArray(List lst) {
  217. for (int idx = 0; idx < lst.size(); idx++) {
  218. write(lst.get(idx));
  219. }
  220. }
  221. private static synchronized void write(final Object obj) {
  222. Runnable runnable = new Runnable() {
  223. public void run() {
  224. for (int i = 0; i < loggers.size(); i++) {
  225. loggers.get(i).log(obj);
  226. ;
  227. }
  228. }
  229. };
  230. Thread thread = new Thread(runnable);
  231. thread.start();
  232. }
  233. private static synchronized void writeDirect(final Object obj) {
  234. for (int i = 0; i < loggers.size(); i++) {
  235. loggers.get(i).log(obj);
  236. }
  237. }
  238. /**
  239. * Debug an Exception
  240. * @param ex
  241. */
  242. public static void Debug(Throwable ex) {
  243. Debug(Log.class, ex);
  244. }
  245. /**
  246. *
  247. * @return
  248. */
  249. public static List<LogConsole> getLogger() {
  250. return Collections.unmodifiableList(loggers);
  251. }
  252. /**
  253. * Set the log level<br/>
  254. * <li>LOGLEVEL_NONE = no logging</li>
  255. * <li>LOGLEVEL_HIGH = basic logging</li>
  256. * <li>LOGLEVEL_DEBUG = verbose logging</li>
  257. * @param level
  258. */
  259. public static void setLogLevel(int level) {
  260. Log.setLoglevel(level);
  261. }
  262. /**
  263. * Print a table model
  264. * @param model
  265. */
  266. public static void PrintArray(TableModel model) {
  267. for (int i = 0; i < model.getRowCount(); i++) {
  268. for (int j = 0; j < model.getColumnCount(); j++) {
  269. write(model.getValueAt(i, j));
  270. }
  271. }
  272. }
  273. /**
  274. *
  275. */
  276. public static void PrintArray(Enumeration<String> keys) {
  277. while (keys.hasMoreElements()) {
  278. Print(keys.nextElement());
  279. }
  280. }
  281. /**
  282. * @param aLoglevel the loglevel to set
  283. */
  284. public static void setLoglevel(int aLoglevel) {
  285. loglevel = aLoglevel;
  286. }
  287. /**
  288. * @param aLogger the logger to set
  289. */
  290. public static void setLogger(LogConsole aLogger) {
  291. loggers.clear();
  292. loggers.add(aLogger);
  293. }
  294. public static void addLogger(LogConsole logConsole) {
  295. loggers.add(logConsole);
  296. }
  297. /**
  298. *
  299. * @return
  300. */
  301. public static boolean isDebugging() {
  302. return loglevel == LOGLEVEL_DEBUG;
  303. }
  304. private Log() {
  305. }
  306. /**
  307. *
  308. * @return The current log level
  309. */
  310. public static int getLoglevel() {
  311. return loglevel;
  312. }
  313. /**
  314. * Writes the stacktrace to a String
  315. * @param t
  316. * @return
  317. */
  318. public static String getStackTrace(Throwable t) {
  319. StringWriter sw = new StringWriter();
  320. PrintWriter pw = new PrintWriter(sw, true);
  321. t.printStackTrace(pw);
  322. pw.flush();
  323. sw.flush();
  324. return sw.toString();
  325. }
  326. }