Wish Log redirection

Discussion in 'B4R Bugs & Wishlist' started by orpailleur, Jun 28, 2019.

  1. orpailleur

    orpailleur Member Licensed User

    I wish have the possibility to redirect the LOG message on UDP
    Thanks
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    Full example of sending "logs" through UDP:
    Code:
    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(1921680104)
       
    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:
    Code:
    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"510428192)
       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.
     

    Attached Files:

  3. orpailleur

    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
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    Once you deserialized the data you should see the original data without any changes.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice