morganstanley / binlog

A high performance C++ log library, producing structured binary logs
http://opensource.morganstanley.com/binlog/
Apache License 2.0
608 stars 72 forks source link

[BUG] Compile failed for packed attribute struct #144

Closed MashoujiangPlusAI closed 2 years ago

MashoujiangPlusAI commented 2 years ago

Hi everyone,

I don't want to be a nuisance :), but I meet another compiling issue for below code, which is a struct with packed attribute. And without the attribute, we could compile the code successfully. See below code or here demo

include <binlog/binlog.hpp>

typedef struct { uint32_t msgid; }__attribute_\((packed)) Message;

int main() { Message* message = new Message; BINLOG_INFO("Hello {}!", message->msg_id); return 0; }

image

erenon commented 2 years ago

This happens because GCC doesn't allow to form a reference to a packed field. Workaround for a single field:

  BINLOG_INFO("Hello {}!", std::uint32_t{message->msg_id});

To make the whole struct loggable with BINLOG_ADAPT_STRUCT, introduce getters, that simply return the field by value: http://binlog.org/UserGuide.html#logging-user-defined-structures

MashoujiangPlusAI commented 2 years ago

Got it, thank you very much!