rcollyer's method looks great but for a reason I have yet to determine it fails in Mathematica 7. Here is a variation that does not.
Block[{$MessagePrePrint},
$MessagePrePrint := Row@{#, " at i = ", i} ~ToString~ StandardForm &;
Do[i^0, {i, -1, 1}]
]
This may not be ideal however, as every field in the message gets this tag. For example, if you enter the spurious Inner[f, {{1, 2}}, {3}] you get:
Inner::incom: Length 2 at i = 1 of dimension 2 at i = 1 in {{1,2}} at i = 1 is incommensurate with length 1 at i = 1 of dimension 1 in {3} at i = 1. >>
Perhaps this is a more flexible and convenient form:
EDIT: Full rewrite to place the tag in the message itself
SetAttributes[withTaggedMsg, HoldAll]
withTaggedMsg[sym_] := Function[,
Internal`InheritedBlock[{MessagePacket},
Unprotect[MessagePacket];
MessagePacket[name__, BoxData[obj_, form_]] /; ! TrueQ[$tagMsg] :=
Block[{$tagMsg = True},
Identity@MessagePacket[name, BoxData[RowBox[{
ToBoxes @ Style[
Row[{"At iteration", HoldForm[sym], "=", sym, Spacer[5]}, " "],
Black],
obj}], form]]
];
#
], HoldAll]
Usage:
Do[i^0, {i, -1, 1}] // withTaggedMsg[i]
