From 6d4d04235b27818b29d0f83206052de90ac8afd8 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Fri, 12 Jun 2020 19:06:28 +0800 Subject: [PATCH] add DumpMsgToLog --- cpp/netmsghandler.cc | 31 ++++++++++ cpp/netmsghandler.h | 3 + cpp/utils.cc | 137 +++++++++++++++++++++++++++++++++++++++++++ cpp/utils.h | 2 + 4 files changed, 173 insertions(+) diff --git a/cpp/netmsghandler.cc b/cpp/netmsghandler.cc index f124e87..00f5f57 100644 --- a/cpp/netmsghandler.cc +++ b/cpp/netmsghandler.cc @@ -1,16 +1,47 @@ #include +#include #include #include +#include "framework/cpp/utils.h" #include "framework/cpp/protoutils.h" #include "framework/cpp/netmsghandler.h" namespace f8 { + NetMsgHandler* GetNetMsgHandler(NetMsgHandlerObject* handlers, unsigned short msgid) { return msgid < MAX_MSG_ID ? handlers->handlers[msgid] : nullptr; } + + void DumpMsgToLog(f8::MsgHdr& hdr, f8::NetMsgHandler* handler, const char* prefix) + { + ::google::protobuf::Message* msg = handler->prototype->New(); + bool ok = msg->ParseFromArray(hdr.buf + hdr.offset, hdr.buflen - hdr.offset); + a8::UdpLog::Instance()->Debug + ( + "%s%s %s", + { + prefix, + msg->GetTypeName(), + f8::PbToJson(msg) + }); + delete msg; + } + + void DumpMsgToLog(const ::google::protobuf::Message& msg, const char* prefix) + { + a8::UdpLog::Instance()->Debug + ( + "%s%s %s", + { + prefix, + msg.GetTypeName(), + f8::PbToJson(&msg) + }); + } + } diff --git a/cpp/netmsghandler.h b/cpp/netmsghandler.h index d83cfaf..9c4e937 100644 --- a/cpp/netmsghandler.h +++ b/cpp/netmsghandler.h @@ -106,5 +106,8 @@ namespace f8 NetMsgHandler* GetNetMsgHandler(NetMsgHandlerObject* handlers, unsigned short msgid); + + void DumpMsgToLog(f8::MsgHdr& hdr, f8::NetMsgHandler* handler, const char* prefix); + void DumpMsgToLog(const ::google::protobuf::Message& msg, const char* prefix); } #endif diff --git a/cpp/utils.cc b/cpp/utils.cc index bbe2b56..b2b7201 100644 --- a/cpp/utils.cc +++ b/cpp/utils.cc @@ -6,6 +6,7 @@ #include #include #include +#include #include @@ -206,6 +207,132 @@ namespace f8 return true; } + static bool MessageToJson(const google::protobuf::Message* msg, a8::MutableXObject& jsonobj) + { + const google::protobuf::Descriptor* descriptor = msg->GetDescriptor(); + const google::protobuf::Reflection* reflection = msg->GetReflection(); + + for (int i = 0; i < descriptor->field_count(); ++i) { + const google::protobuf::FieldDescriptor* field_desc = descriptor->field(i); + const std::string& field_name = field_desc->name(); + + if (field_desc->is_repeated()) { + a8::MutableXObject* array_obj = a8::MutableXObject::NewArray(); + std::shared_ptr repeated_field = jsonobj.At(field_name); + for (int i = 0; i < repeated_field->Size(); ++i) { + auto field_value = repeated_field->At(i); + switch (field_desc->cpp_type()) { + case google::protobuf::FieldDescriptor::CPPTYPE_STRING: + { + array_obj->Push(reflection->GetString(*msg, field_desc)); + } + break; + case google::protobuf::FieldDescriptor::CPPTYPE_INT32: + { + array_obj->Push(reflection->GetInt32(*msg, field_desc)); + } + break; + case google::protobuf::FieldDescriptor::CPPTYPE_UINT32: + { + array_obj->Push(reflection->GetUInt32(*msg, field_desc)); + } + break; + case google::protobuf::FieldDescriptor::CPPTYPE_INT64: + { + array_obj->Push(reflection->GetInt64(*msg, field_desc)); + } + break; + case google::protobuf::FieldDescriptor::CPPTYPE_UINT64: + { + array_obj->Push(reflection->GetUInt64(*msg, field_desc)); + } + break; + case google::protobuf::FieldDescriptor::CPPTYPE_FLOAT: + { + array_obj->Push(reflection->GetFloat(*msg, field_desc)); + } + break; + case google::protobuf::FieldDescriptor::CPPTYPE_DOUBLE: + { + array_obj->Push(reflection->GetDouble(*msg, field_desc)); + } + break; + case google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE: + { + a8::MutableXObject* p = a8::MutableXObject::NewObject(); + MessageToJson(&reflection->GetMessage(*msg, field_desc), + *p + ); + array_obj->Push(*p); + delete p; + } + break; + default: + { + abort(); + } + break; + }//end switch + } + jsonobj.SetVal(field_name, *array_obj); + delete array_obj; + } else { + switch (field_desc->cpp_type()) { + case google::protobuf::FieldDescriptor::CPPTYPE_STRING: + { + jsonobj.SetVal(field_name, reflection->GetString(*msg, field_desc)); + } + break; + case google::protobuf::FieldDescriptor::CPPTYPE_INT32: + { + jsonobj.SetVal(field_name, reflection->GetInt32(*msg, field_desc)); + } + break; + case google::protobuf::FieldDescriptor::CPPTYPE_UINT32: + { + jsonobj.SetVal(field_name, reflection->GetUInt32(*msg, field_desc)); + } + break; + case google::protobuf::FieldDescriptor::CPPTYPE_INT64: + { + jsonobj.SetVal(field_name, reflection->GetInt64(*msg, field_desc)); + } + break; + case google::protobuf::FieldDescriptor::CPPTYPE_UINT64: + { + jsonobj.SetVal(field_name, reflection->GetUInt64(*msg, field_desc)); + } + break; + case google::protobuf::FieldDescriptor::CPPTYPE_FLOAT: + { + jsonobj.SetVal(field_name, reflection->GetFloat(*msg, field_desc)); + } + break; + case google::protobuf::FieldDescriptor::CPPTYPE_DOUBLE: + { + jsonobj.SetVal(field_name, reflection->GetDouble(*msg, field_desc)); + } + break; + case google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE: + { + a8::MutableXObject* p = a8::MutableXObject::NewObject(); + MessageToJson(&reflection->GetMessage(*msg, field_desc), + *p + ); + jsonobj.SetVal(field_name, *p); + } + break; + default: + { + abort(); + } + break; + }//end switch + } + } + return true; + } + bool ReadJsonMetaFile(const std::string& filename, google::protobuf::Message* prototype, std::function push_back_func) @@ -222,6 +349,16 @@ namespace f8 return true; } + std::string PbToJson(const google::protobuf::Message* msg) + { + std::string data; + a8::MutableXObject* p = a8::MutableXObject::NewObject(); + MessageToJson(msg, *p); + p->ToJsonStr(data); + delete p; + return data; + } + void InitMysqlConnection(a8::mysql::Query* query) { a8::UdpLog::Instance()->Info("show variables like 'character%';", {}); diff --git a/cpp/utils.h b/cpp/utils.h index fddb4b4..cd3baf6 100644 --- a/cpp/utils.h +++ b/cpp/utils.h @@ -62,6 +62,8 @@ namespace f8 }); } + std::string PbToJson(const google::protobuf::Message* msg); + template static void RepeatedFieldToVector(const T1& t1, T2& t2)