RtpLogger.cpp (1837B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 // Original author: nohlmeier@mozilla.com 6 7 #include "RtpLogger.h" 8 9 #include <ctime> 10 #include <iomanip> 11 #include <sstream> 12 13 #include "mozilla/Logging.h" 14 #ifdef _WIN32 15 # include <sys/timeb.h> 16 # include <time.h> 17 #else 18 # include <sys/time.h> 19 #endif 20 21 // Logging context 22 using namespace mozilla; 23 24 mozilla::LazyLogModule gRtpLoggerLog("RtpLogger"); 25 26 namespace mozilla { 27 28 bool RtpLogger::IsPacketLoggingOn() { 29 return MOZ_LOG_TEST(gRtpLoggerLog, LogLevel::Debug); 30 } 31 32 void RtpLogger::LogPacket(const MediaPacket& packet, bool input, 33 std::string desc) { 34 if (MOZ_LOG_TEST(gRtpLoggerLog, LogLevel::Debug)) { 35 bool isRtp = (packet.type() == MediaPacket::RTP); 36 std::stringstream ss; 37 /* This creates text2pcap compatible format, e.g.: 38 * RTCP_PACKET O 10:36:26.864934 000000 80 c8 00 06 6d ... 39 */ 40 ss << (input ? "I " : "O "); 41 std::time_t t = std::time(nullptr); 42 std::tm tm = *std::localtime(&t); 43 char buf[9]; 44 if (0 < strftime(buf, sizeof(buf), "%H:%M:%S", &tm)) { 45 ss << buf; 46 } 47 ss << std::setfill('0'); 48 #ifdef _WIN32 49 struct timeb tb; 50 ftime(&tb); 51 ss << "." << (tb.millitm) << " "; 52 #else 53 struct timeval tv; 54 gettimeofday(&tv, NULL); 55 ss << "." << (tv.tv_usec) << " "; 56 #endif 57 ss << " 000000"; 58 ss << std::hex << std::setfill('0'); 59 for (size_t i = 0; i < packet.len(); ++i) { 60 ss << " " << std::setw(2) << (int)packet.data()[i]; 61 } 62 MOZ_LOG(gRtpLoggerLog, LogLevel::Debug, 63 ("%s %s|>> %s", desc.c_str(), 64 (isRtp ? "RTP_PACKET" : "RTCP_PACKET"), ss.str().c_str())); 65 } 66 } 67 68 } // namespace mozilla