digitalmars.D.learn - Can I get caller name?
- AsmMan (12/12) Sep 25 2014 for debugging purposes, I need to get the caller name. Is it
- ketmar via Digitalmars-d-learn (4/6) Sep 25 2014 On Fri, 26 Sep 2014 05:59:32 +0000
- Vladimir Panteleev (44/56) Sep 25 2014 Method 1 (extra template parameter):
- AsmMan (1/1) Sep 26 2014 Thanks guys!
for debugging purposes, I need to get the caller name. Is it
possible? if so, how do I do this?
void foo()
{
baa();
}
void baa()
{
wrilten(caller_name()); // foo
}
I know I could create an extra parameter and pass the current
function name as argument but it isn't a possibility for now.
Sep 25 2014
On Fri, 26 Sep 2014 05:59:32 +0000 AsmMan via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:for debugging purposes, I need to get the caller name. Is it=20 possible? if so, how do I do this?use debugger? or parse debugging info and do stackwalk.
Sep 25 2014
On Friday, 26 September 2014 at 05:59:33 UTC, AsmMan wrote:
for debugging purposes, I need to get the caller name. Is it
possible? if so, how do I do this?
void foo()
{
baa();
}
void baa()
{
wrilten(caller_name()); // foo
}
I know I could create an extra parameter and pass the current
function name as argument but it isn't a possibility for now.
Method 1 (extra template parameter):
////////////// method1.d /////////////
import std.stdio;
void foo()
{
baa();
}
void baa(string caller=__FUNCTION__)()
{
writeln(caller);
}
void main()
{
foo();
}
//////////////////////////////////////
Method 2 (slow, needs -g, also prints line number on Windows):
/////////////////////////// method2.d ///////////////////////////
import std.stdio;
void foo()
{
baa();
}
void baa()
{
writeln(getCaller());
}
void main()
{
foo();
}
string getCaller()
{
try
throw new Exception(null);
catch (Exception e)
{
import std.string, std.algorithm;
string[] lines = e.toString().splitLines()[1..$];
return lines.find!(line => line.canFind("getCaller"))[2];
}
}
/////////////////////////////////////////////////////////////////
Sep 25 2014









ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> 