Accessing variables in a function OUTSIDE of function

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
stoavio
Posts: 43
Joined: Sun Jun 30, 2013 2:09 am

Accessing variables in a function OUTSIDE of function

Post by stoavio » Sat Nov 01, 2014 2:33 am

Hi all -

I am writing a quick log function and am trying to use some variables (specifically: lLogDir and lLogFile) outside of the function to actually create the file. I wanted the Logit function to not only log entries but also handle creating the directory the log files will reside in.

Using

Code: Select all

put Logit("HEADER") into URL ("file:" & lLogDir & lLogFile)
does not create the file in the appropriate location with appropriate name as defined in the function, but rather a file with a title of "lLogDirlLogFile". I notice if I define my variables outside the function it works as expected, but I don't want to do that. I want this function to do as much of the work as possible, which means setting the paths and creating the necessary directory.

I even tried giving the variables a global scope in the function (thinking this would allow them to be accessed outside of the function) but that didn't work either. Please help show a new guy the way... :D

Code: Select all

on mouseUp
   --put the defaultfolder & "\Logs\" into lLogDir
   --put the long date & ".log" into lLogFile
   put Logit("HEADER") into URL ("file:" & lLogDir & lLogFile)
end mouseUp

function Logit pType, pMessage
   switch
      case pType = "HEADER"
         Global tLogDir, tLogFile
         put "=================================================" into tHeaderLine1
         put "Application started on:" && the short system date && "at:" && the short system time into tHeaderLine2
         put "Operating System:" && $OS into tHeaderLine3
         put "User:" && $USERDOMAIN & "\" & $USERNAME into tHeaderLine4
         get the defaultfolder
         put the defaultfolder & "\Logs\" into lLogDir
         create folder lLogDir
         put "Application directory:" && it into tHeaderLine5
         put "Log directory:" && it & slash & "Logs" into tHeaderLine6
         put the long date & ".log" into lLogFile
         put "=================================================" into tHeaderLine7

         return lLogDir & lLogFile & tHeaderLine1 & CRLF & tHeaderLine2 & CRLF & tHeaderLine3 & CRLF & tHeaderLine4 & CRLF & tHeaderLine5 & CRLF & tHeaderLine6 & CRLF & tHeaderLine7 & CRLF 
         break
      case pType = "INFO"
         return pType & ":" && pMessage
         break
      case pType = "ERROR"
         return pType & ":" && pMessage
         break
   end switch
      
      
      end Logit

end mouseUp


Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Accessing variables in a function OUTSIDE of function

Post by Simon » Sat Nov 01, 2014 3:51 am

Hi stoavio,
Lots of funky things in the code.
Try this;

Code: Select all

on mouseUp
   --put the defaultfolder & "\Logs\" into lLogDir
   --put the long date & ".log" into lLogFile
   put Logit("HEADER") into temp  --  URL ("file:" & lLogDir & lLogFile)
   breakpoint
   put item 1 of temp
   put item 2 of temp
   put item 3 of temp
   put the defaultFolder
end mouseUp
Without changing any of your function.
Remember comma is the item delimiter.

Can you see what is going on?

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

stoavio
Posts: 43
Joined: Sun Jun 30, 2013 2:09 am

Re: Accessing variables in a function OUTSIDE of function

Post by stoavio » Sat Nov 01, 2014 11:48 am

Simon wrote:Hi stoavio,
Lots of funky things in the code.
Try this;

Code: Select all

on mouseUp
   --put the defaultfolder & "\Logs\" into lLogDir
   --put the long date & ".log" into lLogFile
   put Logit("HEADER") into temp  --  URL ("file:" & lLogDir & lLogFile)
   breakpoint
   put item 1 of temp
   put item 2 of temp
   put item 3 of temp
   put the defaultFolder
end mouseUp
Without changing any of your function.
Remember comma is the item delimiter.

Can you see what is going on?

Simon
Funky things in the code?! Ouch! That one stung. I'm still learning :oops:

I don't know if I explained myself very well but what I would like to know is, see how I have lLogDir and lLogFile defined in my function? If I call those variables from outside of that function will they work? So far they aren't... but I don't know if I'm doing something wrong.

Also, notice the HEADER case of my Log function will write multiple lines. See how I am explicitly defining the return values one by one? I feel that isn't the most efficient way of doing it. Do you have a creative solution for that to reduce the amount of code?

SparkOut
Posts: 2943
Joined: Sun Sep 23, 2007 4:58 pm

Re: Accessing variables in a function OUTSIDE of function

Post by SparkOut » Sat Nov 01, 2014 12:30 pm

My first question, trying to understand how you are going about things... Did you intentionally comment out the definition of the variables in the mouseUp handler? The code you originally posted would have that result. With the first two lines commented out, LC would try to interpret the variables as string literals,producing what you have seen. This may be what Simon was more subtly asking you to examine, but I am still not that clear.

