comparison src/error.cc @ 3934:31393822395b

[project @ 2002-05-15 19:18:09 by jwe]
author jwe
date Wed, 15 May 2002 19:18:09 +0000
parents c554ad71bafc
children 1ea29376e43e
comparison
equal deleted inserted replaced
3933:f9ea3dcf58ee 3934:31393822395b
55 55
56 // TRUE means that Octave will try to enter the debugger when a warning 56 // TRUE means that Octave will try to enter the debugger when a warning
57 // is encountered. 57 // is encountered.
58 static bool Vdebug_on_warning; 58 static bool Vdebug_on_warning;
59 59
60 // The text of the last warning message.
61 static std::string Vlast_warning_message;
62
63 // The warning frequency for Matlab handle graphics backwards
64 // compatibility warnings (currently not used).
65 static std::string Vwarning_frequency = "once";
66
67 // The current warning state. Valid values are "on", "off",
68 // "backtrace", or "debug".
69 static std::string Vwarning_option = "backtrace";
70
60 // Current error state. 71 // Current error state.
61 int error_state = 0; 72 int error_state = 0;
62 73
63 // Current warning state. 74 // Current warning state.
64 int warning_state = 0; 75 int warning_state = 0;
91 102
92 output_buf << std::endl << std::ends; 103 output_buf << std::endl << std::ends;
93 104
94 char *msg = output_buf.str (); 105 char *msg = output_buf.str ();
95 106
96 octave_diary << msg; 107 Vlast_warning_message = msg;
97 std::cerr << msg;
98 108
99 delete [] msg; 109 delete [] msg;
110
111 octave_diary << Vlast_warning_message;
112
113 std::cerr << Vlast_warning_message;
100 } 114 }
101 115
102 static void 116 static void
103 verror (const char *name, const char *fmt, va_list args) 117 verror (const char *name, const char *fmt, va_list args)
104 { 118 {
301 } 315 }
302 316
303 void 317 void
304 warning (const char *fmt, ...) 318 warning (const char *fmt, ...)
305 { 319 {
306 va_list args; 320 if (Vwarning_option != "off")
307 va_start (args, fmt); 321 {
308 warning_state = 1; 322 va_list args;
309 vwarning ("warning", fmt, args); 323 va_start (args, fmt);
310 va_end (args); 324 warning_state = 1;
311 325 vwarning ("warning", fmt, args);
312 pr_where ("warning"); 326 va_end (args);
313 327
314 if ((interactive || forced_interactive) 328 if (Vwarning_option == "backtrace")
315 && Vdebug_on_warning && curr_function) 329 pr_where ("warning");
316 { 330
317 unwind_protect_bool (Vdebug_on_warning); 331 if ((interactive || forced_interactive)
318 Vdebug_on_warning = false; 332 && Vdebug_on_warning && curr_function)
319 333 {
320 do_keyboard (octave_value_list ()); 334 unwind_protect_bool (Vdebug_on_warning);
321 335 Vdebug_on_warning = false;
322 unwind_protect::run (); 336
337 do_keyboard (octave_value_list ());
338
339 unwind_protect::run ();
340 }
323 } 341 }
324 } 342 }
325 343
326 void 344 void
327 error (const char *fmt, ...) 345 error (const char *fmt, ...)
372 390
373 typedef void (*error_fun)(const char *, ...); 391 typedef void (*error_fun)(const char *, ...);
374 392
375 extern octave_value_list Fsprintf (const octave_value_list&, int); 393 extern octave_value_list Fsprintf (const octave_value_list&, int);
376 394
377 static octave_value_list 395 static std::string
378 handle_message (error_fun f, const char *msg, const octave_value_list& args) 396 handle_message (error_fun f, const char *msg, const octave_value_list& args)
379 { 397 {
380 octave_value_list retval; 398 std::string retval;
381 399
382 std::string tstr; 400 std::string tstr;
383 401
384 int nargin = args.length (); 402 int nargin = args.length ();
385 403
418 if (len > 1) 436 if (len > 1)
419 { 437 {
420 char *tmp_msg = strsave (msg); 438 char *tmp_msg = strsave (msg);
421 tmp_msg[len - 1] = '\0'; 439 tmp_msg[len - 1] = '\0';
422 f ("%s\n", tmp_msg); 440 f ("%s\n", tmp_msg);
441 retval = tmp_msg;
423 delete [] tmp_msg; 442 delete [] tmp_msg;
424 } 443 }
425 } 444 }
426 else 445 else
427 f ("%s", msg); 446 {
447 f ("%s", msg);
448 retval = msg;
449 }
428 450
429 return retval; 451 return retval;
430 } 452 }
431 453
432 DEFUN (error, args, , 454 DEFUN (error, args, ,
483 error: nargin != 1\n\ 505 error: nargin != 1\n\
484 @end group\n\ 506 @end group\n\
485 @end example\n\ 507 @end example\n\
486 @end deftypefn") 508 @end deftypefn")
487 { 509 {
488 return handle_message (error, "unspecified error", args); 510 octave_value_list retval;
489 } 511 handle_message (error, "unspecified error", args);
490 512 return retval;
491 DEFUN (warning, args, , 513 }
514
515 static inline octave_value_list
516 set_warning_option (const std::string& state,
517 const std::string& frequency, int nargout)
518 {
519 octave_value_list retval;
520
521 if (nargout > 1)
522 retval(1) = Vwarning_frequency;
523
524 if (nargout > 0)
525 retval(0) = Vwarning_option;
526
527 if (! state.empty ())
528 Vwarning_option = state;
529
530 if (! frequency.empty ())
531 Vwarning_frequency = frequency;
532
533 return retval;
534 }
535
536 DEFUN_TEXT (warning, args, nargout,
492 "-*- texinfo -*-\n\ 537 "-*- texinfo -*-\n\
493 @deftypefn {Built-in Function} {} warning (@var{msg})\n\ 538 @deftypefn {Built-in Function} {} warning (@var{msg})\n\
494 Print a warning message @var{msg} prefixed by the string @samp{warning: }. \n\ 539 Print a warning message @var{msg} prefixed by the string @samp{warning: }. \n\
495 After printing the warning message, Octave will continue to execute\n\ 540 After printing the warning message, Octave will continue to execute\n\
496 commands. You should use this function when you want to notify the user\n\ 541 commands. You should use this function when you want to notify the user\n\
497 of an unusual condition, but only when it makes sense for your program\n\ 542 of an unusual condition, but only when it makes sense for your program\n\
498 to go on.\n\ 543 to go on.\n\
499 @end deftypefn") 544 @end deftypefn")
500 { 545 {
501 return handle_message (warning, "unspecified warning", args); 546 octave_value_list retval;
547
548 int argc = args.length () + 1;
549
550 string_vector argv = args.make_argv ("warning");
551
552 if (! error_state)
553 {
554 bool done = false;
555
556 if (argc == 1)
557 {
558 retval = set_warning_option ("", "", nargout);
559 done = true;
560 }
561 else if (argc == 2)
562 {
563 std::string arg = argv(1);
564
565 if (arg == "on" || arg == "off" || arg == "backtrace")
566 {
567 retval = set_warning_option (arg, "", nargout);
568 done = true;
569 }
570 else if (arg == "once" || arg == "always")
571 {
572 retval = set_warning_option ("", arg, nargout);
573 done = true;
574 }
575 else if (arg == "debug")
576 {
577 bind_builtin_variable ("debug_on_warning", 1.0);
578 retval = set_warning_option ("", "", nargout);
579 done = true;
580 }
581 }
582
583 if (! done)
584 {
585 std::string curr_msg
586 = handle_message (warning, "unspecified warning", args);
587
588 if (nargout > 0)
589 retval(0) = Vlast_warning_message;
590
591 Vlast_warning_message = curr_msg;
592 }
593 }
594
595 return retval;
596 }
597
598 DEFUN (lastwarn, args, ,
599 "-*- texinfo -*-\n\
600 @deftypefn {Built-in Function} {} lastwarn ()\n\
601 @deftypefnx {Built-in Function} {} lastwarn (@var{msg})\n\
602 Without any arguments, return the last error message. With one\n\
603 argument, set the last warning message to @var{msg}.\n\
604 @end deftypefn")
605 {
606 octave_value_list retval;
607
608 int argc = args.length () + 1;
609
610 string_vector argv = args.make_argv ("lastwarn");
611
612 if (argc == 1)
613 retval(0) = Vlast_warning_message;
614 else if (argc == 2)
615 Vlast_warning_message = argv(1);
616 else
617 print_usage ("lastwarn");
618
619 return retval;
502 } 620 }
503 621
504 DEFUN (usage, args, , 622 DEFUN (usage, args, ,
505 "-*- texinfo -*-\n\ 623 "-*- texinfo -*-\n\
506 @deftypefn {Built-in Function} {} usage (@var{msg})\n\ 624 @deftypefn {Built-in Function} {} usage (@var{msg})\n\
528 \n\ 646 \n\
529 @noindent\n\ 647 @noindent\n\
530 to check for the proper number of arguments.\n\ 648 to check for the proper number of arguments.\n\
531 @end deftypefn") 649 @end deftypefn")
532 { 650 {
533 return handle_message (usage, "unknown", args); 651 octave_value_list retval;
652 handle_message (usage, "unknown", args);
653 return retval;
534 } 654 }
535 655
536 void 656 void
537 bind_global_error_variable (void) 657 bind_global_error_variable (void)
538 { 658 {