From 3025022f984eb0b67a70ccca803f83a7eb3b3e45 Mon Sep 17 00:00:00 2001 From: dmiller Date: Sun, 1 Mar 2015 04:18:33 +0000 Subject: [PATCH] Optimize stdnse.debug and stdnse.verbose Unnecessary, probably, but this eliminates 2 of 7 string creations/collections due to concatenation and reassignment. Also eliminates 2 unneeded table lookups and tests (since host always has host.ip and port always has port.number), and eliminates 1 extra test for prerule and postrule scripts, since we can't have a port if we don't have a host. --- nselib/stdnse.lua | 36 +++++++++++------------------------- 1 file changed, 11 insertions(+), 25 deletions(-) diff --git a/nselib/stdnse.lua b/nselib/stdnse.lua index 0d32720cb..c88be73a4 100644 --- a/nselib/stdnse.lua +++ b/nselib/stdnse.lua @@ -80,22 +80,11 @@ local function debug (level, ...) end local current = nmap.debugging() if level <= current then - local prefix = "[" - if current >= 2 then - prefix = prefix .. (getinfo() or "") - else - prefix = prefix .. (getid() or "") - end local host, port = gethostport() - if host and host.ip then - prefix = prefix .. " " .. host.ip - end - if port and port.number then - prefix = prefix .. ":" .. port.number - end - prefix = prefix .. "] " - if prefix ~= "[] " then - nmap.log_write("stdout", prefix..format(...)) + local prefix = ( (current >= 2 and getinfo or getid)() or "") + .. (host and " "..host.ip .. (port and ":"..port.number or "") or "") + if prefix ~= "" then + nmap.log_write("stdout", "[" .. prefix .. "] " .. format(...)) else nmap.log_write("stdout", format(...)) end @@ -148,19 +137,16 @@ local function verbose (level, ...) end local current = nmap.verbosity() if level <= current then - local prefix = "[" .. (getid() or "") + local prefix if current >= 2 then local host, port = gethostport() - if host and host.ip then - prefix = prefix .. " " .. host.ip - end - if port and port.number then - prefix = prefix .. ":" .. port.number - end + prefix = (getid() or "") + .. (host and " "..host.ip .. (port and ":"..port.number or "") or "") + else + prefix = getid() or "" end - prefix = prefix .. "] " - if prefix ~= "[] " then - nmap.log_write("stdout", prefix..format(...)) + if prefix ~= "" then + nmap.log_write("stdout", "[" .. prefix .. "] " .. format(...)) else nmap.log_write("stdout", format(...)) end