Forum

> > CS2D > Scripts > file.write on linux
ForenübersichtCS2D-Übersicht Scripts-ÜbersichtEinloggen, um zu antworten

Englisch file.write on linux

5 Antworten
Zum Anfang Vorherige 1 Nächste Zum Anfang

alt file.write on linux

G3tWr3ck3d
User Off Offline

Zitieren
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]

alt Re: file.write on linux

Hajt
User Off Offline

Zitieren
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

alt Re: file.write on linux

VADemon
User Off Offline

Zitieren
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.
1× editiert, zuletzt 27.12.17 23:57:18

alt Re: file.write on linux

SQ
Moderator Off Offline

Zitieren
Steam ID is returned as a string because Lua does not support 64 bit integers.
Zum Anfang Vorherige 1 Nächste Zum Anfang
Einloggen, um zu antworten Scripts-ÜbersichtCS2D-ÜbersichtForenübersicht