I’m looking for a tool which generates subroutine for checking a return code of some other subroutine.
For example, pthread_create can returns 0, EAGAIN, EINVAL and EPERM codes. It would be nice to have such checker:
void pthread_create_check(int retcode) {
switch (retcode) {
case 0:
printf("pthread_create success.\n");
break;
case EAGAIN:
printf("pthread_create EAGAIN error: insufficient resources"
" to create another thread, or a system-imposed"
" limit on the number of threads was encountered.\n");
break;
case EINVAL:
printf("pthread_create EINVAL error: invalid settings in"
" attr.\n");
break;
case EPERM:
printf("pthread_create EPERM error: no permission to set the"
" scheduling policy and parameters specified in"
" attr.\n");
break;
}
}
And use it in such manner:
iret = pthread_create(&thread_desc,
NULL,
thread_function,
(void *) thread_param);
pthread_create_check(iret);
There are explanation of each error code in man page. Creating such checker is nothing but copy-paste error codes and explanation from man page. I think that computer can done this job much better than human since computer never get tire. Also, I’m too lazy to do it for every subroutine call. Is there any automation tool?
Just make message tables. It will save coding time and space.
There’s nothing stopping you from sharing message lists between functions, so you can write as little or as much as you want.
If you’re insane, you can make a macro out of the call:
In this case, to share a message list means you need to create a pointer with the proper name for each additional function pointing to the list of the first function. Still a lot less work.
For the record, I just wrote one check function with generic messages (except for the success messages, they’re spammy) and used it everywhere in my C++ wrapper around pthread. (Don’t carp at me about Boost, this was ten years ago.)