I'm new to the software engineering side of C development; does anyone have a good guide on how to design an error tracking or error control system for a C project (especially embedded)? Something talking about error tracking for C libraries would be helpful too.
相关问题
- Multiple sockets for clients to connect to
- What is the best way to do a search in a large fil
- glDrawElements only draws half a quad
- Index of single bit in long integer (in C) [duplic
- Equivalent of std::pair in C
These links may be helpful:
In my experience the strategies here fall into a few camps.
Use of global variables ala errno. How this works is essentially any function can store an error code in the global variable, so after executing the function you can read the error code to see if it executed properly. There are some obvious issues here when working in a multithreaded environment. Although it appears that POSIX.1c specifies a solution to that problem.
Have every function return an error code. For example:
The problem with this approach is that you lose your ability to return values from the function directly.
Every function has an extra parameter that stores the result code. For example:
I've seen this approach used successfully in commercial RTOS, and personally prefer it since I find it the least restrictive. The only potential disadvantage is that you have to explicitly declare a variable to store the error code, even if you don't care about the result. In some sense I actually kind of like that requirement, though, since it forces you to be constantly thinking about how errors are going to be handled.
These are the basic things you will need to define:
So your error method will be something like this:
You could also have more parameters to indicate which module is sending the error, but I think that is basically it.