Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Testing this answer I used:

stream = OpenWrite["A:\\out.txt"];

Block[{$Messages = stream},
  Mod[1, 2, 3, 4, 5]
]

and I was surprised to find that the contents of out.txt read:

Intersection::normal: 
   Nonatomic expression expected at position \[NoBreak]\!\(\*StyleBox[2
    , "MT"]\)\[NoBreak] in \[NoBreak]\!\(\*StyleBox[
    Intersection[{StandardForm, TraditionalForm}, FormatType]
    , "MT"]\)\[NoBreak]. >>

Intersection::normal: 
   Nonatomic expression expected at position \[NoBreak]\!\(\*StyleBox[2
    , "MT"]\)\[NoBreak] in \[NoBreak]\!\(\*StyleBox[
    Intersection[{StandardForm, TraditionalForm}, FormatType]
    , "MT"]\)\[NoBreak]. >>

Intersection::normal: 
   Nonatomic expression expected at position \[NoBreak]\!\(\*StyleBox[2
    , "MT"]\)\[NoBreak] in \[NoBreak]\!\(\*StyleBox[
    Intersection[{StandardForm, TraditionalForm}, FormatType]
    , "MT"]\)\[NoBreak]. >>

General::stop: Further output of \[NoBreak]\!\(\*StyleBox[Intersection::normal
    , "MT"]\)\[NoBreak] will be suppressed during this calculation. >>

Options::optnf: 
   \[NoBreak]\!\(\*StyleBox[FormatType
    , "MT"]\)\[NoBreak] is not a known option for \[NoBreak]\!\(\*StyleBox[
    Intersection[OutputStream[A:\out.txt, 12], 
     {OutputStream[stdout, 1], OutputStream[stderr, 2], 
      OutputStream[A:\out.txt, 12], stdout, stderr}], "MT"]\)\[NoBreak]. >>

Options::optnf: 
   \[NoBreak]\!\(\*StyleBox[FormatType
    , "MT"]\)\[NoBreak] is not a known option for \[NoBreak]\!\(\*StyleBox[
    Intersection[OutputStream[A:\out.txt, 12], 
     {OutputStream[stdout, 1], OutputStream[stderr, 2], 
      OutputStream[A:\out.txt, 12], stdout, stderr}], "MT"]\)\[NoBreak]. >>

Options::optnf: 
   \[NoBreak]\!\(\*StyleBox[FormatType
    , "MT"]\)\[NoBreak] is not a known option for \[NoBreak]\!\(\*StyleBox[
    Intersection[OutputStream[A:\out.txt, 12], 
     {OutputStream[stdout, 1], OutputStream[stderr, 2], 
      OutputStream[A:\out.txt, 12], stdout, stderr}], "MT"]\)\[NoBreak]. >>

General::stop: Further output of \[NoBreak]\!\(\*StyleBox[Options::optnf
    , "MT"]\)\[NoBreak] will be suppressed during this calculation. >>

Intersection::heads: 
   Heads \[NoBreak]\!\(\*StyleBox[List
    , "MT"]\)\[NoBreak] and \[NoBreak]\!\(\*StyleBox[OutputStream
    , "MT"]\)\[NoBreak] at positions \[NoBreak]\!\(\*StyleBox[2
    , "MT"]\)\[NoBreak] and \[NoBreak]\!\(\*StyleBox[1
    , "MT"]\)\[NoBreak] are expected to be the same. >>

Intersection::heads: 
   Heads \[NoBreak]\!\(\*StyleBox[List
    , "MT"]\)\[NoBreak] and \[NoBreak]\!\(\*StyleBox[OutputStream
    , "MT"]\)\[NoBreak] at positions \[NoBreak]\!\(\*StyleBox[2
    , "MT"]\)\[NoBreak] and \[NoBreak]\!\(\*StyleBox[1
    , "MT"]\)\[NoBreak] are expected to be the same. >>

Intersection::heads: 
   Heads \[NoBreak]\!\(\*StyleBox[List
    , "MT"]\)\[NoBreak] and \[NoBreak]\!\(\*StyleBox[OutputStream
    , "MT"]\)\[NoBreak] at positions \[NoBreak]\!\(\*StyleBox[2
    , "MT"]\)\[NoBreak] and \[NoBreak]\!\(\*StyleBox[1
    , "MT"]\)\[NoBreak] are expected to be the same. >>

General::stop: Further output of \[NoBreak]\!\(\*StyleBox[Intersection::heads
    , "MT"]\)\[NoBreak] will be suppressed during this calculation. >>

Mod::argt: \[NoBreak]\!\(\*StyleBox[Mod
    , "MT"]\)\[NoBreak] called with \[NoBreak]\!\(\*StyleBox[5
    , "MT"]\)\[NoBreak] arguments; \[NoBreak]\!\(\*StyleBox[2
    , "MT"]\)\[NoBreak] or \[NoBreak]\!\(\*StyleBox[3
    , "MT"]\)\[NoBreak] arguments are expected. >>

I expected only the last message. This looks like the result of a misconfiguration or Style Sheet problem but I don't know what it is.

Do others get these messages? If yes, what is the explanation? If not, what might I have done to cause this?

share|improve this question
3  
You should re-read the usage message for $Messages very carefully. –  Brett Champion Feb 1 '12 at 18:08
    
@Brett actually I didn't read it at all; I trusted Szabolcs. I blame him! –  Mr.Wizard Feb 1 '12 at 18:12
    
To clarify a bit the somewhat cryptic answer of @Brett - $Messages point to a channel, which can be a list of streams, but not a single stream without a list around it. –  Leonid Shifrin Feb 1 '12 at 18:13
    
@LeonidShifrin Don't answer in comments! –  Brett Champion Feb 1 '12 at 18:16
1  
@Brett Did not indend to. This was your answer, I just commented on it :). Why don't you put this as an answer? –  Leonid Shifrin Feb 1 '12 at 18:16

1 Answer 1

up vote 6 down vote accepted

The documentation for $Messages clearly states:

$Messages gives the list of files and pipes to which message output is sent.

Therefore Block[{$Messages = {stream}}, ... ] is the correct syntax.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.