UVES Pipeline Reference Manual  5.4.6
uves_msg.c
1 /* *
2  * This file is part of the ESO UVES 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: 2013-07-01 15:36:29 $
23  * $Revision: 1.31 $
24  * $Name: not supported by cvs2svn $
25  * $Log: not supported by cvs2svn $
26  * Revision 1.30 2010/09/24 09:32:04 amodigli
27  * put back QFITS dependency to fix problem spot by NRI on FIBER mode (with MIDAS calibs) data
28  *
29  * Revision 1.28 2007/06/06 08:17:33 amodigli
30  * replace tab with 4 spaces
31  *
32  * Revision 1.27 2007/05/23 13:03:19 jmlarsen
33  * Added missing include directive
34  *
35  * Revision 1.26 2007/01/10 12:38:22 jmlarsen
36  * Added commented out signal handling code
37  *
38  * Revision 1.25 2006/09/06 14:44:55 jmlarsen
39  * Added documentation about non-use of the cpl_error_code
40  *
41  * Revision 1.24 2006/08/17 13:56:53 jmlarsen
42  * Reduced max line length
43  *
44  * Revision 1.23 2006/08/11 14:56:05 amodigli
45  * removed Doxygen warnings
46  *
47  * Revision 1.22 2006/08/11 11:29:26 jmlarsen
48  * Added explicit void at function definition
49  *
50  * Revision 1.21 2006/06/01 14:43:17 jmlarsen
51  * Added missing documentation
52  *
53  * Revision 1.20 2006/03/24 14:13:11 jmlarsen
54  * Conditionally set time stamp on/off
55  *
56  * Revision 1.19 2006/03/09 10:57:07 jmlarsen
57  * Minor bugfix: #if -> #ifdef
58  *
59  * Revision 1.18 2006/02/28 09:15:22 jmlarsen
60  * Minor update
61  *
62  * Revision 1.17 2006/02/21 14:26:54 jmlarsen
63  * Minor changes
64  *
65  * Revision 1.16 2005/12/19 16:17:56 jmlarsen
66  * Replaced bool -> int
67  *
68  */
69 
70 #ifdef HAVE_CONFIG_H
71 # include <config.h>
72 #endif
73 
74 #include <uves_msg.h>
75 
76 #include <cpl.h>
77 
78 #include <stdarg.h>
79 #include <stdio.h>
80 
81 /*----------------------------------------------------------------------------*/
95 /*----------------------------------------------------------------------------*/
96 
97 #undef DEBUG_CALLER /* Define whether to check consistency
98  of msg_louder/softer calls */
99 /* #define DEBUG_CALLER */
100 
101 #define MAXLEVEL 256
102 #define MAXSTRINGLENGTH 1000
103 
104 
105 static int level = 0; /* Current message & indentation level from 0 to MAXLEVEL-1.
106  0 is the most verbose level. */
107 static int outlevel = -1; /* Only print message if level is in {0, 1, ..., outlevel}.
108  Always print if outlevel = - 1 */
109 #ifdef DEBUG_CALLER
110 static const char *callers[MAXLEVEL]; /* Check the consistency of calls to softer/louder */
111 #endif
112 
113 static char printbuffer[MAXSTRINGLENGTH]; /* Used to pass variable argument list
114  to cpl_msg_info() */
115 
116 static const char *domain = "Undefined domain";
117  /* This is to support getting the current domain
118  * which is currently not available in CPL
119  */
120 static bool initialized = false;
121 
122 static int number_of_warnings = 0; /* Coun't the number of warnings
123  since initialization */
124 
126 /*-----------------------------------------------------------------------------
127  Implementation
128  -----------------------------------------------------------------------------*/
129 //static void signal_handler(int signum)
130 //{
131 // fprintf(stderr, "Panic! Signal %d caught, I'll just dump a trace and die\n", signum);
132 //
133 // abort();
134 //}
135 
136 /*----------------------------------------------------------------------------*/
152 /*----------------------------------------------------------------------------*/
153 void uves_msg_init(int olevel, const char *dom)
154 {
155  /* Initialize per recipe: */
156  number_of_warnings = 0;
157 
158 // signal(SIGSEGV, signal_handler);
159 // raise(SIGSEGV);
160 
161  if (!initialized)
162  {
163  /* Initialize once: */
164  outlevel = olevel;
165 
166  cpl_msg_set_indentation(2);
167 
168  /* CPL message format is
169  * [Time][Verbosity][domain][component] message
170  *
171  * Don't show the (variable length and wildly
172  * fluctuating) component. It interferes with
173  * indentation. The component is available anyway
174  * on CPL_MSG_DEBUG_MODE level.
175  *
176  * Don't show the time. This is available on
177  * the DEBUG_MODE level. Use esorex --time to time
178  * a recipe.
179  */
180 #if WANT_TIME_MEASURE
181  cpl_msg_set_time_on();
182 #else
183  cpl_msg_set_time_off();
184 #endif
185  uves_msg_set_domain(dom);
186  cpl_msg_set_domain_on();
187  cpl_msg_set_component_off();
188 
189  initialized = true;
190  }
191 }
192 
193 
194 /*----------------------------------------------------------------------------*/
201 /*----------------------------------------------------------------------------*/
202 void uves_msg_set_level(int olevel)
203 {
204  outlevel = olevel;
205 }
206 
207 /*----------------------------------------------------------------------------*/
215 /*----------------------------------------------------------------------------*/
216 void uves_msg_softer_macro(const char *fct)
217 {
218  if (level + 1 < MAXLEVEL)
219  {
220  level++;
221  cpl_msg_indent_more();
222 #ifdef DEBUG_CALLER
223  callers[level] = fct;
224 #else
225  fct = fct; /* Satisfy compiler */
226 #endif
227  }
228 }
229 
230 /*----------------------------------------------------------------------------*/
238 /*----------------------------------------------------------------------------*/
239 void uves_msg_louder_macro(const char *fct)
240 {
241  if (level == 0)
242  {
243  /* 0 is the loudest, ignore request */
244  return;
245  }
246 
247  /* Only make louder, if called from the same function which called
248  uves_msg_softer. (disable check if level is more than MAXLEVEL)
249  */
250 #ifdef DEBUG_CALLER
251  if (level >= MAXLEVEL || strcmp(callers[level], fct) == 0)
252 #else
253  fct = fct; /* Satisfy compiler */
254 #endif
255  {
256  level--;
257  cpl_msg_indent_less();
258  }
259 #ifdef DEBUG_CALLER
260  else
261  {
262  uves_msg_warning("Message level decreased by '%s' but increased by '%s'",
263  callers[level], fct);
264  }
265 #endif
266 }
267 
268 /*----------------------------------------------------------------------------*/
280 /*----------------------------------------------------------------------------*/
281 void uves_msg_macro(const char *fct, const char *format, ...)
282 {
283  va_list al;
284 
285  va_start(al, format);
286  vsnprintf(printbuffer, MAXSTRINGLENGTH - 1, format, al);
287  va_end(al);
288 
289  printbuffer[MAXSTRINGLENGTH - 1] = '\0';
290 
291  if (outlevel < 0 || level <= outlevel)
292  {
293 //#undef cpl_msg_info
294  cpl_msg_info(fct, "%s", printbuffer);
295 //#define cpl_msg_info(...) use__uves_msg__instead__of__cpl_msg_info
296  }
297  else
298  {
299  cpl_msg_debug(fct, "%s", printbuffer);
300  }
301 }
302 
303 /*----------------------------------------------------------------------------*/
308 /*----------------------------------------------------------------------------*/
310 {
311  return number_of_warnings;
312 }
313 
314 /*----------------------------------------------------------------------------*/
323 /*----------------------------------------------------------------------------*/
325 {
326  number_of_warnings += n;
327 }
328 
329 
330 /*----------------------------------------------------------------------------*/
346 /*----------------------------------------------------------------------------*/
347 void uves_msg_warning_macro(const char *fct, const char *format, ...)
348 {
349  va_list al;
350 
351  va_start(al, format);
352  vsnprintf(printbuffer, MAXSTRINGLENGTH - 1, format, al);
353  va_end(al);
354 
355  printbuffer[MAXSTRINGLENGTH - 1] = '\0';
356 
357  cpl_msg_warning(fct, "%s", printbuffer);
358 
359  number_of_warnings += 1;
360 }
361 
362 /*----------------------------------------------------------------------------*/
367 /*----------------------------------------------------------------------------*/
368 const char *uves_msg_get_domain(void)
369 {
370  return domain;
371 }
372 
373 /*----------------------------------------------------------------------------*/
378 /*----------------------------------------------------------------------------*/
379 void uves_msg_set_domain(const char *d)
380 {
381  /* Set domain and remember */
382  cpl_msg_set_domain(d);
383  domain = d;
384 }
385 
const char * uves_msg_get_domain(void)
Get current message domain.
Definition: uves_msg.c:368
void uves_msg_init(int olevel, const char *dom)
Initialize messaging.
Definition: uves_msg.c:153
void uves_msg_softer_macro(const char *fct)
Decrease message volume.
Definition: uves_msg.c:216
void uves_msg_louder_macro(const char *fct)
Increase message volume.
Definition: uves_msg.c:239
#define uves_msg_warning(...)
Print an warning message.
Definition: uves_msg.h:87
void uves_msg_set_level(int olevel)
Set output level.
Definition: uves_msg.c:202
int uves_msg_get_warnings(void)
Get number of warnings printed so far.
Definition: uves_msg.c:309
void uves_msg_add_warnings(int n)
Accumulate warnings.
Definition: uves_msg.c:324
void uves_msg_macro(const char *fct, const char *format,...)
Print a message on 'info' or 'debug' level.
Definition: uves_msg.c:281
void uves_msg_warning_macro(const char *fct, const char *format,...)
Print a warning message.
Definition: uves_msg.c:347
void uves_msg_set_domain(const char *d)
Set message domain.
Definition: uves_msg.c:379