add DumpMsgToLog

This commit is contained in:
aozhiwei 2020-06-12 19:06:28 +08:00
parent 6a5392433c
commit 6d4d04235b
4 changed files with 173 additions and 0 deletions

View File

@ -1,16 +1,47 @@
#include <a8/a8.h>
#include <a8/udplog.h>
#include <google/protobuf/message.h>
#include <google/protobuf/descriptor.h>
#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)
});
}
}

View File

@ -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

View File

@ -6,6 +6,7 @@
#include <a8/mysql.h>
#include <a8/openssl.h>
#include <a8/strutils.h>
#include <a8/mutable_xobject.h>
#include <google/protobuf/message.h>
@ -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<a8::XObject> 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<void (google::protobuf::Message*)> 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%';", {});

View File

@ -62,6 +62,8 @@ namespace f8
});
}
std::string PbToJson(const google::protobuf::Message* msg);
template <typename T1,
typename T2>
static void RepeatedFieldToVector(const T1& t1, T2& t2)