Save / Log "Print Message" to file

More
06 Nov 2021 19:35 - 06 Nov 2021 19:40 #225522 by +Jan+
Save / Log "Print Message" to file was created by +Jan+
Hello,
I would like to log the runtime of my jobs including job names in a continuous file.
Via my postprocessor of my CAM system I can easily insert the "Print Messages" (linuxcnc.org/docs/html/gcode/overview.html#gcode:print) automatically. This example ( found here www.forum.linuxcnc.org/21-axis/12553-axis-log) with an M code basically fits what I am looking for:
#!/bin/bash

## writes a log based upon gcode calls to M120 n
## where n = the occurence to be logged

if [ ! $# -ge 1 ]; then
echo "Usage: M120 n - where n log entry type num"
exit 1
fi

float=$1
int=${float/\.*}

case $int in

1 ) echo "Started machining" > ~/emc2/logfile
date >> ~/emc2/logfile;;


2 ) echo "Toolchange started" >> ~/emc2/logfile
date >> ~/emc2/logfile;;


3 ) echo "Toolchange ended" >> ~/emc2/logfile
date >> ~/emc2/logfile;;


4 ) echo "Stopped machining" >> ~/emc2/logfile
date >> ~/emc2/logfile;;

esac

exit 0

The only thing I am missing is the job name. 
This I could enter via

(print,job name 123)

into the G code. Linux CNC sends the print result to the stderr according to the G code reference. How can I intercept the data with the M code?

Best regards and thanks in advance

Jan
Last edit: 06 Nov 2021 19:40 by +Jan+.

Please Log in or Create an account to join the conversation.

More
09 Nov 2021 11:31 #225803 by +Jan+
Replied by +Jan+ on topic Save / Log "Print Message" to file
Doesn't anyone have an approach? Wrong subforum? My Python is limited to bumbling copy and paste.... :-)

Jan

Please Log in or Create an account to join the conversation.

