int rmb_send_log_for_error()

in eventmesh-sdks/eventmesh-sdk-c/src/message_log_api.c [200:378]


int rmb_send_log_for_error (stContextProxy * pContextProxy, int errCode,
                            char *errMsg, StRmbMsg * ptSendMsg)
{
  if (pRmbStConfig->iApiLogserverSwitch == 0)
  {
    return 0;
  }

  if (pContextProxy == NULL || NULL == ptSendMsg)
  {
    LOGRMB (RMB_LOG_ERROR, "pContextProxy or ptSendMsg is null");
    return -1;
  }

  StWemqThreadMsg stThreadMsg;
  memset (&stThreadMsg, 0x00, sizeof (StWemqThreadMsg));
  stThreadMsg.m_iCmd = THREAD_MSG_CMD_SEND_LOG;

  int iRet = -1;
  WEMQJSON *jsonHeader = json_object_new_object ();

  // 组装消息
  json_object_object_add (jsonHeader, MSG_HEAD_COMMAND_STR,
                          json_object_new_string (TRACE_LOG_TO_LOGSERVER));
  json_object_object_add (jsonHeader, MSG_HEAD_SEQ_INT,
                          json_object_new_int (0));
  json_object_object_add (jsonHeader, MSG_HEAD_CODE_INT,
                          json_object_new_int (0));

  WEMQJSON *jsonBody = json_object_new_object ();
  if (jsonBody == NULL)
  {
    LOGRMB (RMB_LOG_ERROR, "json_object_new_object return null");
    json_object_put (jsonHeader);
    return -1;
  }
  WEMQJSON *jsonMessage = json_object_new_object ();
  if (jsonMessage == NULL)
  {
    LOGRMB (RMB_LOG_ERROR, "json_object_new_object return null");
    json_object_put (jsonHeader);
    json_object_put (jsonBody);
    return -1;
  }
  char cTopic[128];
  char serviceOrEvent = (*(ptSendMsg->strServiceId + 3) == '0') ? 's' : 'e';
  snprintf (cTopic, sizeof (cTopic), "%s-%c-%s-%s-%c",
            ptSendMsg->strTargetDcn, serviceOrEvent, ptSendMsg->strServiceId,
            ptSendMsg->strScenarioId, *(ptSendMsg->strServiceId + 3));
  json_object_object_add (jsonMessage, MSG_BODY_TOPIC_STR,
                          json_object_new_string (cTopic));

  WEMQJSON *jsonBodyProperty =
    rmb_pub_encode_property_for_wemq (THREAD_MSG_CMD_SEND_LOG, ptSendMsg);
  if (jsonBodyProperty == NULL)
  {
    LOGRMB (RMB_LOG_ERROR, "rmb_pub_encode_property_for_wemq return null");
    json_object_put (jsonHeader);
    json_object_put (jsonMessage);
    json_object_put (jsonBody);
    return -1;
  }

  json_object_object_add (jsonMessage, MSG_BODY_PROPERTY_JSON,
                          jsonBodyProperty);

  WEMQJSON *jsonByteBody =
    rmb_pub_encode_byte_body_for_wemq (THREAD_MSG_CMD_SEND_LOG, ptSendMsg);
  if (jsonByteBody == NULL)
  {
    LOGRMB (RMB_LOG_ERROR, "rmb_pub_encode_byte_body_for_wemq return null");
    json_object_put (jsonHeader);
    json_object_put (jsonMessage);
    json_object_put (jsonBody);
    return -1;
  }
  const char *byteBodyStr = json_object_get_string (jsonByteBody);

  json_object_object_add (jsonMessage, MSG_BODY_BYTE_BODY_JSON,
                          json_object_new_string (byteBodyStr));

  const char *message_str = json_object_get_string (jsonMessage);
  if (message_str == NULL)
  {
    LOGRMB (RMB_LOG_ERROR, "Get thread msg body failed\n");
    json_object_put (jsonHeader);
    json_object_put (jsonMessage);
    json_object_put (jsonByteBody);
    json_object_put (jsonBody);

    return -1;
  }

  json_object_object_add (jsonBody, "retCode", json_object_new_int (errCode));
  json_object_object_add (jsonBody, "retMsg",
                          json_object_new_string (errMsg));
  json_object_object_add (jsonBody, "level",
                          json_object_new_string ("error"));
  json_object_object_add (jsonBody, "logPoint",
                          json_object_new_string (LOG_ERROR_POINT));
  json_object_object_add (jsonBody, "model",
                          json_object_new_string ("model"));
  json_object_object_add (jsonBody, "lang", json_object_new_string ("c"));
  json_object_object_add (jsonBody, "message",
                          json_object_new_string (message_str));

  const char *header_str = json_object_get_string (jsonHeader);

  if (header_str == NULL)
  {
    LOGRMB (RMB_LOG_ERROR, "json_object_get_string for header is null");
    json_object_put (jsonHeader);
    json_object_put (jsonMessage);
    json_object_put (jsonByteBody);
    json_object_put (jsonBody);
    return -2;
  }

  const char *body_str = json_object_get_string (jsonBody);
  if (body_str == NULL)
  {
    LOGRMB (RMB_LOG_ERROR, "Get thread msg body failed\n");
    json_object_put (jsonHeader);
    json_object_put (jsonMessage);
    json_object_put (jsonByteBody);
    json_object_put (jsonBody);
    return -1;
  }

  stThreadMsg.m_iHeaderLen = strlen (header_str);

  stThreadMsg.m_pHeader =
    (char *) malloc (stThreadMsg.m_iHeaderLen * sizeof (char) + 1);
  if (stThreadMsg.m_pHeader == NULL)
  {
    LOGRMB (RMB_LOG_ERROR, "malloc for m_pHeader failed, errno=%d", errno);
    json_object_put (jsonHeader);
    json_object_put (jsonMessage);
    json_object_put (jsonByteBody);
    json_object_put (jsonBody);
    return -1;
  }
  strncpy (stThreadMsg.m_pHeader, header_str, stThreadMsg.m_iHeaderLen);
  stThreadMsg.m_pHeader[stThreadMsg.m_iHeaderLen] = '\0';

  stThreadMsg.m_iBodyLen = strlen (body_str);
  stThreadMsg.m_pBody =
    (char *) malloc (stThreadMsg.m_iBodyLen * sizeof (char) + 1);
  if (stThreadMsg.m_pBody == NULL)
  {
    LOGRMB (RMB_LOG_ERROR, "malloc for hello body failed");
    json_object_put (jsonHeader);
    json_object_put (jsonMessage);
    json_object_put (jsonByteBody);
    json_object_put (jsonBody);
    return -1;
  }
  memcpy (stThreadMsg.m_pBody, body_str, stThreadMsg.m_iBodyLen);
  stThreadMsg.m_pBody[stThreadMsg.m_iBodyLen] = '\0';
  LOGRMB (RMB_LOG_INFO, "Gen error log succ, header :%s, body:%s", header_str,
          body_str);
  json_object_put (jsonHeader);
  json_object_put (jsonMessage);
  json_object_put (jsonByteBody);
  json_object_put (jsonBody);

  iRet = wemq_kfifo_put (&pContextProxy->pubFifo, stThreadMsg);
  if (iRet <= 0)
  {
    LOGRMB (RMB_LOG_ERROR, "wemq_kfifo_put error!,iRet=%d", iRet);
    rmb_errno = RMB_ERROR_WORKER_PUT_FIFO_ERROR;
    return -5;
  }
  else
  {
    LOGRMB (RMB_LOG_DEBUG, "error log msg  put into fofo");
    return 0;
  }
}