Often I will send debugging info to my log file from within Java, and it helps to give decent information as to where in the code the log message was generated so I can quickly refer back to it. I don't want to hard-code anything in the log message about its location, because I have to remember to update the log message if I rename its method or otherwise refactor the code into a different structure. I'd love to have a static function that simply returns the calling function's class and method name. For example, if I have a class called Machine and a method called doStuff, I'd like my log message generation code in that method to look something like this:
String log_msg = Blah.getClassAndMethodName()+" did the wrong thing!";
The static method getClassAndMethodName() in class Blah would return "Machine.doStuff". That way, when I look at the log file, I can easily tell where in the code the message was generated. I realize this is a bit tricky, since I need this method in Blah to return information about its calling function in a separate class, Machine, but this would make it easy to just use this static call anywhere.
I don't know if this is possible, but I'd appreciate any pointers towards an answer! Also, assuming it is possible, is this a particularly high overhead kind of operation? (I'm assuming reflection would be needed, not my strong suit)
Thanks!