The MessageControl Package
By Ted Ersek
      ted.ersek@navy.navy.mil

Sometimes I define a function and I want certain messages Off during evaluation of the function. Then when the function is finished evaluating I want the message back On, but if the message was Off before my function evaluated the message should remain Off.  Mathematica provides no straight forward way to do this. The functions in this package solve this problem. The code for the package is short and elegant, but not included in this notebook. You can open the file (MessageControl.m) and review the code if you are interested.

Installing and Loading the Package

To determine where to put the (MessageControl.m) file evaluate the next cell.  If this directory doesn't already have a sub-directory called Ersek add one now.  The (MessageControl.m) file should then be copied to this Ersek directory.

\(If[$VersionNumber < 
        5, 
ToFileName[{$TopDirectory, "AddOns", "ExtraPackages"}], 
ToFileName[{$BaseDirectory, "AddOns", "ExtraPackages"}]
]\)

Once the (MessageControl.m) file is copied to the right folder the package can be loaded by evaluating the Needs statement in the next cell.

Needs["Ersek`MessageControl`"]

The package implements three new functions. MessageSuppress, TurnOn, and TurnOff.  Each of the functions are demonstrated below.

SuppressMessage Example

The SuppressMessage usage message is shown below.

?SuppressMessage

The next cell defines a function that compares two arguments, and will never display the 
($MaxExtraPrecision::meprec) message or (N::meprec) message when comparing two values that can be shown to be equal.

EqualsQ[x_?NumericQ, 
          y_?NumericQ] := 
SuppressMessage[{$MaxExtraPrecision::meprec, 
            N::meprec}, FullSimplify[x == y]];

EqualsQ[x_, y_] := Equal[x, y]

EqualsQ[2 + 2  π, 2 (1 + π)]

This notebook was evaluated using Mathematica 4.0 under Windows 2000 and in that case evaluating the next cell results in a ($MaxExtraPrecision::meprec) message. If using certain later versions the next cell results in a (N::meprec) message.

2 + 2  π == 2 (1 + π)

In the example above the ($MaxExtraPrecision::meprec) message was turned back On after EqualsQ finished evaluating. Next we evaluate Off[$MaxExtraPrecision::meprec] before evaluating EqualsQ, and the message is still Off after EqualsQ is finished.

Off[$MaxExtraPrecision::meprec];
EqualsQ[3 + 3  π, 3 (1 + π)]

3 + 3  π == 3 (1 + π)

In the example above SuppressMessage was used to suppress a single message, but we could also suppress several messages. Once SuppressMessage is finished evaluating, the messages that were on will be back on.

TurnOn, TurnOff Examples

The TurnOn usage message is shown below.

?TurnOn

In the next cell two messages are turned Off. Then TurnOn[...] is given four messages. The result returned indicates which messages were turned On.

Off[Set::setraw, Sin::argx];
TurnOn[Set::setraw, Sin::argx, Prime::intpp, Less::nord]

The TurnOff usage message is shown below.

?TurnOff

In the next cell we two messages are turned Off. Then TurnOff[...] is given four messages. The result returned indicates which messages were turned Off.

Off[Set::setraw, Sin::argx];
TurnOff[Set::setraw, Sin::argx, Prime::intpp, Less::nord]

TurnOff and TurnOn can be used instead of SuppressMessage, but using SuppressMessage is usually more concise.

Suppress all Messages

Appropriate use of Block will suppress all messages inside the Block.  After the block evaluates all messages that were On will be On again.  An example is given below.

Block[{Message}, 
Part[{1, 2, 
            3},   6]; 
5 = 
          8
];
4 <= 3  I

Evaluate the cell above and messages in the block are suppressed. As a result you don't get a message that you are taking a part of {1,2,3} that does not exist. You also don't get a message that you can't assign to the raw object 5. After the block is finished you get a message that (4≤3i) is an invalid comparison.