Parcourir la source

输出写入到日志

leon il y a 4 semaines
Parent
commit
9177015955

+ 1 - 1
src/main.cpp

@@ -120,7 +120,7 @@ void test_multi()
 int main()
 {
     plog::init(plog::info, "log/vsp.log", 1000000, 5);
-    PLOGI << "Start pipepline test_yolo";
+    PLOGI.printf("Start pipepline test_yolo");
     test_yolo();
     
     return 0;

+ 1 - 1
src/nodes/base/base.hpp

@@ -103,7 +103,7 @@ protected:
 static inline void LinkNode(const std::shared_ptr<BaseNode> &front,
     const std::shared_ptr<BaseNode> &back, int queue_size = 100, OverflowStrategy strategy = OverflowStrategy::Block) 
 {
-    PLOGI << "Link " << front->get_name() << "--->" << back->get_name();
+    PLOGI.printf("Link Node %s --> %s", front->get_name().c_str(), back->get_name().c_str());
     auto queue = std::make_shared<SharedQueue<std::shared_ptr<meta::MetaData>>>();
     back->add_input_buffer(front->get_name(), queue);
     front->add_output_buffer(back->get_name(), queue);

+ 1 - 1
src/nodes/draw/drawNode.cpp

@@ -61,7 +61,7 @@ static std::tuple<uint8_t, uint8_t, uint8_t> random_color(int id)
 
 void DrawNode::work() 
 {
-    printf("DrawNode %s\n", name_.c_str());
+    PLOGI.printf("DrawNode : [%s] start", name_.c_str());
     while (running_)
     {
         bool has_data = false;

+ 4 - 0
src/nodes/draw/drawNode.hpp

@@ -2,6 +2,10 @@
 #define DRAWNODE_HPP__
 #include "nodes/base/base.hpp"
 #include <opencv2/opencv.hpp>
+// 日志库
+#include "plog/Log.h"
+#include "plog/Initializers/RollingFileInitializer.h"
+
 
 namespace GNode
 {

+ 3 - 9
src/nodes/infer/inferNode.cpp

@@ -24,10 +24,10 @@ void print_mat(const cv::Mat& mat, int max_rows = 10, int max_cols = 10)
 
 void InferNode::work()
 {
-    printf("InferNode %s\n", name_.c_str());
+    PLOGI.printf("InferNode : [%s] start", name_.c_str());
     if (!model_)
     {
-        printf("model is nullptr\n");
+        PLOGE.printf("InferNode : [%s] model is nullptr", name_.c_str());
         return;
     }
     // 不同线程都需要指定显卡id,默认为0号显卡
@@ -51,12 +51,6 @@ void InferNode::work()
             int width = image.cols;
             int height = image.rows;
 
-            // auto res = model_->forward(tensor::cvimg(image), image.cols, image.rows, 0.0f, 0.0f);
-            if (!model_)
-            {
-                printf("model is nullptr\n");
-                continue;
-            }
             auto det_result = model_->forward(tensor::cvimg(image), image.cols, image.rows, 0.0f, 0.0f);
 
             if (std::holds_alternative<data::BoxArray>(det_result)) 
@@ -76,7 +70,7 @@ void InferNode::work()
             }
             else
             {
-                printf("Unexpected result type from model");
+                PLOGE.printf("InferNode : [%s] Unexpected result type from model", name_.c_str());
                 throw std::runtime_error("Unexpected result type from model");
             }
             for (auto& output_buffer : output_buffers_)

+ 4 - 0
src/nodes/infer/inferNode.hpp

@@ -5,6 +5,10 @@
 #include <opencv2/opencv.hpp>
 #include "infer/infer.hpp"
 
+// 日志库
+#include "plog/Log.h"
+#include "plog/Initializers/RollingFileInitializer.h"
+
 namespace GNode
 {
 

+ 2 - 2
src/nodes/record/recordNode.cpp

@@ -7,7 +7,7 @@ namespace GNode
 
 void RecordNode::work()
 {
-    printf("RecordNode %s\n", name_.c_str());
+    PLOGI.printf("RecordNode : [%s] start", name_.c_str());
     while (running_)
     {
         bool has_data = false;
@@ -26,7 +26,7 @@ void RecordNode::work()
                 writer_.open(record_path_, fourcc_, fps_, cv::Size(image.cols, image.rows));
                 if (!writer_.isOpened())
                 {
-                    std::cerr << "Error: cannot open video writer" << std::endl;
+                    PLOGE.printf("RecordNode : [%s] cannot open video writer", name_.c_str());
                     break;
                 }
             }

+ 4 - 0
src/nodes/record/recordNode.hpp

@@ -3,6 +3,10 @@
 #include "nodes/base/base.hpp"
 #include <opencv2/opencv.hpp>
 
+// 日志库
+#include "plog/Log.h"
+#include "plog/Initializers/RollingFileInitializer.h"
+
 namespace GNode
 {
 

+ 33 - 33
src/nodes/stream/streamNode.cpp

@@ -10,7 +10,7 @@ namespace GNode
 // --- Private Helper Methods ---
 
 void StreamNode::close_stream() {
-    PLOGI.printf("StreamNode [%s]: Closing stream...\n", name_.c_str());
+    PLOGI.printf("StreamNode [%s]: Closing stream...", name_.c_str());
     // Reset pointers, which will call destructors if they are unique owners
     cap_.reset();
     decoder_.reset(); // Decoder depends on demuxer info, close it first potentially
@@ -23,7 +23,7 @@ bool StreamNode::open_stream() {
     // Ensure any previous stream is closed before opening a new one
     close_stream();
 
-    PLOGI.printf("StreamNode [%s]: Attempting to open stream: %s\n", name_.c_str(), stream_url_.c_str());
+    PLOGI.printf("StreamNode [%s]: Attempting to open stream: %s", name_.c_str(), stream_url_.c_str());
     status_ = StreamStatus::CLOSED; // Start as closed before trying
 
     if (decode_type_ == DecodeType::GPU)
@@ -31,7 +31,7 @@ bool StreamNode::open_stream() {
         demuxer_ = FFHDDemuxer::create_ffmpeg_demuxer(stream_url_);
         if (demuxer_ == nullptr)
         {
-            PLOGE.printf("StreamNode [%s] Error: GPU demuxer creation failed for %s\n", name_.c_str(), stream_url_.c_str());
+            PLOGE.printf("StreamNode [%s] Error: GPU demuxer creation failed for %s", name_.c_str(), stream_url_.c_str());
             status_ = StreamStatus::OPEN_FAILED;
             return false;
         }
@@ -44,12 +44,12 @@ bool StreamNode::open_stream() {
 
         if (decoder_ == nullptr)
         {
-            PLOGE.printf("StreamNode [%s] Error: GPU decoder creation failed for %s (Codec: %d)\n", name_.c_str(), stream_url_.c_str(), codec_id);
+            PLOGE.printf("StreamNode [%s] Error: GPU decoder creation failed for %s (Codec: %d)", name_.c_str(), stream_url_.c_str(), codec_id);
             demuxer_.reset(); // Clean up demuxer if decoder fails
             status_ = StreamStatus::OPEN_FAILED;
             return false;
         }
-        printf("StreamNode [%s]: GPU Demuxer and Decoder created successfully.\n", name_.c_str());
+        printf("StreamNode [%s]: GPU Demuxer and Decoder created successfully.", name_.c_str());
         status_ = StreamStatus::OPENED;
     }
     else // DecodeType::CPU
@@ -59,19 +59,19 @@ bool StreamNode::open_stream() {
         // cap_->open(stream_url_, cv::CAP_FFMPEG);
         if (!cap_->open(stream_url_)) // Check return value of open
         {
-            PLOGI.printf("StreamNode [%s] Error: CPU cv::VideoCapture failed to open %s\n", name_.c_str(), stream_url_.c_str());
+            PLOGI.printf("StreamNode [%s] Error: CPU cv::VideoCapture failed to open %s", name_.c_str(), stream_url_.c_str());
             cap_.reset(); // Release the failed object
             status_ = StreamStatus::OPEN_FAILED;
             return false;
         }
         if (!cap_->isOpened()) // Double check
         {
-            PLOGE.printf("StreamNode [%s] Error: CPU cv::VideoCapture not opened after call for %s\n", name_.c_str(), stream_url_.c_str());
+            PLOGE.printf("StreamNode [%s] Error: CPU cv::VideoCapture not opened after call for %s", name_.c_str(), stream_url_.c_str());
             cap_.reset();
             status_ = StreamStatus::OPEN_FAILED;
             return false;
         }
-        PLOGI.printf("StreamNode [%s]: CPU cv::VideoCapture opened successfully.\n", name_.c_str());
+        PLOGI.printf("StreamNode [%s]: CPU cv::VideoCapture opened successfully.", name_.c_str());
         status_ = StreamStatus::OPENED;
     }
 
@@ -84,24 +84,24 @@ bool StreamNode::open_stream() {
 
 void StreamNode::work()
 {
-    PLOGI.printf("StreamNode [%s] starting work loop. Decode type: %s\n",
+    PLOGI.printf("StreamNode [%s] starting work loop. Decode type: %s",
            name_.c_str(), (decode_type_ == DecodeType::GPU ? "GPU" : "CPU"));
     while (running_) // Main loop continues as long as the node is supposed to run
     {
         if (status_ != StreamStatus::OPENED) // Check if stream needs opening/reopening
         {
-            PLOGI.printf("StreamNode [%s]: Stream not open (Status: %d). Attempting to open...\n",
+            PLOGI.printf("StreamNode [%s]: Stream not open (Status: %d). Attempting to open...",
                    name_.c_str(), static_cast<int>(status_));
 
             if (open_stream()) // Try to open
             {
-                PLOGI.printf("StreamNode [%s]: Stream opened successfully.\n", name_.c_str());
+                PLOGI.printf("StreamNode [%s]: Stream opened successfully.", name_.c_str());
                 // Continue to processing immediately after successful open
             }
             else
             {
                 // Opening failed, wait before retrying
-                PLOGI.printf("StreamNode [%s]: Failed to open stream. Retrying in %d ms...\n",
+                PLOGI.printf("StreamNode [%s]: Failed to open stream. Retrying in %d ms...",
                         name_.c_str(), retry_delay_ms_);
                 status_ = StreamStatus::OPEN_FAILED; // Ensure status reflects failure
 
@@ -119,7 +119,7 @@ void StreamNode::work()
         // If we reach here, the stream should be OPENED
         if (status_ == StreamStatus::OPENED)
         {
-            PLOGI.printf("StreamNode [%s]: Starting stream processing...\n", name_.c_str());
+            PLOGI.printf("StreamNode [%s]: Starting stream processing...", name_.c_str());
             if (decode_type_ == DecodeType::CPU)
             {
                 process_stream_cpu();
@@ -130,13 +130,13 @@ void StreamNode::work()
             }
             // After processing function returns, the stream might be closed or encountered an error.
             // The loop will re-evaluate the status_ at the beginning.
-            PLOGI.printf("StreamNode [%s]: Stream processing finished or stopped (Status: %d).\n",
+            PLOGI.printf("StreamNode [%s]: Stream processing finished or stopped (Status: %d).",
                    name_.c_str(), static_cast<int>(status_));
 
             if (status_ == StreamStatus::CLOSED || status_ == StreamStatus::ERROR) 
             {
                 close_stream(); // Ensure resources are released if processing stopped abnormally
-                PLOGI.printf("StreamNode [%s]: Stream closed or errored. Will attempt reconnection if running.\n", name_.c_str());
+                PLOGI.printf("StreamNode [%s]: Stream closed or errored. Will attempt reconnection if running.", name_.c_str());
                   // Optional short delay even after normal close before retry?
                   // std::this_thread::sleep_for(std::chrono::milliseconds(100));
             }
@@ -144,13 +144,13 @@ void StreamNode::work()
         else 
         {
             // Should not happen if open_stream logic is correct, but good for debugging
-            PLOGD.printf("StreamNode [%s]: Unexpected status %d in work loop.\n", name_.c_str(), static_cast<int>(status_));
+            PLOGD.printf("StreamNode [%s]: Unexpected status %d in work loop.", name_.c_str(), static_cast<int>(status_));
             std::this_thread::sleep_for(std::chrono::milliseconds(1000)); // Avoid tight loop on unexpected state
         }
 
     } // End while(running_)
 
-    PLOGI.printf("StreamNode [%s] work loop finished.\n", name_.c_str());
+    PLOGI.printf("StreamNode [%s] work loop finished.", name_.c_str());
     close_stream(); // Final cleanup
 }
 
@@ -160,12 +160,12 @@ void StreamNode::work()
 void StreamNode::process_stream_cpu()
 {
     if (!cap_ || !cap_->isOpened()) {
-        PLOGD.printf("StreamNode [%s] Error: process_stream_cpu called with closed/invalid VideoCapture.\n", name_.c_str());
+        PLOGD.printf("StreamNode [%s] Error: process_stream_cpu called with closed/invalid VideoCapture.", name_.c_str());
         status_ = StreamStatus::ERROR; // Indicate an unexpected state
         return;
     }
 
-    PLOGI.printf("StreamNode [%s]: Processing CPU stream...\n", name_.c_str());
+    PLOGI.printf("StreamNode [%s]: Processing CPU stream...", name_.c_str());
     while (running_ && status_ == StreamStatus::OPENED)
     {
         cv::Mat frame;
@@ -173,14 +173,14 @@ void StreamNode::process_stream_cpu()
         try {
              success = cap_->read(frame);
         } catch (const cv::Exception& ex) {
-            PLOGE.printf("StreamNode [%s] Error: Exception during cv::VideoCapture::read(): %s\n", name_.c_str(), ex.what());
+            PLOGE.printf("StreamNode [%s] Error: Exception during cv::VideoCapture::read(): %s", name_.c_str(), ex.what());
             status_ = StreamStatus::ERROR; // Treat OpenCV exception as an error
             break; // Exit processing loop
         }
 
         if (!success || frame.empty())
         {
-            PLOGE.printf("StreamNode [%s]: Cannot read frame (End of stream or error).\n", name_.c_str());
+            PLOGE.printf("StreamNode [%s]: Cannot read frame (End of stream or error).", name_.c_str());
             status_ = StreamStatus::CLOSED; // Assume normal closure or recoverable error
             break; // Exit processing loop, work() will handle retry/stop
         }
@@ -204,19 +204,19 @@ void StreamNode::process_stream_cpu()
             }
         }
     }
-    PLOGI.printf("StreamNode [%s]: Exiting CPU processing loop (Running: %s, Status: %d).\n",
+    PLOGI.printf("StreamNode [%s]: Exiting CPU processing loop (Running: %s, Status: %d).",
             name_.c_str(), running_ ? "true" : "false", static_cast<int>(status_));
 }
 
 void StreamNode::process_stream_gpu()
 {
      if (!demuxer_ || !decoder_) {
-        PLOGE.printf("StreamNode [%s] Error: process_stream_gpu called with invalid demuxer/decoder.\n", name_.c_str());
+        PLOGE.printf("StreamNode [%s] Error: process_stream_gpu called with invalid demuxer/decoder.", name_.c_str());
         status_ = StreamStatus::ERROR;
         return;
     }
 
-    printf("StreamNode [%s]: Processing GPU stream...\n", name_.c_str());
+    printf("StreamNode [%s]: Processing GPU stream...", name_.c_str());
     uint8_t* packet_data = nullptr;
     int packet_size = 0;
     int64_t pts = 0;
@@ -224,10 +224,10 @@ void StreamNode::process_stream_gpu()
     // Send extradata once (important for some codecs)
     demuxer_->get_extra_data(&packet_data, &packet_size);
     if (packet_size > 0) {
-        PLOGI.printf("StreamNode [%s]: Sending %d bytes of extradata to decoder.\n", name_.c_str(), packet_size);
+        PLOGI.printf("StreamNode [%s]: Sending %d bytes of extradata to decoder.", name_.c_str(), packet_size);
         decoder_->decode(packet_data, packet_size);
     } else {
-        PLOGI.printf("StreamNode [%s]: No extradata found or needed.\n", name_.c_str());
+        PLOGI.printf("StreamNode [%s]: No extradata found or needed.", name_.c_str());
     }
 
 
@@ -241,14 +241,14 @@ void StreamNode::process_stream_gpu()
              demux_ok = demuxer_->demux(&packet_data, &packet_size, &pts);
         } 
         catch (const std::exception& ex) { // Catch potential exceptions from demuxer implementation
-            PLOGE.printf("StreamNode [%s] Error: Exception during demuxer_->demux(): %s\n", name_.c_str(), ex.what());
+            PLOGE.printf("StreamNode [%s] Error: Exception during demuxer_->demux(): %s", name_.c_str(), ex.what());
             status_ = StreamStatus::ERROR;
             break;
         }
 
         if (!demux_ok || packet_size <= 0 || !running_) // Check running_ again after potentially blocking demux call
         {
-            PLOGI.printf("StreamNode [%s]: Demuxing finished or failed (packet_size: %d, running: %s).\n",
+            PLOGI.printf("StreamNode [%s]: Demuxing finished or failed (packet_size: %d, running: %s).",
                    name_.c_str(), packet_size, running_ ? "true":"false");
             status_ = StreamStatus::CLOSED; // Assume normal end or recoverable error
             break; // Exit processing loop
@@ -262,14 +262,14 @@ void StreamNode::process_stream_gpu()
         } 
         catch (const std::exception& ex) 
         {
-            PLOGE.printf("StreamNode [%s] Error: Exception during decoder_->decode(): %s\n", name_.c_str(), ex.what());
+            PLOGE.printf("StreamNode [%s] Error: Exception during decoder_->decode(): %s", name_.c_str(), ex.what());
             status_ = StreamStatus::ERROR;
             break;
          }
 
         if (ndecoded_frame < 0) 
         {
-            PLOGE.printf("StreamNode [%s] Error: Decoder returned error (%d).\n", name_.c_str(), ndecoded_frame);
+            PLOGE.printf("StreamNode [%s] Error: Decoder returned error (%d).", name_.c_str(), ndecoded_frame);
             status_ = StreamStatus::ERROR; // Treat decoder error as critical
             break; // Exit processing loop
         }
@@ -288,7 +288,7 @@ void StreamNode::process_stream_gpu()
             } 
             catch (const std::exception& ex) 
             {
-                PLOGE.printf("StreamNode [%s] Error: Exception during decoder_->get_frame(): %s\n", name_.c_str(), ex.what());
+                PLOGE.printf("StreamNode [%s] Error: Exception during decoder_->get_frame(): %s", name_.c_str(), ex.what());
                 status_ = StreamStatus::ERROR;
                 ndecoded_frame = 0; // Stop processing frames from this packet
                 break; // Break inner frame loop
@@ -296,7 +296,7 @@ void StreamNode::process_stream_gpu()
 
             if (!frame_data) 
             {
-                PLOGE.printf("StreamNode [%s] Error: Decoder returned null frame data for frame %d.\n", name_.c_str(), i);
+                PLOGE.printf("StreamNode [%s] Error: Decoder returned null frame data for frame %d.", name_.c_str(), i);
                 status_ = StreamStatus::ERROR; // Treat null frame data as error
                 ndecoded_frame = 0; // Stop processing frames from this packet
                 break; // Break inner frame loop
@@ -348,7 +348,7 @@ void StreamNode::process_stream_gpu()
 
     }; // End while(running_ && status_ == StreamStatus::OPENED)
 
-	PLOGI.printf("StreamNode [%s]: Exiting GPU processing loop (Running: %s, Status: %d, Total frames processed this session: %d).\n",
+	PLOGI.printf("StreamNode [%s]: Exiting GPU processing loop (Running: %s, Status: %d, Total frames processed this session: %d).",
            name_.c_str(), running_ ? "true" : "false", static_cast<int>(status_), frame_count_ + 1);
 }
 

+ 5 - 5
src/nodes/stream/streamNode.hpp

@@ -40,7 +40,7 @@ public:
           decode_type_(type),
           retry_delay_ms_(retry_delay_ms)
     {
-        PLOGI.printf("StreamNode [%s]: Created for URL: %s (Decode: %s)\n",
+        PLOGI.printf("StreamNode [%s]: Created for URL: %s (Decode: %s)",
                name_.c_str(), stream_url_.c_str(), (decode_type_ == DecodeType::GPU ? "GPU" : "CPU"));
         open_stream();
     }
@@ -49,7 +49,7 @@ public:
     {
         stop(); // Ensure stop is called (should set running_ to false)
         close_stream(); // Clean up resources
-        PLOGI.printf("StreamNode [%s] destroyed.\n", name_.c_str());
+        PLOGI.printf("StreamNode [%s] destroyed.", name_.c_str());
     };
 
     void set_stream_url(const std::string& stream_url)
@@ -58,7 +58,7 @@ public:
         // to stop, close, update, and restart. For simplicity, assume
         // it's set before starting or requires manual stop/start.
         stream_url_ = stream_url;
-        PLOGI.printf("StreamNode [%s] URL set to: %s\n", name_.c_str(), stream_url_.c_str());
+        PLOGI.printf("StreamNode [%s] URL set to: %s", name_.c_str(), stream_url_.c_str());
         // If called after initial failure, we might want to reset status
         // if (status_ == StreamStatus::OPEN_FAILED) {
         //     status_ = StreamStatus::CLOSED; // Allow work() to retry opening
@@ -71,13 +71,13 @@ public:
         {
             skip_frame = 1;
         }
-        PLOGI.printf("StreamNode [%s] Skip frame set to: %d\n", name_.c_str(), skip_frame);
+        PLOGI.printf("StreamNode [%s] Skip frame set to: %d", name_.c_str(), skip_frame);
         skip_frame_ = skip_frame;
     }
 
     void set_retry_delay(int delay_ms) {
         retry_delay_ms_ = std::max(100, delay_ms); // Ensure a minimum delay
-        PLOGI.printf("StreamNode [%s] Retry delay set to: %d ms\n", name_.c_str(), retry_delay_ms_);
+        PLOGI.printf("StreamNode [%s] Retry delay set to: %d ms", name_.c_str(), retry_delay_ms_);
     }
 
     StreamStatus get_status() const {

+ 2 - 2
src/nodes/track/trackNode.cpp

@@ -5,7 +5,7 @@ namespace GNode
 
 void TrackNode::work()
 {
-    printf("TrackNode %s\n", name_.c_str());
+    PLOGI.printf("TrackNode : [%s] start", name_.c_str());
     for (const auto& input_buffer : input_buffers_)
     {
         track_map_[input_buffer.first] = std::make_shared<BYTETracker>(frame_rate_, track_buffer_);
@@ -26,7 +26,7 @@ void TrackNode::work()
             // auto res = model_->forward(tensor::cvimg(image), image.cols, image.rows, 0.0f, 0.0f);
             if (!track_map_[input_buffer.first] )
             {
-                printf("track is nullptr\n");
+                PLOGE.printf("TrackNode : [%s] track is nullptr", name_.c_str());
                 continue;
             }
             std::vector<Object> objects;

+ 4 - 2
src/nodes/track/trackNode.hpp

@@ -8,6 +8,9 @@
 #include "BYTETracker.h"
 #include "infer/infer.hpp"
 
+// 日志库
+#include "plog/Log.h"
+#include "plog/Initializers/RollingFileInitializer.h"
 namespace GNode
 {
 
@@ -20,13 +23,12 @@ public:
         track_label_ = track_label;
         frame_rate_ = frame_rate;
         track_buffer_ = track_buffer;
-        // tracker_ = std::make_shared<BYTETracker>(frame_rate, track_buffer);
+        PLOGI.printf("TrackNode : [%s] Init. track label is %s, rate is %d, buffer is %d", name_.c_str(), track_label_.c_str(), frame_rate_, track_buffer_)
     }
     virtual ~TrackNode()  { stop(); };
     void work() override;
 
 private:
-    // std::shared_ptr<BYTETracker> tracker_;
     std::unordered_map<std::string, std::shared_ptr<BYTETracker>> track_map_;
     std::string track_label_;