Save / Log "Print Message" to file

More
4 years 11 months ago #225522 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:
Code:
#!/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

Code:
(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

Please Log in to join the conversation.

More
4 years 11 months ago #225803 by +Jan+
Doesn't anyone have an approach? Wrong subforum? My Python is limited to bumbling copy and paste.... :-)

Jan

Please Log in to join the conversation.

More
4 years 11 months ago #225852 by MaHa
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

Please Log in to join the conversation.

More
4 years 11 months ago #225853 by d2inventory
You can use the LOG function to write to a text file.


job_name_123.ngc
Code:
% (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.

Please Log in to join the conversation.

More
4 years 11 months ago #225859 by nkp
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!

Please Log in to join the conversation.

More
4 years 11 months ago #225867 by +Jan+
Thanks, I'll look through all the solution approaches and get back to you, try out what works well for me.
@nkp 
Code:
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 to join the conversation.

More
4 years 11 months ago #225879 by nkp
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 to join the conversation.

More
4 years 11 months ago #225882 by nkp
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"

Please Log in to join the conversation.

More
4 years 11 months ago #225892 by +Jan+
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:
Code:
    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:
Code:
    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 to join the conversation.

More
4 years 11 months ago #225932 by nkp
##############################################

    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!

Please Log in to join the conversation.

Time to create page: 0.107 seconds
Powered by Kunena Forum