Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanly exit with valid error message on serial port error #37

Merged
merged 1 commit into from
Jan 9, 2022

Conversation

gunnbr
Copy link
Contributor

@gunnbr gunnbr commented Jan 9, 2022

Log serial port messages to a category level that is enabled by default
Update FAQ to explain how to fix serial port permission problems

Log serial port messages to a category level that is enabled by default
Update FAQ to explain how to fix serial port permission problems
enum sp_return result;

result = sp_open(m8_port, SP_MODE_READ_WRITE);
if (check(result) != SP_OK) return NULL;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since main.c is expecting a NULL return on error, I changed this to return NULL instead of exiting. This allows the program to cleanly close, flushing all buffers and closing all handles.

@@ -84,21 +96,22 @@ static int check(enum sp_return result) {

switch (result) {
case SP_ERR_ARG:
SDL_LogError(SDL_LOG_CATEGORY_SYSTEM, "Error: Invalid argument.\n");
abort();
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error: Invalid argument.\n");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't seeing any of these messages. According to SDL2/SDL_log.h, the default values log INFO and higher messages for APPLICATION category, but only CRITICAL or higher for (almost) everything else. So logging at SYSTEM level meant that these messages weren't being displayed by default, so I changed them.

@@ -84,21 +96,22 @@ static int check(enum sp_return result) {

switch (result) {
case SP_ERR_ARG:
SDL_LogError(SDL_LOG_CATEGORY_SYSTEM, "Error: Invalid argument.\n");
abort();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You rarely want to call abort() to exit a program--that's an "abnormal" process exit like it crashed and it even dumps a crash log. According to the man page, it doesn't even flush the open streams. It would have been better to call exit() here since that causes a normal program exit. That's the standard way to exit a program when you want to.

But even better (in my opinion) it to return an error so that the higher level callers are notified of the error and shutdown more gracefully, so that's what I've done here.

README.md Show resolved Hide resolved
README.md Show resolved Hide resolved
@laamaa laamaa merged commit a6c1854 into laamaa:main Jan 9, 2022
@laamaa
Copy link
Owner

laamaa commented Jan 9, 2022

Looks good to me, thank you :)

laamaa added a commit that referenced this pull request May 27, 2022
Cleanly exit with valid error message on serial port error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants