Wish Log redirection

Erel

Administrator
Staff member
Licensed User
Full example of sending "logs" through UDP:
B4X:
Sub Process_Globals
   Public Serial1 As Serial
   Private wifi As ESP8266WiFi
   Private udp As WiFiUDP
   Private timer1 As Timer
   Private ip() As Byte = Array As Byte(192, 168, 0, 104)
   Private port As UInt = 51042
   Private serializator As B4RSerializator
End Sub

Private Sub AppStart
   Serial1.Initialize(115200)
   Log("AppStart")
   If wifi.Connect2("TP-LINK_D577", "xxx") = False Then
       Log("Failed to connect")
       Return
   End If
   Log("connected")
   If udp.Initialize(54329, "udp_PacketArrived") = False Then
       Log("Failed to create UDP socket")
       Return
   End If
   timer1.Initialize("timer1_Tick", 1000)
   timer1.Enabled = True
End Sub

Sub MyLog(Message() As Object)
   udp.BeginPacket(ip, port)
   udp.Write(serializator.ConvertArrayToBytes(Message))
   udp.SendPacket
End Sub

Sub Timer1_Tick
   MyLog(Array("Time here is: ", Millis))
End Sub

Sub udp_PacketArrived (Data() As Byte, ip1() As Byte, port1 As UInt)
   
End Sub
B4J code that listens for the logs:
B4X:
Sub Process_Globals
   Private socket As UDPSocket   
   Private ser As B4RSerializator
   Private buffer1 As B4XBytesBuilder
End Sub

Sub AppStart (Args() As String)
   socket.Initialize("socket", 51042, 8192)
   ser.Initialize
   buffer1.Initialize
   StartMessageLoop
End Sub

Sub socket_PacketArrived (Packet As UDPPacket)
   buffer1.Clear
   buffer1.Append(Packet.Data)
   Dim data() As Object = ser.ConvertBytesToArray(buffer1.SubArray2(0, Packet.Length))
   Dim sb As StringBuilder
   sb.Initialize
   For Each o As Object In data
       sb.Append(o)
   Next
   Log(sb.ToString)
End Sub
The B4J project is attached.
 

Attachments

orpailleur

Member
Licensed User
Hello,
Thank you for the MyLog routine, its what i needed, now the log come up over UDP.
But i have some caracters at the beginning and at the end
I send WatchDog and Toto as an array
I suppose the serializator add at the beginning 2 caractères, the ~ and an other one
At the end of each string \0
I use tcpbuilder for displaying the UDP ticket
Is it so i should use a DESERIALIZATOR on receive the UDP ticket ?

~Watchdog\0toto\0~Watchdog\0toto\0
 
Top