| 1 | /******************************************************************************* |
|---|
| 2 | * |
|---|
| 3 | * McStas, neutron ray-tracing package |
|---|
| 4 | * Copyright 1997-2002, All rights reserved |
|---|
| 5 | * Risoe National Laboratory, Roskilde, Denmark |
|---|
| 6 | * Institut Laue Langevin, Grenoble, France |
|---|
| 7 | * |
|---|
| 8 | * Kernel: debug.c |
|---|
| 9 | * |
|---|
| 10 | * %Identification |
|---|
| 11 | * Written by: K.N. |
|---|
| 12 | * Date: Jul 1, 1997 |
|---|
| 13 | * Origin: Risoe |
|---|
| 14 | * Release: McStas 1.6 |
|---|
| 15 | * Version: $Revision: 1.19 $ |
|---|
| 16 | * |
|---|
| 17 | * Support for conditional output of debugging information. |
|---|
| 18 | * |
|---|
| 19 | * $Id: debug.c,v 1.19 2006-04-19 13:06:25 farhi Exp $ |
|---|
| 20 | * |
|---|
| 21 | * $Log: debug.c,v $ |
|---|
| 22 | * Revision 1.19 2006-04-19 13:06:25 farhi |
|---|
| 23 | * * Updated Release, Version and Origin fields in headers |
|---|
| 24 | * * Improved setversion to update all McStasx.y occurencies into current release |
|---|
| 25 | * * Added 'string' type for DEFINITION parameters to be handled as this type so that auto-quoting occurs in mcgui |
|---|
| 26 | * * Added possibility to save log of the session to a file (appended) in mcgui |
|---|
| 27 | * * Made Scilab use either TCL_EvalStr or TK_EvalStr |
|---|
| 28 | * |
|---|
| 29 | * Revision 1.18 2003/02/11 12:28:45 farhi |
|---|
| 30 | * Variouxs bug fixes after tests in the lib directory |
|---|
| 31 | * mcstas_r : disable output with --no-out.. flag. Fix 1D McStas output |
|---|
| 32 | * read_table:corrected MC_SYS_DIR -> MCSTAS define |
|---|
| 33 | * monitor_nd-lib: fix Log(signal) log(coord) |
|---|
| 34 | * HOPG.trm: reduce 4000 points -> 400 which is enough and faster to resample |
|---|
| 35 | * Progress_bar: precent -> percent parameter |
|---|
| 36 | * CS: ---------------------------------------------------------------------- |
|---|
| 37 | * |
|---|
| 38 | * Revision 1.6 1999/03/18 07:29:10 kn |
|---|
| 39 | * Fix bug in print_warn(). |
|---|
| 40 | * |
|---|
| 41 | * Revision 1.5 1998/10/02 08:36:12 kn |
|---|
| 42 | * Fixed header comment. |
|---|
| 43 | * |
|---|
| 44 | * Revision 1.4 1998/10/01 11:44:33 kn |
|---|
| 45 | * Set a flag when print_error is called. |
|---|
| 46 | * |
|---|
| 47 | * Revision 1.3 1998/09/24 12:15:30 kn |
|---|
| 48 | * Added print_warn() function. |
|---|
| 49 | * |
|---|
| 50 | * Revision 1.2 1997/07/02 07:22:53 kn |
|---|
| 51 | * Error reporting functions. |
|---|
| 52 | * |
|---|
| 53 | * Revision 1.1 1997/07/01 08:17:57 kn |
|---|
| 54 | * Initial revision |
|---|
| 55 | * |
|---|
| 56 | *******************************************************************************/ |
|---|
| 57 | |
|---|
| 58 | #include <stdarg.h> |
|---|
| 59 | #include <stdio.h> |
|---|
| 60 | |
|---|
| 61 | #include "mcstas.h" |
|---|
| 62 | |
|---|
| 63 | /******************************************************************************* |
|---|
| 64 | * Error messages. |
|---|
| 65 | *******************************************************************************/ |
|---|
| 66 | int error_encountered = 0; |
|---|
| 67 | |
|---|
| 68 | void |
|---|
| 69 | print_error(char *format, ...) |
|---|
| 70 | { |
|---|
| 71 | va_list ap; |
|---|
| 72 | |
|---|
| 73 | va_start(ap, format); |
|---|
| 74 | vfprintf(stderr, format, ap); |
|---|
| 75 | va_end(ap); |
|---|
| 76 | error_encountered = 1; |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | /* Print a warning message. May optionally take a pointer to a flag of type |
|---|
| 81 | * int; this should be NULL or point to a variable that is initialized to |
|---|
| 82 | * zero. It the flag is given, the warning will only be displayed the first |
|---|
| 83 | * time this function is called. |
|---|
| 84 | */ |
|---|
| 85 | void |
|---|
| 86 | print_warn(int *flag, char *format, ...) |
|---|
| 87 | { |
|---|
| 88 | va_list ap; |
|---|
| 89 | |
|---|
| 90 | va_start(ap, format); |
|---|
| 91 | if(flag == NULL || *flag == 0) |
|---|
| 92 | { |
|---|
| 93 | fprintf(stderr, "Warning: "); |
|---|
| 94 | vfprintf(stderr, format, ap); |
|---|
| 95 | if(flag) |
|---|
| 96 | *flag = 1; |
|---|
| 97 | } |
|---|
| 98 | va_end(ap); |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | /******************************************************************************* |
|---|
| 103 | * Fatal errors. These cause the program to terminate immediately. This is not |
|---|
| 104 | * very user friendly, so it should be avoided if possible. However, it is |
|---|
| 105 | * useful for such things as failed memory allocations of small sizes that are |
|---|
| 106 | * a pain to handle correctly and extremely unlikely to occur in modern |
|---|
| 107 | * virtual memory-capable systems. |
|---|
| 108 | * |
|---|
| 109 | * Outputs a message passed in printf-style to stderr and exits. |
|---|
| 110 | *******************************************************************************/ |
|---|
| 111 | void |
|---|
| 112 | fatal_error(char *format, ...) |
|---|
| 113 | { |
|---|
| 114 | va_list ap; |
|---|
| 115 | |
|---|
| 116 | va_start(ap, format); |
|---|
| 117 | fprintf(stderr, "\n\nFatal error: "); |
|---|
| 118 | vfprintf(stderr, format, ap); |
|---|
| 119 | fprintf(stderr, "\n\nProgram aborted.\n"); |
|---|
| 120 | va_end(ap); |
|---|
| 121 | |
|---|
| 122 | exit(1); |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | #ifdef DEBUG |
|---|
| 127 | |
|---|
| 128 | int debug_current_level = DEBUG; |
|---|
| 129 | |
|---|
| 130 | /******************************************************************************* |
|---|
| 131 | * Output debug information, printf-style. Only included when DEBUG is |
|---|
| 132 | * defined. |
|---|
| 133 | *******************************************************************************/ |
|---|
| 134 | void |
|---|
| 135 | debug_printf(char *format, ...) |
|---|
| 136 | { |
|---|
| 137 | va_list ap; |
|---|
| 138 | |
|---|
| 139 | va_start(ap, format); |
|---|
| 140 | vfprintf(stderr, format, ap); |
|---|
| 141 | va_end(ap); |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | /******************************************************************************* |
|---|
| 145 | * Like 'debug_printf', but only produce output if the current debugging level |
|---|
| 146 | * is greater than or equal to the first argument. |
|---|
| 147 | *******************************************************************************/ |
|---|
| 148 | void |
|---|
| 149 | debugn_printf(int level, char *format, ...) |
|---|
| 150 | { |
|---|
| 151 | va_list ap; |
|---|
| 152 | |
|---|
| 153 | if(level <= debug_current_level) |
|---|
| 154 | { |
|---|
| 155 | va_start(ap, format); |
|---|
| 156 | vfprintf(stderr, format, ap); |
|---|
| 157 | va_end(ap); |
|---|
| 158 | } |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | #endif /* DEBUG */ |
|---|