Bladeren bron

Refactored logcounter and added headers

Staz M 4 jaren geleden
bovenliggende
commit
f41baa5c51
3 gewijzigde bestanden met toevoegingen van 69 en 25 verwijderingen
  1. 1 0
      CMakeLists.txt
  2. 46 0
      src/logcounter.cpp
  3. 22 25
      src/logcounter.hpp

+ 1 - 0
CMakeLists.txt

@@ -57,6 +57,7 @@ set(LIBDATACHANNEL_SOURCES
 	${CMAKE_CURRENT_SOURCE_DIR}/src/log.cpp
 	${CMAKE_CURRENT_SOURCE_DIR}/src/message.cpp
 	${CMAKE_CURRENT_SOURCE_DIR}/src/peerconnection.cpp
+	${CMAKE_CURRENT_SOURCE_DIR}/src/logcounter.cpp
 	${CMAKE_CURRENT_SOURCE_DIR}/src/rtcp.cpp
 	${CMAKE_CURRENT_SOURCE_DIR}/src/sctptransport.cpp
 	${CMAKE_CURRENT_SOURCE_DIR}/src/threadpool.cpp

+ 46 - 0
src/logcounter.cpp

@@ -0,0 +1,46 @@
+/**
+ * Copyright (c) 2021 Staz Modrzynski
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "logcounter.hpp"
+
+rtc::LogCounter::LogCounter(plog::Severity severity, const std::string &text, std::chrono::seconds duration) :
+        severity(severity), text(text), duration(duration) {}
+
+rtc::LogCounter& rtc::LogCounter::operator++(int) {
+    std::lock_guard lock(mutex);
+    count++;
+    if (!future) {
+        future = ThreadPool::Instance().schedule(duration, [this]() {
+            int countCopy;
+            {
+                std::lock_guard lock(mutex);
+                countCopy = count;
+                count = 0;
+                future = std::nullopt;
+            }
+            PLOG(severity) << text << ": " << countCopy << " (over " << std::chrono::duration_cast<std::chrono::seconds>(duration).count() << " seconds)";
+        });
+    }
+    return *this;
+}
+
+rtc::LogCounter::~LogCounter() {
+    if (future) {
+        future->wait();
+    }
+}

+ 22 - 25
src/logcounter.hpp

@@ -1,12 +1,24 @@
-//
-// Created by staz on 1/3/21.
-//
+/**
+ * Copyright (c) 2021 Staz Modrzynski
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
 
 #ifndef WEBRTC_SERVER_LOGCOUNTER_HPP
 #define WEBRTC_SERVER_LOGCOUNTER_HPP
 
-#include <plog/Util.h>
-#include <plog/Severity.h>
 #include "threadpool.hpp"
 #include "include.hpp"
 
@@ -22,26 +34,11 @@ private:
     std::optional<invoke_future_t<void (*)()>> future;
 public:
 
-    LogCounter(plog::Severity severity, const std::string& text, std::chrono::seconds duration=std::chrono::seconds(1)):
-        severity(severity), text(text), duration(duration) {}
-
-    LogCounter& operator++(int) {
-        std::lock_guard lock(mutex);
-        count++;
-        if (!future) {
-            future = ThreadPool::Instance().schedule(duration, [this]() {
-                int countCopy;
-                {
-                    std::lock_guard lock(mutex);
-                    countCopy = count;
-                    count = 0;
-                    future = std::nullopt;
-                }
-                PLOG(severity) << text << ": " << countCopy << " (over " << std::chrono::duration_cast<std::chrono::seconds>(duration).count() << " seconds)";
-            });
-        }
-        return *this;
-    }
+    LogCounter(plog::Severity severity, const std::string& text, std::chrono::seconds duration=std::chrono::seconds(1));
+
+    ~LogCounter();
+
+    LogCounter& operator++(int);
 };
 }