I'm assuming that you have the WriteLog() UDF loaded and have defined $textlog somewhere.

All you need to do is
  1. Assign the function return value to a variable ($rc)
  2. Check the both the value and @ERROR for error states - they are often the same when the function returns error states.
  3. Write to the log and handle the error

This very simple example attempts to open a non-existent file then logs the open error and exits the code segment.
 Code:
;Write entry in the text based log file.
$sFile="C:\FOO\BAR"
$RC=Open(1,$sFile)
If $RC OR @ERROR
	$RC=WriteLog($txtlog,@WKSTA+" "+Join(Split(@IPADDRESS0),"")+" "+@USERID+" RC="+$RC+" @@ERROR="+@ERROR+" @@SERROR="+@SERROR,1)
	Exit 1
EndIf


Be carefull with @ERROR and @SERROR - if you do not save them and you call another function their values will be reset.