浏览代码

convert cout to glog

youchen 6 年之前
父节点
当前提交
be635eb0a4
共有 4 个文件被更改,包括 18 次插入11 次删除
  1. 2 2
      CMakeLists.txt
  2. 12 8
      plc/plc_communicator.cpp
  3. 1 0
      plc/plc_communicator.h
  4. 3 1
      test/plc_test.cpp

+ 2 - 2
CMakeLists.txt

@@ -39,12 +39,12 @@ aux_source_directory(${CMAKE_CURRENT_LIST_DIR}/error_code ERROR_CODE_SRC )
 
 add_executable(LidarMeasure ./main.cpp ${PLC_SRC} ${TASK_MANAGER_SRC} ${TOOL_SRC} ${ERROR_CODE_SRC})
 target_link_libraries(LidarMeasure ${OpenCV_LIBS}
-        ${GLOG_LIBRARIES} ${PCL_LIBRARIES} ${PROTOBUF_LIBRARIES} ipopt modbus libnanomsg.so libnnxx.a)
+        ${GLOG_LIBRARIES} ${PCL_LIBRARIES} ${PROTOBUF_LIBRARIES} ipopt modbus libnanomsg.so libnnxx.a libglog.a libgflags.a)
 
 # plc test module
 add_executable(plc_test  ./test/plc_test.cpp ${PLC_SRC} ${TASK_MANAGER_SRC} ${TOOL_SRC} ${ERROR_CODE_SRC})
 target_link_libraries(plc_test ${OpenCV_LIBS}
-        ${GLOG_LIBRARIES} ${PCL_LIBRARIES} ${PROTOBUF_LIBRARIES} ipopt modbus libnanomsg.so libnnxx.a )
+        ${GLOG_LIBRARIES} ${PCL_LIBRARIES} ${PROTOBUF_LIBRARIES} ipopt modbus libnanomsg.so libnnxx.a libglog.a libgflags.a)
 
 
 #add_executable(LidarMeasure ./main.cpp ${LASER_SRC} ${PLC_SRC} ${TERMINOR_SRC} ${LOCATE_SRC} ${TASK_MANAGER_SRC})

+ 12 - 8
plc/plc_communicator.cpp

@@ -46,10 +46,10 @@ bool Plc_Communicator::get_connection()
 }
 
 Error_manager Plc_Communicator::get_error(){
-    if(mb_plc_is_connected){
-        return Error_manager(Error_code::SUCCESS, Error_level::NORMAL, "连接正常");
+    if(mb_plc_initialized && mb_plc_is_connected){
+        return Error_manager(Error_code::SUCCESS, Error_level::NORMAL, "状态正常");
     }else{
-        return Error_manager(Error_code::PLC_CONNECTION_FAILED, Error_level::NEGLIGIBLE_ERROR, "连接失败");
+        return Error_manager(Error_code::PLC_CONNECTION_FAILED, Error_level::NEGLIGIBLE_ERROR, "状态异常");
     }
 }
 
@@ -119,10 +119,10 @@ Error_manager Plc_Communicator::execute_task(Task_Base *task)
     }
     Plc_Task *plc_task_temp = (Plc_Task *)task;
     plc_task_temp->update_statu(Task_statu::TASK_SIGNED, "received by plc_communicator.");
-    std::cout << " execute task, update status " << std::endl;
+    LOG(INFO) << " execute task, update status ";
     struct measure_result measure_result_temp;
     Error_manager err = plc_task_temp->get_result(measure_result_temp);
-    std::cout << " execute task, get result " << std::endl;
+    LOG(INFO) << " execute task, get result ";
     if (err.is_equal_error_manager(Error_manager(Error_code::SUCCESS)))
     {
         Error_manager write_err = write_result_to_plc(measure_result_temp);
@@ -227,7 +227,7 @@ void Plc_Communicator::plc_update_thread(Plc_Communicator *plc_communicator)
             plc_module::Plc_msg plc_msg;
             int rc = plc_communicator->m_plc_wrapper.read_registers(PLC_SIGNAL_BEGIN_OFFSET, plc_length_temp, plc_data_temp);
             if(rc == -2){
-                std::cout<<"find plc disconnected while read. try to reconnect."<<std::endl;
+                LOG(WARNING)<<"find plc disconnected while read. try to reconnect.";
                 plc_communicator->mb_plc_is_connected = false;
                 plc_msg.set_status(plc_module::PLC_STATUS::ePLCDisconnected);
                 MeasureTopicPublisher* publisher = MeasureTopicPublisher::GetInstance();
@@ -294,8 +294,10 @@ void Plc_Communicator::plc_update_thread(Plc_Communicator *plc_communicator)
                             int address_temp = PLC_SIGNAL_BEGIN_OFFSET + PLC_SIGNAL_NUM_PER_REGION * terminal_id_temp + PLC_LASER_STATUS_ADDR;
                             uint16_t value_temp = plc_communicator->m_plc_region_status[terminal_id_temp].current_status;
                             int rc = plc_communicator->m_plc_wrapper.write_registers(address_temp, 1, &value_temp);
-                            if (rc != 0)
+                            if (rc != 0){
+                                LOG(ERROR)<<"状态1写入PLC失败";
                                 plc_communicator->m_plc_current_error = Error_manager(Error_code::PLC_WRITE_FAILED, Error_level::MINOR_ERROR, "写入状态1失败");
+                            }
                         }
                     }
                 }
@@ -381,8 +383,10 @@ void Plc_Communicator::plc_update_thread(Plc_Communicator *plc_communicator)
                     int address_temp = PLC_SIGNAL_BEGIN_OFFSET + PLC_SIGNAL_NUM_PER_REGION * i + PLC_LASER_STATUS_ADDR;
                     uint16_t value_temp = plc_communicator->m_plc_region_status[i].current_status;
                     int rc = plc_communicator->m_plc_wrapper.write_registers(address_temp, 1, &value_temp);
-                    if (rc != 0)
+                    if (rc != 0){
+                        LOG(ERROR)<<i+1<<"号写入当前状态"<<value_temp<<"失败";
                         plc_communicator->m_plc_current_error = Error_manager(Error_code::PLC_WRITE_FAILED, Error_level::MINOR_ERROR, "写入当前状态失败");
+                    }
                     plc_communicator->m_plc_mutex.unlock();
                     // std::cout<<" thread 555 "<<std::endl;
                 }

+ 1 - 0
plc/plc_communicator.h

@@ -35,6 +35,7 @@ using google::protobuf::io::CodedInputStream;
 using google::protobuf::io::ZeroCopyOutputStream;
 using google::protobuf::io::CodedOutputStream;
 using google::protobuf::Message;
+#include "glog/logging.h"
 
 #include <nnxx/message.h>
 #include <nnxx/message_control.h>

+ 3 - 1
test/plc_test.cpp

@@ -37,7 +37,7 @@ public:
             Error_manager ec = t->mp_pc->execute_task(&t->m_task);
             std::cout << "thread execute task " << ec.to_string() << std::endl;
 
-            int count = 2;
+            int count = 1;
             while (count-- > 0)
             {
                 std::cout << "---------- "<<count<<" --------" << std::endl;
@@ -74,6 +74,7 @@ private:
 
 int main(int argc,char* argv[])
 {
+    google::InitGoogleLogging("plc");
     plc_module::plc_connection_params params;
     params.set_ip("192.168.2.131");
     params.set_port(502);
@@ -92,5 +93,6 @@ int main(int argc,char* argv[])
     // std::cout<<"test "<<ec.to_string()<<std::endl;
 
     getchar();
+    google::ShutdownGoogleLogging();
     return 0;
 }