caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] filename and line number.
@ 2011-08-02 12:20 Anders Fugmann
  2011-08-02 12:45 ` Gabriel Scherer
  0 siblings, 1 reply; 11+ messages in thread
From: Anders Fugmann @ 2011-08-02 12:20 UTC (permalink / raw)
  To: caml-list

Hi,

Do there exist a way to get filename and linenumber of the calling 
function - Or at least the of the current filename and line number?

I guess this would involve a syntax camlp4 syntax extension, but I'm not 
a camlp4 wizard and google did not come up with any suggestions.

Regards
Anders Fugmann


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Caml-list] filename and line number.
  2011-08-02 12:20 [Caml-list] filename and line number Anders Fugmann
@ 2011-08-02 12:45 ` Gabriel Scherer
  2011-08-02 21:21   ` Martin Jambon
  2011-08-03 19:57   ` Till Varoquaux
  0 siblings, 2 replies; 11+ messages in thread
From: Gabriel Scherer @ 2011-08-02 12:45 UTC (permalink / raw)
  To: Anders Fugmann; +Cc: caml-list

There is a "macro" syntax extension that is distributed with Camlp4,
and can do basic cpp-like stuff, including __FILE__ and a __LOCATION__
macros.

For example, the following content, named test.ml:

  let test =
    __LOCATION__

When processed through 'camlp4o pa_macro.cmo', will result in:

  let test = Loc.of_tuple ("test.ml", 2, 13, 17, 2, 13, 29, false)

(To compile:  ocamlc -pp 'camlp4o pa_macro.cmo' ...)

The "Loc.of_tuple" call is a reference to a function implemented in
Camlp4 Loc module; if you make you project depend (at runtime, not
camlp4-time) on Camlp4 loc-handling libraries, you'll get functions to
manipulate the location and its information. You can also define your
own Loc module in test.ml:

  module Loc = struct
    let of_tuple
      ((file_name, start_line, start_bol, start_off, stop_line,
stop_bol, stop_off, is_ghost) as loc) =
        loc
  end

  let test =
    __LOCATION__

The source code (and some documentation in the head comment) for the
"macro" camlp4 extension is in
camlp4/Camlp4Parsers/Camlp4MacroParser.ml in the ocaml source tree.

The meaning of the weird tuple arguments can be found in the Camlp4
documentation. I have a not exactly up-to-date (I guess ocaml 3.11)
version of the documentation on my website, see:
  http://bluestorm.info/camlp4//camlp4-doc/Sig.Loc.html

Finally, Martin Jambon also has its own "cppo" tools mimicking cpp,
which I suppose doesn't rely on camlp4, and has __FILE__ and __LINE__
macros which may be in a more directly exploitable format. I have
never tried it though. See:
  http://martin.jambon.free.fr/cppo.html

On Tue, Aug 2, 2011 at 2:20 PM, Anders Fugmann <anders@fugmann.net> wrote:
> Hi,
>
> Do there exist a way to get filename and linenumber of the calling function
> - Or at least the of the current filename and line number?
>
> I guess this would involve a syntax camlp4 syntax extension, but I'm not a
> camlp4 wizard and google did not come up with any suggestions.
>
> Regards
> Anders Fugmann
>
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa-roc.inria.fr/wws/info/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>
>


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Caml-list] filename and line number.
  2011-08-02 12:45 ` Gabriel Scherer
@ 2011-08-02 21:21   ` Martin Jambon
  2011-08-03  6:04     ` Anders Fugmann
  2011-08-03 19:57   ` Till Varoquaux
  1 sibling, 1 reply; 11+ messages in thread
From: Martin Jambon @ 2011-08-02 21:21 UTC (permalink / raw)
  To: caml-list

On 08/02/11 05:45, Gabriel Scherer wrote:
> Finally, Martin Jambon also has its own "cppo" tools mimicking cpp,
> which I suppose doesn't rely on camlp4, and has __FILE__ and __LINE__
> macros which may be in a more directly exploitable format. I have
> never tried it though. See:
>   http://martin.jambon.free.fr/cppo.html

That's correct. Thanks for the plug.

Here is an example:

$ cat loc.ml
#define loc (Printf.sprintf "File %S, line %i" __FILE__ __LINE__)

print_endline loc;;
print_endline loc;;

$ ocamlopt -o loc -pp cppo loc.ml; ./loc
File "loc.ml", line 3
File "loc.ml", line 4



