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

Garbled log #1557

Open
Junsoo0607 opened this issue Dec 15, 2024 · 7 comments
Open

Garbled log #1557

Junsoo0607 opened this issue Dec 15, 2024 · 7 comments

Comments

@Junsoo0607
Copy link

Junsoo0607 commented Dec 15, 2024

Hello,
My project uses libcoap version 4.3.4. When calling the coap_send interface, it returns COAP_INVALID_MID. I noticed that there is a native print statement before printing the return value, but it displays as garbled text. Even though the log encoding is set to UTF-8, the log cannot be viewed. The code also calls the following two interfaces:

coap_set_log_level(LOG_ERR);
coap_set_log_handler(CoapResourceSaveLog);

image

@mrdeep1
Copy link
Collaborator

mrdeep1 commented Dec 15, 2024

You do not specify what platform / OS you are running your code on. It would appear that the strerror() function is returning the garbage that you are seeing. libcoap would be expecting the returned data from strerror() to be a null terminated ascii string as it is printing it with '%s'.

@Junsoo0607
Copy link
Author

You do not specify what platform / OS you are running your code on. It would appear that the strerror() function is returning the garbage that you are seeing. libcoap would be expecting the returned data from strerror() to be a null terminated ascii string as it is printing it with '%s'.

My code will run on Windows 11. Do you mean that I need to manually concatenate an empty string to the message parameter?

@mrdeep1
Copy link
Collaborator

mrdeep1 commented Dec 16, 2024

It looks like this code is causing your issues in src/coap_io.c

#ifdef _WIN32
const char *
coap_socket_format_errno(int error) {
  static char szError[256];
  if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
                    NULL, (DWORD)error, MAKELANGID(LANG_NEUTRAL,
                                                   SUBLANG_DEFAULT), (LPSTR)szError, (DWORD)sizeof(szError),
                    NULL) == 0)
    strcpy(szError, "Unknown error");
  return szError;
}

where I suspect for your locale it is returning WCHAR characters. Not immediately sure how to fix this.

@mrdeep1
Copy link
Collaborator

mrdeep1 commented Dec 17, 2024

Can you please try #1559 to see if this cures what you are seeing.

@mrdeep1
Copy link
Collaborator

mrdeep1 commented Jan 2, 2025

Can this be closed now?

@Junsoo0607
Copy link
Author

Can this be closed now?

I tried to add maintenance logs in the send_coap(), but I am currently unable to print any logs.
image

@mrdeep1
Copy link
Collaborator

mrdeep1 commented Jan 6, 2025

As you have set coap_set_log_level(LOG_ERR);, only log levels higher than COAP_LOG_ERR will get logged.

If you change coap_log_info("Junsoo test coap_send\n"); to coap_log_err("Junsoo test coap_send\n"); then you should get some log output.

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

No branches or pull requests

2 participants