[Util] Better Debugging

Discussion in 'Resources' started by Jogy34, Aug 27, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    Jogy34

    I little bit ago I found that I was using System.out.println(); a lot for debugging things and this got really annoying because I would have to search through a lot of printouts to find the debug line I was looking for and then I would continually get lost with where I was in the file or which file which debug statement was from so I decided to create my own debug prints and thought others might be able to use this to help them.

    This is a self contained class. The only thing that you might want to do is call the initialize method from your main class or you could just use a static block, I was just finding places where I would want to use this before I finished setting up my config and I wanted the config values for this to be at the bottom of the file.

    Anyways, this allows you to use formatted debug prints with different levels and it allows the use of color. Each level creates it's own config variable under the "BASE_CONFIG_PATH" when it is initialized so that if you accidentally leave plain debug prints in it when you submit your plugin you don't have to worry about your users seeing those. They each then have their own header/footer print, a color to print with, and if they should print out the class, method, and/or line that it was called from. What I did was to have 4 levels of statements:
    • Info: This is just for when I want data from something and don't really care where it is and in what class, method, or line so those are all disabled.
    • Debug: I use this when I trying to fix a problem. I typically end up having 5 or 6 of these if I can't figure it out so I print out the class, method, and line so that I can get to exactly where it is in a matter of seconds.
    • Warning: This is something that went somewhat wrong but won't break the plugin and is something that my users might be good to know about as some things might not work correctly. I print the class and line with this just in case something gets screwed up on someone's server and these get thrown a lot so that I can find where the source of the problem is faster than just searching through a bunch of different files first.
    • Error: This one is special, I bypass the config system and force this to always be shown. This is for if the code gets somewhere that will probably end up breaking the plugin or crashing the server and is something that I should probably be alerted from.
    The header I made so that the start and end are easy to find of the debug print but can easily be taken out if you find them annoying. I also made quick access methods for convince but they all go to the main print methods with the correct level.

    The print method takes in a message priority and a list of messages. It first checks if the priority should be displayed and then does nothing if it shouldn't. If it should, then retrieves the console sender so that it can use colors when printing out and retrieves the current threads StackTrace. It looks through that until it finds the last call from a class that isn't its own class and uses that one to get the class, method, and line number. It then prints the header and checks if it should print the class, method, and lines and if so then it prints them out. After that it prints out "Messages: " so that you (or others) know what you are actually trying to say. It then prints out every message that was sent in on a different line and once again prints out the header to end it. It also checks if there are any resets are puts its own color after that so you can use a reset but it will just revert back to its own color instead of going to your console's text color.

    To call this you would just use DebugPrint.println(pritoity, messages) or you would use one of the shortcut methods like (in this one) info, debug, warning, or error.

    Anyone that wants to use this go ahead and use it how you want.Anyone that has any ideas on me improving this tell me.

    Class (I use this for my TARDIS plugin if you can't tell):
    Code:java
    1.  
    2. public class DebugPrint
    3. {
    4. protected static final String BASE_CONFIG_PATH = "ConsoleMessages.";
    5. public static enum MessagePriority
    6. {
    7. INFO("Info", false, "[TARDIS Info]", ChatColor.GRAY, false, false, false),
    8. DEBUG("Debug", false, "***[TARDIS Debug]***", ChatColor.DARK_GREEN, true, true, true),
    9. WARNING("Warning", true, "****[TARDIS Warning]****", ChatColor.GOLD, true, false, true),
    10. ERROR("*****[TARDIS ERROR]*****", ChatColor.DARK_RED, true, true, true);
    11.  
    12. protected boolean use = false;
    13. protected boolean defaultUse = false;
    14. protected String headerFormat;
    15. protected String configPath = null;
    16. protected ChatColor displayColor;
    17. protected boolean classPrint, methodPrint, linePrint;
    18.  
    19. //Forces this one to be used
    20. private MessagePriority(String headerFormat, ChatColor color, boolean classPrint, boolean methodPrint, boolean linePrint)
    21. {
    22. this.use = true;
    23. this.headerFormat = headerFormat;
    24. this.displayColor = color;
    25. this.classPrint = classPrint;
    26. this.methodPrint = methodPrint;
    27. this.linePrint = linePrint;
    28. }
    29.  
    30. //The config path dictates if this should be on or off
    31. private MessagePriority(String configPath, boolean defaultUse, String headerFormat, ChatColor color, boolean classPrint, boolean methodPrint, boolean linePrint)
    32. {
    33. this.defaultUse = defaultUse;
    34. this.configPath = configPath;
    35. this.headerFormat = headerFormat;
    36. this.displayColor = color;
    37. this.classPrint = classPrint;
    38. this.methodPrint = methodPrint;
    39. this.linePrint = linePrint;
    40. }
    41.  
    42. public boolean shouldUse()
    43. {
    44. return this.use;
    45. }
    46.  
    47. public String getHeaderFormat()
    48. {
    49. return this.headerFormat;
    50. }
    51.  
    52. public ChatColor getDisplayColor()
    53. {
    54. return this.displayColor;
    55. }
    56.  
    57. public boolean isClassPrint()
    58. {
    59. return this.classPrint;
    60. }
    61.  
    62. public boolean isMethodPrint()
    63. {
    64. return this.methodPrint;
    65. }
    66.  
    67. public boolean isLinePrint()
    68. {
    69. return this.linePrint;
    70. }
    71.  
    72. //Get if this should be turned on from the config
    73. public void initalize(TARDISMain plugin)
    74. {
    75. if(this.configPath != null)
    76. {
    77. if(!plugin.getConfig().contains(BASE_CONFIG_PATH + this.configPath))
    78. {
    79. plugin.getConfig().set(BASE_CONFIG_PATH + this.configPath, this.defaultUse);
    80. plugin.saveConfig();
    81. }
    82. this.use = plugin.getConfig().getBoolean(BASE_CONFIG_PATH + this.configPath);
    83. }
    84. }
    85. }
    86.  
    87. //Shortcut for info messages
    88. public static void info(String... messages)
    89. {
    90. DebugPrint.println(MessagePriority.INFO, messages);
    91. }
    92.  
    93. //Shortcut for debug messages
    94. public static void debug(String... messages)
    95. {
    96. DebugPrint.println(MessagePriority.DEBUG, messages);
    97. }
    98.  
    99. //Shortcut for warning messages
    100. public static void warning(String... messages)
    101. {
    102. DebugPrint.println(MessagePriority.WARNING, messages);
    103. }
    104.  
    105. //Shortcut for error messages
    106. public static void error(String... messages)
    107. {
    108. DebugPrint.println(MessagePriority.ERROR, messages);
    109. }
    110.  
    111. //prints out with the specified priority
    112. public static void println(MessagePriority priority, String... messages)
    113. {
    114. if(priority.shouldUse())
    115. {
    116. ConsoleCommandSender console = Bukkit.getConsoleSender();
    117. ChatColor color = priority.getDisplayColor();
    118. StackTraceElement[] elements = Thread.currentThread().getStackTrace();
    119. int callIndex = 1;
    120. while(elements[callIndex].getClassName().equals(DebugPrint.class.getName()) &&
    121. callIndex < elements.length - 1) callIndex++;
    122. console.sendMessage(color + priority.getHeaderFormat());
    123. if(priority.isClassPrint()) console.sendMessage(color + "Class: " + elements[callIndex].getClassName());
    124. if(priority.isMethodPrint()) console.sendMessage(color + "Method: " + elements[callIndex].getMethodName());
    125. if(priority.isLinePrint()) console.sendMessage(color + "Line: " + elements[callIndex].getLineNumber());
    126. if(messages.length > 0) console.sendMessage(color + "Message:");
    127. for(String s : messages)
    128. {
    129. console.sendMessage(color + " " + s.replaceAll("" + ChatColor.RESET, "" +
    130. ChatColor.RESET + color));
    131. }
    132. console.sendMessage(color + priority.getHeaderFormat());
    133. }
    134. }
    135.  
    136. //Tells the priorities to check the config file if they should be used
    137. public static void initalize(TARDISMain plugin)
    138. {
    139. for(MessagePriority priority : MessagePriority.values())
    140. {
    141. priority.initalize(plugin);
    142. }
    143. }
    144. }
    145.  

    TARDISMain is my main class to get the config file from.
     
  2. Offline

    MTN

Thread Status:
Not open for further replies.

Share This Page