Martin

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Caml-list] filename and line number.
  2011-08-02 21:21   ` Martin Jambon
@ 2011-08-03  6:04     ` Anders Fugmann
  2011-08-03  7:10       ` Gabriel Scherer
  0 siblings, 1 reply; 11+ messages in thread
From: Anders Fugmann @ 2011-08-03  6:04 UTC (permalink / raw)
  To: Martin Jambon; +Cc: caml-list

Thanks for the replies,

Using cppo, would I need to define 'loc' in each file using log, or can 
it be defined in another module?

If not, I guess the alternative is to create a syntax extenstion that 
will include Loc.t structure as a parameter to some print function - or 
add it manually, but I would like to avoid cluttering the code with 
__LOCATION__ everywhere.

Regards
Anders Fugmann



On 08/02/2011 11:21 PM, Martin Jambon wrote:
> On 08/02/11 05:45, Gabriel Scherer wrote:
>> Finally, Martin Jambon also has its own "cppo" tools mimicking cpp,
>> which I suppose doesn't rely on camlp4, and has __FILE__ and __LINE__
>> macros which may be in a more directly exploitable format. I have
>> never tried it though. See:
>>    http://martin.jambon.free.fr/cppo.html
>
> That's correct. Thanks for the plug.
>
> Here is an example:
>
> $ cat loc.ml
> #define loc (Printf.sprintf "File %S, line %i" __FILE__ __LINE__)
>
> print_endline loc;;
> print_endline loc;;
>
> $ ocamlopt -o loc -pp cppo loc.ml; ./loc
> File "loc.ml", line 3
> File "loc.ml", line 4
>
>
>
> Martin
>


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Caml-list] filename and line number.
  2011-08-03  6:04     ` Anders Fugmann
@ 2011-08-03  7:10       ` Gabriel Scherer
  2011-08-04  7:58         ` Anders Fugmann
  0 siblings, 1 reply; 11+ messages in thread
From: Gabriel Scherer @ 2011-08-03  7:10 UTC (permalink / raw)
  To: Anders Fugmann; +Cc: Martin Jambon, caml-list

Both pa_macros and cppo have a file-inclusion directive, so you could
define your preferred location-reporting macro in a header file.
That said, I would personally use the __LOCATION__ macro directly: the
less preprocessing stuff the better, and if it "clutters your code"
you could restrict your reporting a bit, which may not be a bad thing:
having a log full of useless stuff is only a bit better than having no
log.

You may also be interested in the existing logging frameworks for
OCaml, such as Bolt:
  http://bolt.x9c.fr/

On Wed, Aug 3, 2011 at 8:04 AM, Anders Fugmann <anders@fugmann.net> wrote:
> Thanks for the replies,
>
> Using cppo, would I need to define 'loc' in each file using log, or can it
> be defined in another module?
>
> If not, I guess the alternative is to create a syntax extenstion that will
> include Loc.t structure as a parameter to some print function - or add it
> manually, but I would like to avoid cluttering the code with __LOCATION__
> everywhere.
>
> Regards
> Anders Fugmann
>
>
>
> On 08/02/2011 11:21 PM, Martin Jambon wrote:
>>
>> On 08/02/11 05:45, Gabriel Scherer wrote:
>>>
>>> Finally, Martin Jambon also has its own "cppo" tools mimicking cpp,
>>> which I suppose doesn't rely on camlp4, and has __FILE__ and __LINE__
>>> macros which may be in a more directly exploitable format. I have
>>> never tried it though. See:
>>>   http://martin.jambon.free.fr/cppo.html
>>
>> That's correct. Thanks for the plug.
>>
>> Here is an example:
>>
>> $ cat loc.ml
>> #define loc (Printf.sprintf "File %S, line %i" __FILE__ __LINE__)
>>
>> print_endline loc;;
>> print_endline loc;;
>>
>> $ ocamlopt -o loc -pp cppo loc.ml; ./loc
>> File "loc.ml", line 3
>> File "loc.ml", line 4
>>
>>
>>
>> Martin
>>
>
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa-roc.inria.fr/wws/info/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>
>


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Caml-list] filename and line number.
  2011-08-02 12:45 ` Gabriel Scherer
  2011-08-02 21:21   ` Martin Jambon
@ 2011-08-03 19:57   ` Till Varoquaux
  2011-08-03 20:52     ` Gabriel Scherer
  1 sibling, 1 reply; 11+ messages in thread
