Link to home
Start Free TrialLog in
Avatar of hank1
hank1

asked on

get method and class names

I have created a class to format error messages.  I
would like to use it instead of things like
System.out.println().  I
would like this object to tell me who called it.
Is the a way to discover the class and method name of
the caller?  printStackTrace() would be nice but it looks
like I need to catch().
Avatar of imladris
imladris
Flag of Canada image

printStackTrace will work fine. In your error class this code:

Throwable e=new Throwable();
ByteArrayOutputStream bo;
e.printStackTrace(new PrintStream(bo=new                                                     ByteArrayOutputStream()));
String stktrc=bo.toString();

Now the String stktrc contains a stacktrace, and you can slice it and dice it any way you like; print the first line, or the second line, or the whole thing!

Avatar of hank1
hank1

ASKER

Works!  But let me follow up 1st.  I get a deprecation
notice.  Is this something I can correct or should I close
my eyes?

+5S:\lab\688sys\>javac -deprecation CtlLogger.java
CtlLogger.java:21: Note: The constructor java.io.PrintStream(java.io.OutputSt
ream) has been deprecated.
    e.printStackTrace(new PrintStream(bo =
                      ^
Note: CtlLogger.java uses a deprecated API.  Please consult the documentation
 for a better alternative.
2 warnings
ASKER CERTIFIED SOLUTION
Avatar of imladris
imladris
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of hank1

ASKER

Thanks alot.
Avatar of hank1

ASKER

ps  Use PrintWriter instead of PrintStream in 1.1
Avatar of hank1

ASKER

Spoke too soon.  It compiles with printwriter but
no stack info.