uves_msg.c

00001 /*                                                                              *
00002  *   This file is part of the ESO UVES Pipeline                                 *
00003  *   Copyright (C) 2004,2005 European Southern Observatory                      *
00004  *                                                                              *
00005  *   This library is free software; you can redistribute it and/or modify       *
00006  *   it under the terms of the GNU General Public License as published by       *
00007  *   the Free Software Foundation; either version 2 of the License, or          *
00008  *   (at your option) any later version.                                        *
00009  *                                                                              *
00010  *   This program is distributed in the hope that it will be useful,            *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of             *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
00013  *   GNU General Public License for more details.                               *
00014  *                                                                              *
00015  *   You should have received a copy of the GNU General Public License          *
00016  *   along with this program; if not, write to the Free Software                *
00017  *   Foundation, 51 Franklin St, Fifth Floor, Boston, MA  02111-1307  USA       *
00018  *                                                                              */
00019 
00020 /*
00021  * $Author: amodigli $
00022  * $Date: 2007/06/06 08:17:33 $
00023  * $Revision: 1.28 $
00024  * $Name: uves-3_9_0 $
00025  * $Log: uves_msg.c,v $
00026  * Revision 1.28  2007/06/06 08:17:33  amodigli
00027  * replace tab with 4 spaces
00028  *
00029  * Revision 1.27  2007/05/23 13:03:19  jmlarsen
00030  * Added missing include directive
00031  *
00032  * Revision 1.26  2007/01/10 12:38:22  jmlarsen
00033  * Added commented out signal handling code
00034  *
00035  * Revision 1.25  2006/09/06 14:44:55  jmlarsen
00036  * Added documentation about non-use of the cpl_error_code
00037  *
00038  * Revision 1.24  2006/08/17 13:56:53  jmlarsen
00039  * Reduced max line length
00040  *
00041  * Revision 1.23  2006/08/11 14:56:05  amodigli
00042  * removed Doxygen warnings
00043  *
00044  * Revision 1.22  2006/08/11 11:29:26  jmlarsen
00045  * Added explicit void at function definition
00046  *
00047  * Revision 1.21  2006/06/01 14:43:17  jmlarsen
00048  * Added missing documentation
00049  *
00050  * Revision 1.20  2006/03/24 14:13:11  jmlarsen
00051  * Conditionally set time stamp on/off
00052  *
00053  * Revision 1.19  2006/03/09 10:57:07  jmlarsen
00054  * Minor bugfix: #if -> #ifdef
00055  *
00056  * Revision 1.18  2006/02/28 09:15:22  jmlarsen
00057  * Minor update
00058  *
00059  * Revision 1.17  2006/02/21 14:26:54  jmlarsen
00060  * Minor changes
00061  *
00062  * Revision 1.16  2005/12/19 16:17:56  jmlarsen
00063  * Replaced bool -> int
00064  *
00065  */
00066 
00067 #ifdef HAVE_CONFIG_H
00068 #  include <config.h>
00069 #endif
00070 
00071 #include <uves_msg.h>
00072 
00073 #include <cpl.h>
00074 
00075 #include <stdarg.h>
00076 #include <stdio.h>
00077 
00078 /*----------------------------------------------------------------------------*/
00092 /*----------------------------------------------------------------------------*/
00093 
00094 #undef DEBUG_CALLER              /* Define whether to check consistency 
00095                     of msg_louder/softer calls */
00096 /* #define DEBUG_CALLER */
00097 
00098 #define MAXLEVEL 256
00099 #define MAXSTRINGLENGTH 1000
00100 
00101 
00102 static int level = 0;                 /* Current message & indentation level  from 0 to MAXLEVEL-1.
00103                      0 is the most verbose level. */
00104 static int outlevel = -1;             /* Only print message if level is in {0, 1, ..., outlevel}.
00105                      Always print if outlevel = - 1 */
00106 #ifdef DEBUG_CALLER
00107 static const char *callers[MAXLEVEL]; /* Check the consistency of calls to softer/louder  */
00108 #endif
00109 
00110 static char printbuffer[MAXSTRINGLENGTH]; /* Used to pass variable argument list 
00111                          to cpl_msg_info() */
00112 
00113 static const char *domain = "Undefined domain";
00114                                      /* This is to support getting the current domain 
00115                       * which is currently not available in CPL
00116                       */
00117 static bool initialized = false;
00118 
00119 static int number_of_warnings = 0;     /* Coun't the number of warnings 
00120                       since initialization */
00121 
00123 /*-----------------------------------------------------------------------------
00124                             Implementation
00125  -----------------------------------------------------------------------------*/
00126 //static void signal_handler(int signum)
00127 //{
00128 //    fprintf(stderr, "Panic! Signal %d caught, I'll just dump a trace and die\n", signum);
00129 //
00130 //    abort();
00131 //}
00132 
00133 /*----------------------------------------------------------------------------*/
00149 /*----------------------------------------------------------------------------*/
00150 void uves_msg_init(int olevel, const char *dom)
00151 {
00152     /* Initialize per recipe: */
00153     number_of_warnings = 0;
00154 
00155 //    signal(SIGSEGV, signal_handler);
00156 //    raise(SIGSEGV);
00157         
00158     if (!initialized)
00159     {
00160         /* Initialize once: */
00161         outlevel = olevel;
00162 
00163         cpl_msg_set_indentation(2);
00164         
00165         /*  CPL message format is
00166          *  [Time][Verbosity][domain][component] message
00167          *
00168          *  Don't show the (variable length and wildly
00169          *  fluctuating) component. It interferes with
00170          *  indentation. The component is available anyway
00171          *  on CPL_MSG_DEBUG level.
00172          *
00173          *  Don't show the time. This is available on
00174          *  the DEBUG level. Use esorex --time to time
00175          *  a recipe.
00176          */
00177 #if WANT_TIME_MEASURE
00178         cpl_msg_set_time_on();
00179 #else
00180         cpl_msg_set_time_off();
00181 #endif
00182         uves_msg_set_domain(dom);
00183         cpl_msg_set_domain_on();
00184         cpl_msg_set_component_off();
00185 
00186         initialized = true;
00187     }
00188 }
00189 
00190 
00191 /*----------------------------------------------------------------------------*/
00198 /*----------------------------------------------------------------------------*/
00199 void uves_msg_set_level(int olevel) 
00200 {
00201     outlevel = olevel; 
00202 } 
00203 
00204 /*----------------------------------------------------------------------------*/
00212 /*----------------------------------------------------------------------------*/
00213 void uves_msg_softer_macro(const char *fct)
00214 {
00215     if (level + 1 < MAXLEVEL)
00216     {
00217         level++;
00218         cpl_msg_indent_more();
00219 #ifdef DEBUG_CALLER
00220         callers[level] = fct;
00221 #else
00222         fct = fct; /* Satisfy compiler */
00223 #endif
00224         }
00225 }
00226 
00227 /*----------------------------------------------------------------------------*/
00235 /*----------------------------------------------------------------------------*/
00236 void uves_msg_louder_macro(const char *fct)
00237 {
00238     if (level == 0)
00239     {
00240         /* 0 is the loudest, ignore request */
00241         return;
00242     }
00243     
00244     /* Only make louder, if called from the same function which called
00245        uves_msg_softer. (disable check if level is more than MAXLEVEL)
00246     */
00247 #ifdef DEBUG_CALLER
00248     if (level >= MAXLEVEL || strcmp(callers[level], fct) == 0)
00249 #else
00250     fct = fct;              /* Satisfy compiler */
00251 #endif
00252     {
00253         level--;
00254         cpl_msg_indent_less();
00255     }
00256 #ifdef DEBUG_CALLER
00257     else
00258     {
00259         uves_msg_warning("Message level decreased by '%s' but increased by '%s'",
00260                  callers[level], fct);
00261     }
00262 #endif
00263 }
00264 
00265 /*----------------------------------------------------------------------------*/
00277 /*----------------------------------------------------------------------------*/
00278 void uves_msg_macro(const char *fct, const char *format, ...)
00279 {
00280     va_list al;
00281     
00282     va_start(al, format);
00283     vsnprintf(printbuffer, MAXSTRINGLENGTH - 1, format, al);
00284     va_end(al);
00285 
00286     printbuffer[MAXSTRINGLENGTH - 1] = '\0';
00287     
00288     if (outlevel < 0 || level <= outlevel)
00289     {
00290 //#undef cpl_msg_info
00291         cpl_msg_info(fct, "%s", printbuffer);
00292 //#define cpl_msg_info(...)  use__uves_msg__instead__of__cpl_msg_info
00293     }
00294     else
00295     {
00296         cpl_msg_debug(fct, "%s", printbuffer);
00297     }
00298 }
00299 
00300 /*----------------------------------------------------------------------------*/
00305 /*----------------------------------------------------------------------------*/
00306 int uves_msg_get_warnings(void)
00307 {
00308     return number_of_warnings;
00309 }
00310 
00311 /*----------------------------------------------------------------------------*/
00320 /*----------------------------------------------------------------------------*/
00321 void uves_msg_add_warnings(int n)
00322 {
00323     number_of_warnings += n;
00324 }
00325 
00326 
00327 /*----------------------------------------------------------------------------*/
00343 /*----------------------------------------------------------------------------*/
00344 void uves_msg_warning_macro(const char *fct, const char *format, ...)
00345 {
00346     va_list al;
00347     
00348     va_start(al, format);
00349     vsnprintf(printbuffer, MAXSTRINGLENGTH - 1, format, al);
00350     va_end(al);
00351 
00352     printbuffer[MAXSTRINGLENGTH - 1] = '\0';
00353     
00354     cpl_msg_warning(fct, "%s", printbuffer);
00355 
00356     number_of_warnings += 1;
00357 }
00358 
00359 /*----------------------------------------------------------------------------*/
00364 /*----------------------------------------------------------------------------*/
00365 const char *uves_msg_get_domain(void)
00366 {
00367     return domain;
00368 }
00369 
00370 /*----------------------------------------------------------------------------*/
00375 /*----------------------------------------------------------------------------*/
00376 void uves_msg_set_domain(const char *d)
00377 {
00378     /* Set domain and remember */
00379     cpl_msg_set_domain(d);
00380     domain = d;
00381 }
00382 

Generated on Fri Apr 18 14:11:42 2008 for UVES Pipeline Reference Manual by  doxygen 1.5.1