Problem description:
I am capturing all FE messages to redirect them to my own window while blocking them from showing in the evaluation notebook using this method by rcollyer. It works great, but I have noticed a strange thing. If my code includes ReadString, then my custom error window is flooded with a lot of messages:
The function run has already been loaded with argument types {Integer,UTF8String,UTF8String,UTF8String,UTF8String}. The new function will be an overload for different types.
This happens only the first time with a fresh kernel (or after Quit). The second and all consecutive calls to ReadString with the same or other arguments work fine without any errors. There are no errors as well if I call ReadString with any arguments even one time on a fresh kernel prior to message capturing, which means it has something to do with the initial loading of built-in functions.
I have experimented with other functions (I/O such as Read, Import, and standard Map, Applyetc., and even WriteString), but none of them produces that error.
This is the simplified version of the code that generates the same behavior (using Print here for simplicity and retaining Messageto display all messages in the evaluation notebook for debugging -- not using it in the main code):
Internal`InheritedBlock[{Message, $InMsg = False}, Unprotect[Message];
Message[msg_, vars___] /; (! $InMsg && msg =!= $Off[]) :=
Block[{$InMsg = True, controlstring},
controlstring =
First@ReleaseHold[
msg /. Cases[HoldPattern@msg,
HoldPattern[MessageName[x_, _]] :> Messages@x]];
Print[StringForm[controlstring, vars]];
Message[msg, vars];
];
Module[{}, ReadString["ew"]];
]
The message name for this error is LibraryFunction::overload, but the strange thing is that msg doesn't get that value, instead it is set to the control string already, the same we can find by calling Messages[LibraryFunction]. In this example code, I am still allowing for the messages to be displayed normally through Message, which shows all messages (including the message that a file cannot be opened) except for this one, so we see it only because of Print, but it is also why my custom window gets all those errors, as I am using the controlstring variable. Is there a hidden reason why Message doesn't print this error?
The questions are:
1) Why is ReadStringso special and what is that error?
2) Are there other functions like that? I can of course, call ReadStringwith a dummy parameter before I set up message capturing to make sure it is loaded, but what if there are other functions with a similar behavior? I don't want to see the internal messages that are normally not visible in the front end -- Message doesn't print it under normal circumstances.
3) What can be done to eliminate that error -- e.g. pre-loading some packages etc.? Other versions of the code that allows for message capturing (and also preventing it from displaying normally, so EvaluationDatawouldn't work here)?
ReadString;preceding your code block preloads the library function. $\endgroup$