1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2 /*                                                                           */
3 /*                  This file is part of the program and library             */
4 /*         SCIP --- Solving Constraint Integer Programs                      */
5 /*                                                                           */
6 /*    Copyright (C) 2002-2021 Konrad-Zuse-Zentrum                            */
7 /*                            fuer Informationstechnik Berlin                */
8 /*                                                                           */
9 /*  SCIP is distributed under the terms of the ZIB Academic License.         */
10 /*                                                                           */
11 /*  You should have received a copy of the ZIB Academic License              */
12 /*  along with SCIP; see the file COPYING. If not visit scipopt.org.         */
13 /*                                                                           */
14 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
15 
16 /**@file   scip_message.c
17  * @ingroup OTHER_CFILES
18  * @brief  public methods for message handling
19  * @author Tobias Achterberg
20  * @author Timo Berthold
21  * @author Gerald Gamrath
22  * @author Leona Gottwald
23  * @author Stefan Heinz
24  * @author Gregor Hendel
25  * @author Thorsten Koch
26  * @author Alexander Martin
27  * @author Marc Pfetsch
28  * @author Michael Winkler
29  * @author Kati Wolter
30  *
31  * @todo check all SCIP_STAGE_* switches, and include the new stages TRANSFORMED and INITSOLVE
32  */
33 
34 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
35 
36 #include "nlpi/nlpi.h"
37 #include "scip/debug.h"
38 #include "scip/pub_message.h"
39 #include "scip/scip_message.h"
40 #include "scip/struct_scip.h"
41 #include "scip/struct_set.h"
42 #include "scip/struct_stat.h"
43 
44 /** installs the given message handler, such that all messages are passed to this handler. A messages handler can be
45  *  created via SCIPmessagehdlrCreate().
46  *
47  *  @return \ref SCIP_OKAY is returned if everything worked. Otherwise a suitable error code is passed. See \ref
48  *          SCIP_Retcode "SCIP_RETCODE" for a complete list of error codes.
49  *
50  *  @pre this method can be called in one of the following stages of the SCIP solving process:
51  *       - \ref SCIP_STAGE_INIT
52  *       - \ref SCIP_STAGE_PROBLEM
53  *
54  *  @note The currently installed messages handler gets freed if this SCIP instance is its last user (w.r.t. capture/release).
55  */
SCIPsetMessagehdlr(SCIP * scip,SCIP_MESSAGEHDLR * messagehdlr)56 SCIP_RETCODE SCIPsetMessagehdlr(
57    SCIP*                 scip,               /**< SCIP data structure */
58    SCIP_MESSAGEHDLR*     messagehdlr         /**< message handler to install, or NULL to suppress all output */
59    )
60 {
61    int i;
62 
63    SCIP_CALL( SCIPcheckStage(scip, "SCIPsetMessagehdlr", TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, TRUE) );
64 
65    assert(scip != NULL);
66    assert(scip->set != NULL);
67    assert(scip->set->nlpis != NULL || scip->set->nnlpis == 0);
68 
69    /* update message handler in NLP solver interfaces */
70    for( i = 0; i < scip->set->nnlpis; ++i )
71    {
72       assert(scip->set->nlpis[i] != NULL);
73 
74       SCIP_CALL( SCIPnlpiSetMessageHdlr(scip->set->nlpis[i], messagehdlr) );
75    }
76 
77    SCIPmessagehdlrCapture(messagehdlr);
78 
79    SCIP_CALL( SCIPmessagehdlrRelease(&scip->messagehdlr) );
80    assert(scip->messagehdlr == NULL);
81 
82    scip->messagehdlr = messagehdlr;
83 
84    return SCIP_OKAY;
85 }
86 
87 /** returns the currently installed message handler
88  *
89  *  @return the currently installed message handler, or NULL if messages are currently suppressed
90  */
SCIPgetMessagehdlr(SCIP * scip)91 SCIP_MESSAGEHDLR* SCIPgetMessagehdlr(
92    SCIP*                 scip                /**< SCIP data structure */
93    )
94 {
95    return scip->messagehdlr;
96 }
97 
98 /** sets the log file name for the currently installed message handler */
SCIPsetMessagehdlrLogfile(SCIP * scip,const char * filename)99 void SCIPsetMessagehdlrLogfile(
100    SCIP*                 scip,               /**< SCIP data structure */
101    const char*           filename            /**< name of log file, or NULL (no log) */
102    )
103 {
104    if( scip->messagehdlr != NULL )
105    {
106       SCIPmessagehdlrSetLogfile(scip->messagehdlr, filename);
107    }
108 }
109 
110 /** sets the currently installed message handler to be quiet (or not) */
SCIPsetMessagehdlrQuiet(SCIP * scip,SCIP_Bool quiet)111 void SCIPsetMessagehdlrQuiet(
112    SCIP*                 scip,               /**< SCIP data structure */
113    SCIP_Bool             quiet               /**< should screen messages be suppressed? */
114    )
115 {
116    if( scip->messagehdlr != NULL )
117    {
118       SCIPmessagehdlrSetQuiet(scip->messagehdlr, quiet);
119    }
120 }
121 
122 /** prints a warning message via the message handler */
SCIPwarningMessage(SCIP * scip,const char * formatstr,...)123 void SCIPwarningMessage(
124    SCIP*                 scip,               /**< SCIP data structure */
125    const char*           formatstr,          /**< format string like in printf() function */
126    ...                                       /**< format arguments line in printf() function */
127    )
128 {
129    va_list ap;
130 
131    assert(scip != NULL);
132 
133    va_start(ap, formatstr); /*lint !e838*/
134    SCIPmessageVFPrintWarning(scip->messagehdlr, formatstr, ap);
135    va_end(ap);
136 }
137 
138 /** prints a debug message */
SCIPprintDebugMessage(SCIP * scip,const char * sourcefile,int sourceline,const char * formatstr,...)139 void SCIPprintDebugMessage(
140    SCIP*                 scip,               /**< SCIP data structure */
141    const char*           sourcefile,         /**< name of the source file that called the function */
142    int                   sourceline,         /**< line in the source file where the function was called */
143    const char*           formatstr,          /**< format string like in printf() function */
144    ...                                       /**< format arguments line in printf() function */
145    )
146 {
147    int subscipdepth = 0;
148    va_list ap;
149 
150    assert( sourcefile != NULL );
151    assert( scip != NULL );
152 
153    if ( scip->stat != NULL )
154       subscipdepth = scip->stat->subscipdepth;
155    if ( subscipdepth > 0 )
156       SCIPmessageFPrintInfo(scip->messagehdlr, NULL, "%d: [%s:%d] debug: ", subscipdepth, sourcefile, sourceline);
157    else
158       SCIPmessageFPrintInfo(scip->messagehdlr, NULL, "[%s:%d] debug: ", sourcefile, sourceline);
159 
160    va_start(ap, formatstr); /*lint !e838*/
161    SCIPmessageVFPrintInfo(scip->messagehdlr, NULL, formatstr, ap);
162    va_end(ap);
163 }
164 
165 /** prints a debug message without precode */
SCIPdebugMessagePrint(SCIP * scip,const char * formatstr,...)166 void SCIPdebugMessagePrint(
167    SCIP*                 scip,               /**< SCIP data structure */
168    const char*           formatstr,          /**< format string like in printf() function */
169    ...                                       /**< format arguments line in printf() function */
170    )
171 {
172    va_list ap;
173 
174    assert( scip != NULL );
175 
176    va_start(ap, formatstr); /*lint !e838*/
177    SCIPmessageVFPrintInfo(scip->messagehdlr, NULL, formatstr, ap);
178    va_end(ap);
179 }
180 
181 /** prints a dialog message that requests user interaction or is a direct response to a user interactive command */
SCIPdialogMessage(SCIP * scip,FILE * file,const char * formatstr,...)182 void SCIPdialogMessage(
183    SCIP*                 scip,               /**< SCIP data structure */
184    FILE*                 file,               /**< file stream to print into, or NULL for stdout */
185    const char*           formatstr,          /**< format string like in printf() function */
186    ...                                       /**< format arguments line in printf() function */
187    )
188 {
189    va_list ap;
190 
191    assert(scip != NULL);
192 
193    va_start(ap, formatstr); /*lint !e838*/
194    SCIPmessageVFPrintDialog(scip->messagehdlr, file, formatstr, ap);
195    va_end(ap);
196 }
197 
198 /** prints a message */
SCIPinfoMessage(SCIP * scip,FILE * file,const char * formatstr,...)199 void SCIPinfoMessage(
200    SCIP*                 scip,               /**< SCIP data structure */
201    FILE*                 file,               /**< file stream to print into, or NULL for stdout */
202    const char*           formatstr,          /**< format string like in printf() function */
203    ...                                       /**< format arguments line in printf() function */
204    )
205 {
206    va_list ap;
207 
208    assert(scip != NULL);
209 
210    va_start(ap, formatstr); /*lint !e838*/
211    SCIPmessageVFPrintInfo(scip->messagehdlr, file, formatstr, ap);
212    va_end(ap);
213 }
214 
215 /** prints a message depending on the verbosity level */
SCIPverbMessage(SCIP * scip,SCIP_VERBLEVEL msgverblevel,FILE * file,const char * formatstr,...)216 void SCIPverbMessage(
217    SCIP*                 scip,               /**< SCIP data structure */
218    SCIP_VERBLEVEL        msgverblevel,       /**< verbosity level of this message */
219    FILE*                 file,               /**< file stream to print into, or NULL for stdout */
220    const char*           formatstr,          /**< format string like in printf() function */
221    ...                                       /**< format arguments line in printf() function */
222    )
223 {
224    va_list ap;
225 
226    assert(scip != NULL);
227    assert(scip->set != NULL);
228 
229    va_start(ap, formatstr); /*lint !e838*/
230    SCIPmessageVFPrintVerbInfo(scip->messagehdlr, scip->set->disp_verblevel, msgverblevel, file, formatstr, ap);
231    va_end(ap);
232 }
233 
234 /** returns the current message verbosity level
235  *
236  *  @return message verbosity level of SCIP
237  *
238  *  @see \ref SCIP_VerbLevel "SCIP_VERBLEVEL" for a list of all verbosity levels
239  */
SCIPgetVerbLevel(SCIP * scip)240 SCIP_VERBLEVEL SCIPgetVerbLevel(
241    SCIP*                 scip                /**< SCIP data structure */
242    )
243 {
244    assert(scip != NULL);
245    assert(scip->set != NULL);
246 
247    return scip->set->disp_verblevel;
248 }
249