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: 2010/09/24 09:32:04 $ 00023 * $Revision: 1.30 $ 00024 * $Name: uves-4_9_1 $ 00025 * $Log: uves_msg.c,v $ 00026 * Revision 1.30 2010/09/24 09:32:04 amodigli 00027 * put back QFITS dependency to fix problem spot by NRI on FIBER mode (with MIDAS calibs) data 00028 * 00029 * Revision 1.28 2007/06/06 08:17:33 amodigli 00030 * replace tab with 4 spaces 00031 * 00032 * Revision 1.27 2007/05/23 13:03:19 jmlarsen 00033 * Added missing include directive 00034 * 00035 * Revision 1.26 2007/01/10 12:38:22 jmlarsen 00036 * Added commented out signal handling code 00037 * 00038 * Revision 1.25 2006/09/06 14:44:55 jmlarsen 00039 * Added documentation about non-use of the cpl_error_code 00040 * 00041 * Revision 1.24 2006/08/17 13:56:53 jmlarsen 00042 * Reduced max line length 00043 * 00044 * Revision 1.23 2006/08/11 14:56:05 amodigli 00045 * removed Doxygen warnings 00046 * 00047 * Revision 1.22 2006/08/11 11:29:26 jmlarsen 00048 * Added explicit void at function definition 00049 * 00050 * Revision 1.21 2006/06/01 14:43:17 jmlarsen 00051 * Added missing documentation 00052 * 00053 * Revision 1.20 2006/03/24 14:13:11 jmlarsen 00054 * Conditionally set time stamp on/off 00055 * 00056 * Revision 1.19 2006/03/09 10:57:07 jmlarsen 00057 * Minor bugfix: #if -> #ifdef 00058 * 00059 * Revision 1.18 2006/02/28 09:15:22 jmlarsen 00060 * Minor update 00061 * 00062 * Revision 1.17 2006/02/21 14:26:54 jmlarsen 00063 * Minor changes 00064 * 00065 * Revision 1.16 2005/12/19 16:17:56 jmlarsen 00066 * Replaced bool -> int 00067 * 00068 */ 00069 00070 #ifdef HAVE_CONFIG_H 00071 # include <config.h> 00072 #endif 00073 00074 #include <uves_msg.h> 00075 00076 #include <cpl.h> 00077 00078 #include <stdarg.h> 00079 #include <stdio.h> 00080 00081 /*----------------------------------------------------------------------------*/ 00095 /*----------------------------------------------------------------------------*/ 00096 00097 #undef DEBUG_CALLER /* Define whether to check consistency 00098 of msg_louder/softer calls */ 00099 /* #define DEBUG_CALLER */ 00100 00101 #define MAXLEVEL 256 00102 #define MAXSTRINGLENGTH 1000 00103 00104 00105 static int level = 0; /* Current message & indentation level from 0 to MAXLEVEL-1. 00106 0 is the most verbose level. */ 00107 static int outlevel = -1; /* Only print message if level is in {0, 1, ..., outlevel}. 00108 Always print if outlevel = - 1 */ 00109 #ifdef DEBUG_CALLER 00110 static const char *callers[MAXLEVEL]; /* Check the consistency of calls to softer/louder */ 00111 #endif 00112 00113 static char printbuffer[MAXSTRINGLENGTH]; /* Used to pass variable argument list 00114 to cpl_msg_info() */ 00115 00116 static const char *domain = "Undefined domain"; 00117 /* This is to support getting the current domain 00118 * which is currently not available in CPL 00119 */ 00120 static bool initialized = false; 00121 00122 static int number_of_warnings = 0; /* Coun't the number of warnings 00123 since initialization */ 00124 00126 /*----------------------------------------------------------------------------- 00127 Implementation 00128 -----------------------------------------------------------------------------*/ 00129 //static void signal_handler(int signum) 00130 //{ 00131 // fprintf(stderr, "Panic! Signal %d caught, I'll just dump a trace and die\n", signum); 00132 // 00133 // abort(); 00134 //} 00135 00136 /*----------------------------------------------------------------------------*/ 00152 /*----------------------------------------------------------------------------*/ 00153 void uves_msg_init(int olevel, const char *dom) 00154 { 00155 /* Initialize per recipe: */ 00156 number_of_warnings = 0; 00157 00158 // signal(SIGSEGV, signal_handler); 00159 // raise(SIGSEGV); 00160 00161 if (!initialized) 00162 { 00163 /* Initialize once: */ 00164 outlevel = olevel; 00165 00166 cpl_msg_set_indentation(2); 00167 00168 /* CPL message format is 00169 * [Time][Verbosity][domain][component] message 00170 * 00171 * Don't show the (variable length and wildly 00172 * fluctuating) component. It interferes with 00173 * indentation. The component is available anyway 00174 * on CPL_MSG_DEBUG level. 00175 * 00176 * Don't show the time. This is available on 00177 * the DEBUG level. Use esorex --time to time 00178 * a recipe. 00179 */ 00180 #if WANT_TIME_MEASURE 00181 cpl_msg_set_time_on(); 00182 #else 00183 cpl_msg_set_time_off(); 00184 #endif 00185 uves_msg_set_domain(dom); 00186 cpl_msg_set_domain_on(); 00187 cpl_msg_set_component_off(); 00188 00189 initialized = true; 00190 } 00191 } 00192 00193 00194 /*----------------------------------------------------------------------------*/ 00201 /*----------------------------------------------------------------------------*/ 00202 void uves_msg_set_level(int olevel) 00203 { 00204 outlevel = olevel; 00205 } 00206 00207 /*----------------------------------------------------------------------------*/ 00215 /*----------------------------------------------------------------------------*/ 00216 void uves_msg_softer_macro(const char *fct) 00217 { 00218 if (level + 1 < MAXLEVEL) 00219 { 00220 level++; 00221 cpl_msg_indent_more(); 00222 #ifdef DEBUG_CALLER 00223 callers[level] = fct; 00224 #else 00225 fct = fct; /* Satisfy compiler */ 00226 #endif 00227 } 00228 } 00229 00230 /*----------------------------------------------------------------------------*/ 00238 /*----------------------------------------------------------------------------*/ 00239 void uves_msg_louder_macro(const char *fct) 00240 { 00241 if (level == 0) 00242 { 00243 /* 0 is the loudest, ignore request */ 00244 return; 00245 } 00246 00247 /* Only make louder, if called from the same function which called 00248 uves_msg_softer. (disable check if level is more than MAXLEVEL) 00249 */ 00250 #ifdef DEBUG_CALLER 00251 if (level >= MAXLEVEL || strcmp(callers[level], fct) == 0) 00252 #else 00253 fct = fct; /* Satisfy compiler */ 00254 #endif 00255 { 00256 level--; 00257 cpl_msg_indent_less(); 00258 } 00259 #ifdef DEBUG_CALLER 00260 else 00261 { 00262 uves_msg_warning("Message level decreased by '%s' but increased by '%s'", 00263 callers[level], fct); 00264 } 00265 #endif 00266 } 00267 00268 /*----------------------------------------------------------------------------*/ 00280 /*----------------------------------------------------------------------------*/ 00281 void uves_msg_macro(const char *fct, const char *format, ...) 00282 { 00283 va_list al; 00284 00285 va_start(al, format); 00286 vsnprintf(printbuffer, MAXSTRINGLENGTH - 1, format, al); 00287 va_end(al); 00288 00289 printbuffer[MAXSTRINGLENGTH - 1] = '\0'; 00290 00291 if (outlevel < 0 || level <= outlevel) 00292 { 00293 //#undef cpl_msg_info 00294 cpl_msg_info(fct, "%s", printbuffer); 00295 //#define cpl_msg_info(...) use__uves_msg__instead__of__cpl_msg_info 00296 } 00297 else 00298 { 00299 cpl_msg_debug(fct, "%s", printbuffer); 00300 } 00301 } 00302 00303 /*----------------------------------------------------------------------------*/ 00308 /*----------------------------------------------------------------------------*/ 00309 int uves_msg_get_warnings(void) 00310 { 00311 return number_of_warnings; 00312 } 00313 00314 /*----------------------------------------------------------------------------*/ 00323 /*----------------------------------------------------------------------------*/ 00324 void uves_msg_add_warnings(int n) 00325 { 00326 number_of_warnings += n; 00327 } 00328 00329 00330 /*----------------------------------------------------------------------------*/ 00346 /*----------------------------------------------------------------------------*/ 00347 void uves_msg_warning_macro(const char *fct, const char *format, ...) 00348 { 00349 va_list al; 00350 00351 va_start(al, format); 00352 vsnprintf(printbuffer, MAXSTRINGLENGTH - 1, format, al); 00353 va_end(al); 00354 00355 printbuffer[MAXSTRINGLENGTH - 1] = '\0'; 00356 00357 cpl_msg_warning(fct, "%s", printbuffer); 00358 00359 number_of_warnings += 1; 00360 } 00361 00362 /*----------------------------------------------------------------------------*/ 00367 /*----------------------------------------------------------------------------*/ 00368 const char *uves_msg_get_domain(void) 00369 { 00370 return domain; 00371 } 00372 00373 /*----------------------------------------------------------------------------*/ 00378 /*----------------------------------------------------------------------------*/ 00379 void uves_msg_set_domain(const char *d) 00380 { 00381 /* Set domain and remember */ 00382 cpl_msg_set_domain(d); 00383 domain = d; 00384 } 00385