• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

need the caller name

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
is there any method by which i can know a particular fuction has been called from which class.
For eg

In method called i want to know from where is method called .
Is there any way to do that .
Thanks,
Tirthankar
 
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try getStackTrace() method in java.lang.Thread class (JDK 5) or getStackTrace() method in java.lang.Throwable ( JDK 1.4+). It might work.

This should return a StackTraceElement object which will have the required information.

By the way, just curious: Why do you need such info?
 
Tirthankar Dutta Chaudhuri
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
thanks for the prompt reply . well i need it for logging .
yes we get it from a stacktrace .
Thanks,
Tirthankar
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you writing your own logging implementation?

You don't need to do that. Use Log4J - it contains everything you need, and it can log the name of the class and method and even the line number in the source code where you're executing the logging statement.
 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remember that generating a stack trace to know the caller name, slows down your execution process.
So you should disable if you don't want to log the caller in production like environment.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, it can slow things down considerably if you do a lot of logging. But it depends on the code. You may well find that the performance overhead of logging the method names is unnoticeable in your application. The important thing is to remember that you can turn it off, or limit it to certain classes, by configuring the log4j.xml file.
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Generating a stack trace" never occurs - a thread always knows its current call stack so 'generating' it has no meaning. Generating some structure that resembles the current call stack (such as a StackTraceElement[]) is asymptotic in cost.
 
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I assume you are using 1.4 or greater in which case call stack information is cheap and available. There are several uses for this but of course you should avoid injecting dependencies on a particular caller in any method. Here's one way you can call a method to find out who's calling.



Note that you don't care who called "whence() but rather who called the method containing whence, hence the index is one not zero. Example:

 
Are you here to take over the surface world? Because this tiny ad will stop you!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic