Forum

> > CS2D > Scripts > file.write on linux
Forums overviewCS2D overview Scripts overviewLog in to reply

English file.write on linux

5 replies
To the start Previous 1 Next To the start

old file.write on linux

G3tWr3ck3d
User Off Offline

Quote
I use a script to log commands but it wont write my commands properly on linux (it wont add spaces between texts).

1
2
3
4
5
6
7
8
9
10
11
12
function LogCommands(id,txt)
	local usgn = player(id,"usgn")
	local ip = player(id,"ip")
	local name = player(id,"name")
	local team = player(id,"team")
	local steam = tonumber(player(id,"steamid"))
	local file = io.open("sys/lua/alex_admin/logs.txt","a+")
	file:write((os.date("%H:%M:%S").." - "..os.date("%d").."-"..os.date("%m").."-"..os.date("%Y")),"\n")
	file:write(("[ID: "..id.."] [USGN: "..usgn.."] [STEAM: "..steam.."] [IP: "..ip.."] [Team: "..team.."] [Name: "..player(id, "name").."]: "..txt),"\n")
	file:flush()
	file:close()
end

The result of this function on linux centos >

1
14:46:56 - 27-12-2017[ID: 2] [USGN: 57648] [STEAM: 0] [IP: ] [Team: 1] [Name: Player]: !14:47:02 - 27-12-2017[ID: 2] [USGN: 57648] [STEAM: 0] [IP: ] [Team: 1] [Name: Player]: !rcon
instead of
1
2
3
4
14:46:56 - 27-12-2017
[ID: 2] [USGN: 57648] [STEAM: 0] [IP: 188.26.247.177] [Team: 1] [Name: Player]: !
14:47:02 - 27-12-2017
[ID: 2] [USGN: 57648] [STEAM: 0] [IP: 188.26.247.177] [Team: 1] [Name: Player]: !rcon

Also just noticed that steamid is written like this >
1
[STEAM: 7.6561198305518e+16]

old Re: file.write on linux

Hajt
User Off Offline

Quote
1
2
3
4
5
6
7
8
9
10
11
12
function LogCommands(id,txt)
	local usgn = player(id,"usgn")
	local ip = player(id,"ip")
	local name = player(id,"name")
	local team = player(id,"team")
	local steam = player(id,"steamid")
	local file = io.open("sys/lua/alex_admin/logs.txt","a+")
	file:write(os.date("%H:%M:%S - %d-%m-%Y").."\n")
	file:write("[ID: "..id.."] [USGN: "..usgn.."] [STEAM: "..steam.."] [IP: "..ip.."] [Team: "..team.."] [Name: "..name.."]: "..txt.."\n")
	file:flush()
	file:close()
end

old Re: file.write on linux

VADemon
User Off Offline

Quote
Your code works better than the whacky notepad you use to open the log file with.

Line breaks on Windows are \r\n
Line breaks on Linux are \n


If you open the file with e.g. Notepad++ it will show everything correctly.
ProTip: instead of writing logs as a text file, write them as .csv to be able to open them in Excel / LibreOffice Calc for better readability. You can use \t as a separation character.

I hope it works:
1
2
3
4
5
6
7
8
9
10
11
12
function LogCommands(id,txt)
     local usgn = player(id,"usgn")
     local ip = player(id,"ip")
     local name = player(id,"name")
     local team = player(id,"team")
     local steam = player(id,"steamid") -- don't tonumber() steamid!
     local file = io.open("sys/lua/alex_admin/logs.csv","a+")
     file:write((os.date("%H:%M:%S").." - "..os.date("%d").."-"..os.date("%m").."-"..os.date("%Y")),"\t")
     file:write(("[ID: "..id.."]\t[USGN: "..usgn.."]\t[STEAM: "..steam.."]\t[IP: "..ip.."]\t[Team: "..team.."]\t[Name: "..player(id, "name").."]\t"..txt),"\n")
     file:flush()
     file:close()
end
Upon opening the file you will be asked about the Separation Characters. Enable "tab" as separator.
edited 1×, last 27.12.17 11:57:18 pm

old Re: file.write on linux

SQ
Moderator Off Offline

Quote
Steam ID is returned as a string because Lua does not support 64 bit integers.

old Re: file.write on linux

G3tWr3ck3d
User Off Offline

Quote
Thank you, so it was because of my notepad ;_; also changed to string for steam thanks you can close this
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview