【问题标题】:Logging enums on the pebble watch在 pebble watch 上记录枚举
【发布时间】:2014-01-15 23:06:58
【问题描述】:

当我像这样在 Pebble 上记录错误时:

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

我只是得到错误消息的 int 值。有没有一种简单的方法来记录枚举的文本?喜欢:

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"

【问题讨论】:

    标签: c debugging pebble-watch pebble-sdk


    【解决方案1】:

    没有 API 函数可以做到这一点。您可以将此函数用于AppMessageResult 枚举:

    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";
      }
    }
    

    我使用pebble analyze-size 命令来测量内存影响。此功能将花费您额外的 228 字节程序存储器。对于大多数开发人员来说,这可能是值得的;)

    这就是您将如何使用上述函数,例如在丢弃的消息处理程序中:

    static void appmsg_in_dropped(AppMessageResult reason, void *context) {
       APP_LOG(APP_LOG_LEVEL_DEBUG, "In dropped: %i - %s", reason, translate_error(reason));
    }
    

    【讨论】:

    • 这太棒了!但是,我该如何调用这个函数呢?我尝试从我的错误回调中调用 translate_error(),并尝试将 case 行也放入我的回调中。 (我认为这行不通,但我还是尝试了)。 t 不识别将 char 指针创建为函数名的语法,虽然我可以使用 char *translate_error 的指针和值来做事,但我似乎无法从任何地方调用 ,因此无法填充它与价值。我也试过: char errString = translate_error(AppMessageResult "%d"); APP_LOG(APP_LOG_LEVEL_DEBUG, errString);
    • 试试这个:APP_LOG(APP_LOG_LEVEL_DEBUG, "Got error: %s", translate_error(result));
    • 我在帖子中添加了一个示例。完整代码在这里可见:github.com/sarfata/pbsat/blob/master/src/comm.c
    猜你喜欢
    • 2010-10-07
    • 1970-01-01
    • 2012-11-24
    • 1970-01-01
    • 1970-01-01
    • 2014-09-03
    • 2012-08-15
    • 2014-03-02
    • 2014-10-07
    相关资源
    最近更新 更多