From 7db477622206a20ceba9cc132ad3a95911493275 Mon Sep 17 00:00:00 2001
From: Dana <Dana_Lee1016@126.com>
Date: 星期一, 01 十二月 2025 11:51:43 +0800
Subject: [PATCH] 1.JT1076TcpClient 加日志到文件

---
 app/src/main/java/com/anyun/h264/JT1076TcpClient.java |   41 ++++++++++++++++++++---------------------
 1 files changed, 20 insertions(+), 21 deletions(-)

diff --git a/app/src/main/java/com/anyun/h264/JT1076TcpClient.java b/app/src/main/java/com/anyun/h264/JT1076TcpClient.java
index ca08e42..aeeb62c 100644
--- a/app/src/main/java/com/anyun/h264/JT1076TcpClient.java
+++ b/app/src/main/java/com/anyun/h264/JT1076TcpClient.java
@@ -1,6 +1,5 @@
 package com.anyun.h264;
 
-import android.util.Log;
 
 import com.anyun.h264.util.BytesUtils;
 
@@ -14,11 +13,11 @@
 import io.netty.channel.ChannelInboundHandlerAdapter;
 import io.netty.channel.ChannelInitializer;
 import io.netty.channel.ChannelOption;
-import io.netty.channel.ChannelPromise;
 import io.netty.channel.EventLoopGroup;
 import io.netty.channel.nio.NioEventLoopGroup;
 import io.netty.channel.socket.SocketChannel;
 import io.netty.channel.socket.nio.NioSocketChannel;
+import timber.log.Timber;
 
 import java.util.concurrent.TimeUnit;
 
@@ -64,7 +63,7 @@
      */
     public void connect() {
         if (serverIp == null || serverIp.isEmpty()) {
-            Log.e(TAG, "Server IP not set");
+            Timber.e( "Server IP not set");
             if (connectionListener != null) {
                 connectionListener.onError(new IllegalArgumentException("Server IP not set"));
             }
@@ -72,7 +71,7 @@
         }
         
         if (workerGroup != null) {
-            Log.w(TAG, "TCP client already initialized, disconnecting first");
+            Timber.w("TCP client already initialized, disconnecting first");
             disconnect();
         }
         
@@ -93,7 +92,7 @@
                         }
                     });
             
-            Log.d(TAG, "Connecting to TCP server: " + serverIp + ":" + serverPort);
+            Timber.d("Connecting to TCP server: " + serverIp + ":" + serverPort);
             
             // 寮傛杩炴帴
             ChannelFuture future = bootstrap.connect(serverIp, serverPort);
@@ -103,14 +102,14 @@
                     if (future.isSuccess()) {
                         channel = future.channel();
                         isConnected = true;
-                        Log.d(TAG, "TCP connection established: " + serverIp + ":" + serverPort);
+                        Timber.d("TCP connection established: " + serverIp + ":" + serverPort);
                         if (connectionListener != null) {
                             connectionListener.onConnected();
                         }
                     } else {
                         isConnected = false;
                         Throwable cause = future.cause();
-                        Log.e(TAG, "TCP connection failed: " + serverIp + ":" + serverPort, cause);
+                        Timber.e(cause, "TCP connection failed: " + serverIp + ":" + serverPort);
                         if (connectionListener != null) {
                             connectionListener.onError(cause);
                         }
@@ -121,7 +120,7 @@
             });
             
         } catch (Exception e) {
-            Log.e(TAG, "Initialize TCP client failed", e);
+            Timber.e(e, "Initialize TCP client failed");
             isConnected = false;
             if (connectionListener != null) {
                 connectionListener.onError(e);
@@ -135,7 +134,7 @@
      */
     public void sendPacket(byte[] packet) {
         if (!isConnected || channel == null || !channel.isActive()) {
-            Log.w(TAG, "TCP channel not connected, packet dropped");
+            Timber.w( "TCP channel not connected, packet dropped");
             return;
         }
         
@@ -143,7 +142,7 @@
             // 灏嗗瓧鑺傛暟缁勫寘瑁呬负ByteBuf
             ByteBuf buffer = Unpooled.wrappedBuffer(packet);
             String str = BytesUtils.bytesToHexString(  BytesUtils.subArray(buffer.array(),0,30));
-            Log.i(TAG, "Send TCP packet:"+ str);
+            Timber.i( "Send TCP packet:"+ str);
             // 寮傛鍐欏叆
             ChannelFuture future = channel.writeAndFlush(buffer);
             
@@ -152,13 +151,13 @@
                 @Override
                 public void operationComplete(ChannelFuture future) throws Exception {
                     if (!future.isSuccess()) {
-                        Log.e(TAG, "Send TCP packet failed", future.cause());
+                        Timber.e(future.cause(), "Send TCP packet failed");
                     }
                 }
             });
             
         } catch (Exception e) {
-            Log.e(TAG, "Send TCP packet error", e);
+            Timber.e(e, "Send TCP packet error");
         }
     }
     
