Go equivalent to __FILE__ & __LINE__ ???

2,285 views
Skip to first unread message

Valentin Kasas

unread,
Sep 13, 2010, 12:33:04 PM9/13/10
to golang-nuts
Hi,

Is there, or will there be a way to know on which line of a source
file is written an arbitrary instruction, such as __FILE__ and
__LINE__ macros in C/C++ ?
I suppose that if there was such a way, the standard "log" package
would use it. I'm aware that Go does not and will certainly never have
a standard preprocessor, for the sake of compiling speed but, these
informations are already known by the compiler, as we can see
(hopefuly) when doing compilation errors.
I'm writing a /true/ logger inspired of the log4j I daily use, but
without thes informations, a logger is quite useless...


Regards

Vil-1

Valentin Kasas

unread,
Sep 13, 2010, 12:37:52 PM9/13/10
to golang-nuts
Damn !

Done things in the wrong order : ask my question and then search in
the groups ...
The answer is runtime.Caller() (which may be a little slower than
__FILE__ and __LINE__).

sorry

Cory Mainwaring

unread,
Sep 13, 2010, 1:16:59 PM9/13/10
to Valentin Kasas, golang-nuts
Slower: Probably
A Macro: No
Speedy Compile Time: Yes

Jessta

unread,
Sep 13, 2010, 6:02:51 PM9/13/10
to Valentin Kasas, golang-nuts
On Tue, Sep 14, 2010 at 2:33 AM, Valentin Kasas
<valenti...@gmail.com> wrote:
> Hi,
>
> Is there, or will there be a way to know on which line of a source
> file is written an arbitrary instruction, such as __FILE__ and
> __LINE__ macros in C/C++ ?

http://golang.org/pkg/runtime/#Caller

> I suppose that if there was such a way, the standard "log" package
> would use it. I'm aware that Go does not and will certainly never have
> a standard preprocessor, for the sake of compiling speed but, these
> informations are already known by the compiler, as we can see
> (hopefuly) when doing compilation errors.
> I'm writing a /true/ logger inspired of the log4j I daily use, but
> without thes informations, a logger is quite useless...

http://code.google.com/p/log4go/


- jessta
--
=====================
http://jessta.id.au

Rob 'Commander' Pike

unread,
Sep 13, 2010, 6:24:55 PM9/13/10
to Valentin Kasas, golang-nuts
The standard log package does use it. The predeclared functions in
log do not, but you can create new Loggers that will print source
information if you use the Llongfile or Lshortfile flags to New().

-rob

Reply all
Reply to author
Forward
0 new messages