Sign in to follow this  
Followers 0
NiVZ

Best Programming Practice

7 posts in this topic

#1 ·  Posted (edited)

Hello,

First I must stress I am NOT asking anyone to write this for me. I already have something working but want to re-write it in a cleaner more concise way and wanted to ask some good programming practices.

I've written a number of single .au3 scripts and am generally happy with what I'm doing.

I've now gone on to write something a bit bigger (it's a program to download readings from a diabetic blood glucose meter) and would appreciate some help.

I'm hoping to make the program modular so that support for other meters can be added. So far I have the following:

GUI.au3 - this is the main program to handle the gui, settings and display blood glucose data and uses #includes to add the rest of the files below

COM.au3 - this handles all the communication with the meter via a COM port (USB to serial cable)

MeterName.au3 - this is a meter module that contains the initialisation code, data reading code, CRC checks, etc

Upload.au3 - this will handle the upload of the data to a website using REST protocol

The thing I am struggling with is the use of common functions. The main thing I want to implement is some sort of log file but allow all of my .au3 to write to the common log file. eg The GUI.au3 can log that things have been clicked, and the COM.au3 can show me the HEX values that are being sent/received all in one log file.

So my main question is, where would you set up the FileOpen and FileWrite so that each of the .au3 files can access it?

Is the best idea to create a Common.au3 and then put a #include-once <common.au3> in all of my .au3 files?

And how can I make all of the .au3 files check to see if there is a log file open (ie check that there is a log file variable declared)?

Thanks,

NiVZ

Edited by NiVZ

Share this post


Link to post
Share on other sites



I would just make a _Log() function.

something like

Func _Log($Module,$Event)
    Local $LogFile = @ScriptDir & "\logfile.txt"
    FileWriteLine($LogFile,_Now() & " - " & $Module & " - " & $Event)
EndFunc

Func _Gui()
    ;your gui code
    $msg = GUIGetMsg()
    Switch $msg
        Case $Button1
            _Log("_Gui","Button 1 Pressed")
            ;your code
    EndSwitch
EndFunc

Where you put the function is up to you.


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

There is already _FileWriteLog() in the File.au3 UDF (see help file) which applies standard date/time stamps to the entries. I use it extensively myself, as all my scripts are quite verbose in logging.

The main script should declare a global variable, like $sLogFile, and then you use that by convention in all your calls to _FileWriteLog(), both in the main script and your UDFs.

The _FileWriteLog() function does an open/write/close cycle on each call. That has never been a performance issue for me and I just don't like the idea of keeping the log file in an open state the whole time.

;)

Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

Hello,

Thanks for the replies. Can't believe in all my time coding in AutoIT I've never spotted _FileWriteLog() DOH! *facepalm*

Just one more question then, say for safety sake I want to program the include files to check the log file has been declared before writing to it, could I just do:

If Not IsDeclared($logfile) then $logfile = @ScriptDir & "\mylogfile.log"

at the start of each include file?

Thanks,

NiVZ

Share this post


Link to post
Share on other sites

Hello,

Thanks for the replies. Can't believe in all my time coding in AutoIT I've never spotted _FileWriteLog() DOH! *facepalm*

Just one more question then, say for safety sake I want to program the include files to check the log file has been declared before writing to it, could I just do:

If Not IsDeclared($logfile) then $logfile = @ScriptDir & "\mylogfile.log"

at the start of each include file?

Thanks,

NiVZ

When you think about the code as a computer would it makes no sense. I know it looks sensible to you, but the computer see's the $logfile as containing a string representation of the variable.

If Not IsDeclared("logfile") then $logfile = @ScriptDir & "\mylogfile.log"

Even that is not quite perfect, as the ideal way to program is declare all your variables properly (e.g. with 'local' or 'global'). AutoIt does let you get away with it, but it's not good practice when you want to write large programs.

If Not IsDeclared("logfile") then
    Global $logfile = @ScriptDir & "\mylogfile.log"
EndIf

Mat

Share this post


Link to post
Share on other sites

I always use #AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 at the start of the program to ensure all variables are correctly declared. It will also tell you if you have declared a variable but haven't used.


_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 04/09/2015

Share this post


Link to post
Share on other sites

Best Programming Practice Help me keep things simple

Plan, Group, Document, ..., And code last.

Who needs topic text's when you have a clear cut tile like this. ;)

(if only I could follow my own advise.)


"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0