SINFONI Pipeline Reference Manual  2.5.2
sinfo_msg.c
1 /* *
2  * This file is part of the ESO SINFO Pipeline *
3  * Copyright (C) 2004,2005 European Southern Observatory *
4  * *
5  * This library is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the Free Software *
17  * Foundation, 51 Franklin St, Fifth Floor, Boston, MA 02111-1307 USA *
18  * */
19 
20 /*
21  * $Author: amodigli $
22  * $Date: 2008-02-12 14:57:39 $
23  * $Revision: 1.7 $
24  * $Name: not supported by cvs2svn $
25  */
26 
27 #ifdef HAVE_CONFIG_H
28 # include <config.h>
29 #endif
30 
31 #include <sinfo_msg.h>
32 #include <cpl.h>
33 #include <stdarg.h>
34 #include <stdio.h>
35 
37 /*--------------------------------------------------------------------------*/
50 /*---------------------------------------------------------------------------*/
51 
52 #define DEBUG_CALLER 0 /* Define whether to check consistency of
53  msg_louder/softer calls */
54 /* #define DEBUG_CALLER */
55 
56 #define MAXLEVEL 256
57 #define MAXSTRINGLENGTH 1000
58 
59 static int level = 0; /* Current message & indentation level
60  from 0 to MAXLEVEL-1.
61  0 is the most verbose level. */
62 static int outlevel = -1; /* Only print message if level is
63  in {0, 1, ..., outlevel}.
64  Always print if outlevel = - 1 */
65 #ifdef DEBUG_CALLER
66 const char *sinfo_callers[MAXLEVEL]; /* Check the consistency of
67  calls to softer/louder */
68 #endif
69 
70 static char printbuffer[MAXSTRINGLENGTH]; /* Used to pass variable argument
71  list to cpl_msg_info() */
72 
73 static const char *domain = "Undefined domain";
74 /* This is to support getting the
75  current domain
76  * which is currently not available in CPL
77  */
78 static int initialized = FALSE;
79 
80 static int number_of_warnings = 0; /* Coun't the number of warnings since
81  initialization */
82 
83 /*--------------------------------------------------------------------------*/
100 /*--------------------------------------------------------------------------*/
101 void
102 sinfo_msg_init(int olevel, const char *dom)
103 {
104  /* Initialize per recipe: */
105  number_of_warnings = 0;
106 
107  if (!initialized) {
108  /* Initialize once: */
109  outlevel = olevel;
110 
111  cpl_msg_set_indentation(2);
112 
113  /* CPL message format is
114  * [Time][Verbosity][domain][component] message
115  *
116  * Don't show the (variable length and wildly
117  * fluctuating) component. It interferes with
118  * indentation. The component is available anyway
119  * on CPL_MSG_DEBUG level.
120  */
121  cpl_msg_set_time_on();
123  cpl_msg_set_domain_on();
124  cpl_msg_set_component_off();
125 
126  initialized = TRUE;
127  }
128 }
129 
130 /*---------------------------------------------------------------------------*/
137 /*---------------------------------------------------------------------------*/
138 void
139 sinfo_msg_set_level(int olevel)
140 {
141  outlevel = olevel;
142 }
143 
144 /*---------------------------------------------------------------------------*/
152 /*---------------------------------------------------------------------------*/
153 void
154 sinfo_msg_softer_macro(const char *fctid)
155 {
156  if (level + 1 < MAXLEVEL) {
157  level++;
158  cpl_msg_indent_more();
159 #if DEBUG_CALLER
160  sinfo_callers[level] = fctid;
161 #else
162  fctid = fctid; /* Satisfy compiler */
163 #endif
164  }
165 }
166 
167 /*---------------------------------------------------------------------------*/
175 /*---------------------------------------------------------------------------*/
176 void
177 sinfo_msg_louder_macro(const char *fctid)
178 {
179  if (level == 0) {
180  /* 0 is the loudest, ignore request */
181  return;
182  }
183 
184  /* Only make louder, if called from the same function which called
185  sinfo_msg_softer. (disable check if level is more than MAXLEVEL)
186  */
187 #if DEBUG_CALLER
188  if (level >= MAXLEVEL || strcmp(sinfo_callers[level], fctid) == 0)
189 #else
190  fctid = fctid; /* Satisfy compiler */
191 #endif
192  {
193  level--;
194  cpl_msg_indent_less();
195  }
196 #if DEBUG_CALLER
197  else
198  {
199  sinfo_msg_warning("Message level decreased by '%s' but increased by '%s'",
200  sinfo_callers[level], fctid);
201  }
202 #endif
203 }
204 
205 /*---------------------------------------------------------------------------*/
218 /*---------------------------------------------------------------------------*/
219 void
220 sinfo_msg_macro(const char *fct, const char *format, ...)
221 {
222  va_list al;
223 
224  va_start(al, format);
225  vsnprintf(printbuffer, MAXSTRINGLENGTH - 1, format, al);
226  va_end(al);
227 
228  printbuffer[MAXSTRINGLENGTH - 1] = '\0';
229 
230  if (outlevel < 0 || level <= outlevel) {
231  /*
232  #undef cpl_msg_info
233  */
234  cpl_msg_info(fct, "%s", printbuffer);
235  /*
236  #define cpl_msg_info(...) use__sinfo_msg__instead__of__cpl_msg_info
237  */
238  }
239  else {
240  cpl_msg_debug(fct, "%s", printbuffer);
241  }
242 }
243 
244 /*---------------------------------------------------------------------------*/
249 /*---------------------------------------------------------------------------*/
250 int
252 {
253  return number_of_warnings;
254 }
255 
256 /*---------------------------------------------------------------------------*/
265 /*---------------------------------------------------------------------------*/
266 void
268 {
269  number_of_warnings += n;
270 }
271 
272 /*---------------------------------------------------------------------------*/
286 /*---------------------------------------------------------------------------*/
287 void
288 sinfo_msg_warning_macro(const char *fct, const char *format, ...)
289 {
290  va_list al;
291 
292  va_start(al, format);
293  vsnprintf(printbuffer, MAXSTRINGLENGTH - 1, format, al);
294  va_end(al);
295 
296  printbuffer[MAXSTRINGLENGTH - 1] = '\0';
297 
298  cpl_msg_warning(fct, "%s", printbuffer);
299 
300  number_of_warnings += 1;
301 }
302 
303 /*---------------------------------------------------------------------------*/
309 /*---------------------------------------------------------------------------*/
310 const char *
312 {
313  return domain;
314 }
315 
316 /*---------------------------------------------------------------------------*/
321 /*---------------------------------------------------------------------------*/
322 void
323 sinfo_msg_set_domain(const char *d)
324 {
325  /* Set domain and remember */
326  cpl_msg_set_domain(d);
327  domain = d;
328 }
329