StackTrace.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright (C) 2004-2013 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server 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. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.util;
  20. import java.util.logging.Level;
  21. import java.util.logging.Logger;
  22. public class StackTrace
  23. {
  24. private static Logger _log = Logger.getLogger(StackTrace.class.getName());
  25. public static boolean displayStackTraceInformation(Throwable ex)
  26. {
  27. return displayStackTraceInformation(ex, false);
  28. }
  29. public static boolean displayStackTraceInformation(Throwable ex, boolean displayAll)
  30. {
  31. if (ex == null)
  32. {
  33. return false;
  34. }
  35. _log.log(Level.INFO, "", ex);
  36. if (!displayAll)
  37. {
  38. return true;
  39. }
  40. StackTraceElement[] stackElements = ex.getStackTrace();
  41. _log.log(Level.INFO, "The " + stackElements.length + " element" + ((stackElements.length == 1) ? "" : "s") + " of the stack trace:\n");
  42. for (StackTraceElement stackElement : stackElements)
  43. {
  44. _log.log(Level.INFO, "File name: " + stackElement.getFileName());
  45. _log.log(Level.INFO, "Line number: " + stackElement.getLineNumber());
  46. String className = stackElement.getClassName();
  47. String packageName = extractPackageName(className);
  48. String simpleClassName = extractSimpleClassName(className);
  49. _log.log(Level.INFO, "Package name: " + ("".equals(packageName) ? "[default package]" : packageName));
  50. _log.log(Level.INFO, "Full class name: " + className);
  51. _log.log(Level.INFO, "Simple class name: " + simpleClassName);
  52. _log.log(Level.INFO, "Unmunged class name: " + unmungeSimpleClassName(simpleClassName));
  53. _log.log(Level.INFO, "Direct class name: " + extractDirectClassName(simpleClassName));
  54. _log.log(Level.INFO, "Method name: " + stackElement.getMethodName());
  55. _log.log(Level.INFO, "Native method?: " + stackElement.isNativeMethod());
  56. _log.log(Level.INFO, "toString(): " + stackElement.toString());
  57. _log.log(Level.INFO, "");
  58. }
  59. _log.log(Level.INFO, "");
  60. return true;
  61. }
  62. private static String extractPackageName(String fullClassName)
  63. {
  64. if ((null == fullClassName) || ("".equals(fullClassName)))
  65. {
  66. return "";
  67. }
  68. int lastDot = fullClassName.lastIndexOf('.');
  69. if (0 >= lastDot)
  70. {
  71. return "";
  72. }
  73. return fullClassName.substring(0, lastDot);
  74. }
  75. private static String extractSimpleClassName(String fullClassName)
  76. {
  77. if ((null == fullClassName) || ("".equals(fullClassName)))
  78. {
  79. return "";
  80. }
  81. int lastDot = fullClassName.lastIndexOf('.');
  82. if (0 > lastDot)
  83. {
  84. return fullClassName;
  85. }
  86. return fullClassName.substring(++lastDot);
  87. }
  88. private static String extractDirectClassName(String simpleClassName)
  89. {
  90. if ((null == simpleClassName) || ("".equals(simpleClassName)))
  91. {
  92. return "";
  93. }
  94. int lastSign = simpleClassName.lastIndexOf('$');
  95. if (0 > lastSign)
  96. {
  97. return simpleClassName;
  98. }
  99. return simpleClassName.substring(++lastSign);
  100. }
  101. private static String unmungeSimpleClassName(String simpleClassName)
  102. {
  103. if ((null == simpleClassName) || ("".equals(simpleClassName)))
  104. {
  105. return "";
  106. }
  107. return simpleClassName.replace('$', '.');
  108. }
  109. }