March 15, 2005 Re: How to printf __FILE__ ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to AEon | On Tue, 15 Mar 2005 10:29:32 +0000 (UTC), AEon <AEon_member@pathlink.com> wrote: > Derek Parnell wrote: > >> For example, you can also write ... >> writefln("Line: ", __LINE__); >> or >> writefln("Line: %d", __LINE__); >> to get the same results. > > Indeed. Both work, you can even do this (as was pointed out, writefln is *very* > flexible), though the formatting would be less than optimal: > > // Line: test.d 96Mar 15 200511:17:12Tue Mar 15 11:17:12 2005 > writefln("Line: %d ", __FILE__,__LINE__,__DATE__,__TIME__,__TIMESTAMP__); > > // Line: test.d97Mar 15 200511:17:12Tue Mar 15 11:17:12 2005 > writefln("Line: ", __FILE__,__LINE__,__DATE__,__TIME__,__TIMESTAMP__); writefln accepts the same formatting as printf, eg. printf("Line: %s:%d %s %s %s\n",__FILE__,__LINE__,__DATE__,__TIME__,__TIMESTAMP__); writefln("Line: %s:%d %s %s %s",__FILE__,__LINE__,__DATE__,__TIME__,__TIMESTAMP__); should be identical. Or you could write it like: writefln("Line: ",__FILE__,":",__LINE__," ",__DATE__," ",__TIME__," ",__TIMESTAMP__); or even better (safer, no chance of stray %s causing problems) writefln("Line: %s",__FILE__,":%d",__LINE__," %s",__DATE__," %s",__TIME__," %s",__TIMESTAMP__); notice, writef<etc> can accept formatting strings anywhere. > I did a full search in the dmd\ development tree for "__TIMESTAMP__", other than > in the dmd, and dmd.exe files, the only source related mention of the latter is > in > > E:\d\dmd\src\dmd\idgen.c > > struct Msgtable > { > char *ident; // name to use in DMD source > char *name; // name in D executable > }; > > Msgtable msgtable[] = > { > { "LINE", "__LINE__" }, > { "FILE", "__FILE__" }, > { "DATE", "__DATE__" }, > { "TIME", "__TIME__" }, > { "TIMESTAMP", "__TIMESTAMP__" }, > .. > } > > Would there have been any way to look up what type "__LINE__" is for printf? I notice Anders has given an example, which should work, but appears to highlight a bug in the compiler. > I > had assumed that simply all those __ vars are strings (as we found out D Strings > to be exact)? I can see why you'd assume that. The only reason I knew __LINE__ was an 'int' is because it's an int in C/C++. > PS: Any chance to fix this forum to actually respect indentations, to make the reading of source samples easier to read? I suspect not, because it has been voiced before and nothing has happened. The best you can do is prefix every line with a character i.e. # void main() { # int i; ..etc.. Regan |
March 15, 2005 Re: How to printf __FILE__ ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Regan Heath | On Wed, 16 Mar 2005 09:02:11 +1300, Regan Heath wrote: [snip] >> I >> had assumed that simply all those __ vars are strings (as we found out D >> Strings >> to be exact)? > > I can see why you'd assume that. The only reason I knew __LINE__ was an 'int' is because it's an int in C/C++. Also it is documented http://www.digitalmars.com/d/lex.html#specialtokens -- Derek Parnell Melbourne, Australia 16/03/2005 7:12:42 AM |
March 15, 2005 Re: How to printf __FILE__ ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Anders F Björklund | >> import std.stdio; >> void main() >> { >> writefln("__FILE__ ", typeid(typeof(__FILE__))); >> writefln("__LINE__ ", typeid(typeof(__LINE__))); >> writefln("__DATE__ ", typeid(typeof(__DATE__))); >> writefln("__TIME__ ", typeid(typeof(__TIME__))); >> } > > *Should* print the types, but: > > __FILE__ TypeInfo > __LINE__ long > __DATE__ TypeInfo > __TIME__ TypeInfo > > It seems broken for char[]... ? Duh, forgot that string literals are of an unnamed type... They're converted into char[]/wchar[]/dchar[] from context. In this case, there was no context, so they renamed unnamed. > import std.stdio; > void main() > { > writefln("%s", typeid(typeof(""))); > writefln("%s", typeid(typeof(cast( char[]) ""))); > writefln("%s", typeid(typeof(cast(wchar[]) ""))); > writefln("%s", typeid(typeof(cast(dchar[]) ""))); > } TypeInfo char[] wchar[] dchar[] Not a bug. *However*, string literals in D should all be zero-terminated... These new ones are not, and that's a genuine implementation bug. --anders |
March 15, 2005 Re: How to printf __FILE__ ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Anders F Björklund | (continuing talking to myself, thanks for listening :-) )
> *However*, string literals in D should all be zero-terminated...
> These new ones are not, and that's a genuine implementation bug.
Never mind, they are all zero-terminated. (sorry, Walter!)
Just "forgot" that when passed as variadic arguments they
are dynamic arrays and not pointers as when passed to
the first argument of printf ("char*") for instance...
Thus:
printf(__FILE__); // works
and:
printf("%s\n", __FILE__); // segfaults
but:
printf("%.*s\n", __FILE__); // works
printf("%s\n", cast(char*) __FILE__); // works
But it's easier to use writef, which does the right thing.
Regular string literal and printf/writef handling, in D...
--anders
|
March 15, 2005 Re: How to printf __FILE__ ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Regan Heath | Regan Heath wrote... >writefln("Line: %s",__FILE__,":%d",__LINE__," %s",__DATE__," %s",__TIME__," %s",__TIMESTAMP__); > >notice, writef<etc> can accept formatting strings anywhere. Very interesting tip, I am starting to learn what amazing things you can *finally* do, but I would never have tried the above. >Also it is documented http://www.digitalmars.com/d/lex.html#specialtokens Sigh, I had read that but somehow I seem to have skipped the "__LINE__ integer literal" part every time. Thanx :) AEon |
Copyright © 1999-2021 by the D Language Foundation