@@ -173,10 +172,10 @@
                 ChannelFuture future = channel.close();
                 future.await(2, TimeUnit.SECONDS);
             } catch (InterruptedException e) {
-                Log.w(TAG, "Close channel interrupted", e);
+                Timber.w(e,"Close channel interrupted");
                 Thread.currentThread().interrupt();
             } catch (Exception e) {
-                Log.e(TAG, "Close channel error", e);
+                Timber.e(e, "Close channel error");
             }
             channel = null;
         }
@@ -187,7 +186,7 @@
             connectionListener.onDisconnected();
         }
         
-        Log.d(TAG, "TCP connection closed");
+        Timber.d("TCP connection closed");
     }
     
     /**
@@ -198,10 +197,10 @@
             try {
                 workerGroup.shutdownGracefully().await(3, TimeUnit.SECONDS);
             } catch (InterruptedException e) {
-                Log.w(TAG, "Shutdown worker group interrupted", e);
+                Timber.w(e, "Shutdown worker group interrupted");
                 Thread.currentThread().interrupt();
             } catch (Exception e) {
-                Log.e(TAG, "Shutdown worker group error", e);
+                Timber.e(e, "Shutdown worker group error");
             }
             workerGroup = null;
         }
@@ -222,14 +221,14 @@
         @Override
         public void channelActive(ChannelHandlerContext ctx) throws Exception {
             super.channelActive(ctx);
-            Log.d(TAG, "TCP channel active: " + ctx.channel().remoteAddress());
+            Timber.d("TCP channel active: " + ctx.channel().remoteAddress());
             isConnected = true;
         }
         
         @Override
         public void channelInactive(ChannelHandlerContext ctx) throws Exception {
             super.channelInactive(ctx);
-            Log.d(TAG, "TCP channel inactive: " + ctx.channel().remoteAddress());
+            Timber.d("TCP channel inactive: " + ctx.channel().remoteAddress());
             isConnected = false;
             channel = null;
             if (connectionListener != null) {
@@ -239,7 +238,7 @@
         
         @Override
         public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
-            Log.e(TAG, "TCP channel exception", cause);
+            Timber.e(cause, "TCP channel exception");
             isConnected = false;
             if (connectionListener != null) {
                 connectionListener.onError(cause);
@@ -252,7 +251,7 @@
             // 濡傛灉鏈嶅姟鍣ㄦ湁鍝嶅簲锛屽彲浠ュ湪杩欓噷澶勭悊
             // 瀵逛簬JT/T 1076-2016 RTP鍙戦�侊紝閫氬父涓嶉渶瑕佸鐞嗗搷搴�
             ByteBuf buf = (ByteBuf) msg;
-            Log.d(TAG, "Received data from server: " + buf.readableBytes() + " bytes");
+            Timber.d("Received data from server: " + buf.readableBytes() + " bytes");
             buf.release(); // 閲婃斁ByteBuf
         }
     }

--
Gitblit v1.8.0