comparison src/error.cc @ 9753:892e2aa7bc75

improve error messages by auto-prepending current function name
author Jaroslav Hajek <highegg@gmail.com>
date Thu, 22 Oct 2009 08:56:58 +0200
parents ef45d191d833
children 2cd940306a06
comparison
equal deleted inserted replaced
9752:51c21837686f 9753:892e2aa7bc75
202 } 202 }
203 } 203 }
204 204
205 static void 205 static void
206 verror (bool save_last_error, std::ostream& os, 206 verror (bool save_last_error, std::ostream& os,
207 const char *name, const char *id, const char *fmt, va_list args) 207 const char *name, const char *id, const char *fmt, va_list args,
208 bool with_cfn = false)
208 { 209 {
209 if (discard_error_messages) 210 if (discard_error_messages)
210 return; 211 return;
211 212
212 if (! buffer_error_messages) 213 if (! buffer_error_messages)
229 if (to_beep_or_not_to_beep_p) 230 if (to_beep_or_not_to_beep_p)
230 msg_string = "\a"; 231 msg_string = "\a";
231 232
232 if (name) 233 if (name)
233 msg_string += std::string (name) + ": "; 234 msg_string += std::string (name) + ": ";
235
236 // If with_fcn is specified, we'll attempt to prefix the message with the name
237 // of the current executing function. But we'll do so only if:
238 // 1. the name is not empty (anonymous function)
239 // 2. it is not already there (including the following colon)
240 if (with_cfn)
241 {
242 octave_function *curfcn = octave_call_stack::current ();
243 if (curfcn)
244 {
245 std::string cfn = curfcn->name ();
246 if (! cfn.empty ())
247 {
248 cfn += ':';
249 if (cfn.length () > base_msg.length ()
250 || base_msg.compare (0, cfn.length (), cfn) != 0)
251 {
252 msg_string += cfn + ' ';
253 }
254 }
255 }
256 }
234 257
235 msg_string += base_msg + "\n"; 258 msg_string += base_msg + "\n";
236 259
237 if (! error_state && save_last_error) 260 if (! error_state && save_last_error)
238 { 261 {
273 // is just "" or "\n". This allows error ("") and error ("\n") to 296 // is just "" or "\n". This allows error ("") and error ("\n") to
274 // just set the error state. 297 // just set the error state.
275 298
276 static void 299 static void
277 error_1 (std::ostream& os, const char *name, const char *id, 300 error_1 (std::ostream& os, const char *name, const char *id,
278 const char *fmt, va_list args) 301 const char *fmt, va_list args, bool with_cfn = false)
279 { 302 {
280 if (error_state != -2) 303 if (error_state != -2)
281 { 304 {
282 if (fmt) 305 if (fmt)
283 { 306 {
291 { 314 {
292 if (len > 1) 315 if (len > 1)
293 { 316 {
294 char *tmp_fmt = strsave (fmt); 317 char *tmp_fmt = strsave (fmt);
295 tmp_fmt[len - 1] = '\0'; 318 tmp_fmt[len - 1] = '\0';
296 verror (true, os, name, id, tmp_fmt, args); 319 verror (true, os, name, id, tmp_fmt, args, with_cfn);
297 delete [] tmp_fmt; 320 delete [] tmp_fmt;
298 } 321 }
299 322
300 error_state = -2; 323 error_state = -2;
301 } 324 }
302 else 325 else
303 { 326 {
304 verror (true, os, name, id, fmt, args); 327 verror (true, os, name, id, fmt, args, with_cfn);
305 328
306 if (! error_state) 329 if (! error_state)
307 error_state = 1; 330 error_state = 1;
308 } 331 }
309 } 332 }
451 } 474 }
452 } 475 }
453 } 476 }
454 477
455 static void 478 static void
456 error_2 (const char *id, const char *fmt, va_list args) 479 error_2 (const char *id, const char *fmt, va_list args, bool with_cfn = false)
457 { 480 {
458 int init_state = error_state; 481 int init_state = error_state;
459 482
460 error_1 (std::cerr, "error", id, fmt, args); 483 error_1 (std::cerr, "error", id, fmt, args, with_cfn);
461 484
462 if ((interactive || forced_interactive) 485 if ((interactive || forced_interactive)
463 && Vdebug_on_error && init_state == 0 486 && Vdebug_on_error && init_state == 0
464 && octave_call_stack::caller_user_code ()) 487 && octave_call_stack::caller_user_code ())
465 { 488 {
490 verror (fmt, args); 513 verror (fmt, args);
491 va_end (args); 514 va_end (args);
492 } 515 }
493 516
494 void 517 void
518 verror_with_cfn (const char *fmt, va_list args)
519 {
520 error_2 ("", fmt, args, true);
521 }
522
523 void
524 error_with_cfn (const char *fmt, ...)
525 {
526 va_list args;
527 va_start (args, fmt);
528 verror_with_cfn (fmt, args);
529 va_end (args);
530 }
531
532 void
495 verror_with_id (const char *id, const char *fmt, va_list args) 533 verror_with_id (const char *id, const char *fmt, va_list args)
496 { 534 {
497 error_2 (id, fmt, args); 535 error_2 (id, fmt, args);
498 } 536 }
499 537
501 error_with_id (const char *id, const char *fmt, ...) 539 error_with_id (const char *id, const char *fmt, ...)
502 { 540 {
503 va_list args; 541 va_list args;
504 va_start (args, fmt); 542 va_start (args, fmt);
505 verror_with_id (id, fmt, args); 543 verror_with_id (id, fmt, args);
544 va_end (args);
545 }
546
547 void
548 verror_with_id_cfn (const char *id, const char *fmt, va_list args)
549 {
550 error_2 (id, fmt, args, true);
551 }
552
553 void
554 error_with_id_cfn (const char *id, const char *fmt, ...)
555 {
556 va_list args;
557 va_start (args, fmt);
558 verror_with_id_cfn (id, fmt, args);
506 va_end (args); 559 va_end (args);
507 } 560 }
508 561
509 static int 562 static int
510 check_state (const std::string& state) 563 check_state (const std::string& state)