console log under linux

Here we can discuss about the problem found
Post Reply
Stuart
Posts: 126
Joined: Fri Feb 19, 2021 7:46 pm
Has thanked: 5 times
Been thanked: 20 times

console log under linux

Post by Stuart »

I'm sure I have already done this a couple of years ago but can't get it to work now, so a reminder would be useful!

I want a log file that works even if the ESP32 loses touch with the wlog window, which does happen and makes debugging tricky. I'm assuming that a UDP.write to a terminal window running a UDP read script would do it. I have the read script:

#!/bin/sh
echo "udp listening"
while true; do
nc -luC 5001
echo -e $l
printf $l
echo
sleep 1s
echo $l >> /home/stuart/Dropbox/picaxe/smart\ timer/udp.log
done

and had expected that a UDP.begin port followed by UDP.write "IP", port, "text" messages would do it. But it isn't working.

Could some kind soul put me out of my misery? I'm using Ubuntu.

Stuart
User avatar
cicciocb
Site Admin
Posts: 1900
Joined: Mon Feb 03, 2020 1:15 pm
Location: Toulouse
Has thanked: 407 times
Been thanked: 1271 times
Contact:

Re: console log under linux

Post by cicciocb »

Hi Stuart,
the help says :


UDP.WRITE ip, port, msg$
Send a UDP message to the client defined with the IP address ‘ip’ and the port ‘port". The message ‘msg$" must be a String.

UDP.Begin must be used to initialise the UDP port before using UDP.write


UDP.BEGIN port
Start the UDP Server.

‘port’ is the udp port to be open (numerical).

All the messages received on this port can be read with the function UDP.READ$



User avatar
Electroguard
Posts: 836
Joined: Mon Feb 08, 2021 6:22 pm
Has thanked: 268 times
Been thanked: 317 times

Re: console log under linux

Post by Electroguard »

It might help to send UDP from one Annex device to another Annex device first... then once you know they are communicating ok you can focus on getting your computer to also receive the network broadcasts.
Stuart
Posts: 126
Joined: Fri Feb 19, 2021 7:46 pm
Has thanked: 5 times
Been thanked: 20 times

Re: console log under linux

Post by Stuart »

Thanks guys. The issue turned out to be the number of instances of netcat running in Ubuntu as a result of experimentation. I will post a full working recipe shortly.
Stuart
Posts: 126
Joined: Fri Feb 19, 2021 7:46 pm
Has thanked: 5 times
Been thanked: 20 times

Re: console log under linux

Post by Stuart »

A way to log to a file whilst viewing the real-time log

You may wish to keep a record of events in your AnnexRDS program, which would mean writing to a file that you can access later when investigating the program's behaviour. This is simple to do but there are some gotchas. This note sets out a solution for linux, much of which would also apply to other operating systems.

Step 1: write the log string to a udp port on your server
---------------------------------------------------------
I have a small fanless machine running permanently to act as a file server, for MQTT, and other things. My log files will end up on there. You use the commands in the Annex Basic language to do this, exactly as per the help file:

udp.begin port_number 'example: udp.begin 5001

udp.write recipient_ip_string$, port_number, packet_string$ 'example: udp.write "192.168.1.99",5001,"text to log"

I do my logging using a timestamp routine. This example logs both to wlog and udp. All logs are made using the statement "timestamp message_string$". Note that complex string assignments should be done prior to calling the sub. In my case I want a record of
the time and of various internal data, which is assembled as 'inputs$'. The cr/lf addition is to put each udp log message on its own line.
nc should have done that but my version did not, even though the -C option is used in the script.

'==================
sub timestamp(event$)
'==================
event$ = "["+local_time$+";"+inputs$+"] "+event$
wlog event$
event$ = event$ +chr$(13)+chr$(10)
udp.write "192.168.1.99", 5001, event$
end sub

Step 2: check this works
------------------------
install wireshark to monitor the network. Select udp as the capture filter. You will see all the network udp messages and will be able to spot the ip addresses and the port number of your messages in the record.

Step 3: capture the udp datagrams into a file
---------------------------------------------
This is where linux diverges from other operating systems. In linux, you need to run netcat (nc) in a script in a terminal which captures the messages and files them. Users of Windows Powershell (derived from UNIX) may be able to do similar.

#!/bin/sh
echo "udp listening"
while true; do
today=$(date +"%d-%b-%y")
_file="fully/qualified/log/file/destination/your_app_name_$today.log"
nc -luC 5001 >> "$_file"
sleep 1s
done

The log file created is dated the day it is originated. I had expected nc to terminate after each datagram, thus creating daily files but as written this doesn't, so certainly this can be improved.

Step 4: view the log
--------------------
Writing to a file is very useful, but much of the time you will want to see what is being written in real-time. To achieve this, open another terminal and issue the command 'tail -f your_app_name_nn-nnn-nn.log'. This will show you the last 10 lines of the file, and, crucially, add any new lines to the listing as they are created, exactly as per the AnnexRDS, except they are now also in the log file and survive loss of connection to the RDS.

What can go wrong
-----------------
Be aware that the netcat (nc) daemon is hard to kill. While experimenting you may unknowingly create multiple instances of nc which may not behave as you expect. If this happens you may have a defunct (zombie) parent process that needs finding and killing. The command 'ps -ef | grep defunct' may help.
User avatar
Electroguard
Posts: 836
Joined: Mon Feb 08, 2021 6:22 pm
Has thanked: 268 times
Been thanked: 317 times

Re: console log under linux

Post by Electroguard »

Thanks for the thorough and useful explanation, Stuart.
Post Reply