-- For functions in which the return value type depends on the input and a condition.

local function SelectiveReturn(inval,...)
	local args = {...}
	return args[inval]
end

local function ArgsLength(inval,nextval,...)
	if inval ~= nil or nextval ~= nil then
		return 1 + ArgsLength(nextval,...)
	end
	return 0
end

local function ConcatenateValuesWithSpaces(inval,inval2,...)
	if inval ~= nil and inval2 ~= nil then
		if type(inval) ~= "string" then inval = tostring(inval) end
		if type(inval2) ~= "string" then inval2 = tostring(inval2) end
		return ConcatenateValuesWithSpaces(inval .. " " .. inval2,...)
	else
		if inval ~= nil then
			if type(inval) ~= "string" then inval = tostring(inval) end
			return inval
		end
	end
end

local function note(what,where,replace)
	if where == nil then where = ".logfile" end
		local wherefile = io.open(where,"r")
		if wherefile == nil or replace == true then
			if wherefile ~= nil then wherefile:close() end
			wherefile = io.open(where,"w")
		else
			wherefile:close()
			wherefile = io.open(where,"a")
		end
		wherefile:write(tostring(what) .. "\n")
		wherefile:close()
end

local function printnote(...)
	note(SelectiveReturn(1,ConcatenateValuesWithSpaces(...)))
end

local function noteandprint(inval,...)
	Print(inval,...)
	local args = {...}
	args[ArgsLength(...)+1] = ":STDOUT"
	printnote("STDOUT:" .. tostring(inval),TableUnpack(args))
end

print = noteandprint -- MITM the print function and carbon copy it to the log.