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().
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().
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.i o.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
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.i
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks alot.
ASKER
ps Use PrintWriter instead of PrintStream in 1.1
ASKER
Spoke too soon. It compiles with printwriter but
no stack info.
no stack info.
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!