Jump to content

new log


Hugos

Recommended Posts

  • Scripting Moderators
55 minutes ago, Hugos said:

A question arose... is it possible to write a function in the resource that would create a new log file, set a specific name for it? (well, to be able to record this log some events)

Wrong category.

About function, here's my function which i've created for custom logs system. First version which could be improved and it should be improved (in my final ver, i've added buffer to reduce usage of file functions.)

local function logEvent(file, text)
	local file_exists = fileExists("logs/"..file)
	local real_time = getRealTime()
	local year = real_time.year + 1900
	local month = real_time.month < 10 and "0"..real_time.month + 1 or real_time.month + 1
	local month_day = real_time.monthday < 10 and "0"..real_time.monthday or real_time.monthday
	local hour = real_time.hour < 10 and "0"..real_time.hour or real_time.hour
	local minute = real_time.minute < 10 and "0"..real_time.minute or real_time.minute
	local second = real_time.second < 10 and "0"..real_time.second or real_time.second
	if file_exists then
		local log_file = fileOpen("logs/"..file)
		if log_file then
			fileSetPos(log_file, fileGetSize(log_file))
			fileWrite(log_file, "\r\n["..year.."-"..month.."-"..month_day.." "..hour..":"..minute..":"..second.."] "..text)
			fileFlush(log_file)
			fileClose(log_file)
		else
			outputDebugString("Couldn't open log file.", 0, 255, 127, 0)
			return false
		end
	else
		local new_log = fileCreate("logs/"..file)
		if new_log then
			outputDebugString(file.." log file created successfully.", 0, 255, 127, 0)
			fileWrite(new_log, "["..year.."-"..month.."-"..month_day.." "..hour..":"..minute..":"..second.."] Log file created successfully.")
			if text then
				fileSetPos(new_log, fileGetSize(new_log))
				fileWrite(new_log, "\r\n["..year.."-"..month.."-"..month_day.." "..hour..":"..minute..":"..second.."] "..text)
			end
			fileFlush(new_log)
			fileClose(new_log)
		else
			outputDebugString("Couldn't create new log file.", 0, 255, 127, 0)
			return false
		end	
	end
end

Usage:

logEvent("path_to_file.txt", "Text you want to save")

 

Edited by majqq
  • Thanks 1
Link to comment
On 05/09/2019 at 00:53, majqq said:

Wrong category.

About function, here's my function which i've created for custom logs system. First version which could be improved and it should be improved (in my final ver, i've added buffer to reduce usage of file functions.)


local function logEvent(file, text)
	local file_exists = fileExists("logs/"..file)
	local real_time = getRealTime()
	local year = real_time.year + 1900
	local month = real_time.month < 10 and "0"..real_time.month + 1 or real_time.month + 1
	local month_day = real_time.monthday < 10 and "0"..real_time.monthday or real_time.monthday
	local hour = real_time.hour < 10 and "0"..real_time.hour or real_time.hour
	local minute = real_time.minute < 10 and "0"..real_time.minute or real_time.minute
	local second = real_time.second < 10 and "0"..real_time.second or real_time.second
	if file_exists then
		local log_file = fileOpen("logs/"..file)
		if log_file then
			fileSetPos(log_file, fileGetSize(log_file))
			fileWrite(log_file, "\r\n["..year.."-"..month.."-"..month_day.." "..hour..":"..minute..":"..second.."] "..text)
			fileFlush(log_file)
			fileClose(log_file)
		else
			outputDebugString("Couldn't open log file.", 0, 255, 127, 0)
			return false
		end
	else
		local new_log = fileCreate("logs/"..file)
		if new_log then
			outputDebugString(file.." log file created successfully.", 0, 255, 127, 0)
			fileWrite(new_log, "["..year.."-"..month.."-"..month_day.." "..hour..":"..minute..":"..second.."] Log file created successfully.")
			if text then
				fileSetPos(new_log, fileGetSize(new_log))
				fileWrite(new_log, "\r\n["..year.."-"..month.."-"..month_day.." "..hour..":"..minute..":"..second.."] "..text)
			end
			fileFlush(new_log)
			fileClose(new_log)
		else
			outputDebugString("Couldn't create new log file.", 0, 255, 127, 0)
			return false
		end	
	end
end

Usage:


logEvent("path_to_file.txt", "Text you want to save")

 

one problem...

Quote

Warning: As of 1.5.4 r10413, this function will fail when trying to access a script file of another resource, even with general.ModifyOtherObjects rights granted, which uses a mysql connection through dbConnect when database_credentials_protection is enabled in the server configuration. Additionally, meta.xml will be un-writable and will always open in read-only mode.

https://wiki.multitheftauto.com/wiki/FileOpen

I will need to edit a file that is in another resource. But how do I do this in version 1.5.4 r10413+?

Link to comment
  • Scripting Moderators
2 minutes ago, Hugos said:

one problem...

I will need to edit a file that is in another resource. But how do I do this in version 1.5.4 r10413+?

Are you trying to edit script file or text file? This matters.

Link to comment
Just now, majqq said:

Are you trying to edit script file or text file? This matters.

 text file.

Quote
  • filePath: The filepath of the file in the following format: ":resourceName/path". 'resourceName' is the name of the resource the file is in, and 'path' is the path from the root directory of the resource to the file.
For example, if there is a file named 'coolObjects.txt' in the resource 'objectSearch', it can be opened from another resource this way: fileOpen(":objectSearch/coolObjects.txt").
If the file is in the current resource, only the file path is necessary, e.g. fileOpen("coolObjects.txt").

I just don 't understand if this argument can be used in the new version?

Link to comment
  • Scripting Moderators
6 minutes ago, Hugos said:

Thanks... And the question is, can you create a file in the logs package, not in the resource itself?

Honestly, i don't know.

Never tried. But according to this, this could be done for database.

YLiY9Go.png

https://wiki.multitheftauto.com/wiki/DbConnect

Edited by majqq
  • Thanks 1
Link to comment

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...