Palermo
Offline
Full Member
Karma: 0
Posts: 154
|
 |
« Reply #15 on: January 24, 2013, 07:00:18 pm » |
If you do nothing special... #include <assert.h>
void setup( void ) { assert( false ); }
void loop( void ) { }
abort is called if the assertion fails. stderr does not come into the picture. That's all clear to me. That's how I already used it after you suggested it to me. But ... that way, it does no more than calling abort(), right? Why should I even bother with calling assert() then? What's the point if it does'nt add anything new to the picture?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Sr. Member
Karma: 7
Posts: 419
|
 |
« Reply #16 on: January 24, 2013, 07:07:58 pm » |
You can disable it with the #define. Just a few posts up you said that was a good thing. 8^)
|
|
|
|
|
Logged
|
|
|
|
|
Temple, Texas
Offline
Sr. Member
Karma: 14
Posts: 354
|
 |
« Reply #17 on: January 24, 2013, 07:17:06 pm » |
But now that it has been brought up, *is* there any way to redirect stderr to Serial, and then enable with #define __ASSERT_USE_STDERR , like int std c++ you could do
std::cerr.rdbuf(myOStream.rdbuf());
?
|
|
|
|
|
Logged
|
|
|
|
|
UK
Offline
Tesla Member
Karma: 100
Posts: 6784
-
|
 |
« Reply #18 on: January 24, 2013, 07:37:18 pm » |
But now that it has been brought up, *is* there any way to redirect stderr to Serial, Yes, there is. You can associate stdin, stdout and stderr with the hardware Serial if you want (or any other device) by writing put and get functions for the device and then associating these functions with each stream by calls to fdevopen().
|
|
|
|
|
Logged
|
|
|
|
|
Offline
God Member
Karma: 9
Posts: 839
|
 |
« Reply #19 on: January 24, 2013, 07:55:53 pm » |
That's the most interesting piece of information I have seen all week.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Dallas
Online
Shannon Member
Karma: 129
Posts: 10379
|
 |
« Reply #20 on: January 24, 2013, 08:40:20 pm » |
Why should I even bother with calling assert() then? Welcome to "Design by Contract" ... http://en.wikipedia.org/wiki/Design_by_contract ... I highly recommend Bertrand Meyer's book.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Online
Shannon Member
Karma: 226
Posts: 14101
Lua rocks!
|
 |
« Reply #21 on: January 24, 2013, 08:42:09 pm » |
What does actually abort() do in Arduino?
From stdlib.h: /** The abort() function causes abnormal program termination to occur. This realization disables interrupts and jumps to _exit() function with argument equal to 1. In the limited AVR environment, execution is effectively halted by entering an infinite loop. */ extern void abort(void) __ATTR_NORETURN__;
A simple error handler could be: if (somethingBadHappens) { Serial.println (F("Bad thing XYZ happened.")); Serial.flush (); exit (1); }
The flush call will ensure the message appears before you go into the infinite loop. The F macro saves wasting valuable RAM on the message.
|
|
|
|
|
Logged
|
|
|
|
|
|
|
Global Moderator
Melbourne, Australia
Online
Shannon Member
Karma: 226
Posts: 14101
Lua rocks!
|
 |
« Reply #23 on: January 24, 2013, 08:49:11 pm » |
Actually I got bitten in one of my libraries because exit() isn't always implemented. This might be neater: if (somethingBadHappens) { Serial.println (F("Bad thing XYZ happened.")); Serial.flush (); noInterrupts (); while (true) { } // give up }
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Dallas
Online
Shannon Member
Karma: 129
Posts: 10379
|
 |
« Reply #24 on: January 24, 2013, 08:53:05 pm » |
This should get you started... #define __ASSERT_USE_STDERR #include <assert.h>
extern void __assert( const char *__func, const char *__file, int __lineno, const char *__sexp ) { // Put your stuff here. // __func is the function name (setup) // __file is the module name (the name of your sketch) // __lineno is the source code line number of the assert // __sexp is the assert expression (as a C string)
Serial.print( F( "ASSERT FAILURE: " ) ); Serial.print( __file ); Serial.print( F( ": " ) ); Serial.print( __func ); Serial.print( F( ": #" ) ); Serial.print( __lineno ); Serial.println(); Serial.flush (); noInterrupts (); while (true) { } // give up }
void setup( void ) { assert( false ); }
void loop( void ) { }
Bear in mind that each assert puts up to three string constants in SRAM. A better version would keep the string constants in Flash.
|
|
|
|
|
Logged
|
|
|
|
|
Pittsburgh, PA, USA
Offline
Faraday Member
Karma: 33
Posts: 3015
I only know some basic electricity....
|
 |
« Reply #25 on: January 24, 2013, 11:17:31 pm » |
Silly me, I thought I'd need an underlying OS to abort to! But then when I write code, I expect users to make the occasional error... since 1980.
|
|
|
|
|
Logged
|
Examples can be found at Learning in the Main Site and at the Playground
|
|
|
|
Global Moderator
Melbourne, Australia
Online
Shannon Member
Karma: 226
Posts: 14101
Lua rocks!
|
 |
« Reply #26 on: January 25, 2013, 12:09:38 am » |
Look on the bright side. Users expect programmers to make the occasional error, too. 
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 334
Posts: 36433
Seattle, WA USA
|
 |
« Reply #27 on: January 25, 2013, 12:24:25 am » |
Users expect programmers to make the occasional error, too. And I never disappoint them.
|
|
|
|
|
Logged
|
|
|
|
|
Pittsburgh, PA, USA
Offline
Faraday Member
Karma: 33
Posts: 3015
I only know some basic electricity....
|
 |
« Reply #28 on: January 25, 2013, 01:14:54 am » |
From the OLD (it was old in 85) Programmer's Joke Sheet:
Users should not complain about the code they get. They are lucky to get anything at all.
1980 is when I started writing programs for engineers. Error handling became a must!
|
|
|
|
|
Logged
|
Examples can be found at Learning in the Main Site and at the Playground
|
|
|
|
Palermo
Offline
Full Member
Karma: 0
Posts: 154
|
 |
« Reply #29 on: January 25, 2013, 12:10:40 pm » |
Users should not complain about the code they get. They are lucky to get anything at all.
Priceless!!! 
|
|
|
|
|
Logged
|
|
|
|
|
|