/src/laserschein/Logger.java

https://github.com/allesblinkt/Laserschein · Java · 171 lines · 82 code · 46 blank · 43 comment · 14 complexity · b5c68f2f9fba31c172167a82db3d268f MD5 · raw file

  1. /**
  2. *
  3. * Laserschein. interactive ILDA output from processing and java
  4. *
  5. * 2012 by Benjamin Maus
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public License
  9. * as published by the Free Software Foundation; either version 2.1
  10. * of the License, or (at your option) any later version.
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General
  17. * Public License along with this library; if not, write to the
  18. * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  19. * Boston, MA 02111-1307 USA
  20. *
  21. * @author Benjamin Maus (http://www.allesblinkt.com)
  22. * @author Andreas Schlegel
  23. *
  24. */
  25. package laserschein;
  26. import java.text.SimpleDateFormat;
  27. import java.util.Calendar;
  28. import sun.reflect.Reflection;
  29. /**
  30. * A simple logger class. Derived from the Logger class in Andreas Schlegels NetP5
  31. *
  32. * @author Benjamin Maus
  33. *
  34. */
  35. public class Logger {
  36. private static SimpleDateFormat dateTimeFormat = new SimpleDateFormat("[yyyy.MM.dd HH:mm:ss]");
  37. private static boolean[] flags = new boolean[] { true, true, true, true, false };
  38. //private static String STRIP = "com.allesblinkt.psa.";
  39. /**
  40. * Determines if a certain log level should be displayed
  41. *
  42. * @param theLevel
  43. * @param theFlag
  44. */
  45. public static void set(LogLevel theLevel, boolean theFlag) {
  46. flags[theLevel.ordinal()] = theFlag;
  47. }
  48. /**
  49. * Switches logging on or off for all levels
  50. *
  51. * @param theFlag
  52. */
  53. public static void setAll(boolean theFlag) {
  54. for(int i = 0; i < flags.length; i++) {
  55. flags[i] = theFlag;
  56. }
  57. }
  58. public static void printError(String theLocation, String theMsg) {
  59. if (flags[LogLevel.ERROR.ordinal()] == true) {
  60. println(formattedDateTime() + " ERROR @ " + theLocation + " | " + theMsg);
  61. }
  62. }
  63. public static void printProcess(String theLocation, String theMsg) {
  64. if (flags[LogLevel.PROCESS.ordinal()] == true) {
  65. println(formattedDateTime() + " PROCESS @ " + theLocation + " | " + theMsg);
  66. }
  67. }
  68. public static void printWarning(String theLocation, String theMsg) {
  69. if (flags[LogLevel.WARNING.ordinal()] == true) {
  70. println(formattedDateTime() + " WARNING @ " + theLocation + " | " + theMsg);
  71. }
  72. }
  73. public static void printInfo(String theLocation, String theMsg) {
  74. if (flags[LogLevel.INFO.ordinal()] == true) {
  75. println(formattedDateTime() + " INFO @ " + theLocation + " | " + theMsg);
  76. }
  77. }
  78. public static void printDebug(String theLocation, String theMsg) {
  79. if (flags[LogLevel.DEBUG.ordinal()] == true) {
  80. println(formattedDateTime() + " DEBUG @ " + theLocation + " | " + theMsg);
  81. }
  82. }
  83. public static void printError(String theMsg) {
  84. printError(callerLocation(), theMsg);
  85. }
  86. public static void printProcess(String theMsg) {
  87. printProcess(callerLocation(), theMsg);
  88. }
  89. public static void printWarning(String theMsg) {
  90. printWarning(callerLocation(), theMsg);
  91. }
  92. public static void printInfo(String theMsg) {
  93. printInfo(callerLocation(), theMsg);
  94. }
  95. public static void printDebug(String theMsg) {
  96. printDebug(callerLocation(), theMsg);
  97. }
  98. public static void print(String theMsg) {
  99. System.out.print(theMsg);
  100. }
  101. public static void println(String theMsg) {
  102. System.out.println(theMsg);
  103. }
  104. public static String callerLocation() {
  105. String myString = Reflection.getCallerClass(3).getCanonicalName() + "/" + new Throwable().fillInStackTrace().getStackTrace()[2].getMethodName();
  106. //return myString.replaceFirst(STRIP, "");
  107. return myString;
  108. }
  109. public static void printBytes(byte[] theByteArray) {
  110. for (int i = 0; i < theByteArray.length; i++) {
  111. print(theByteArray[i] + " (" + (char) theByteArray[i] + ") ");
  112. if ((i + 1) % 4 == 0) {
  113. print("\n");
  114. }
  115. }
  116. print("\n");
  117. }
  118. public static String formattedDateTime() {
  119. Calendar myCalendar = Calendar.getInstance();
  120. return dateTimeFormat.format(myCalendar.getTime());
  121. }
  122. public enum LogLevel {
  123. ERROR, WARNING, PROCESS, INFO, DEBUG;
  124. }
  125. }