caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Damien <Damien.Pous@ens-lyon.fr>
To: caml-list@inria.fr
Subject: Re: [Caml-list] Printf question
Date: Mon, 19 May 2003 11:39:07 +0200	[thread overview]
Message-ID: <20030519113907.07074d4a.Damien.Pous@ens-lyon.fr> (raw)
In-Reply-To: <16071.8904.507961.569277@hector.lesours>

[-- Attachment #1: Type: text/plain, Size: 994 bytes --]

> You might have a look into the tracing facilities I developed for
> Poesia. These are tracing macros for camlp4.
looks really fine ! but also really complicated...

I personally use the less powerful attached code :

advantages are :
	* you don't need to recompile the code each time you change debug
	options
	* it can _easily_ be adapted to the newbie needs 
	* it doesn't need camlp4 (is this an actual advantage !?)

disadvantages are :
	* you can't automagically display the location in the source code 
	* you need to remove debug statements by hand when compiling efficient
	binaries
	* Basile will certainly find more :-)

note : for the first disadvantage, why not to modify the assert function
in order it also prints the execution stack. 
I don't know if this is possible in both byte and native code, but I
guess it should be when debugging informations are saved (ocamlc -g).
from my mind, this would be really useful.

hope this help,
damien

--
<http://www.ens-lyon.fr/~dpous>

[-- Attachment #2: Makefile --]
[-- Type: application/octet-stream, Size: 162 bytes --]

toto: msg.cmo toto.ml
	ocamlc msg.cmo toto.ml -o toto

msg.cmo: msg.cmi msg.ml
	ocamlc -c msg.ml

msg.cmi: msg.mli
	ocamlc -c msg.mli

clean: 	
	rm -f toto *.cm?

[-- Attachment #3: msg.ml --]
[-- Type: application/octet-stream, Size: 785 bytes --]

(* warning classes  *)
type warning_t = [ 
| `Foo
| `Bar 
| `Arf
]

let map s = 
  match String.uppercase s with
    | "FOO" -> `Foo
    | "BAR" -> `Bar
    | "ARF" -> `Arf
    | _     -> failwith ("Unknown warning : "^s)

(* warnings handled as errors *)
let err_l = ref []
let as_error w = err_l := (map w) :: !err_l

(* ignored warnings *)
let warn_l = ref []
let add_warning w = warn_l := (map w) :: !warn_l


let error () = Printf.kprintf (fun s -> prerr_endline s; exit 1)
let warning w = Obj.magic (
  if List.mem w !err_l then 
    error ()
  else if List.mem w !warn_l then
    Printf.kprintf (fun s -> prerr_endline s; "")
  else
    Printf.kprintf (fun _ -> ""))
(* Obj.magic is used to cast a string to unit and avoid "this expression should have type unit" warnings... *)

[-- Attachment #4: msg.mli --]
[-- Type: application/octet-stream, Size: 334 bytes --]

(* warning classes  *)
type warning_t = [ 
| `Foo
| `Bar 
| `Arf
]

(* warnings to activate or to handle as errors *)
val as_error    : string -> unit
val add_warning : string -> unit

(* warnings / errors *)
val warning : warning_t -> ('a, out_channel, unit) format -> 'a 
val error   :      unit -> ('a, unit, string) format -> 'a


[-- Attachment #5: toto.ml --]
[-- Type: application/octet-stream, Size: 390 bytes --]

let _ = 
  Arg.parse 
    [ "-W", Arg.String Msg.add_warning, " <w>\tenable warnings of kind w"; 
      "-E", Arg.String Msg.as_error,    " <w>\thandle warnings of kind w as errors" ]
    (fun _ -> raise (Arg.Bad "too many arguments"))
    "toto [options]";
  
  Msg.warning `Foo "foo=%d" 51;
  Msg.warning `Bar "bar%s" "jo";
  Msg.warning `Arf "glou: %f" 3.3;
  Msg.error() "boom !"
    


[-- Attachment #6: EXAMPLE --]
[-- Type: application/octet-stream, Size: 124 bytes --]

>./toto
boom !

>./toto -W FOO
foo=51
boom !

>./toto -W FOO -W BAR
foo=51
barjo
boom !

>./toto -W FOO -E BAR
foo=51
barjo

  reply	other threads:[~2003-05-19  9:41 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-05-18  1:34 Brian Hurt
2003-05-18  3:23 ` Manos Renieris
2003-05-18  3:32 ` William Lovas
2003-05-18  6:06 ` Basile STARYNKEVITCH
2003-05-19  9:39   ` Damien [this message]
2003-09-26 18:02 Richard Jones
2003-09-26 19:04 ` Alain.Frisch
2003-09-29  7:44   ` Mike Potanin
2003-09-27  0:11 ` Olivier Andrieu
2003-09-27  7:23   ` Richard Jones
2003-09-27  8:20     ` Basile Starynkevitch
2003-09-27  9:14       ` Richard Jones
2003-09-27  9:39         ` Maxence Guesdon
2003-09-29 16:42         ` Pierre Weis
2003-09-29 18:13           ` Richard Jones
2003-09-29 19:57             ` Pierre Weis
2003-09-29 21:50               ` Richard Jones
2003-09-29 22:36                 ` Pierre Weis
2003-09-30  8:03                   ` Richard Jones
2003-09-30  8:45                     ` Pierre Weis
2003-09-30  9:17                       ` Michal Moskal
2003-09-30 14:14                         ` Christophe TROESTLER
2003-09-30 13:19                   ` skaller
2003-09-30 20:52                     ` Pierre Weis
2003-10-01 14:39                       ` Christophe TROESTLER
2003-10-01 14:57                         ` Richard Jones
2003-10-01 16:21                         ` Florian Hars

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20030519113907.07074d4a.Damien.Pous@ens-lyon.fr \
    --to=damien.pous@ens-lyon.fr \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).