eclipse-cyclonedds / cyclonedds-cxx

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to change the log output from version C to version C++?

usstmiracle opened this issue · comments

As for cyclonedds-cxx
Is there a similar log API and how should it be used?
Is there an example of how to use the C++version log

#include "dds/dds.h"
#include "dds/ddsrt/log.h"
#include "HelloWorldData.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Log callback function
static void my_log_callback(void *ptr, const dds_log_data_t *data)
{
  printf("----------my_log_callback is called----------\n");
  printf("file:%s \n",data->file);
  printf("function:%s\n",data->function);
  printf("line: %d \n",data->line);
  printf("priority: %d \n",data->priority);
  printf("message:%s\n",data->message);
 
  // Cast ptr to FILE pointer
  FILE *log_fp = (FILE *)ptr; 
  if (log_fp != NULL) {
    fprintf(log_fp, "----------my_log_callback is called----------\n");
    fprintf(log_fp, "file:%s \n", data->file);
    fprintf(log_fp, "function:%s\n", data->function);
    fprintf(log_fp, "line: %d \n", data->line);
    fprintf(log_fp, "priority: %d \n", data->priority);
    fprintf(log_fp, "message:%s\n", data->message);
    fflush(log_fp); // Flush the stream
  }else{
    printf("cyclonedds.log file pointer is not valid \n");
  }
}
 
int main (int argc, char ** argv)
{
  dds_entity_t participant;
  dds_entity_t topic;
  dds_entity_t writer;
  dds_return_t rc;
  HelloWorldData_Msg msg;
  uint32_t status = 0;
  (void)argc;
  (void)argv;
 
  //add for log by lx
  FILE *log_fp = fopen("/opt/vrte/eclipse_cyclonedds/log/cyclonedds.log", "a");
  if (log_fp == NULL) {
      perror("can not open the log file\n");
      return EXIT_FAILURE;
  }
 
  printf("dds_set_log_sink\n");
  dds_set_log_sink(my_log_callback, log_fp);
 
  //dds_set_log_mask(DDS_LOG_MASK);
  //dds_set_log_mask(DDS_LC_FATAL | DDS_LC_ERROR | DDS_LC_WARNING | DDS_LC_WARNING |DDS_LC_INFO);
  dds_set_log_mask(DDS_LC_ERROR | DDS_LC_WARNING | DDS_LC_INFO);
  /* Create a Participant. */
  participant = dds_create_participant (DDS_DOMAIN_DEFAULT, NULL, NULL);
  if (participant < 0)
    DDS_FATAL("dds_create_participant: %s\n", dds_strretcode(-participant));
    printf("DDS_ERROR is called\n");
    DDS_ERROR("--------------------This is a error log message-------------------\n");
 
    printf("DDS_WARNING is called\n");
    DDS_WARNING("----------------This is a WARNING log message---------------------\n");
 
    printf("DDS_INFO is called\n");
    DDS_INFO("--------------------------This is a info log message-----------------\n");
 
    //The log message will be written to cyclonedds.log.${CYCLONEDDS_PID} file
    DDS_INFO("++++++++++++++++++This is a info log message+++++++++++++++++++++++");
 
  /* Create a Topic. */
  topic = dds_create_topic (
    participant, &HelloWorldData_Msg_desc, "HelloWorldData_Msg", NULL, NULL);
  if (topic < 0)
    DDS_FATAL("dds_create_topic: %s\n", dds_strretcode(-topic));

Is there a similar log API for cyclonedds cxx, and how should it be used? Is there an example of how to use the C++version log