Logging Console Command Usage

I’m editing a damagelog system to add all types of new logs. Everything I’ve added so far has been pretty simple: take the hooks for the event and make it append the logs with the relevant information when the event happens. I’ve kind of run into a wall when I try to make something run when a console command is executed. I can’t find any relevant hooks, so I tried to do something like this:



// I didn't append anything just so I could get this to run and properly notify.
function allcp(msg)
	for i, v in pairs(player.GetAll()) do 
		v:ChatPrint(msg)
	end
end

local crun = concommand.Run

function concommand.Run(ply, cmd, args)
	if !IsValid(ply) then return crun(ply,cmd,args) end
	if !cmd then return crun(ply,cmd,args) end

	if args and args ~= "" then
		allcp(ply:Name() .. " has executed this command: " .. cmd .. " " .. tostring(args) .. ".")
	else
		allcp(ply:Name() .. " has executed this command: " .. cmd .. ".")
	end
	return crun(ply, cmd, args)

end


This just doesn’t work in general. To see where it breaks, I took out the returns and figured out the problem. Apparently ply is not valid, so it simply returns (I got an error that the method “Name” was a nil value). Oddly enough, this does notify if the command is not run from the console manually, but by a script. For example, this appears in my chat automatically:
AKTS has executed this command: fas2_handrig_applynow
For some context, this is just a function automatically run to adjust the handrigs.

Does anyone have any suggestions as to how to go about doing this? I’m at an impasse at the moment.

The wiki suggest not using this command

“This is an internal function.
This means you will be able to call it, but you really shouldn’t.”

I have not had to do this before so not sure if there is a way, But you should probably find another way, If any :confused:

Yeah, I saw that and looked for any other way. I don’t really care about it too much, I’d just want to get the command logging system to work.

concommand.Add?

Wouldn’t that only log when console commands are added? I’m trying to log the name of the player and the command whenever a player executes a command.

I actually wrote a console command logger which injects an override function in concommand.Add and any console commands that are added before the function is overwritten gets pulled in too.

You should be able to do the same thing with concommand.Run but I haven’t done many tests with it to be sure it’s executed all the time…

https://dl.dropboxusercontent.com/u/26074909/tutoring/_utilities/concommand_tracker_bypass_handler.lua

Depending on the settings you can output the data to console, log it to individual player files plus an everything file for each day, etc… It also has a whitelist / blacklist feature.

If you use concommand.Run as the overwrite, it’ll call when something is executed -snip-…

Also, you’re opening it up to infinite loops… if !cmd / !ply you have it call crun which is concommand.Run, the same function…

Ah, perfect! This was quite a bit more complicated than I originally thought. Acecool to the rescue yet again, it seems. I’ll try this out later and adjust it to my liking.

I rechecked the code with the concommand.Run and you can block commands so I ported the code over to it which simplified things and there are 2 new hooks for it… ShouldLog and CanPlayerRun ConCommand…

Here it is: https://dl.dropboxusercontent.com/u/26074909/tutoring/_utilities/concommand_logger.lua I hope I included all the helper functions, but this one should be more streamlined and easier to use / understand.