After reading:
How to count the actual occurence of a warning message in a Do loop?
I wonder about a broader question: how to count the number of occurences of a warning messages in the current session ?
My first try was
counter[arg_] := Module[{}, count++; Return[arg]];
$MessagePrePrint = counter[#] &;
count = 0;
count
Do[1/0, {k, 1, 3}];
count
Obviously,
count
Quiet[Do[1/0, {k, 1, 3}]];
count
doesn't increment the counter.
Now, to keep a tally of all message and hide them, I experimented
counterQuiet[arg_] := Module[{}, count++; Return[Null]];
$MessagePrePrint = counterQuiet[#] &;
count = 0;
count
Do[1/0, {k, 1, 3}];
count
but it isn't a smart trick, because this way messages are still visible, albeit containing Null
instead of the original argument.
Can the goal be achieved without redirecting stderr
to a dummy stream ?
Otherwise, how to implement the redirection and still count messages?