More
09 Nov 2021 16:09 #225852 by MaHa
Replied by MaHa on topic Save / Log "Print Message" to file
I tried to solve the loging this way. As the most recent entry is always on top, i don`t bother for the files growing large.

forum.linuxcnc.org/20-g-code/36148-times...ogfile-solved#128037
The following user(s) said Thank You: +Jan+

Please Log in or Create an account to join the conversation.

More
09 Nov 2021 16:20 - 09 Nov 2021 16:20 #225853 by d2inventory
Replied by d2inventory on topic Save / Log "Print Message" to file
You can use the LOG function to write to a text file.


job_name_123.ngc
%
(LOGAPPEND,/path/to/your/logfile.log)
(LOG,job name 123)
(LOGCLOSE)
M30
%

This wont add a timestamp though, you still gotta use the M-File method for that.
Last edit: 09 Nov 2021 16:20 by d2inventory.
The following user(s) said Thank You: +Jan+

Please Log in or Create an account to join the conversation.

More
09 Nov 2021 17:30 - 09 Nov 2021 17:46 #225859 by nkp
Replied by nkp on topic Save / Log "Print Message" to file
The file axis (the function task_run) can be changed like this:
def task_run(*event):
    if run_warn(): return

    global program_start_line, program_start_line_last
    program_start_line_last = program_start_line;
    ensure_mode(linuxcnc.MODE_AUTO)
    c.auto(linuxcnc.AUTO_RUN, program_start_line)
    program_start_line = 0
    t.tag_remove("ignored", "0.0", "end")
    o.set_highlight_line(None)
    
    try:
        fl = open('/home/nkp/emc2/logfile', "a")
        fl.write(s.file + "\n")
        fl.write(time.ctime() + "\n")
        fl.write("=========================\n")        
        fl.close()        
    except :
        print "nkplogerror" 

it works without  Mcodes 
Warning: Spoiler!
Attachments:
Last edit: 09 Nov 2021 17:46 by nkp.
The following user(s) said Thank You: +Jan+

Please Log in or Create an account to join the conversation.

More
09 Nov 2021 19:51 #225867 by +Jan+
Replied by +Jan+ on topic Save / Log "Print Message" to file
Thanks, I'll look through all the solution approaches and get back to you, try out what works well for me.
@nkp 
The file axis (the function task_run) can be changed like this:...

This looks like a very practical KISS solution. Is there any way to still generate a timestamp at the end of the cycle?

Best regards

Jan

Please Log in or Create an account to join the conversation.

More
09 Nov 2021 21:41 #225879 by nkp
Replied by nkp on topic Save / Log "Print Message" to file
Is there any way to still generate a timestamp at the end of the cycle?

in one line?

try:
    fl = open('/home/nkp/emc2/logfile', "a")
    fl.write(s.file + 12*' ' + time.ctime() + "\n")
    fl.close()
except :
    print "nkplogerror"

Please Log in or Create an account to join the conversation.

More
09 Nov 2021 21:51 #225882 by nkp
Replied by nkp on topic Save / Log "Print Message" to file
Warning: Spoiler!


        try:
            fl = open('/home/nkp/emc2/logfile', "a")
            lnf = len(os.path.basename(s.file))
            fl.write(os.path.basename(s.file) + (28-lnf)*' ' + time.ctime() + "\n")       
            fl.close()        
        except :
            print "nkplogerror"
Attachments:

Please Log in or Create an account to join the conversation.

More
09 Nov 2021 23:17 #225892 by +Jan+
Replied by +Jan+ on topic Save / Log "Print Message" to file
Thanks for the quick response.
I think I have unfortunately expressed myself unclearly.
I imagine it as follows:

I created the logfile in the directory /home/cnc/linuxcnc with the name logfile (and gave each user read&write rights) and then inserted the code snippet (for all amateurs like me: the axis.py file can be found in the path /usr/bin) in the Axis file adapted to my needs:
    def task_run(*event):
        if run_warn(): return

        global program_start_line, program_start_line_last
        program_start_line_last = program_start_line;
        ensure_mode(linuxcnc.MODE_AUTO)
        c.auto(linuxcnc.AUTO_RUN, program_start_line)
        program_start_line = 0
        t.tag_remove("ignored", "0.0", "end")
        o.set_highlight_line(None)
        
        try:
            fl = open('/home/cnc/linuxcnc/logfile', "a")
            fl.write(s.file)
            fl.write(" Cycle Start ")
            fl.write(time.ctime() + "\n")        
            fl.close()        
        except :
            print "nkplogerror"

Also I found other events (Pause, Resume, Abort) I would like to log:
    def task_stop(*event):
        if s.task_mode == linuxcnc.MODE_AUTO and vars.running_line.get() != 0:
            o.set_highlight_line(vars.running_line.get())
        comp["abort"] = True
        c.abort()
        c.wait_complete()
        time.sleep(0.3)
        comp["abort"] = False
        try:
            fl = open('/home/cnc/linuxcnc/logfile', "a")
            fl.write(s.file)
            fl.write(" Cycle aborted ")
            fl.write(time.ctime() + "\n")        
            fl.close()        
        except :
            print "nkplogerror"

The only thing I am missing now is a way to log the time after a successful cycle run. Where would be the right place for this?

Best wishes

Jan

Please Log in or Create an account to join the conversation.

More
10 Nov 2021 07:20 - 10 Nov 2021 07:23 #225932 by nkp
Replied by nkp on topic Save / Log "Print Message" to file
##############################################

    def update(self):
        .............
        .............
        ...........
        
        
        if vars.len_program.get():
            if self.stat.motion_line > (vars.len_program.get() - 4):
                vars.len_program.set(0)
                try:
                    fl = open('/home/nkp/emc2/logfile', "a")
                    fl.write("successful" + 18*' ' + time.ctime() + "\n")       
                    fl.close()        
                except :
                    print "nkplogerror"
                    
                    
         user_live_update()  
         
         
#######################################################         
         
vars = nf.Variables(root_window,         
         
         ....
         ....
         ....

    ("len_program", IntVar),




###############################


    def task_run(*event):
        ...
        ...

        try:
            s.poll()
            count = 0
            for line in open(s.file).xreadlines(): count += 1
            vars.len_program.set(count)

            fl = open('/home/nkp/emc2/logfile', "a")
            lnf = len(os.path.basename(s.file))
            fl.write(os.path.basename(s.file) + (28-lnf)*' ' + time.ctime() + "\n")       
            fl.close()        
        except :
            print "nkplogerror"
Warning: Spoiler!
Attachments:
Last edit: 10 Nov 2021 07:23 by nkp.

Please Log in or Create an account to join the conversation.

Time to create page: 0.118 seconds
Powered by Kunena Forum