From: Till Varoquaux @ 2011-08-03 19:57 UTC (permalink / raw)
  To: Gabriel Scherer; +Cc: caml-list

On Tue, Aug 2, 2011 at 8:45 AM, Gabriel Scherer
<gabriel.scherer@gmail.com> wrote:
> There is a "macro" syntax extension that is distributed with Camlp4,
> and can do basic cpp-like stuff, including __FILE__ and a __LOCATION__
> macros.
>
> For example, the following content, named test.ml:
>
>  let test =
>    __LOCATION__
>
> When processed through 'camlp4o pa_macro.cmo', will result in:
>
>  let test = Loc.of_tuple ("test.ml", 2, 13, 17, 2, 13, 29, false)
>
> (To compile:  ocamlc -pp 'camlp4o pa_macro.cmo' ...)
>
> The "Loc.of_tuple" call is a reference to a function implemented in
> Camlp4 Loc module; if you make you project depend (at runtime, not
> camlp4-time) on Camlp4 loc-handling libraries, you'll get functions to
> manipulate the location and its information. You can also define your
> own Loc module in test.ml:
>
>  module Loc = struct
>    let of_tuple
>      ((file_name, start_line, start_bol, start_off, stop_line,
> stop_bol, stop_off, is_ghost) as loc) =
>        loc
>  end
>
>  let test =
>    __LOCATION__
>
> The source code (and some documentation in the head comment) for the
> "macro" camlp4 extension is in
> camlp4/Camlp4Parsers/Camlp4MacroParser.ml in the ocaml source tree.
>
> The meaning of the weird tuple arguments can be found in the Camlp4
> documentation. I have a not exactly up-to-date (I guess ocaml 3.11)
> version of the documentation on my website, see:
>  http://bluestorm.info/camlp4//camlp4-doc/Sig.Loc.html

Having camlp4's ocamldoc is pretty nice. How did you generate those?
Is there a makefile target?

Till
>
> Finally, Martin Jambon also has its own "cppo" tools mimicking cpp,
> which I suppose doesn't rely on camlp4, and has __FILE__ and __LINE__
> macros which may be in a more directly exploitable format. I have
> never tried it though. See:
>  http://martin.jambon.free.fr/cppo.html
>
> On Tue, Aug 2, 2011 at 2:20 PM, Anders Fugmann <anders@fugmann.net> wrote:
>> Hi,
>>
>> Do there exist a way to get filename and linenumber of the calling function
>> - Or at least the of the current filename and line number?
>>
>> I guess this would involve a syntax camlp4 syntax extension, but I'm not a
>> camlp4 wizard and google did not come up with any suggestions.
>>
>> Regards
>> Anders Fugmann
>>
>>
>> --
>> Caml-list mailing list.  Subscription management and archives:
>> https://sympa-roc.inria.fr/wws/info/caml-list
>> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
>> Bug reports: http://caml.inria.fr/bin/caml-bugs
>>
>>
>
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa-roc.inria.fr/wws/info/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>
>


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Caml-list] filename and line number.
  2011-08-03 19:57   ` Till Varoquaux
@ 2011-08-03 20:52     ` Gabriel Scherer
  0 siblings, 0 replies; 11+ messages in thread
From: Gabriel Scherer @ 2011-08-03 20:52 UTC (permalink / raw)
  To: Till Varoquaux; +Cc: caml-list

I called ocamldoc by hand, the signatures I was interested in are
actually all contained in camlp4/Camlp4/Sig.ml so there is not much to
do.

On Wed, Aug 3, 2011 at 9:57 PM, Till Varoquaux <till@pps.jussieu.fr> wrote:
> On Tue, Aug 2, 2011 at 8:45 AM, Gabriel Scherer
> <gabriel.scherer@gmail.com> wrote:
>> There is a "macro" syntax extension that is distributed with Camlp4,
>> and can do basic cpp-like stuff, including __FILE__ and a __LOCATION__
>> macros.
>>
>> For example, the following content, named test.ml:
>>
>>  let test =
>>    __LOCATION__
>>
>> When processed through 'camlp4o pa_macro.cmo', will result in:
>>
>>  let test = Loc.of_tuple ("test.ml", 2, 13, 17, 2, 13, 29, false)
>>
>> (To compile:  ocamlc -pp 'camlp4o pa_macro.cmo' ...)
>>
>> The "Loc.of_tuple" call is a reference to a function implemented in
>> Camlp4 Loc module; if you make you project depend (at runtime, not
>> camlp4-time) on Camlp4 loc-handling libraries, you'll get functions to
>> manipulate the location and its information. You can also define your
>> own Loc module in test.ml:
>>
>>  module Loc = struct
>>    let of_tuple
>>      ((file_name, start_line, start_bol, start_off, stop_line,
>> stop_bol, stop_off, is_ghost) as loc) =
>>        loc
>>  end
>>
>>  let test =
>>    __LOCATION__
>>
>> The source code (and some documentation in the head comment) for the
>> "macro" camlp4 extension is in
>> camlp4/Camlp4Parsers/Camlp4MacroParser.ml in the ocaml source tree.
>>
>> The meaning of the weird tuple arguments can be found in the Camlp4
>> documentation. I have a not exactly up-to-date (I guess ocaml 3.11)
>> version of the documentation on my website, see:
>>  http://bluestorm.info/camlp4//camlp4-doc/Sig.Loc.html
>
> Having camlp4's ocamldoc is pretty nice. How did you generate those?
> Is there a makefile target?
>
> Till
>>
>> Finally, Martin Jambon also has its own "cppo" tools mimicking cpp,
>> which I suppose doesn't rely on camlp4, and has __FILE__ and __LINE__
>> macros which may be in a more directly exploitable format. I have
>> never tried it though. See:
>>  http://martin.jambon.free.fr/cppo.html
>>
>> On Tue, Aug 2, 2011 at 2:20 PM, Anders Fugmann <anders@fugmann.net> wrote:
>>> Hi,
>>>
>>> Do there exist a way to get filename and linenumber of the calling function
>>> - Or at least the of the current filename and line number?
>>>
>>> I guess this would involve a syntax camlp4 syntax extension, but I'm not a
>>> camlp4 wizard and google did not come up with any suggestions.
>>>
>>> Regards
>>> Anders Fugmann
>>>
>>>
>>> --
>>> Caml-list mailing list.  Subscription management and archives:
>>> https://sympa-roc.inria.fr/wws/info/caml-list
>>> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
>>> Bug reports: http://caml.inria.fr/bin/caml-bugs
>>>
>>>
>>
>>
>> --
>> Caml-list mailing list.  Subscription management and archives:
>> https://sympa-roc.inria.fr/wws/info/caml-list
>> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
>> Bug reports: http://caml.inria.fr/bin/caml-bugs
>>
>>
>


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Caml-list] filename and line number.
  2011-08-03  7:10       ` Gabriel Scherer
@ 2011-08-04  7:58         ` Anders Fugmann
  2011-08-04 13:03           ` forum
  0 siblings, 1 reply; 11+ messages in thread
From: Anders Fugmann @ 2011-08-04  7:58 UTC (permalink / raw)
  To: Gabriel Scherer; +Cc: Martin Jambon, caml-list

On 08/03/2011 09:10 AM, Gabriel Scherer wrote:
> Both pa_macros and cppo have a file-inclusion directive, so you could
> define your preferred location-reporting macro in a header file.
> That said, I would personally use the __LOCATION__ macro directly: the
> less preprocessing stuff the better, and if it "clutters your code"
> you could restrict your reporting a bit, which may not be a bad thing:
> having a log full of useless stuff is only a bit better than having no
> log.
I fully agree that too much log information makes to log pretty useless.
But I was not allowed to use __LOCATION__ all over the place, so I wrote 
a small syntax extension that will automatically call our log function 
with location as argument.

>
> You may also be interested in the existing logging frameworks for
> OCaml, such as Bolt:
>    http://bolt.x9c.fr/
We actually did try it out, but we could not get it to do log rotation 
properly. We ended up logging to stdout and pipe stdout through a 
program that would send all data to disk, and reopen files whenever it 
received HUP sigal. logrotate would then do the actual log rotation.

Thanks for all the suggestions and pointers. Very helpful indeed.
Regards
Anders




^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Caml-list] filename and line number.
  2011-08-04  7:58         ` Anders Fugmann
@ 2011-08-04 13:03           ` forum
  2011-08-08 19:08             ` Anders Peter Fugmann
  0 siblings, 1 reply; 11+ messages in thread
From: forum @ 2011-08-04 13:03 UTC (permalink / raw)
  To: caml-list; +Cc: Xavier Clerc


Le 4 août 2011 à 09:58, Anders Fugmann a écrit :

> On 08/03/2011 09:10 AM, Gabriel Scherer wrote:

(…)

