Author Topic: Are you logging your lisp routine usage?  (Read 12378 times)

0 Members and 1 Guest are viewing this topic.

mkweaver

  • Bull Frog
  • Posts: 346
Re: Are you logging your lisp routine usage?
« Reply #30 on: November 09, 2011, 07:27:16 AM »
I log lisp routine usage, but I also log errors.

Here is an example:
Code: [Select]
(progn
  (princ (strcat "\n\nProblem Title Block: " OLD_BLOCK_NAME))
  (princ (strcat "\nUn-Mapped attribute: " ATT_NAME "\n"))
  (setq LogMSG (strcat "Problem Title Block: " OLD_BLOCK_NAME ", Un-Mapped attribute: " ATT_NAME))
  (writetoerrorlog LogMSG "Title Swap routine")
  (setq PROBLEMS "YES")
        )

This particular routine has 19 calls to WriteToErrorLog, capturing different problems during data/input validation.

And the error log routine:
Code: [Select]
;|;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;  Routine: WriteToErrorLog
;;;  Purpose: Format and write data to the error log file
;;;  Arguments: ErrorMessage, string - the error message we're going to write
;;; ErrorSource, string - the name of the routine that produced the
;;; error.
;;;  Returns: nothing of value
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;|;
(defun WriteToErrorLog( ErrorMessage ErrorSource / )
  (cond
    ((and
       (= 'STR (type
(setq Logerror
(WriteToLog
  (list
    (log:now)
    (log:unc)
    (getvar "loginname")
    ErrorMessage
    ErrorSource
    )
  (strcat Log:Folder Log:ErrorLog)
  )
       )
)
  )
       (wcmatch Logerror "ERROR|*")
       )
     (alert (strcat "ERROR|WriteToERRorLog - " Logerror))
     )
    )
  )


This allows me to capture the errors without interrupting the user.

2cook2

  • Guest
Re: Are you logging your lisp routine usage?
« Reply #31 on: February 07, 2012, 11:54:13 AM »
Hey cmwade77,

Can you post your modified version of the LISP logger?

Thanks

Simon B

  • Mosquito
  • Posts: 4
Re: Are you logging your lisp routine usage?
« Reply #32 on: August 20, 2015, 09:13:32 AM »
I know this is a very old post, however it's worth a try. I have only just started using LISPLogV1-0.lsp

Please can someone help me to modify the code for LISPLogV1-0 to create only one csv file, not a new one daily.

I'd like to monitor which lisps I am using & which I'm not, so after about 6 months I can archive the one's I use little or not at all.

The folder I keep lisps in now has about 300 files, some of which I'm sure have been supersueded

Simon


Code: [Select]
;;--------------------=={ Log LISP Usage }==------------------;;
;;                                                            ;;
;;  Automatically records daily LISP command usage to a CSV   ;;
;;  log file, stored in the folder indicated at the top of    ;;
;;  the code.                                                 ;;
;;                                                            ;;
;;  To enable automatic logging, load this LISP file on       ;;
;;  startup (recommend using the ACADDOC.lsp).                ;;
;;                                                            ;;
;;  To enable or disable LISP command logging at any time,    ;;
;;  type at the command-line 'LispLogON' or 'LispLogOFF'      ;;
;;  respectively.                                             ;;
;;------------------------------------------------------------;;
;;  Author: Lee Mac, Copyright 2011 - www.lee-mac.com       ;;
;;------------------------------------------------------------;;
;;  Version 1.0    -    07-11-2011                            ;;
;;------------------------------------------------------------;;

;;------------------------------------------------------------;;
;;  Log File Save Folder                                      ;;
;;------------------------------------------------------------;;
;;  LISP Log files will be saved to this folder.              ;;
;;  The folder will be created if it doesn't exist.           ;;
;;------------------------------------------------------------;;

(setq *lisp-log-folder* "C:\\AutoCAD\\LISPS\\Logs")

;;------------------------------------------------------------;;

