The logic of Message internals

允我心安 提交于 2019-12-01 19:52:02

I am not understanding why it is necessary to turn on all the messages with On. Can you not only activate the subset you need. The reference page of On in "More Information" section lists various categories which you might find useful. In case you choose to proceed as stated, you can suppress trace messages by explicitly turning them off right after On[]:

On[]; Off[General::trace];
Sin[1, 1]

This outputs only two messages. Hence 830 of the messages you see are ::trace messages and originate from execution of some top-level code which is not necessarily message related, might be typesetting...

Alexey Popkov

It seems that I have found one way to achieve with built-in function Message what I want:

Unprotect[Message];
Message[_, HoldForm[Block[List[$MyMagicalTag$, ___], _]], _] := Null;
Message[args___] /; 
   Block[{$MyMagicalTag$, Message}, Not@TrueQ[inMsg]] := 
  Block[{$MyMagicalTag$, inMsg = True, lastargs = HoldComplete[args]},
    Message[args]];
Message[args___] /; 
   Block[{$MyMagicalTag$, 
     Message}, (inMsg && (HoldComplete[args] =!= lastargs))] := Null;
Protect[Message];

Now things work as expected:

In[6]:= On[]
In[7]:= Sin[1,1]//AbsoluteTiming
During evaluation of In[7]:= Message::trace: Message[Sin::argx,Sin,2] --> Block[{$MyMagicalTag$,inMsg=True,lastargs=HoldComplete[Sin::argx,Sin,2]},Message[Sin::argx,Sin,2]]. >>
During evaluation of In[7]:= Sin::argx: Sin called with 2 arguments; 1 argument is expected. >>
During evaluation of In[7]:= AbsoluteTiming::trace: AbsoluteTiming[Sin[1,1]] --> {0.1502160,Sin[1,1]}. >>
Out[7]= {0.1502160,Sin[1,1]}

The only problem with the above is that CPU load is still high as you can see from the timings.

Other tested cases also work correctly:

In[8]:= 1+1//AbsoluteTiming
During evaluation of In[8]:= Plus::trace: 1+1 --> 2. >>
During evaluation of In[8]:= AbsoluteTiming::trace: AbsoluteTiming[1+1] --> {0.0400576,2}. >>
Out[8]= {0.0400576,2}

Thanks to Mr.Wizard for his help.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!