UVES Pipeline Reference Manual  5.5.3
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  */
26 
27 #ifdef HAVE_CONFIG_H
28 # include <config.h>
29 #endif
30 
31 #include <uves_msg.h>
32 
33 #include <cpl.h>
34 
35 #include <stdarg.h>
36 #include <stdio.h>
37 
38 /*----------------------------------------------------------------------------*/
52 /*----------------------------------------------------------------------------*/
53 
54 #undef DEBUG_CALLER /* Define whether to check consistency
55  of msg_louder/softer calls */
56 /* #define DEBUG_CALLER */
57 
58 #define MAXLEVEL 256
59 #define MAXSTRINGLENGTH 1000
60 
61 
62 static int level = 0; /* Current message & indentation level from 0 to MAXLEVEL-1.
63  0 is the most verbose level. */
64 static int outlevel = -1; /* Only print message if level is in {0, 1, ..., outlevel}.
65  Always print if outlevel = - 1 */
66 #ifdef DEBUG_CALLER
67 static const char *callers[MAXLEVEL]; /* Check the consistency of calls to softer/louder */
68 #endif
69 
70 static char printbuffer[MAXSTRINGLENGTH]; /* Used to pass variable argument list
71  to cpl_msg_info() */
72 
73 static const char *domain = "Undefined domain";
74  /* This is to support getting the current domain
75  * which is currently not available in CPL
76  */
77 static bool initialized = false;
78 
79 static int number_of_warnings = 0; /* Coun't the number of warnings
80  since initialization */
81 
83 /*-----------------------------------------------------------------------------
84  Implementation
85  -----------------------------------------------------------------------------*/
86 //static void signal_handler(int signum)
87 //{
88 // fprintf(stderr, "Panic! Signal %d caught, I'll just dump a trace and die\n", signum);
89 //
90 // abort();
91 //}
92 
93 /*----------------------------------------------------------------------------*/
109 /*----------------------------------------------------------------------------*/
110 void uves_msg_init(int olevel, const char *dom)
111 {
112  /* Initialize per recipe: */
113  number_of_warnings = 0;
114 
115 // signal(SIGSEGV, signal_handler);
116 // raise(SIGSEGV);
117 
118  if (!initialized)
119  {
120  /* Initialize once: */
121  outlevel = olevel;
122 
123  cpl_msg_set_indentation(2);
124 
125  /* CPL message format is
126  * [Time][Verbosity][domain][component] message
127  *
128  * Don't show the (variable length and wildly
129  * fluctuating) component. It interferes with
130  * indentation. The component is available anyway
131  * on CPL_MSG_DEBUG_MODE level.
132  *
133  * Don't show the time. This is available on
134  * the DEBUG_MODE level. Use esorex --time to time
135  * a recipe.
136  */
137 #if WANT_TIME_MEASURE
138  cpl_msg_set_time_on();
139 #else
140  cpl_msg_set_time_off();
141 #endif
142  uves_msg_set_domain(dom);
143  cpl_msg_set_domain_on();
144  cpl_msg_set_component_off();
145 
146  initialized = true;
147  }
148 }
149 
150 
151 /*----------------------------------------------------------------------------*/
158 /*----------------------------------------------------------------------------*/
159 void uves_msg_set_level(int olevel)
160 {
161  outlevel = olevel;
162 }
163 
164 /*----------------------------------------------------------------------------*/
172 /*----------------------------------------------------------------------------*/
173 void uves_msg_softer_macro(const char *fct)
174 {
175  if (level + 1 < MAXLEVEL)
176  {
177  level++;
178  cpl_msg_indent_more();
179 #ifdef DEBUG_CALLER
180  callers[level] = fct;
181 #else
182  fct = fct; /* Satisfy compiler */
183 #endif
184  }
185 }
186 
187 /*----------------------------------------------------------------------------*/
195 /*----------------------------------------------------------------------------*/
196 void uves_msg_louder_macro(const char *fct)
197 {
198  if (level == 0)
199  {
200  /* 0 is the loudest, ignore request */
201  return;
202  }
203 
204  /* Only make louder, if called from the same function which called
205  uves_msg_softer. (disable check if level is more than MAXLEVEL)
206  */
207 #ifdef DEBUG_CALLER
208  if (level >= MAXLEVEL || strcmp(callers[level], fct) == 0)
209 #else
210  fct = fct; /* Satisfy compiler */
211 #endif
212  {
213  level--;
214  cpl_msg_indent_less();
215  }
216 #ifdef DEBUG_CALLER
217  else
218  {
219  uves_msg_warning("Message level decreased by '%s' but increased by '%s'",
220  callers[level], fct);
221  }
222 #endif
223 }
224 
225 /*----------------------------------------------------------------------------*/
237 /*----------------------------------------------------------------------------*/
238 void uves_msg_macro(const char *fct, const char *format, ...)
239 {
240  va_list al;
241 
242  va_start(al, format);
243  vsnprintf(printbuffer, MAXSTRINGLENGTH - 1, format, al);
244  va_end(al);
245 
246  printbuffer[MAXSTRINGLENGTH - 1] = '\0';
247 
248  if (outlevel < 0 || level <= outlevel)
249  {
250 //#undef cpl_msg_info
251  cpl_msg_info(fct, "%s", printbuffer);
252 //#define cpl_msg_info(...) use__uves_msg__instead__of__cpl_msg_info
253  }
254  else
255  {
256  cpl_msg_debug(fct, "%s", printbuffer);
257  }
258 }
259 
260 /*----------------------------------------------------------------------------*/
265 /*----------------------------------------------------------------------------*/
267 {
268  return number_of_warnings;
269 }
270 
271 /*----------------------------------------------------------------------------*/
280 /*----------------------------------------------------------------------------*/
282 {
283  number_of_warnings += n;
284 }
285 
286 
287 /*----------------------------------------------------------------------------*/
303 /*----------------------------------------------------------------------------*/
304 void uves_msg_warning_macro(const char *fct, const char *format, ...)
305 {
306  va_list al;
307 
308  va_start(al, format);
309  vsnprintf(printbuffer, MAXSTRINGLENGTH - 1, format, al);
310  va_end(al);
311 
312  printbuffer[MAXSTRINGLENGTH - 1] = '\0';
313 
314  cpl_msg_warning(fct, "%s", printbuffer);
315 
316  number_of_warnings += 1;
317 }
318 
319 /*----------------------------------------------------------------------------*/
324 /*----------------------------------------------------------------------------*/
325 const char *uves_msg_get_domain(void)
326 {
327  return domain;
328 }
329 
330 /*----------------------------------------------------------------------------*/
335 /*----------------------------------------------------------------------------*/
336 void uves_msg_set_domain(const char *d)
337 {
338  /* Set domain and remember */
339  cpl_msg_set_domain(d);
340  domain = d;
341 }
342 
const char * uves_msg_get_domain(void)
Get current message domain.
Definition: uves_msg.c:325
void uves_msg_init(int olevel, const char *dom)
Initialize messaging.
Definition: uves_msg.c:110
void uves_msg_softer_macro(const char *fct)
Decrease message volume.
Definition: uves_msg.c:173
void uves_msg_louder_macro(const char *fct)
Increase message volume.
Definition: uves_msg.c:196
#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:159
int uves_msg_get_warnings(void)
Get number of warnings printed so far.
Definition: uves_msg.c:266
void uves_msg_add_warnings(int n)
Accumulate warnings.
Definition: uves_msg.c:281
void uves_msg_macro(const char *fct, const char *format,...)
Print a message on 'info' or 'debug' level.
Definition: uves_msg.c:238
void uves_msg_warning_macro(const char *fct, const char *format,...)
Print a warning message.
Definition: uves_msg.c:304
void uves_msg_set_domain(const char *d)
Set message domain.
Definition: uves_msg.c:336