Example: Callable Library message channels
Demonstrates control of message channels in the C API.
This example shows you how to use the CPLEX message handler from the Callable Library. It captures all messages generated by CPLEX and displays them on screen along with a label indicating which channel sent the message. It also creates a user channel to receive output generated by the program itself. The user channel accepts user-generated messages, displays them on screen with a label, and records them in a file without the label.
The complete program lpex5.c appears online in
the standard distribution at yourCPLEXinstallation /examples/src.
This example derives from lpex1.c, a program in Getting
Started. There are a few differences between the two examples:
In this example, the function
ourmsgfuncmanages all output. The program itself, or ratherCPXXmsgstr and CPXmsgstrfrom the CPLEX Callable Library, callsourmsgfunc. In fact,CPXmsgstris a replacement forprintf, allowing a message to appear in more than one place, for example, both on screen and in a file.Only after you initialize the CPLEX environment by calling
CPXopenCPLEXcan you callCPXmsgstr. And only after you callCPXgetchannelscan you use the default CPLEX channels. Therefore, calls toourmsgfuncprint directly any messages that occur before the program gets the address ofcpxerror(a channel). After a call toCPXgetchannelsgets the address ofcpxerror, and after a call toCPXaddfuncdestassociates the message functionourmsgfuncwithcpxerror, then error messages are generated by calls toCPXmsgstr.After the
TERMINATE:label, any error must be generated with care in case the error message function has not been set up properly. Thus,ourmsgfuncis also called directly to generate any error messages there.System function
fopenopens the filelpex5.msgto accept solution information. Solution information is also displayed on screen sinceourmsgfuncis associated with that new channel, too. Thus in the loops near the end ofmain, when the solution is printed, only one call toCPXmsgstrsuffices to put the output both on screen and into the file.Although
CPXcloseCPLEXwill automatically delete file- and function-destinations for channels, it is a good practice to callCPXXdelfuncdest and CPXdelfuncdestat the end of your programs.