"logging" operations

Hi all,

I am comparing several characteristics of a list of files against those files contained in a folder (more that 500 files). This operation takes approximately 2 hours.

Since my script sometime crashes (for reasons that - for the sake of time - is not important to write here)
I would like to writing a “log” so in case it crashes I can still recover important information.
To write a log I am using the script below, which unfortunately make my script 10% slower.
Is there a better way to do this?
(the loop in the script below “simulate” the file-versus-file analysis)

set TempfileName to "myTempFileLog_Lux.txt"
set aTemp to "/tmp/" & TempfileName
set theFileTemp to POSIX path of aTemp
writeToTempFile(POSIX path of theFileTemp, ("Starting logging" & return), false)

# writing the log info (this loop is to simulate my file-versus-file analysis)
repeat with x from 1 to 100
	do shell script "/bin/echo -n " & ((x as string) & return) & " >>" & space & quoted form of (POSIX path of theFileTemp)
	-- or use: writeToTempFile(POSIX path of theFileTemp, (linefeed & x as string), true)
end repeat

# revelaing the log file
set myFinalTempFile to POSIX file theFileTemp
tell application "Finder" to reveal myFinalTempFile

on writeToTempFile(p, d, a)
	set cmd to "/bin/echo -n " & quoted form of d & " >"
	if a then set cmd to cmd & ">"
	do shell script cmd & space & quoted form of p
end writeToTempFile

Thanks !
L.

On my side I never used do shell script for that.
I use:

use AppleScript version "2.4" # for the timer code
use scripting additions # for the timer code
use framework "Foundation" # for the timer code

set startDate to current application's NSDate's |date|() # for the timer code

set TempfileName to "myTempFileLog_Lux.txt"
set aTemp to "/tmp/" & TempfileName
set theFileTemp to aTemp as «class furl»

my writeto(theFileTemp, ("Starting logging" & linefeed), false)

# writing the log info (this loop is to simulate my file-versus-file analysis)
repeat with x from 1 to 100
	my writeto(theFileTemp, linefeed & x, true)
end repeat

# revelaing the log file

tell application "Finder" to reveal theFileTemp

set timeDiff to startDate's timeIntervalSinceNow() # for the timer code
display dialog "That took " & (-timeDiff as real) & " seconds." # for the timer code
--> "That took 0,119057059288 seconds."
#=====
(*
Handler borrowed to Regulus6633 - http://macscripter.net/viewtopic.php?id=36861
*)
on writeto(targetFile, theData, apendData)
	-- targetFile is the path to the file you want to write
	-- theData is the data you want in the file.
	-- apendData is true to append theData to the end of the current contents of the file or false to overwrite it
	try
		-- set targetFile to targetFile as «class furl» # was useless as targetFile is already of «class furl»
		set openFile to open for access targetFile with write permission
		if not apendData then set eof of openFile to 0
		write theData to openFile starting at eof as «class utf8»
		close access openFile
		return true
	on error
		try
			close access targetFile
		end try
		return false
	end try
end writeto

#=====

with the same timer code your code returned : “That took 7,728153944016 seconds.” which is 70 times more than mine.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) jeudi 14 mars 2019 16:05:06

EDIT : I disabled an instruction which is useless.

This is one way to log with applescript



property logFileNameExtension : ".log"
property logFileName : "Name of logfile" & logFileNameExtension as string
property logFile : ""

set logFile to (((path to library folder) as string) & "Logs:" & logFileName) as string

-- do your stuff here
my logThis("Enter what shall be written to the log " & variableOne & " more logging")
-- do more stuff
my logThis("Do more stuff is done")

----------------------------------------------------------------------------------
-- Script log
----------------------------------------------------------------------------------

on logThis(logMessage)
	try
		close access file logFile
	end try
	open for access file logFile with write permission
	write "[ " & short date string of (current date) & " " & time string of (current date) & " ]  " starting at eof to file logFile
	write logMessage starting at eof to file logFile
	write return starting at eof to file logFile
	close access file logFile
end logThis


To read to logs i’m using the app for system messages, not sure what the english name is for it.

Interestingly !!!
I thought that a single-line Unix command should have been much faster than:

  1. call a subroutine
  2. open filestream
    3)evaluate an “if” condition
  3. write the data
  4. close the file access

Thanks a lot, Yvan!
This is already a big improvement.

@ldicroce
do shell script requires a lot of time to be triggered so often good old plain code written to execute small tasks is more efficient.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) jeudi 14 mars 2019 16:33:41

When I try to write the log file in the my user library/Logs/ I get a error as:
'obj '{ ‘form’:‘name’, ‘want’:‘file’, ‘seld’:‘utxt’(“MacBook HD:Library:Logs:Lux_logfile.log”), ‘from’:[0x0,2 “Script Debugger”] }
I guess is a permission problem, although “Script Debugger” has Full Disk Access in the System Preferences.

Got it!
I had to change it into
path to library folder from user domain

@ldicroce

Under Script Editor, I got an error message which seams to be more clear:
error “Erreur d’autorisation de fichier réseau.” number -5000 from file “SSD 500:Library:Logs:Name of logfile.log” to «class fsrf»

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) jeudi 14 mars 2019 19:06:20

Sorry i forgot to mention that the folder Logs is write protected, you have to allow this first. Nice that you found another solution. Do you get any speed issues with this way to create a log?

Your solution (writing the log as a Console file) and Yvan solution (write the log to the /tmp/ folder) are very similar in speed, which is much faster than my original post.

The only thing I noticed is that your script acts funny when I use it with the Console application open.
But if I keep the Console application close, everything is OK.

Thanks a lot to both.

L.