sinfo_msg.c

00001 /*                                                                          *
00002  *   This file is part of the ESO SINFO 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: 2008/02/12 14:57:39 $
00023  * $Revision: 1.7 $
00024  * $Name: sinfo-2_2_5 $
00025  */
00026 
00027 #ifdef HAVE_CONFIG_H
00028 #  include <config.h>
00029 #endif
00030 
00031 #include <sinfo_msg.h>
00032 #include <cpl.h>
00033 #include <stdarg.h>
00034 #include <stdio.h>
00035 
00037 /*--------------------------------------------------------------------------*/
00050 /*---------------------------------------------------------------------------*/
00051 
00052 #define DEBUG_CALLER 0  /* Define whether to check consistency of 
00053                            msg_louder/softer calls */
00054 /* #define DEBUG_CALLER */
00055 
00056 #define MAXLEVEL 256
00057 #define MAXSTRINGLENGTH 1000
00058 
00059 static int level = 0;  /* Current message & indentation level 
00060                           from 0 to MAXLEVEL-1.
00061             0 is the most verbose level. */
00062 static int outlevel = -1; /* Only print message if level is 
00063                              in {0, 1, ..., outlevel}.
00064              Always print if outlevel = - 1 */
00065 #ifdef DEBUG_CALLER
00066 const char *sinfo_callers[MAXLEVEL]; /* Check the consistency of 
00067                                          calls to softer/louder  */
00068 #endif
00069 
00070 static char printbuffer[MAXSTRINGLENGTH]; /* Used to pass variable argument 
00071                                              list to cpl_msg_info() */
00072 
00073 static const char *domain = "Undefined domain";
00074                                      /* This is to support getting the 
00075                                         current domain 
00076                       * which is currently not available in CPL
00077                       */
00078 static int initialized = FALSE;
00079 
00080 static int number_of_warnings = 0;     /* Coun't the number of warnings since 
00081                                           initialization */
00082 
00083 /*--------------------------------------------------------------------------*/
00100 /*--------------------------------------------------------------------------*/
00101 void sinfo_msg_init(int olevel, const char *dom)
00102 {
00103     /* Initialize per recipe: */
00104     number_of_warnings = 0;
00105         
00106     if (!initialized)
00107     {
00108         /* Initialize once: */
00109         outlevel = olevel;
00110 
00111         cpl_msg_set_indentation(2);
00112         
00113         /*  CPL message format is
00114          *  [Time][Verbosity][domain][component] message
00115          *
00116          *  Don't show the (variable length and wildly
00117          *  fluctuating) component. It interferes with
00118          *  indentation. The component is available anyway
00119          *  on CPL_MSG_DEBUG level.
00120          */
00121         cpl_msg_set_time_on();
00122         sinfo_msg_set_domain(dom);
00123         cpl_msg_set_domain_on();
00124         cpl_msg_set_component_off();
00125 
00126         initialized = TRUE;
00127     }
00128 }
00129 
00130 
00131 /*---------------------------------------------------------------------------*/
00138 /*---------------------------------------------------------------------------*/
00139 void sinfo_msg_set_level(int olevel) 
00140 {
00141     outlevel = olevel; 
00142 } 
00143 
00144 /*---------------------------------------------------------------------------*/
00152 /*---------------------------------------------------------------------------*/
00153 void sinfo_msg_softer_macro(const char *fctid)
00154 {
00155     if (level + 1 < MAXLEVEL)
00156     {
00157         level++;
00158         cpl_msg_indent_more();
00159 #if DEBUG_CALLER
00160         sinfo_callers[level] = fctid;
00161 #else
00162         fctid = fctid; /* Satisfy compiler */
00163 #endif
00164         }
00165 }
00166 
00167 /*---------------------------------------------------------------------------*/
00175 /*---------------------------------------------------------------------------*/
00176 void sinfo_msg_louder_macro(const char *fctid)
00177 {
00178     if (level == 0)
00179     {
00180         /* 0 is the loudest, ignore request */
00181         return;
00182     }
00183     
00184     /* Only make louder, if called from the same function which called
00185        sinfo_msg_softer. (disable check if level is more than MAXLEVEL)
00186     */
00187 #if DEBUG_CALLER
00188     if (level >= MAXLEVEL || strcmp(sinfo_callers[level], fctid) == 0)
00189 #else
00190     fctid = fctid;              /* Satisfy compiler */
00191 #endif
00192     {
00193         level--;
00194         cpl_msg_indent_less();
00195     }
00196 #if DEBUG_CALLER
00197     else
00198     {
00199      sinfo_msg_warning("Message level decreased by '%s' but increased by '%s'",
00200                sinfo_callers[level], fctid);
00201     }
00202 #endif
00203 }
00204 
00205 /*---------------------------------------------------------------------------*/
00218 /*---------------------------------------------------------------------------*/
00219 void sinfo_msg_macro(const char *fct, const char *format, ...)
00220 {
00221     va_list al;
00222     
00223     va_start(al, format);
00224     vsnprintf(printbuffer, MAXSTRINGLENGTH - 1, format, al);
00225     va_end(al);
00226 
00227     printbuffer[MAXSTRINGLENGTH - 1] = '\0';
00228     
00229     if (outlevel < 0 || level <= outlevel)
00230     {
00231 /*
00232 #undef cpl_msg_info
00233 */
00234         cpl_msg_info(fct, "%s", printbuffer);
00235 /*
00236 #define cpl_msg_info(...)  use__sinfo_msg__instead__of__cpl_msg_info
00237 */
00238     }
00239     else
00240     {
00241         cpl_msg_debug(fct, "%s", printbuffer);
00242     }
00243 }
00244 
00245 /*---------------------------------------------------------------------------*/
00250 /*---------------------------------------------------------------------------*/
00251 int sinfo_msg_get_warnings(void)
00252 {
00253     return number_of_warnings;
00254 }
00255 
00256 /*---------------------------------------------------------------------------*/
00265 /*---------------------------------------------------------------------------*/
00266 void sinfo_msg_add_warnings(int n)
00267 {
00268     number_of_warnings += n;
00269 }
00270 
00271 
00272 /*---------------------------------------------------------------------------*/
00286 /*---------------------------------------------------------------------------*/
00287 void sinfo_msg_warning_macro(const char *fct, const char *format, ...)
00288 {
00289     va_list al;
00290     
00291     va_start(al, format);
00292     vsnprintf(printbuffer, MAXSTRINGLENGTH - 1, format, al);
00293     va_end(al);
00294 
00295     printbuffer[MAXSTRINGLENGTH - 1] = '\0';
00296     
00297     cpl_msg_warning(fct, "%s", printbuffer);
00298 
00299     number_of_warnings += 1;
00300 }
00301 
00302 /*---------------------------------------------------------------------------*/
00308 /*---------------------------------------------------------------------------*/
00309 const char *sinfo_msg_get_domain(void)
00310 {
00311     return domain;
00312 }
00313 
00314 /*---------------------------------------------------------------------------*/
00319 /*---------------------------------------------------------------------------*/
00320 void sinfo_msg_set_domain(const char *d)
00321 {
00322     /* Set domain and remember */
00323     cpl_msg_set_domain(d);
00324     domain = d;
00325 }
00326 

Generated on 8 Mar 2011 for SINFONI Pipeline Reference Manual by  doxygen 1.6.1