stoavio
Posts: 43
Joined: Sun Jun 30, 2013 2:09 am

Re: Accessing variables in a function OUTSIDE of function

Post by stoavio » Sat Nov 01, 2014 12:34 pm

Alright, I cleaned it up reconsidered my approach. Now the log function does what I want it to and I can call it from anywhere in the script.

It's still a work in progress and but it will help me with troubleshooting and error handling. Speaking of error handling, what tips do you experts have? Any nifty things built into LC that help with this?

Code: Select all

on mouseUp

   put Logit("HEADER") 
   put Logit("INFO", "Drives already mapped")
   put Logit("ERROR", "Location not found")
   
end mouseUp

function Logit pType, pMessage
   Local lLogDir, lLogFile
   get the defaultfolder
   put the defaultfolder & slash & "Logs" into lLogDir
   create folder lLogDir
   put lLogDir & slash & the long date & ".log" into lLogFile
   switch
      case pType = "HEADER"
         put "=========================================================" & CR after tLogHeader
         put "Application started on:" && the short system date && "at" && the short system time & CR after tLogHeader
         put "Operating System:" && $OS & CR after tLogHeader
         put "User:" && $USERDOMAIN & "\" & $USERNAME & CR after tLogHeader
         put "Application directory:" && it & CR after tLogHeader
         put "Log directory:" && it & slash & "Logs" & CR after tLogHeader
         put "=========================================================" & CRLF after tLogHeader
         put tLogHeader into URL ("file:" & lLogFile)
         return tLogHeader
         break
      case pType = "INFO"
         put the short system time && "-" && pType & ":" && pMessage & CR after tLogInfo
         put tLogInfo after URL ("file:" & lLogFile)
         return tLogInfo
         break
      case pType = "ERROR"
         put the short system time && "-" && pType & ":" && pMessage & CR after tLogError
         put tLogError after URL ("file:" & lLogFile)
         return tLogError
         break
   end switch
   
   
end Logit

end mouseUp

stoavio
Posts: 43
Joined: Sun Jun 30, 2013 2:09 am

Re: Accessing variables in a function OUTSIDE of function

Post by stoavio » Sat Nov 01, 2014 12:38 pm

SparkOut wrote:My first question, trying to understand how you are going about things... Did you intentionally comment out the definition of the variables in the mouseUp handler? The code you originally posted would have that result. With the first two lines commented out, LC would try to interpret the variables as string literals,producing what you have seen. This may be what Simon was more subtly asking you to examine, but I am still not that clear.
Hi SparkOut, thanks for trying to dig in and help out. I actually just needed to look at things with a fresh pair of eyes. I believe I have found an approach that is clean and works for my purposes. I posted it above. Take a look and let me know if there's anything I can do to improve it. You should be able to copy and paste it into LC and see it work. It will create a Log directory and Log file in the defaultfolder location. Anything I can do to make it better, do you think?

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Accessing variables in a function OUTSIDE of function

Post by Klaus » Sat Nov 01, 2014 3:42 pm

Hi Stavio,
troubleshooting and error handling
OK, here we go :D

Code: Select all

get the defaultfolder
Don't use IT if not neccessary!
IT will change its content when you least exspect it!
Always use a variable instead of IT!:

Code: Select all

put the defaultfolder into tOldDefaultFolder

Code: Select all

create folder lLogDir
Here you should check 2 things:
1. is the folder already present?
If NOT create it!

Code: Select all

if there is NOT a folder tFolder then
   create folder tFolder
  ...
end if
2. After you created the folder check THE RESULT immediately,*** this will be EMPTY on success
and hold a hint on what might have gone wrong IF LC could not create the folder.
*** This also applies to any writings of files like "export snapshot...", "put xyz into url..." etc.!

Code: Select all

create folder tFolder
if the result <> EMPTY then
  answer "Problem creating folder:" & CR & the result
  ## Don't carry on in this case!
  exit to top
end if
Hope that helps!


Best

Klaus

stoavio
Posts: 43
Joined: Sun Jun 30, 2013 2:09 am

Re: Accessing variables in a function OUTSIDE of function

Post by stoavio » Mon Nov 03, 2014 2:24 pm

Hi Klaus -

Great recommendations, thank you! A couple of questions though. Does "exit to top" stop the execution of a standalone application? What is the difference between that and "quit"?

Do you know of any way to "reset" the defaultfolder to its initial value of the working directory when the application is launched? The issue I am running into is that I am using the defaultfolder in my main script, but also using it in a function, but my main script is changing the value of it as it moves along in order to work with files in various folders, which as you can imagine then renders my function inoperable because the path has changed from its initial value.

I wish there were some other way to get the current working directory without using the defaultfolder function.

Using the defaultfolder location is also tricky because it points to C:\Windows\System32\ while in "editing" mode but changes once the the script is compiled into a standalone application, obviously. Frustrating! :?

Post Reply