2

I'm using eclipse and want to have a "macro" that the preprocessor will replace with the name of the current method before compiling it.

I have an error reporting function, that is called as: reportthis(String errormessage) - different functions throughout the application have try/catch blocks, that call reportthis(...) from the catch block upon errors.

I'd like to be able to specify something like reportthis(MACRO_CURRENT_METHOD_NAME + ":" + e.ToString()); - where MACRO_CURRENT_METHOD_NAME will be preprocessed by eclipse before compilation and result in the name of the method where the catch {} block calls reportthis().

So if the catch{} block happens in main(), the macro should return the string "main" (or "main()", etc.).

Is this possible? how do i go about achieving my goal?

Thank you!

Edit I wish to get this done by preprocessors in Eclipse - are those impossible? isn't it possible to perhaps write a plugin for eclipse to replace all occurrences of "MACRO_CURRENT_METHOD_NAME" with the current function name?

Michael Donohue
  • 11,776
  • 5
  • 31
  • 44

3 Answers3

2

I've not found an automated way of doing this, so have manually added a string literal that indicates the name of the caller at each invocation of the logging code.

Nokia's S40 platform is also based on Java-ME, and I know some Nokia S40 developers have made good use of Jarrut, which is available on Sourceforge, to produce stack traces by modifying the program to track the stack. You could leverage this functionality to get the calling function name in your logging code, but you may need to modify Jarrut a bit to make that work.

Michael Donohue
  • 11,776
  • 5
  • 31
  • 44
1

Java does not support Macros. But what you can do to determine the current method is something like

  final StackTraceElement aTop = Thread.currentThread ().getStackTrace ()[1];
  System.out.println (aTop.getMethodName ());

By using the element at index [1] you get the calling method, because the element at [0] is Thread.getStackTrace(). If you wrap this code in an additional method, you must adopt the array index e.g. to 2, depending on the number of wrapping methods you are using.

Philip Helger
  • 1,814
  • 18
  • 28
  • As responded to Durandal, this is a blackberry application and it does not have getStackTrace() so this, unfortunately, is impossible there. – user2418661 May 24 '13 at 18:59
  • Than look at http://stackoverflow.com/questions/4389758/is-there-a-way-to-get-the-stack-trace-of-an-exception-in-form-of-a-string-in-bla and follow the Link :) – Philip Helger May 24 '13 at 19:06
  • I don't want to work with the Event Log.. it makes it much more complex and likely to not work IRL – user2418661 May 24 '13 at 19:09
  • Than I give up. Maybe you can edit the question and state the Blackberry thing on top... – Philip Helger May 24 '13 at 19:12
  • Edited... please see it, isn't there a way to solve it from within Eclipse? e.g. by writing an eclipse plugin/preprocessor – user2418661 May 24 '13 at 19:12
  • You could search a Maven plugin, or what build system are you using? Of course it may be possible with something like AOP... – Philip Helger May 24 '13 at 19:13
0

There is no preprocessor in java, and no macro language either.

While there are situations where either could be useful, if I understand your problem its entirely pointless, since the stack trace of the exception will already contain class and method of the place where the excetion occured.

Instead of passing a String to your "reportthis()", make a signature that just takes the exception and prints it (or just write e.printStackTrace()).

Durandal
  • 19,919
  • 4
  • 36
  • 70
  • I'm developing a blackberry application... so printing the stack trace to stdout/err will not help - reportthis() will report errors to the server (when run in debug mode) - and while right now i can manually use it as 'reportthis("main(): " + e.ToString());', i'd like to see a better way than to manually have to do this for every catch{} block. – user2418661 May 24 '13 at 18:58
  • Well then print the exception to another target in reportthis(). There is a signature of printStackTrace() that takes a PrintStream, only your imagination is the limit where you can print it to. Why would you throw away all the useful info from the stack trace *voluntarily* anyway? – Durandal May 24 '13 at 19:03
  • What do you mean by "There is a signature of printStackTrace() that takes a PrintStream" ? the only printStackTrace() I have available, prints to stderr – user2418661 May 24 '13 at 19:07
  • I see, too bad blackberry doesn't support elementary runtime methods. Looks like you're screwed, you may resume panic. – Durandal May 24 '13 at 19:12
  • 1
    Actually, the BlackBerry plugin's Java build tools **do** support limited preprocessor directives, [but does not support macros](http://stackoverflow.com/a/1189573/119114), which is what this question needs. – Nate May 25 '13 at 01:16