For example, let's say there is a function A that contains a logging action:
function A() {
tracing::debug!("abc");
}
Now, assume that function B can call A(), and function C can also call A().
Is there a way to make the logging occur when A is called from B, but not when it is called from C?
I don't want to touch function A here.
Thanks for the answer.
So, you mean using std::panic::Location::caller() and then applying an if condition to only log in specific callers. Am I correct?
if B and C cannot be instrumented, then it's unavoidable to make some changes to A, in order to figure out the caller's identity.
with #[track_caller], Location::caller() gives you a Location, from which you can get the file name, line number and column number, but it's up to you to map the file name and line number to functios names.
an alternative way is to capture the runtime callstack and walk up the stack frames to figure out the caller funciton, but this only works if you don't strip off the debug symbols, and you might need to use a crate (e.g. backtrace) for that, since BacktraceFrame in the standard library needs nightly feature, and is very limited.