Logging enums on the pebble watch
Asked Answered
K

1

10

When i log an error on the Pebble like this:

static void message_dropped(AppMessageResult reason, void *context) {
  APP_LOG(APP_LOG_LEVEL_DEBUG, "Message dropped. Reason: %d", reason);
}

i just get the int value of the error message. Is there an easy way to log the text of the enum? Like:

static void message_dropped(AppMessageResult reason, void *context) {
  APP_LOG(APP_LOG_LEVEL_DEBUG, "Message dropped. Reason: %fancy", reason);
}
// Would return "APP_MSG_BUFFER_OVERFLOW"
Koester answered 15/1, 2014 at 23:6 Comment(0)
G
19

There is no API function to do that. You can use this function for the AppMessageResult enum:

char *translate_error(AppMessageResult result) {
  switch (result) {
    case APP_MSG_OK: return "APP_MSG_OK";
    case APP_MSG_SEND_TIMEOUT: return "APP_MSG_SEND_TIMEOUT";
    case APP_MSG_SEND_REJECTED: return "APP_MSG_SEND_REJECTED";
    case APP_MSG_NOT_CONNECTED: return "APP_MSG_NOT_CONNECTED";
    case APP_MSG_APP_NOT_RUNNING: return "APP_MSG_APP_NOT_RUNNING";
    case APP_MSG_INVALID_ARGS: return "APP_MSG_INVALID_ARGS";
    case APP_MSG_BUSY: return "APP_MSG_BUSY";
    case APP_MSG_BUFFER_OVERFLOW: return "APP_MSG_BUFFER_OVERFLOW";
    case APP_MSG_ALREADY_RELEASED: return "APP_MSG_ALREADY_RELEASED";
    case APP_MSG_CALLBACK_ALREADY_REGISTERED: return "APP_MSG_CALLBACK_ALREADY_REGISTERED";
    case APP_MSG_CALLBACK_NOT_REGISTERED: return "APP_MSG_CALLBACK_NOT_REGISTERED";
    case APP_MSG_OUT_OF_MEMORY: return "APP_MSG_OUT_OF_MEMORY";
    case APP_MSG_CLOSED: return "APP_MSG_CLOSED";
    case APP_MSG_INTERNAL_ERROR: return "APP_MSG_INTERNAL_ERROR";
    default: return "UNKNOWN ERROR";
  }
}

I used the pebble analyze-size command to measure the memory impact. This function will cost you an extra 228 bytes of your program memory. It is probably worth it for most developers ;)

And this is how you would use the above function, for example in the dropped message handler:

static void appmsg_in_dropped(AppMessageResult reason, void *context) {
   APP_LOG(APP_LOG_LEVEL_DEBUG, "In dropped: %i - %s", reason, translate_error(reason));
}
Gwendolyn answered 16/1, 2014 at 20:30 Comment(3)
This is great! But, how do I call this function? I've tried calling translate_error() from my error callback, and tried putting the case lines in my callback as well. (I didn't think that would work, but I tried anyway). t don't recognize the syntax of creating a char pointer as a function name, and while I can do things with the pointer and value of char *translate_error, I can't seem to call the from anywhere and so can't populate it with the value. I also tried: char errString = translate_error(AppMessageResult "%d"); APP_LOG(APP_LOG_LEVEL_DEBUG, errString);Liberticide
Try this: APP_LOG(APP_LOG_LEVEL_DEBUG, "Got error: %s", translate_error(result));Gwendolyn
I have added an example in my post. Full code visible here: github.com/sarfata/pbsat/blob/master/src/comm.cGwendolyn

© 2022 - 2024 — McMap. All rights reserved.