Jump to page: 1 2
Thread overview
__FUNC__ in D
Aug 14, 2007
argl
Aug 14, 2007
BCS
Aug 15, 2007
argl
Aug 15, 2007
argl
Aug 15, 2007
BCS
Aug 15, 2007
argl
Aug 15, 2007
Regan Heath
Aug 15, 2007
Regan Heath
Aug 15, 2007
BCS
Aug 15, 2007
Regan Heath
Aug 16, 2007
argl
Sep 18, 2012
Rob T
August 14, 2007
Hi, I know this has come aup a few times in the past, but I have not come across a clear answer.

Is it possible in any way to get the name of the current function for logging purposes? Something to get the functionality of __FUNC__ in C?

August 14, 2007
"argl" <cavenhaus@gmail.com> wrote in message news:f9qrup$nqe$1@digitalmars.com...
> Hi, I know this has come aup a few times in the past, but I have not come across a clear answer.
>
> Is it possible in any way to get the name of the current function for logging purposes? Something to get the functionality of __FUNC__ in C?
>

No.


August 14, 2007
Reply to Jarrett,

> "argl" <cavenhaus@gmail.com> wrote in message
> news:f9qrup$nqe$1@digitalmars.com...
> 
>> Hi, I know this has come aup a few times in the past, but I have not
>> come across a clear answer.
>> 
>> Is it possible in any way to get the name of the current function for
>> logging purposes? Something to get the functionality of __FUNC__ in
>> C?
>> 
> No.
> 

CTFE anyone?

char[] GetLastFnDef(char[] file, int line);

use like this:

const char[] fn = GetLastFnDef(import(__FILE__), __LINE__);


August 15, 2007
That would be wonderful!
I am getting the following errors though.
Where do I find GetLastFnDef() ?

dmd -Jpath -run ./test.d
test.d(80): Error: undefined identifier GetLastFnDef
test.d(80): Error: need -Jpath switch to import text file test.d
test.d(80): Error: function expected before (), not GetLastFnDef of type int
test.d(80): Error: cannot implicitly convert expression (GetLastFnDef("",80L)) of type int to char[]


BCS Wrote:

> Reply to Jarrett,
> 
> > "argl" <cavenhaus@gmail.com> wrote in message news:f9qrup$nqe$1@digitalmars.com...
> > 
> >> Hi, I know this has come aup a few times in the past, but I have not come across a clear answer.
> >> 
> >> Is it possible in any way to get the name of the current function for logging purposes? Something to get the functionality of __FUNC__ in C?
> >> 
> > No.
> > 
> 
> CTFE anyone?
> 
> char[] GetLastFnDef(char[] file, int line);
> 
> use like this:
> 
> const char[] fn = GetLastFnDef(import(__FILE__), __LINE__);
> 
> 

August 15, 2007
Sorry, stupid me forgot the declaration. I am still getting an error though.

dmd -J/tmp test3.d
test3.d(7): Error: cannot evaluate GetLastFnDef("import tango.io.Stdout;\x0a\x0achar[] GetLastFnDef(char[] file, int line);\x0a\x0avoid main()\x0a{\x0a    const char[] fn = GetLastFnDef(import(__FILE__), __LINE__);\x0a    Stdout (\"hello {}\", fn).newline;\x0a}\x0a\x0a\x0a\x0a\x0a",7) at compile time

Any help would be appreciated.

argl Wrote:

> That would be wonderful!
> I am getting the following errors though.
> Where do I find GetLastFnDef() ?
> 
> dmd -Jpath -run ./test.d
> test.d(80): Error: undefined identifier GetLastFnDef
> test.d(80): Error: need -Jpath switch to import text file test.d
> test.d(80): Error: function expected before (), not GetLastFnDef of type int
> test.d(80): Error: cannot implicitly convert expression (GetLastFnDef("",80L)) of type int to char[]
> 
> 
> BCS Wrote:
> 
> > Reply to Jarrett,
> > 
> > > "argl" <cavenhaus@gmail.com> wrote in message news:f9qrup$nqe$1@digitalmars.com...
> > > 
> > >> Hi, I know this has come aup a few times in the past, but I have not come across a clear answer.
> > >> 
> > >> Is it possible in any way to get the name of the current function for logging purposes? Something to get the functionality of __FUNC__ in C?
> > >> 
> > > No.
> > > 
> > 
> > CTFE anyone?
> > 
> > char[] GetLastFnDef(char[] file, int line);
> > 
> > use like this:
> > 
> > const char[] fn = GetLastFnDef(import(__FILE__), __LINE__);
> > 
> > 
> 

August 15, 2007
argl wrote:
> Sorry, stupid me forgot the declaration. I am still getting an error though.
> 
> dmd -J/tmp test3.d
> test3.d(7): Error: cannot evaluate GetLastFnDef("import tango.io.Stdout;\x0a\x0achar[] GetLastFnDef(char[] file, int line);\x0a\x0avoid main()\x0a{\x0a    const char[] fn = GetLastFnDef(import(__FILE__), __LINE__);\x0a    Stdout (\"hello {}\", fn).newline;\x0a}\x0a\x0a\x0a\x0a\x0a",7) at compile time
> 
> Any help would be appreciated.
> 
> argl Wrote:
> 

It's a hypothetical function that doesn't exist (yet). However, it's shouldn't be two hard to wright one that would work for most case.
August 15, 2007
BCS Wrote:
> 
> It's a hypothetical function that doesn't exist (yet). However, it's shouldn't be two hard to wright one that would work for most case.

I see. Thank you.
August 15, 2007
argl wrote:
> BCS Wrote:
>> It's a hypothetical function that doesn't exist (yet). However, it's shouldn't be two hard to wright one that would work for most case.
> 
> I see. Thank you.

Quick and dirty!

import std.stdio, std.string, std.ctype;

alias std.string.tolower tolower;

void main()
{
	foo();
	bar();
	baz();
}

void foo()
{
	string fn = FnName(import(__FILE__), __LINE__);
	writefln(fn);
}

void bar ()
{
	string fn = FnName(import(__FILE__), __LINE__);
	writefln(fn);
}

const(char)baz ()
{
	string fn = FnName(import(__FILE__), __LINE__);
	writefln(fn);
	return cast(const(char))'c';
}

string FnName(string file, uint line)
{
	string name;
	string last;
	int    level;
	int    start;
	int    end;
	
	foreach(nLine, s; splitlines(file))
	{
		foreach(ref p; s.split())
		{
			if (p.contains(cast(const(char))'{')) level++;
			if (p.contains(cast(const(char))'}')) level--;
			if (level > 0) continue;
			if (p.contains(cast(const(char))'#')) continue;
			if (isKeyword(p)) continue;
			if (p.contains(cast(const(char))'('))
			{
				end = p.find('(');
				if (end == 0)
				{
					p = last;
					end = p.length;
				}
				
				for(start = end-1; start > 0; start--)
				{
					if (!isalnum(p[start]))
					{
						start++;
						break;
					}
				}
				
				name = p[start..end].dup;
			}
			last = p;
		}
		
		if (nLine > line) break;
	}
	
	return name;
}

bool isKeyword(string word)
{
	return keywords.contains(tolower(word));
}

bool contains(T)(T[] arr, T val)
{
	foreach(i; arr) if (i == val) return true;
	return false;
}

string[] keywords = [
	"abstract",
	"alias",
	"align",
	"asm",
	"assert",
	"auto",
	"body",
	"bool",
	"break",
	"byte",
	"case",
	"cast",
	"catch",
	"cdouble",
	"cent",
	"cfloat",
	"char",
	"class",
	"const",
	"continue",
	"creal",
	"dchar",
	"debug",
	"default",
	"delegate",
	"delete",
	"deprecated",
	"do",
	"double",
	"else",
	"enum",
	"export",
	"extern",
	"false",
	"final",
	"finally",
	"float",
	"for",
	"foreach",
	"foreach_reverse",
	"function",
	"goto",
	"idouble",
	"if",
	"ifloat",
	"import",
	"in",
	"inout",
	"int",
	"interface",
	"invariant",
	"ireal",
	"is",
	"lazy",
	"long",
	"macro",
	"mixin",
	"module",
	"new",
	"null",
	"out",
	"override",
	"package",
	"pragma",
	"private",
	"protected",
	"public",
	"real",
	"ref",
	"return",
	"scope",
	"short",
	"static",
	"struct",
	"super",
	"switch",
	"synchronized",
	"template",
	"this",
	"throw",
	"__traits",
	"true",
	"try",
	"typedef",
	"typeid",
	"typeof",
	"ubyte",
	"ucent",
	"uint",
	"ulong",
	"union",
	"unittest",
	"ushort",
	"version",
	"void",
	"volatile",
	"wchar",
	"while",
	"with",
	"__FILE__",
	"__LINE__",
	"__DATE__",
	"__TIME__",
	"__TIMESTAMP__",
	"__VENDOR__",
	"__VERSION__"
];
August 15, 2007
On 2nd thought I think this only works by some sheer coincidence and fluke!

A better one could definately be written with the DMDFE.

Regan
August 15, 2007
Regan Heath wrote:
> On 2nd thought I think this only works by some sheer coincidence and fluke!

The whole idea is a hack. Hacks are only /suposed/ to work when the user does things exactly right

> 
> A better one could definately be written with the DMDFE.

yes but do you want to port DMDFE to run using CTFE?

> 
> Regan
« First   ‹ Prev
1 2