(defun c:LispLogON nil
    (if (null *log-lisp-reactor*)
        (setq *log-lisp-reactor* (vlr-lisp-reactor "LISP-Log" '((:vlr-lispwillstart . lisplog:lispstarted))))
    )
    (if (null *log-save-reactor*)
        (setq *log-save-reactor* (vlr-editor-reactor "LISP-Log" '((:vlr-beginsave . lisplog:savelisplogs))))
    )
    (princ "\nLISP Logging Enabled.")
    (princ)
)

;;------------------------------------------------------------;;

(defun c:LispLogOFF nil
    (if *log-lisp-reactor*
        (progn (vlr-remove *log-lisp-reactor*) (setq *log-lisp-reactor* nil))
    )
    (if *log-save-reactor*
        (progn (vlr-remove *log-save-reactor*) (setq *log-save-reactor* nil))
    )
    (setq *lisp-log-list* nil)
    (vl-propagate '*lisp-log-list*)
    (princ "\nLISP Logging Disabled.")
    (princ)
)

;;------------------------------------------------------------;;

(defun lisplog:lispstarted ( reactor params )
    (if
        (and
            (wcmatch (setq params (strcase (car params))) "(C:*")
            (not (member params '("(C:LISPLOGON)" "(C:LISPLOGOFF)")))
        )
        (progn
            (setq *lisp-log-list*
                (LM:nAssoc++
                    (list
                        (strcat (getvar 'DWGPREFIX) (getvar 'DWGNAME))
                        (substr (vl-string-trim "()" params) 3)
                    )
                    *lisp-log-list*
                )
            )
            (vl-propagate '*lisp-log-list*)
        )
    )
    (princ)
)

;;------------------------------------------------------------;;

(defun lisplog:savelisplogs ( reactor params / *error* directory existing filename file )

    (defun *error* ( msg )
        (if (and file (eq 'FILE (type file))) (setq file (close file)))
        (if (not (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*"))
            (princ (strcat "\nError: " msg))
        )
        (princ)
    )
   
    (if *lisp-log-list*
        (progn
            (if *lisp-log-folder*
                (setq *lisp-log-folder* (vl-string-right-trim "\\" (vl-string-translate "/" "\\" *lisp-log-folder*)))
                (setq *lisp-log-folder* (vl-string-right-trim "\\" (getvar 'DWGPREFIX)))
            )
            (setq directory (strcat *lisp-log-folder* "\\" (LM:Date "MONTH YYYY"))
                  filename  (strcat directory "\\Log_" (LM:Date "YYYYMODD") ".csv")
            )
            (if (null (vl-file-directory-p directory))
                (LM:CreateDirectory directory)
            )
            (if (findfile filename)
                (setq existing (LM:ReadLog filename))
            )
            (if (setq file (open filename "w"))
                (progn
                    (if existing
                        (setq *lisp-log-list* (LM:MergeLists *lisp-log-list* existing))
                    )
                    (foreach dwg (vl-sort *lisp-log-list* '(lambda ( a b ) (< (car a) (car b))))
                        (write-line (car dwg) file)
                        (foreach cmd (vl-sort (cdr dwg) '(lambda ( a b ) (> (cadr a) (cadr b))))
                            (write-line (strcat (car cmd) "," (itoa (cadr cmd))) file)
                        )
                        (write-line "" file)
                    )
                    (setq file (close file))
                    (setq *lisp-log-list* nil)
                    (vl-propagate '*lisp-log-list*)
                )
                (princ "\nUnable to write LISP Log - Check that the Log file is not in use.")
            )
        )
    )
    (princ)
)

;;------------------------------------------------------------;;

(defun LM:nAssoc++ ( key lst / pair )
    (if key
        (if (setq pair (assoc (car key) lst))
            (subst (cons (car key) (LM:nAssoc++ (cdr key) (cdr pair))) pair lst)
            (cons  (cons (car key) (LM:nAssoc++ (cdr key) nil)) lst)
        )
        (if lst (list (1+ (car lst))) '(1))
    )
)

;;------------------------------------------------------------;;

(defun LM:Date ( format )
    (menucmd (strcat "m=$(edtime,$(getvar,DATE)," format ")"))
)

;;------------------------------------------------------------;;

(defun LM:CreateDirectory ( dir / CreateDirectory folders )

    (defun CreateDirectory ( root folders )
        (if folders
            (   (lambda ( dir ) (vl-mkdir dir) (CreateDirectory dir (cdr folders)))
                (strcat root "\\" (car folders))
            )
        )
    )
 
    (if (setq folders (LM:str->lst (vl-string-translate "/" "\\" dir) "\\"))
        (CreateDirectory (car folders) (cdr folders))
    )
    (vl-file-directory-p dir)
)

;;------------------------------------------------------------;;

(defun LM:str->lst ( str del / pos )
    (if (setq pos (vl-string-search del str))
        (vl-remove "" (cons (substr str 1 pos) (LM:str->lst (substr str (+ pos 1 (strlen del))) del)))
        (list str)
    )
)

;;------------------------------------------------------------;;

(defun LM:ReadLog ( filename / file line lst sub1 sub2 )
    (if (setq file (open filename "r"))
        (progn
            (while (setq line (read-line file))
                (cond
                    (   (eq "" line)
                    )
                    (   (= 1 (length (setq line (LM:str->lst line ","))))
                        (if (and sub1 sub2)
                            (setq lst (cons (cons sub1 sub2) lst)
                                  sub1 nil
                                  sub2 nil
                            )
                        )
                        (setq sub1 (car line))
                    )
                    (   (= 2 (length line))
                        (setq sub2 (cons (list (car line) (atoi (cadr line))) sub2))
                    )
                )
            )
            (if (and sub1 sub2)
                (setq lst (cons (cons sub1 sub2) lst))
            )
            (setq file (close file))
            lst
        )
    )
)

;;------------------------------------------------------------;;

(defun LM:MergeLists ( l1 l2 / items item )
    (foreach group l2
        (if (setq items (cdr (assoc (car group) l1)))
            (progn
                (foreach pair (cdr group)
                    (if (setq item  (assoc (car pair) items))
                        (setq items (subst (list (car pair) (+ (cadr pair) (cadr item))) item items))
                        (setq items (cons pair items))
                    )
                )
                (setq l1 (subst (cons (car group) items) (assoc (car group) l1) l1))
            )
            (setq l1 (cons group l1))
        )
    )
    l1
)

;;------------------------------------------------------------;;

(vl-load-com) (c:LispLogON) (princ)

;;------------------------------------------------------------;;
;;                         End of File                        ;;
;;------------------------------------------------------------;;

ronjonp

  • Needs a day job
  • Posts: 7409
Re: Are you logging your lisp routine usage?
« Reply #33 on: August 20, 2015, 09:57:17 AM »

Code - Auto/Visual Lisp: [Select]
  1. ;; Change this
  2. (setq directory (strcat *lisp-log-folder* "\\" (lm:date "MONTH YYYY"))
  3.       filename  (strcat directory "\\Log_" (lm:date "YYYYMODD") ".csv")
  4. )
  5. ;; To this
  6. (setq directory *lisp-log-folder*
  7.       filename  (strcat directory "\\LispLog.csv")
  8. )
Welcome to TheSwamp :)

Windows 11 x64 - AutoCAD /C3D 2022

Custom Build PC

Simon B

  • Mosquito
  • Posts: 4
Re: Are you logging your lisp routine usage?
« Reply #34 on: August 20, 2015, 10:07:32 AM »
Thank you for your prompt reply ronjonp & your expertise.

I shall monitor it over the next week or so!

S

Lee Mac

  • Seagull
  • Posts: 12659
  • London, England
Re: Are you logging your lisp routine usage?
« Reply #35 on: August 20, 2015, 12:18:34 PM »
Thank you for assisting in my absence Ron  :-)

I'm glad you find the program useful Simon!

chauhuh

  • Newt
  • Posts: 21
Re: Are you logging your lisp routine usage?
« Reply #36 on: August 20, 2015, 07:56:44 PM »
Been using this for awhile now Lee. Just wanted to say thanks!

Simon B

  • Mosquito
  • Posts: 4
Re: Are you logging your lisp routine usage?
« Reply #37 on: August 21, 2015, 01:51:32 AM »
I'd just like to echo what chauhuh said.

Guess who's lisps the log file records most often Mr Lee....

Thank you for your dedication

Simon

Lee Mac

  • Seagull
  • Posts: 12659
  • London, England
Re: Are you logging your lisp routine usage?
« Reply #38 on: August 21, 2015, 04:01:43 AM »
Cheers guys!  :-)

cmwade77

  • Swamp Rat
  • Posts: 1361
Re: Are you logging your lisp routine usage?
« Reply #39 on: August 21, 2015, 12:19:46 PM »
I'd just like to echo what chauhuh said.

Guess who's lisps the log file records most often Mr Lee....

Thank you for your dedication

Simon
I don't think I have more than about 5 routines that don't use some of Lee's code.

romulo

  • Mosquito
  • Posts: 20
Re: Are you logging your lisp routine usage?
« Reply #40 on: February 28, 2018, 06:38:59 AM »
Very useful routine, but is possible to log not only "(defun c:" routines, but "(defun foo"?

I want log ALL "LISP" functions calls.

Thank you very much Lee !

Lee Mac

  • Seagull
  • Posts: 12659
  • London, England
Re: Are you logging your lisp routine usage?
« Reply #41 on: February 28, 2018, 07:50:04 AM »
Comment/remove line 61.

You're welcome!  :-)


romulo

  • Mosquito
  • Posts: 20
Re: Are you logging your lisp routine usage?
« Reply #42 on: February 28, 2018, 07:47:15 PM »
Lee, thank you by your response.

But, not log all functions :: Log only first CALL, example:

(defun c:foo1 (/)
  (foo2)
  (foo3)
)

(defun foo2 (/)
  (princ "function 2")
)

(defun foo3 (/)
  (princ "function 3")
)


LOG wich I want:

*** (c:foo1)
------- (foo2)
------- (foo3)



« Last Edit: March 01, 2018, 06:50:18 AM by romulo »

cmwade77

  • Swamp Rat
  • Posts: 1361
Re: Are you logging your lisp routine usage?
« Reply #43 on: October 22, 2021, 01:46:10 PM »
Hey cmwade77,

Can you post your modified version of the LISP logger?

Thanks
I know this is a REALLY old thread; however, I never did post it and I actually lost the version I had done, which meant I had to recreate the modifications and made a few more tweaks, such as adding a header, logging the date and time the save occurs, making the filename and path a separate column, removing blank lines between saves.

I have called mine LogTools so as to not be confused with Lee's version, although this is just a modification of his. Hopefully this still helps.