>> You may also be interested in the existing logging frameworks for
>> OCaml, such as Bolt:
>>   http://bolt.x9c.fr/
> We actually did try it out, but we could not get it to do log rotation properly. We ended up logging to stdout and pipe stdout through a program that would send all data to disk, and reopen files whenever it received HUP sigal. logrotate would then do the actual log rotation.

Could you elaborate on what was missing, or buggy in Bolt?
As long as it fits with its design, I may try to fix it to suit your needs.


Regards,

Xavier Clerc



^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Caml-list] filename and line number.
  2011-08-04 13:03           ` forum
@ 2011-08-08 19:08             ` Anders Peter Fugmann
  2011-08-09 16:31               ` forum
  0 siblings, 1 reply; 11+ messages in thread
From: Anders Peter Fugmann @ 2011-08-08 19:08 UTC (permalink / raw)
  To: forum; +Cc: caml-list

Hi Xavier,

On 2011-08-04 15:03, forum@x9c.fr wrote:
> Could you elaborate on what was missing, or buggy in Bolt?
> As long as it fits with its design, I may try to fix it to suit your needs.

When we tried bolt, the main issue was log rotation. We wanted to make 
sure that logs were rotated so they would not fill up all disk space.

Bolt does indeed do log rotation, but the files created files by bold 
includes either timestamp or pid, which changes between program 
invocation. This makes to harder to see which file is the current, and 
leaves a lot of log files during development.

We therefore decided to send all log to stdout, and capture it though a 
simple program that would reopen the file when it received SIGHUP.
(Logging to stdout is also helpful when developing).

As we no longer used Bolt for log rotation, we decided to leave bolt, 
keeping external dependencies down.

If bolt had the ability to reopen the log file on SIGHUB (or a function 
to reopen the log file), we would probably still be using it. That 
combined with the ability to log to stdout when developing would make it 
very useful.

Regards
Anders Fugmann

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Caml-list] filename and line number.
  2011-08-08 19:08             ` Anders Peter Fugmann
@ 2011-08-09 16:31               ` forum
  0 siblings, 0 replies; 11+ messages in thread
From: forum @ 2011-08-09 16:31 UTC (permalink / raw)
  To: caml-list; +Cc: Xavier Clerc


Le 8 août 2011 à 21:08, Anders Peter Fugmann a écrit :

> Hi Xavier,
> 
> On 2011-08-04 15:03, forum@x9c.fr wrote:
>> Could you elaborate on what was missing, or buggy in Bolt?
>> As long as it fits with its design, I may try to fix it to suit your needs.
> 
> When we tried bolt, the main issue was log rotation. We wanted to make sure that logs were rotated so they would not fill up all disk space.
> 
> Bolt does indeed do log rotation, but the files created files by bold includes either timestamp or pid, which changes between program invocation. This makes to harder to see which file is the current, and leaves a lot of log files during development.

The ability to put either the timestamp or the pid is just a possibility;
you can as well just use a "constant" file name. In that latter case, the
file will be replaced at each rotation.


> We therefore decided to send all log to stdout, and capture it though a simple program that would reopen the file when it received SIGHUP.

The feature of rotating not on time information but on the reception
of a signal seems quite easy to add.


> (Logging to stdout is also helpful when developing).

My bad, the documentation is lacking the following information:
if you use "<stdout>" (without the quotes) as the filename, then
the data will be written on standard output.


> As we no longer used Bolt for log rotation, we decided to leave bolt, keeping external dependencies down.
> 
> If bolt had the ability to reopen the log file on SIGHUB (or a function to reopen the log file), we would probably still be using it. That combined with the ability to log to stdout when developing would make it very useful.

As I have other incentives to publish an updated version of Bolt,
I will try to release a version featuring that signal thing "soon".


Regards,

Xavier Clerc



^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2011-08-09 16:31 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-08-02 12:20 [Caml-list] filename and line number Anders Fugmann
2011-08-02 12:45 ` Gabriel Scherer
2011-08-02 21:21   ` Martin Jambon
2011-08-03  6:04     ` Anders Fugmann
2011-08-03  7:10       ` Gabriel Scherer
2011-08-04  7:58         ` Anders Fugmann
2011-08-04 13:03           ` forum
2011-08-08 19:08             ` Anders Peter Fugmann
2011-08-09 16:31               ` forum
2011-08-03 19:57   ` Till Varoquaux
2011-08-03 20:52     ` Gabriel Scherer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).