I have a function running within a Do loop that sometimes issues a warning. I'd like to prepend the warning with the loop ctr so that I can go back and debug that instance later.
Basically, I would like to modify the following line,
Do[i^0, {i, -1, 1}]
so that instead of displaying the warning:
Power::indet: Indeterminate expression 0^0 encountered. >>
it displays:
i=0, Power::indet: Indeterminate expression 0^0 encountered. >>
Where i==0 is the iteration that i^0 issues the warning.
Thanks
Answer
Here is my proposal for tagging messages with (the value of) an arbitrary expression at the time of message generation. The tag is placed inside the the message itself.
ClearAll[withTaggedMsg]
SetAttributes[withTaggedMsg, HoldAll]
withTaggedMsg[exp_, label_: "When"] := Function[,
Internal`InheritedBlock[{MessagePacket},
Unprotect @ MessagePacket;
mp : MessagePacket[__, _BoxData] /; !TrueQ[$tagMsg] :=
Block[{$tagMsg = True},
Style[Row[{label, HoldForm[exp], "=", exp, " "}, " "], "SBO"] /. tag_ :>
MapAt[RowBox[{ToBoxes @ tag, #}] &, mp, {-1, 1}] // Identity
];
#
],
HoldAll]
Usage:
Do[i^0, {i, -1, 1}] // withTaggedMsg[i]
Do[i^0, {i, -1, 1}] // withTaggedMsg[i, "At iteration"]
Note: this only works with variables that are either globally accessible or are scoped using Block
. For example,
f[x_] := Message[f::brains, x]
f[5] // withTaggedMsg[x]
(* At iteration x = x f::brains: -- Message text not found -- (5) *)
Module[{x = 5},
Message[f::brains, x]
] // withTaggedMsg[x]
(* At iteration x = x f::brains: -- Message text not found -- (5) *)
With[{x = 5},
Message[f::brains, x]
] // withTaggedMsg[x]
(* At iteration x = x f::brains: -- Message text not found -- (5) *)
Block[{x = 5},
Message[f::brains, x]
] // withTaggedMsg[x]
(* At iteration x = 5 f::brains: -- Message text not found -- (5) *)
This means that any variable that is scoped using Block
can be used to tag a message. So, loop variables from Do
and Table
are accessible via this method, in addition to any Block
variable. This makes it indispensable as a debugging tool.
Comments
Post a Comment