From 2459cda0accded20fd9d8f2e8258d9c36d69a4c4 Mon Sep 17 00:00:00 2001 From: Wouter R Date: Thu, 17 Apr 2014 14:15:36 +0200 Subject: [PATCH 01/62] Reorder and rename log levels to match those in print server. Fix typo in regex in getLocalIp function. Conflicts: src/script/wifibox.uci.config --- src/main.lua | 10 +++--- src/network/netconfig.lua | 64 ++++++++++++++++++------------------ src/network/signin.lua | 34 +++++++++---------- src/network/wlanconfig.lua | 9 ++--- src/rest/api/api_printer.lua | 2 +- src/rest/api/api_sketch.lua | 2 +- src/rest/request.lua | 3 +- src/rest/response.lua | 10 +++--- src/script/d3d-updater.lua | 4 +-- src/util/logger.lua | 50 +++++++++++++++++----------- src/util/printer.lua | 4 ++- 11 files changed, 103 insertions(+), 89 deletions(-) diff --git a/src/main.lua b/src/main.lua index 097104f..d839669 100644 --- a/src/main.lua +++ b/src/main.lua @@ -125,7 +125,7 @@ end -- @treturn bool True on success, false on error. local function setupLogger() local logStream = io.stderr -- use stderr as hard-coded default target - local logLevel = log.LEVEL.debug -- use debug logging as hard-coded default level + local logLevel = log.LEVEL.verbose -- use verbose logging as hard-coded default level local logTargetSetting = settings.getSystemKey('logfile') local logLevelSetting = settings.get('system.log.level') @@ -178,7 +178,7 @@ local function setupLogger() end if logLevelError then - log:error("uci config specifies invalid log level '" .. logLevelSetting .. "', using debug level as fallback") + log:error("uci config specifies invalid log level '" .. logLevelSetting .. "', using verbose level as fallback") rv = false end @@ -222,10 +222,10 @@ local function main(environment) local rq = RequestClass.new(environment, postData, confDefaults.DEBUG_API) if rq:getRequestMethod() == 'CMDLINE' and rq:get('autowifi') ~= nil then - + local version = updater.formatVersion(updater.getCurrentVersion()); log:info("Doodle3D version: "..util.dump(version)) - + log:info("running in autowifi mode") local rv,msg = setupAutoWifiMode() @@ -255,7 +255,7 @@ local function main(environment) .. "/" .. (rq:getRealApiFunctionName() or "")) if rq:getRequestMethod() ~= 'CMDLINE' then log:info("remote IP/port: " .. rq:getRemoteHost() .. "/" .. rq:getRemotePort()) - --log:debug("user agent: " .. rq:getUserAgent()) + --log:verbose("user agent: " .. rq:getUserAgent()) end local response, err = rq:handle() diff --git a/src/network/netconfig.lua b/src/network/netconfig.lua index addf197..3c2bbb4 100644 --- a/src/network/netconfig.lua +++ b/src/network/netconfig.lua @@ -25,9 +25,9 @@ M.WWW_RENAME_NAME = '/www-regular' M.CONNECTING_FAILED = -1 M.NOT_CONNECTED = 0 M.CONNECTING = 1 -M.CONNECTED = 2 -M.CREATING = 3 -M.CREATED = 4 +M.CONNECTED = 2 +M.CREATING = 3 +M.CREATED = 4 local function reloadBit(dlist, itemname) if dlist[itemname] == nil then dlist[itemname] = '' end @@ -62,10 +62,10 @@ function M.switchConfiguration(components) for k,v in pairs(components) do local fname = k .. '_' .. v if type(reconf[fname]) == 'function' then - log:debug("reconfiguring component '" .. k .. "' (" .. v .. ")") + log:verbose("reconfiguring component '" .. k .. "' (" .. v .. ")") reconf[fname](dirtyList) else - log:warn("unknown component or action '" .. fname .. "' skipped") + log:warning("unknown component or action '" .. fname .. "' skipped") end end @@ -84,15 +84,15 @@ function M.commitComponent(c) end function M.reloadComponent(c, silent) - log:info("reloading component '" .. c .. "'") + log:info("reloading component '" .. c .. "'") local command = 'reload' local cmd = '/etc/init.d/' .. c .. ' '..command - if silent ~= nil and silent then + if silent ~= nil and silent then cmd = cmd .. ' &> /dev/null' os.execute(cmd) else rv = utils.captureCommandOutput(cmd) - log:debug(" result reloading component '" .. c .. "' (cmd: '"..cmd.."'): \n"..utils.dump(rv)) + log:verbose(" result reloading component '" .. c .. "' (cmd: '"..cmd.."'): \n"..utils.dump(rv)) end end @@ -213,7 +213,7 @@ function reconf.dnsredir_add(dirtyList) local redirText = '/#/' .. settings.get('network.ap.address') local sname = utils.getUciSectionName('dhcp', 'dnsmasq') if sname == nil then return log:error("dhcp config does not contain a dnsmasq section") end - if uci:get('dhcp', sname, 'address') ~= nil then return log:debug("DNS address redirection already in place, not re-adding", false) end + if uci:get('dhcp', sname, 'address') ~= nil then return log:verbose("DNS address redirection already in place, not re-adding", false) end uci:set('dhcp', sname, 'address', {redirText}) commitBit(dirtyList, 'dhcp'); reloadBit(dirtyList, 'dnsmasq') @@ -230,7 +230,7 @@ end --TODO: handle os.rename() return values (nil+msg on error) function reconf.wwwcaptive_add(dirtyList) if utils.exists(M.WWW_CAPTIVE_INDICATOR) then - return log:debug("WWW captive directory already in place, not redoing", false) + return log:verbose("WWW captive directory already in place, not redoing", false) end local rv,reason = os.rename('/www', M.WWW_RENAME_NAME) if rv == true then @@ -241,7 +241,7 @@ function reconf.wwwcaptive_add(dirtyList) end end function reconf.wwwcaptive_rm(dirtyList) - if not utils.exists(M.WWW_CAPTIVE_INDICATOR) then return log:debug("WWW captive directory not in place, not undoing", false) end + if not utils.exists(M.WWW_CAPTIVE_INDICATOR) then return log:verbose("WWW captive directory not in place, not undoing", false) end os.remove('/www') if os.rename(M.WWW_RENAME_NAME, '/www') ~= true then return log:error("Could not rename " .. M.WWW_RENAME_NAME .. " to /www") @@ -275,16 +275,16 @@ end -- @return True on success or nil+msg on error. function M.setupAccessPoint(ssid) M.setStatus(M.CREATING,"Creating access point '"..ssid.."'..."); - - -- add access point configuration + + -- add access point configuration M.switchConfiguration({apnet="add_noreload"}) wifi.activateConfig(ssid) -- NOTE: dnsmasq must be reloaded after network or it will be unable to serve IP addresses M.switchConfiguration({ wifiiface="add", network="reload", staticaddr="add", dhcppool="add_noreload", wwwredir="add", dnsredir="add" }) M.switchConfiguration({dhcp="reload"}) - + M.setStatus(M.CREATED,"Access point created"); - + local ds = wifi.getDeviceState() --log:info(" network/status: ") log:info(" ssid: ".. utils.dump(ds.ssid)) @@ -298,10 +298,10 @@ function M.setupAccessPoint(ssid) log:info(" signal: ".. utils.dump(ds.signal)) log:info(" noise: ".. utils.dump(ds.noise)) log:info(" raw: ".. utils.dump(ds)) - + local localip = wifi.getLocalIP() log:info(" localip: "..utils.dump(localip))]]-- - + return true end @@ -311,14 +311,14 @@ end -- @tparam string ssid The SSID to use for the access point. -- @return True on success or nil+msg on error. function M.enableAccessPoint(ssid) - log:debug("enableAccessPoint ssid: ".. utils.dump(ssid)) - + log:verbose("enableAccessPoint ssid: ".. utils.dump(ssid)) + M.switchConfiguration{apnet="add_noreload"} wifi.activateConfig(ssid) - + local ds = wifi.getDeviceState() - log:debug(" ssid: ".. utils.dump(ds.ssid)) - + log:verbose(" ssid: ".. utils.dump(ds.ssid)) + return true end @@ -332,7 +332,7 @@ end function M.associateSsid(ssid, passphrase, recreate) log:info("netconfig:associateSsid: "..(ssid or "")..", "..(recreate or "")) M.setStatus(M.CONNECTING,"Connecting..."); - + -- see if previously configured network for given ssid exists local cfg = nil for _, net in ipairs(wifi.getConfigs()) do @@ -341,7 +341,7 @@ function M.associateSsid(ssid, passphrase, recreate) break end end - + -- if not, or if newly created configuration is requested, create a new configuration if cfg == nil or recreate ~= nil then local scanResult, msg = wifi.getScanInfo(ssid) @@ -365,16 +365,16 @@ function M.associateSsid(ssid, passphrase, recreate) --M.switchConfiguration{ wifiiface="add", apnet="rm", staticaddr="rm", dhcppool="rm", wwwredir="rm", dnsredir="rm", wireless="reload" } --M.switchConfiguration{ wifiiface="add", staticaddr="rm", dhcppool="rm", wwwredir="rm", dnsredir="rm", wireless="reload" } M.switchConfiguration({ wifiiface="add", staticaddr="rm", dhcppool="rm", wwwredir="rm", dnsredir="rm" }) - + -- we check if we get a ssid and ip in max 5 seconds - -- if not there is probably a issue + -- if not there is probably a issue local attemptInterval = 1 local maxAttempts = 5 local attempt = 0 local nextAttemptTime = os.time() while true do if os.time() > nextAttemptTime then - log:debug("associated check "..utils.dump(attempt).."/"..utils.dump(maxAttempts)) + log:verbose("associated check "..utils.dump(attempt).."/"..utils.dump(maxAttempts)) if wifi.getLocalIP() ~= nil and wifi.getDeviceState().ssid == ssid then break else @@ -390,18 +390,18 @@ function M.associateSsid(ssid, passphrase, recreate) end end end - + -- signin to connect.doodle3d.com local success, output = signin.signin() if success then log:info("Signed in") - else + else log:info("Signing in failed") end - + -- report we are connected after signin attempt M.setStatus(M.CONNECTED,"Connected"); - + return true end --- Disassociate wlan device as client from all SSID's. @@ -411,7 +411,7 @@ end function M.disassociate() M.setStatus(M.NOT_CONNECTED,"Not connected"); - + wifi.activateConfig() return wifi.restart() end diff --git a/src/network/signin.lua b/src/network/signin.lua index 7f20f32..3ad9fe2 100644 --- a/src/network/signin.lua +++ b/src/network/signin.lua @@ -28,54 +28,54 @@ local SIGNING_IN_STATUS = 2 -- function M.signin() - --log:debug("signin:signin"); + --log:verbose("signin:signin"); local code, msg = M.getStatus() - --log:debug(" status: "..utils.dump(code).." "..utils.dump(msg)); + --log:verbose(" status: "..utils.dump(code).." "..utils.dump(msg)); -- if we are already signin in, skip if(code == SIGNING_IN_STATUS) then - log:debug(" skipping signin") + log:verbose(" skipping signin") return end M.setStatus(SIGNING_IN_STATUS,"signing in") local baseurl = "http://connect.doodle3d.com/api/signin.php" - + local attemptInterval = 1 local maxAttempts = 20 local attempt = 0 - + local nextAttemptTime = os.time() - + local localip = "" local signinResponse = "" while true do if os.time() > nextAttemptTime then - log:debug("signin attempt "..utils.dump(attempt).."/"..utils.dump(maxAttempts)) + log:verbose("signin attempt "..utils.dump(attempt).."/"..utils.dump(maxAttempts)) local signedin = false local localip = wifi.getLocalIP(); - --log:debug(" localip: "..utils.dump(localip)) + --log:verbose(" localip: "..utils.dump(localip)) if localip ~= nil then - + local wifiboxid = wifi.getSubstitutedSsid(settings.get('network.cl.wifiboxid')) wifiboxid = urlcode.escape(wifiboxid) - + local cmd = "wget -q -T 2 -t 1 -O - "..baseurl.."?wifiboxid="..wifiboxid.."\\&localip="..localip; signinResponse = utils.captureCommandOutput(cmd); - log:debug(" signin response: \n"..utils.dump(signinResponse)) + log:verbose(" signin response: \n"..utils.dump(signinResponse)) local success = signinResponse:match('"status":"success"') - log:debug(" success: "..utils.dump(success)) + log:verbose(" success: "..utils.dump(success)) if success ~= nil then signedin = true else - log:warn("signin failed request failed (response: "..utils.dump(signinResponse)..")") + log:warning("signin failed request failed (response: "..utils.dump(signinResponse)..")") end - else - log:warn("signin failed no local ip found (attempt: "..utils.dump(attempt).."/"..utils.dump(maxAttempts)..")") + else + log:warning("signin failed no local ip found (attempt: "..utils.dump(attempt).."/"..utils.dump(maxAttempts)..")") end - + if signedin then break else @@ -90,7 +90,7 @@ function M.signin() end end end - + M.setStatus(IDLE_STATUS,"idle") return string.len(signinResponse) > 0, signinResponse end diff --git a/src/network/wlanconfig.lua b/src/network/wlanconfig.lua index ad2a5aa..1b149c8 100644 --- a/src/network/wlanconfig.lua +++ b/src/network/wlanconfig.lua @@ -126,9 +126,10 @@ end --returns the wireless local ip address function M.getLocalIP() local ifconfig, rv = utils.captureCommandOutput("ifconfig wlan0"); - --log:debug(" ifconfig: \n"..utils.dump(ifconfig)); - local localip = ifconfig:match('inet addr:([%d\.]+)') - return localip; + --log:verbose(" ifconfig: \n"..utils.dump(ifconfig)); + + local localip = ifconfig:match('inet addr:([%d%.]+)') + return localip end function M.getDeviceName() @@ -241,7 +242,7 @@ function M.createConfigFromScanInfo(info, passphrase, disabled) uci:foreach('wireless', 'wifi-iface', function(s) --if s.bssid == info.bssid then if s.ssid == info.ssid then - log:debug("removing old wireless config for net '" .. s.ssid .. "'") + log:verbose("removing old wireless config for net '" .. s.ssid .. "'") uci:delete('wireless', s['.name']) -- return false --keep looking, just in case multiple entries with this bssid exist end diff --git a/src/rest/api/api_printer.lua b/src/rest/api/api_printer.lua index 7376db5..9cd44bc 100644 --- a/src/rest/api/api_printer.lua +++ b/src/rest/api/api_printer.lua @@ -194,7 +194,7 @@ function M.print_POST(request, response) end if argIsFirst == true then - log:debug("clearing all gcode for " .. printer:getId()) + log:verbose("clearing all gcode for " .. printer:getId()) response:addData('gcode_clear',true) local rv,msg = printer:clearGcode() diff --git a/src/rest/api/api_sketch.lua b/src/rest/api/api_sketch.lua index 021548a..06375d9 100644 --- a/src/rest/api/api_sketch.lua +++ b/src/rest/api/api_sketch.lua @@ -133,7 +133,7 @@ function M._global_POST(request, response) local sketchIdx = listSize > 0 and sketches[listSize] + 1 or 1 local sketchFile = M.SKETCH_DIR .. '/' .. constructSketchFilename(sketchIdx) - log:debug("saving sketch #" .. sketchIdx .. " (" .. argData:len() .. " bytes)") + log:verbose("saving sketch #" .. sketchIdx .. " (" .. argData:len() .. " bytes)") local saveFile,msg = io.open(sketchFile, 'w') if not saveFile then diff --git a/src/rest/request.lua b/src/rest/request.lua index 48d4773..5c52c8f 100644 --- a/src/rest/request.lua +++ b/src/rest/request.lua @@ -167,7 +167,8 @@ setmetatable(M, { }) --This function initializes itself using various environment variables, the arg array and the given postData ---NOTE: if debugging is enabled, commandline arguments 'm' and 'f' override requested module and function +--NOTE: if debugging is enabled, commandline argument 'p' (path) overrides requested module and function and +-- 'r' (rqType) overrides request method function M.new(environment, postData, debugEnabled) local self = setmetatable({}, M) diff --git a/src/rest/response.lua b/src/rest/response.lua index 869b813..580d2b7 100644 --- a/src/rest/response.lua +++ b/src/rest/response.lua @@ -15,7 +15,7 @@ local settings = require('util.settings') local defaults = require('conf_defaults') local utils = require('util.utils') local log = require('util.logger') - + local M = {} M.__index = M @@ -167,10 +167,10 @@ function M:send() io.write("\r\n") io.write(self.binaryData) end - - if self.body.status ~= "success" then - log:debug("Response:"..utils.dump(self.body.status).." ("..utils.dump(self.body.msg)..")") - end + + if self.body.status ~= "success" then + log:verbose("Response:"..utils.dump(self.body.status).." ("..utils.dump(self.body.msg)..")") + end end --- Sets the response object to return binary data instead of JSON as its body. diff --git a/src/script/d3d-updater.lua b/src/script/d3d-updater.lua index 9ba4ed1..9cdb0ff 100644 --- a/src/script/d3d-updater.lua +++ b/src/script/d3d-updater.lua @@ -82,7 +82,7 @@ local baseUrl = M.DEFAULT_BASE_URL -- default, can be overwritten by M.setBaseUr -- @string msg The message to log. local function P(lvl, msg) if log then - if lvl == -1 then log:debug(msg) + if lvl == -1 then log:verbose(msg) elseif lvl == 0 or lvl == 1 then log:info(msg) end else @@ -90,7 +90,7 @@ local function P(lvl, msg) end end ---- Log a debug message, this function wraps @{P}. +--- Log a debug/verbose message, this function wraps @{P}. -- The message will be logged with level -1 and be prefixed with '(DBG)'. -- @string msg The message to log. local function D(msg) P(-1, (log and msg or "(DBG) " .. msg)) end diff --git a/src/util/logger.lua b/src/util/logger.lua index e10072c..39ac173 100644 --- a/src/util/logger.lua +++ b/src/util/logger.lua @@ -13,30 +13,31 @@ local utils = require('util.utils') local M = {} -local logLevel, logVerbose, logStream +local logLevel, logVerboseFmt, logStream ---- Available log levels +--- Available log levels (starting at 1) -- @table LEVEL M.LEVEL = { - 'debug', -- for debug messages - 'info', -- for informational messages - 'warn', -- for warnings (something is wrong/fishy but not neccesarily problematic) - 'error', -- for recoverable errors - 'fatal' -- for unrecoverable errors + 'quiet', -- not used for logging, only for setting levels + 'error', -- fatal or non-fatal error condition + 'warning', -- possibly troublesome situation + 'info', -- information about what the application is doing + 'verbose', -- extra trail information on what the application is doing + 'bulk' -- debug information (in large amounts) } --- M.LEVEL already has idx=>name entries, now create name=>idx entries +-- M.LEVEL already has idx=>name entries, now create name=>idx entries so it can be indexed both ways for i,v in ipairs(M.LEVEL) do M.LEVEL[v] = i end -local function log(level, msg, verbose) - if level >= logLevel then +local function log(level, msg, verboseFmt) + if level <= logLevel then local now = os.date('%m-%d %H:%M:%S') local i = debug.getinfo(3) --the stack frame just above the logger call - local v = verbose - if v == nil then v = logVerbose end + local v = verboseFmt + if v == nil then v = logVerboseFmt end local name = i.name or "(nil)" local vVal = 'nil' local m = (type(msg) == 'string') and msg or utils.dump(msg) @@ -52,21 +53,30 @@ end --- Initializes the logger. -- @tparam @{util.logger.LEVEL} level Minimum level of messages to log. -- @tparam bool verbose Write verbose log messages (include file/line inforomation). -function M:init(level, verbose) - logLevel = level or M.LEVEL.warn - logVerbose = verbose or false +function M:init(level, verboseFmt) + logLevel = level or M.LEVEL.warning + logVerboseFmt = verboseFmt or false logStream = stream or io.stdout end +function M:setLevel(level, verboseFmt) + logLevel = level or M.LEVEL.warning + logVerboseFmt = verboseFmt or false +end + -- pass nil as stream to reset to stdout function M:setStream(stream) logStream = stream or io.stdout end -function M:debug(msg, verbose) log(M.LEVEL.debug, msg, verbose); return true end -function M:info(msg, verbose) log(M.LEVEL.info, msg, verbose); return true end -function M:warn(msg, verbose) log(M.LEVEL.warn, msg, verbose); return true end -function M:error(msg, verbose) log(M.LEVEL.error, msg, verbose); return false end -function M:fatal(msg, verbose) log(M.LEVEL.fatal, msg, verbose); return false end +function M:getLevel() + return logLevel, logVerboseFmt +end + +function M:error(msg, verboseFmt) log(M.LEVEL.error, msg, verboseFmt); return false end +function M:warning(msg, verboseFmt) log(M.LEVEL.warning, msg, verboseFmt); return true end +function M:info(msg, verboseFmt) log(M.LEVEL.info, msg, verboseFmt); return true end +function M:verbose(msg, verboseFmt) log(M.LEVEL.verbose, msg, verboseFmt); return true end +function M:bulk(msg, verboseFmt) log(M.LEVEL.bulk, msg, verboseFmt); return true end return M diff --git a/src/util/printer.lua b/src/util/printer.lua index 4bc1670..13ec827 100644 --- a/src/util/printer.lua +++ b/src/util/printer.lua @@ -81,7 +81,7 @@ end --returns a printer instance or nil (and sets error state on response in the latter case) function M.createPrinterOrFail(deviceId, response) - --log:debug("API:printer:createPrinterOrFail: "..utils.dump(deviceId)) + --log:verbose("API:printer:createPrinterOrFail: "..utils.dump(deviceId)) local msg,printer = nil, nil if deviceId == nil or deviceId == "" then @@ -98,6 +98,8 @@ function M.createPrinterOrFail(deviceId, response) return nil end + printer:setLocalLogLevel(log:getLevel()) + return printer end From 5f59e5e2c98ef2ab8768f2243c9bb794bcfce0f2 Mon Sep 17 00:00:00 2001 From: Wouter R Date: Fri, 25 Apr 2014 14:51:29 +0200 Subject: [PATCH 02/62] Add logging module/facility support. Change log message format to match with that in print3d logger. --- src/main.lua | 49 +++++++++++----------- src/network/netconfig.lua | 78 ++++++++++++++++++------------------ src/network/signin.lua | 21 +++++----- src/network/wlanconfig.lua | 16 ++++---- src/rest/api/api_config.lua | 14 ++++--- src/rest/api/api_info.lua | 13 +++--- src/rest/api/api_network.lua | 12 +++--- src/rest/api/api_printer.lua | 12 ++++-- src/rest/api/api_sketch.lua | 6 ++- src/rest/api/api_update.lua | 4 +- src/rest/response.lua | 5 ++- src/script/d3d-updater.lua | 10 +++-- src/util/access.lua | 16 ++++---- src/util/logger.lua | 25 +++++++----- src/util/printer.lua | 18 +++++++-- src/util/settings.lua | 40 +++++++++--------- src/util/status.lua | 8 ++-- 17 files changed, 196 insertions(+), 151 deletions(-) diff --git a/src/main.lua b/src/main.lua index d839669..c481a2b 100644 --- a/src/main.lua +++ b/src/main.lua @@ -30,6 +30,7 @@ local updater = require('script.d3d-updater') arg = argStash local postData = nil +local MOD_ABBR = "ENTR" --- Switches to wifi client mode or to access point mode based on availability of known wifi networks. @@ -48,11 +49,11 @@ local function setupAutoWifiMode() local wifiState = wifi.getDeviceState() local netName, netMode = wifiState.ssid, wifiState.mode - log:info("current wifi name: " .. (netName or "") .. ", mode: " .. netMode) + log:info(MOD_ABBR, "current wifi name: " .. (netName or "") .. ", mode: " .. netMode) local apSsid = wifi.getSubstitutedSsid(settings.get('network.ap.ssid')) local apMode = (apSsid == netName) and (netMode == 'ap') - log:info("ssid of self: " .. apSsid) + log:info(MOD_ABBR, "ssid of self: " .. apSsid) local scanList,msg = wifi.getScanInfo() if not scanList then @@ -60,7 +61,7 @@ local function setupAutoWifiMode() end local knownSsids = wifi.getConfigs() - -- log:info("current wifi name: " .. (netName or "") .. ", mode: " .. netMode .. ", ssid of self: " .. apSsid) + -- log:info(MOD_ABBR, "current wifi name: " .. (netName or "") .. ", mode: " .. netMode .. ", ssid of self: " .. apSsid) local visNet, knownNet = {}, {} for _,sn in ipairs(scanList) do table.insert(visNet, sn.ssid) @@ -68,8 +69,8 @@ local function setupAutoWifiMode() for _,kn in ipairs(knownSsids) do table.insert(knownNet, kn.ssid .. "/" .. kn.mode) end - log:info("visible networks: " .. table.concat(visNet, ", ")) - log:info("known networks: " .. table.concat(knownNet, ", ")) + log:info(MOD_ABBR, "visible networks: " .. table.concat(visNet, ", ")) + log:info(MOD_ABBR, "known networks: " .. table.concat(knownNet, ", ")) -- if the currently active network is client mode and is also visible, do nothing since it will connect automatically further along the boot process if netMode == 'sta' and netName ~= nil and netName ~= "" and findSsidInList(scanList, netName) then @@ -173,12 +174,12 @@ local function setupLogger() local rv = true if logTargetError then - log:error("could not open logfile '" .. logTargetSetting .. "', using stderr as fallback (" .. logTargetError .. ")") + log:error(MOD_ABBR, "could not open logfile '" .. logTargetSetting .. "', using stderr as fallback (" .. logTargetError .. ")") rv = false end if logLevelError then - log:error("uci config specifies invalid log level '" .. logLevelSetting .. "', using verbose level as fallback") + log:error(MOD_ABBR, "uci config specifies invalid log level '" .. logLevelSetting .. "', using verbose level as fallback") rv = false end @@ -198,7 +199,7 @@ local function init(environment) end if dbgText ~= "" then dbgText = " (" .. dbgText .. " debugging)" end - log:info("=======rest api" .. dbgText .. "=======") + log:info(MOD_ABBR, "=======rest api" .. dbgText .. "=======") if (environment['REQUEST_METHOD'] == 'POST') then local n = tonumber(environment['CONTENT_LENGTH']) @@ -224,48 +225,48 @@ local function main(environment) if rq:getRequestMethod() == 'CMDLINE' and rq:get('autowifi') ~= nil then local version = updater.formatVersion(updater.getCurrentVersion()); - log:info("Doodle3D version: "..util.dump(version)) + log:info(MOD_ABBR, "Doodle3D version: "..util.dump(version)) - log:info("running in autowifi mode") + log:info(MOD_ABBR, "running in autowifi mode") local rv,msg = setupAutoWifiMode() if rv then - log:info("autowifi setup done (" .. msg .. ")") + log:info(MOD_ABBR, "autowifi setup done (" .. msg .. ")") else - log:error("autowifi setup failed (" .. msg .. ")") + log:error(MOD_ABBR, "autowifi setup failed (" .. msg .. ")") end elseif rq:getRequestMethod() == 'CMDLINE' and rq:get('signin') ~= nil then - log:info("running in signin mode") + log:info(MOD_ABBR, "running in signin mode") local ds = wifi.getDeviceState() - log:info(" ds.mode: "..util.dump(ds.mode)) + log:info(MOD_ABBR, " ds.mode: "..util.dump(ds.mode)) if ds.mode == "sta" then - log:info(" attempting signin") + log:info(MOD_ABBR, " attempting signin") local success,msg = Signin.signin() if success then - log:info("Signin successful") + log:info(MOD_ABBR, "Signin successful") else - log:info("Signin failed: "..util.dump(msg)) + log:info(MOD_ABBR, "Signin failed: "..util.dump(msg)) end end elseif rq:getRequestMethod() ~= 'CMDLINE' or confDefaults.DEBUG_API then - -- log:info("received request of type " .. rq:getRequestMethod() .. " for " .. (rq:getRequestedApiModule() or "") + -- log:info(MOD_ABBR, "received request of type " .. rq:getRequestMethod() .. " for " .. (rq:getRequestedApiModule() or "") -- .. "/" .. (rq:getRealApiFunctionName() or "") .. " with arguments: " .. util.dump(rq:getAll())) - log:info("received request of type " .. rq:getRequestMethod() .. " for " .. (rq:getRequestedApiModule() or "") + log:info(MOD_ABBR, "received request of type " .. rq:getRequestMethod() .. " for " .. (rq:getRequestedApiModule() or "") .. "/" .. (rq:getRealApiFunctionName() or "")) if rq:getRequestMethod() ~= 'CMDLINE' then - log:info("remote IP/port: " .. rq:getRemoteHost() .. "/" .. rq:getRemotePort()) - --log:verbose("user agent: " .. rq:getUserAgent()) + log:info(MOD_ABBR, "remote IP/port: " .. rq:getRemoteHost() .. "/" .. rq:getRemotePort()) + --log:verbose(MOD_ABBR, "user agent: " .. rq:getUserAgent()) end local response, err = rq:handle() - if err ~= nil then log:error(err) end + if err ~= nil then log:error(MOD_ABBR, err) end response:send() response:executePostResponseQueue() else - log:info("Nothing to do...bye.\n") + log:info(MOD_ABBR, "Nothing to do...bye.\n") end end @@ -286,7 +287,7 @@ function handle_request(env) resp:setError("initialization failed" .. errSuffix) resp:send() - log:error("initialization failed" .. errSuffix) --NOTE: this assumes the logger has been initialized properly, despite init() having failed + log:error(MOD_ABBR, "initialization failed" .. errSuffix) --NOTE: this assumes the logger has been initialized properly, despite init() having failed return 1 else diff --git a/src/network/netconfig.lua b/src/network/netconfig.lua index 3c2bbb4..14fe69b 100644 --- a/src/network/netconfig.lua +++ b/src/network/netconfig.lua @@ -18,6 +18,8 @@ local reconf = {} local wifi local reloadSilent +local MOD_ABBR = "NTCF" + M.WWW_CAPTIVE_PATH = '/usr/share/lua/wifibox/www' M.WWW_CAPTIVE_INDICATOR = '/www/.wifibox-inplace' M.WWW_RENAME_NAME = '/www-regular' @@ -62,10 +64,10 @@ function M.switchConfiguration(components) for k,v in pairs(components) do local fname = k .. '_' .. v if type(reconf[fname]) == 'function' then - log:verbose("reconfiguring component '" .. k .. "' (" .. v .. ")") + log:verbose(MOD_ABBR, "reconfiguring component '" .. k .. "' (" .. v .. ")") reconf[fname](dirtyList) else - log:warning("unknown component or action '" .. fname .. "' skipped") + log:warning(MOD_ABBR, "unknown component or action '" .. fname .. "' skipped") end end @@ -79,12 +81,12 @@ function M.switchConfiguration(components) end function M.commitComponent(c) - log:info("committing component '" .. c .. "'") + log:info(MOD_ABBR, "committing component '" .. c .. "'") uci:commit(c) end function M.reloadComponent(c, silent) - log:info("reloading component '" .. c .. "'") + log:info(MOD_ABBR, "reloading component '" .. c .. "'") local command = 'reload' local cmd = '/etc/init.d/' .. c .. ' '..command if silent ~= nil and silent then @@ -92,7 +94,7 @@ function M.reloadComponent(c, silent) os.execute(cmd) else rv = utils.captureCommandOutput(cmd) - log:verbose(" result reloading component '" .. c .. "' (cmd: '"..cmd.."'): \n"..utils.dump(rv)) + log:verbose(MOD_ABBR, " result reloading component '" .. c .. "' (cmd: '"..cmd.."'): \n"..utils.dump(rv)) end end @@ -147,7 +149,7 @@ function reconf.apnet_rm(dirtyList) uci:foreach('wireless', 'wifi-iface', function(s) if s.ssid == wifi.getSubstitutedSsid(settings.get('network.ap.ssid')) then sname = s['.name']; return false end end) - if sname == nil then return log:info("AP network configuration does not exist, nothing to remove") end + if sname == nil then return log:info(MOD_ABBR, "AP network configuration does not exist, nothing to remove") end uci:delete('wireless', sname) reloadBit(dirtyList, 'network'); commitBit(dirtyList, 'wireless') end @@ -212,15 +214,15 @@ end function reconf.dnsredir_add(dirtyList) local redirText = '/#/' .. settings.get('network.ap.address') local sname = utils.getUciSectionName('dhcp', 'dnsmasq') - if sname == nil then return log:error("dhcp config does not contain a dnsmasq section") end - if uci:get('dhcp', sname, 'address') ~= nil then return log:verbose("DNS address redirection already in place, not re-adding", false) end + if sname == nil then return log:error(MOD_ABBR, "dhcp config does not contain a dnsmasq section") end + if uci:get('dhcp', sname, 'address') ~= nil then return log:verbose(MOD_ABBR, "DNS address redirection already in place, not re-adding", false) end uci:set('dhcp', sname, 'address', {redirText}) commitBit(dirtyList, 'dhcp'); reloadBit(dirtyList, 'dnsmasq') end function reconf.dnsredir_rm(dirtyList) local sname = utils.getUciSectionName('dhcp', 'dnsmasq') - if sname == nil then return log:error("dhcp config does not contain a dnsmasq section") end + if sname == nil then return log:error(MOD_ABBR, "dhcp config does not contain a dnsmasq section") end uci:delete('dhcp', sname, 'address') commitBit(dirtyList, 'dhcp'); reloadBit(dirtyList, 'dnsmasq') @@ -230,21 +232,21 @@ end --TODO: handle os.rename() return values (nil+msg on error) function reconf.wwwcaptive_add(dirtyList) if utils.exists(M.WWW_CAPTIVE_INDICATOR) then - return log:verbose("WWW captive directory already in place, not redoing", false) + return log:verbose(MOD_ABBR, "WWW captive directory already in place, not redoing", false) end local rv,reason = os.rename('/www', M.WWW_RENAME_NAME) if rv == true then utils.symlink(M.WWW_CAPTIVE_PATH, '/www') return true else - return log:error("Could not rename /www to " .. M.WWW_RENAME_NAME .. "(" .. reason .. ")") + return log:error(MOD_ABBR, "Could not rename /www to " .. M.WWW_RENAME_NAME .. "(" .. reason .. ")") end end function reconf.wwwcaptive_rm(dirtyList) - if not utils.exists(M.WWW_CAPTIVE_INDICATOR) then return log:verbose("WWW captive directory not in place, not undoing", false) end + if not utils.exists(M.WWW_CAPTIVE_INDICATOR) then return log:verbose(MOD_ABBR, "WWW captive directory not in place, not undoing", false) end os.remove('/www') if os.rename(M.WWW_RENAME_NAME, '/www') ~= true then - return log:error("Could not rename " .. M.WWW_RENAME_NAME .. " to /www") + return log:error(MOD_ABBR, "Could not rename " .. M.WWW_RENAME_NAME .. " to /www") end return true end @@ -286,21 +288,21 @@ function M.setupAccessPoint(ssid) M.setStatus(M.CREATED,"Access point created"); local ds = wifi.getDeviceState() - --log:info(" network/status: ") - log:info(" ssid: ".. utils.dump(ds.ssid)) - --[[log:info(" bssid: ".. utils.dump(ds.bssid)) - log:info(" channel: ".. utils.dump(ds.channel)) - log:info(" mode: ".. utils.dump(ds.mode)) - log:info(" encryption: ".. utils.dump(ds.encryption)) - log:info(" quality: ".. utils.dump(ds.quality)) - log:info(" quality_max: ".. utils.dump(ds.quality_max)) - log:info(" txpower: ".. utils.dump(ds.txpower)) - log:info(" signal: ".. utils.dump(ds.signal)) - log:info(" noise: ".. utils.dump(ds.noise)) - log:info(" raw: ".. utils.dump(ds)) + --log:info(MOD_ABBR, " network/status: ") + log:info(MOD_ABBR, " ssid: ".. utils.dump(ds.ssid)) + --[[log:info(MOD_ABBR, " bssid: ".. utils.dump(ds.bssid)) + log:info(MOD_ABBR, " channel: ".. utils.dump(ds.channel)) + log:info(MOD_ABBR, " mode: ".. utils.dump(ds.mode)) + log:info(MOD_ABBR, " encryption: ".. utils.dump(ds.encryption)) + log:info(MOD_ABBR, " quality: ".. utils.dump(ds.quality)) + log:info(MOD_ABBR, " quality_max: ".. utils.dump(ds.quality_max)) + log:info(MOD_ABBR, " txpower: ".. utils.dump(ds.txpower)) + log:info(MOD_ABBR, " signal: ".. utils.dump(ds.signal)) + log:info(MOD_ABBR, " noise: ".. utils.dump(ds.noise)) + log:info(MOD_ABBR, " raw: ".. utils.dump(ds)) local localip = wifi.getLocalIP() - log:info(" localip: "..utils.dump(localip))]]-- + log:info(MOD_ABBR, " localip: "..utils.dump(localip))]]-- return true end @@ -311,13 +313,13 @@ end -- @tparam string ssid The SSID to use for the access point. -- @return True on success or nil+msg on error. function M.enableAccessPoint(ssid) - log:verbose("enableAccessPoint ssid: ".. utils.dump(ssid)) + log:verbose(MOD_ABBR, "enableAccessPoint ssid: ".. utils.dump(ssid)) M.switchConfiguration{apnet="add_noreload"} wifi.activateConfig(ssid) local ds = wifi.getDeviceState() - log:verbose(" ssid: ".. utils.dump(ds.ssid)) + log:verbose(MOD_ABBR, " ssid: ".. utils.dump(ds.ssid)) return true end @@ -330,7 +332,7 @@ end -- @tparam boolean recreate If true, a new UCI configuration based on scan data will always be created, otherwise an attempt will be made to use an existing configuration. -- @return True on success or nil+msg on error. function M.associateSsid(ssid, passphrase, recreate) - log:info("netconfig:associateSsid: "..(ssid or "")..", "..(recreate or "")) + log:info(MOD_ABBR, "netconfig:associateSsid: "..(ssid or "")..", "..(recreate or "")) M.setStatus(M.CONNECTING,"Connecting..."); -- see if previously configured network for given ssid exists @@ -374,7 +376,7 @@ function M.associateSsid(ssid, passphrase, recreate) local nextAttemptTime = os.time() while true do if os.time() > nextAttemptTime then - log:verbose("associated check "..utils.dump(attempt).."/"..utils.dump(maxAttempts)) + log:verbose(MOD_ABBR, "associated check "..utils.dump(attempt).."/"..utils.dump(maxAttempts)) if wifi.getLocalIP() ~= nil and wifi.getDeviceState().ssid == ssid then break else @@ -394,9 +396,9 @@ function M.associateSsid(ssid, passphrase, recreate) -- signin to connect.doodle3d.com local success, output = signin.signin() if success then - log:info("Signed in") + log:info(MOD_ABBR, "Signed in") else - log:info("Signing in failed") + log:info(MOD_ABBR, "Signing in failed") end -- report we are connected after signin attempt @@ -417,24 +419,24 @@ function M.disassociate() end function M.getStatus() - log:info("network:getStatus") + log:info(MOD_ABBR, "network:getStatus") local file, error = io.open('/tmp/networkstatus.txt','r') if file == nil then - --log:error("Util:Access:Can't read controller file. Error: "..error) + --log:error(MOD_ABBR, "Util:Access:Can't read controller file. Error: "..error) return "","" else local status = file:read('*a') - --log:info(" status: "..utils.dump(status)) + --log:info(MOD_ABBR, " status: "..utils.dump(status)) file:close() local code, msg = string.match(status, '([^|]+)|+(.*)') - --log:info(" code: "..utils.dump(code)) - --log:info(" msg: "..utils.dump(msg)) + --log:info(MOD_ABBR, " code: "..utils.dump(code)) + --log:info(MOD_ABBR, " msg: "..utils.dump(msg)) return code,msg end end function M.setStatus(code,msg) - log:info("network:setStatus: "..code.." | "..msg) + log:info(MOD_ABBR, "network:setStatus: "..code.." | "..msg) local file = io.open('/tmp/networkstatus.txt','w') file:write(code.."|"..msg) file:flush() diff --git a/src/network/signin.lua b/src/network/signin.lua index 3ad9fe2..6d8a8cf 100644 --- a/src/network/signin.lua +++ b/src/network/signin.lua @@ -20,6 +20,7 @@ local status = require('util.status') local M = {} local STATUS_FILE = "signinstatus" +local MOD_ABBR = "NTSI" local IDLE_STATUS = 1 local SIGNING_IN_STATUS = 2 @@ -28,14 +29,14 @@ local SIGNING_IN_STATUS = 2 -- function M.signin() - --log:verbose("signin:signin"); + --log:verbose(MOD_ABBR, "signin:signin"); local code, msg = M.getStatus() - --log:verbose(" status: "..utils.dump(code).." "..utils.dump(msg)); + --log:verbose(MOD_ABBR, " status: "..utils.dump(code).." "..utils.dump(msg)); -- if we are already signin in, skip if(code == SIGNING_IN_STATUS) then - log:verbose(" skipping signin") + log:verbose(MOD_ABBR, " skipping signin") return end @@ -53,10 +54,10 @@ function M.signin() local signinResponse = "" while true do if os.time() > nextAttemptTime then - log:verbose("signin attempt "..utils.dump(attempt).."/"..utils.dump(maxAttempts)) + log:verbose(MOD_ABBR, "signin attempt "..utils.dump(attempt).."/"..utils.dump(maxAttempts)) local signedin = false local localip = wifi.getLocalIP(); - --log:verbose(" localip: "..utils.dump(localip)) + --log:verbose(MOD_ABBR, " localip: "..utils.dump(localip)) if localip ~= nil then local wifiboxid = wifi.getSubstitutedSsid(settings.get('network.cl.wifiboxid')) @@ -64,16 +65,16 @@ function M.signin() local cmd = "wget -q -T 2 -t 1 -O - "..baseurl.."?wifiboxid="..wifiboxid.."\\&localip="..localip; signinResponse = utils.captureCommandOutput(cmd); - log:verbose(" signin response: \n"..utils.dump(signinResponse)) + log:verbose(MOD_ABBR, " signin response: \n"..utils.dump(signinResponse)) local success = signinResponse:match('"status":"success"') - log:verbose(" success: "..utils.dump(success)) + log:verbose(MOD_ABBR, " success: "..utils.dump(success)) if success ~= nil then signedin = true else - log:warning("signin failed request failed (response: "..utils.dump(signinResponse)..")") + log:warning(MOD_ABBR, "signin failed request failed (response: "..utils.dump(signinResponse)..")") end else - log:warning("signin failed no local ip found (attempt: "..utils.dump(attempt).."/"..utils.dump(maxAttempts)..")") + log:warning(MOD_ABBR, "signin failed no local ip found (attempt: "..utils.dump(attempt).."/"..utils.dump(maxAttempts)..")") end if signedin then @@ -100,7 +101,7 @@ function M.getStatus() end function M.setStatus(code,msg) - log:info("signin:setStatus: "..code.." | "..msg) + log:info(MOD_ABBR, "signin:setStatus: "..code.." | "..msg) status.set(STATUS_FILE,code,msg); end diff --git a/src/network/wlanconfig.lua b/src/network/wlanconfig.lua index 1b149c8..0a63573 100644 --- a/src/network/wlanconfig.lua +++ b/src/network/wlanconfig.lua @@ -13,6 +13,8 @@ local iwinfo = require('iwinfo') local M = {} +local MOD_ABBR = "NTWL" + -- NOTE: fallback device 'radio0' is required because sometimes the wlan0 device disappears M.DFL_DEVICE = 'wlan0' M.DFL_DEVICE_FALLBACK = 'radio0' @@ -75,7 +77,7 @@ function M.init(device) deviceName = M.DFL_DEVICE_FALLBACK deviceApi = iwinfo.type(deviceName) - log:info("wireless device '" .. devInitial .. "' not found, trying fallback '" .. deviceName .. "'") + log:info(MOD_ABBR, "wireless device '" .. devInitial .. "' not found, trying fallback '" .. deviceName .. "'") if not deviceApi then return false, "No such wireless device: '" .. devInitial .. "' (and fallback '" .. deviceName .. "' does not exist either)" @@ -126,7 +128,7 @@ end --returns the wireless local ip address function M.getLocalIP() local ifconfig, rv = utils.captureCommandOutput("ifconfig wlan0"); - --log:verbose(" ifconfig: \n"..utils.dump(ifconfig)); + --log:verbose(MOD_ABBR, " ifconfig: \n"..utils.dump(ifconfig)); local localip = ifconfig:match('inet addr:([%d%.]+)') return localip @@ -187,12 +189,12 @@ end --- Activate wireless section for given SSID and disable all others -- @param ssid SSID of config to enable, or nil to disable all network configs function M.activateConfig(ssid) - --log:info("wlanconfig.activateConfig: "..ssid); + --log:info(MOD_ABBR, "wlanconfig.activateConfig: "..ssid); -- make sure only one is enabled uci:foreach('wireless', 'wifi-iface', function(s) local disabled = s.ssid ~= ssid and '1' or '0' - --log:info(" "..utils.dump(s.ssid).." disable: "..utils.dump(disabled)) + --log:info(MOD_ABBR, " "..utils.dump(s.ssid).." disable: "..utils.dump(disabled)) uci:set('wireless', s['.name'], 'disabled', disabled) end) @@ -210,10 +212,10 @@ function M.activateConfig(ssid) return false end end) - --[[log:info(" result:"); + --[[log:info(MOD_ABBR, " result:"); uci:foreach('wireless', 'wifi-iface', function(s) local disabled = s.ssid ~= ssid and '1' or '0' - log:info(" "..utils.dump(s.ssid).." disable: "..utils.dump(disabled)) + log:info(MOD_ABBR, " "..utils.dump(s.ssid).." disable: "..utils.dump(disabled)) end)]]-- uci:commit('wireless') @@ -242,7 +244,7 @@ function M.createConfigFromScanInfo(info, passphrase, disabled) uci:foreach('wireless', 'wifi-iface', function(s) --if s.bssid == info.bssid then if s.ssid == info.ssid then - log:verbose("removing old wireless config for net '" .. s.ssid .. "'") + log:verbose(MOD_ABBR, "removing old wireless config for net '" .. s.ssid .. "'") uci:delete('wireless', s['.name']) -- return false --keep looking, just in case multiple entries with this bssid exist end diff --git a/src/rest/api/api_config.lua b/src/rest/api/api_config.lua index 7fcd9fe..9527919 100644 --- a/src/rest/api/api_config.lua +++ b/src/rest/api/api_config.lua @@ -15,6 +15,8 @@ local wifi = require('network.wlanconfig') local accessManager = require('util.access') local printerAPI = require('rest.api.api_printer') +local MOD_ABBR = "ACFG" + local M = { isApi = true } @@ -57,7 +59,7 @@ end -- returns substituted_wifiboxid (since version 0.10.2) -- returns substituted_ssid (since version 0.9.1) function M._global_POST(request, response) - --log:info("API:config:set") + --log:info(MOD_ABBR, "API:config:set") if not operationsAccessOrFail(request, response) then return end @@ -65,14 +67,14 @@ function M._global_POST(request, response) local validation = {} for k,v in pairs(request:getAll()) do - --log:info(" "..k..": "..v); + --log:info(MOD_ABBR, " "..k..": "..v); local r,m = settings.set(k, v, true) if r then validation[k] = "ok" elseif r == false then validation[k] = "could not save setting ('" .. m .. "')" - log:info(" m: "..utils.dump(m)) + log:info(MOD_ABBR, " m: "..utils.dump(m)) elseif r == nil then settings.commit() response:setError(m) @@ -84,7 +86,7 @@ function M._global_POST(request, response) local substitutedSsid = wifi.getSubstitutedSsid(settings.get('network.ap.ssid')) response:addData("substituted_ssid",substitutedSsid) - + local substitutedWiFiBoxID = wifi.getSubstitutedSsid(settings.get('network.cl.wifiboxid')) response:addData("substituted_wifiboxid",substitutedWiFiBoxID) end @@ -109,12 +111,12 @@ end -- and printer.type is set to 'ultimaker' then -- only the printer.startcode under the ultimaker subsection is removed. function M.reset_POST(request, response) - --log:info("API:reset"); + --log:info(MOD_ABBR, "API:reset"); if not operationsAccessOrFail(request, response) then return end response:setSuccess() for k,v in pairs(request:getAll()) do - --log:info(" "..k..": "..v); + --log:info(MOD_ABBR, " "..k..": "..v); local r,m = settings.reset(k); if r ~= nil then response:addData(k, "ok") diff --git a/src/rest/api/api_info.lua b/src/rest/api/api_info.lua index 729d4a6..947a996 100644 --- a/src/rest/api/api_info.lua +++ b/src/rest/api/api_info.lua @@ -21,6 +21,7 @@ local LOG_COLLECT_DIRNAME = 'wifibox-logs' local LOG_COLLECT_DIR = TMP_DIR .. '/' .. LOG_COLLECT_DIRNAME local WIFIBOX_LOG_FILENAME = 'wifibox.log' local WIFIBOX_LOG_FILE = TMP_DIR .. '/' .. WIFIBOX_LOG_FILENAME +local MOD_ABBR = "AINF" local SYSLOG_FILENAME = 'syslog' local PROCESS_LIST_FILENAME = 'processes' @@ -50,10 +51,10 @@ local M = { -- returns wifiboxid (since version 0.10.2) function M._global(request, response) response:setSuccess() - + local wifiboxid = wifi.getSubstitutedSsid(settings.get('network.cl.wifiboxid')) response:addData('wifiboxid', wifiboxid) - + end -- TODO: redirect stdout+stderr; handle errors @@ -147,12 +148,12 @@ function M.logfiles(request, response) end function M.access(request, response) - --log:info(" remoteAddress: |"..utils.dump(request.remoteAddress).."|"); - --log:info(" controller: |"..utils.dump(accessManager.getController()).."|"); + --log:info(MOD_ABBR, " remoteAddress: |"..utils.dump(request.remoteAddress).."|"); + --log:info(MOD_ABBR, " controller: |"..utils.dump(accessManager.getController()).."|"); local hasControl = accessManager.hasControl(request.remoteAddress) - -- if hasControl then log:info(" hasControl: true") - -- else log:info(" hasControl: false") end + -- if hasControl then log:info(MOD_ABBR, " hasControl: true") + -- else log:info(MOD_ABBR, " hasControl: false") end response:setSuccess() response:addData('has_control', hasControl) diff --git a/src/rest/api/api_network.lua b/src/rest/api/api_network.lua index 020176a..4e234bc 100644 --- a/src/rest/api/api_network.lua +++ b/src/rest/api/api_network.lua @@ -16,6 +16,8 @@ local signin = require('network.signin') local accessManager = require('util.access') local printerAPI = require('rest.api.api_printer') +local MOD_ABBR = "ANET" + local M = { isApi = true } @@ -149,9 +151,9 @@ function M.associate_POST(request, response) local rv,msg = netconf.associateSsid(argSsid, argPhrase, argRecreate) if rv then - log:info("associated to wifi: "..utils.dump(argSsid)) + log:info(MOD_ABBR, "associated to wifi: "..utils.dump(argSsid)) else - log:info("failed to associate to wifi: "..utils.dump(argSsid).." ("..utils.dump(msg)..")") + log:info(MOD_ABBR, "failed to associate to wifi: "..utils.dump(argSsid).." ("..utils.dump(msg)..")") end end @@ -201,14 +203,14 @@ function M.remove_POST(request, response) end function M.signin(request, response) - log:info("API:Network:signin") + log:info(MOD_ABBR, "API:Network:signin") local success, output = signin.signin() if success then - log:info("API:Network:signed in") + log:info(MOD_ABBR, "API:Network:signed in") response:setSuccess("API:Network:signed in") response:addData("response", output) else - log:info("API:Network:Signing in failed") + log:info(MOD_ABBR, "API:Network:Signing in failed") response:setError("Signing in failed") end end diff --git a/src/rest/api/api_printer.lua b/src/rest/api/api_printer.lua index 9cd44bc..c90c542 100644 --- a/src/rest/api/api_printer.lua +++ b/src/rest/api/api_printer.lua @@ -14,6 +14,8 @@ local printDriver = require('print3d') local printerUtils = require('util.printer') local accessManager = require('util.access') +local MOD_ABBR = "APRN" + local M = { isApi = true } @@ -68,7 +70,7 @@ function M.progress(request, response) return true; end --- NOTE: onlyReturnState is optional and prevents response from being modified +-- NOTE: onlyReturnState is optional and prevents response from being modified, used when calling from within other api call function M.state(request, response, onlyReturnState) local argId = request:get("id") if not onlyReturnState then response:addData('id', argId) end @@ -136,7 +138,7 @@ end function M.stop_POST(request, response) - log:info("API:printer/stop") + log:info(MOD_ABBR, "API:printer/stop") if not accessManager.hasControl(request.remoteAddress) then response:setFail("No control access") @@ -159,6 +161,8 @@ function M.stop_POST(request, response) end end +--requires: gcode(string) (the gcode to be appended) +--accepts: id(string) (the printer ID to append to) --accepts: first(bool) (chunks will be concatenated but output file will be cleared first if this argument is true) --accepts: start(bool) (only when this argument is true will printing be started) function M.print_POST(request, response) @@ -172,7 +176,7 @@ function M.print_POST(request, response) hasControl = true end - log:info(" hasControl: "..utils.dump(hasControl)) + log:info(MOD_ABBR, " hasControl: "..utils.dump(hasControl)) if not hasControl then response:setFail("No control access") return @@ -194,7 +198,7 @@ function M.print_POST(request, response) end if argIsFirst == true then - log:verbose("clearing all gcode for " .. printer:getId()) + log:verbose(MOD_ABBR, "clearing all gcode for " .. printer:getId()) response:addData('gcode_clear',true) local rv,msg = printer:clearGcode() diff --git a/src/rest/api/api_sketch.lua b/src/rest/api/api_sketch.lua index 06375d9..ae71ce2 100644 --- a/src/rest/api/api_sketch.lua +++ b/src/rest/api/api_sketch.lua @@ -16,6 +16,8 @@ local lfs = require('lfs') local log = require('util.logger') local utils = require('util.utils') +local MOD_ABBR = "ASKE" + local M = { isApi = true, SKETCH_DIR = '/root/sketches', @@ -29,7 +31,7 @@ local SKETCH_FILE_EXTENSION = 'svg' -- creates sketch directory, and sets response to error state on failure local function createSketchDirectory() if os.execute('mkdir -p ' .. M.SKETCH_DIR) ~= 0 then - log:error("Error: could not create sketch directory '" .. M.SKETCH_DIR .. "'") + log:error(MOD_ABBR, "Error: could not create sketch directory '" .. M.SKETCH_DIR .. "'") response:setError("could not create sketch directory") return false end @@ -133,7 +135,7 @@ function M._global_POST(request, response) local sketchIdx = listSize > 0 and sketches[listSize] + 1 or 1 local sketchFile = M.SKETCH_DIR .. '/' .. constructSketchFilename(sketchIdx) - log:verbose("saving sketch #" .. sketchIdx .. " (" .. argData:len() .. " bytes)") + log:verbose(MOD_ABBR, "saving sketch #" .. sketchIdx .. " (" .. argData:len() .. " bytes)") local saveFile,msg = io.open(sketchFile, 'w') if not saveFile then diff --git a/src/rest/api/api_update.lua b/src/rest/api/api_update.lua index bae97a0..61386e4 100644 --- a/src/rest/api/api_update.lua +++ b/src/rest/api/api_update.lua @@ -10,6 +10,8 @@ local wifi = require('network.wlanconfig') local netconf = require('network.netconfig') local settings = require('util.settings') +local MOD_ABBR = "AUPD" + -- NOTE: the updater module 'detects' command-line invocation by existence of 'arg', so we have to make sure it is not defined. argStash = arg arg = nil @@ -158,7 +160,7 @@ end function M.install_POST(request, response) local argVersion = request:get("version") local argNoRetain = request:get("no_retain") - log:info("API:update/install (noRetain: "..utils.dump(argNoRetain)..")") + log:info(MOD_ABBR, "API:update/install (noRetain: "..utils.dump(argNoRetain)..")") local noRetain = argNoRetain == 'true' if not operationsAccessOrFail(request, response) then return end diff --git a/src/rest/response.lua b/src/rest/response.lua index 580d2b7..159e825 100644 --- a/src/rest/response.lua +++ b/src/rest/response.lua @@ -20,6 +20,7 @@ local M = {} M.__index = M local REQUEST_ID_ARGUMENT = 'rq_id' +local MOD_ABBR = "HRSP" M.httpStatusCode, M.httpStatusText, M.contentType = nil, nil, nil M.binaryData, M.binarySavename = nil, nil @@ -131,7 +132,7 @@ end --- Call all function on the post-response queue, see @{M:addPostResponseFunction} for details and a side-note. function M:executePostResponseQueue() - --log:info("Response:executePostResponseQueue: " .. utils.dump(self.postResponseQueue)) + --log:info(MOD_ABBR, "Response:executePostResponseQueue: " .. utils.dump(self.postResponseQueue)) for i,fn in ipairs(self.postResponseQueue) do fn() end end @@ -169,7 +170,7 @@ function M:send() end if self.body.status ~= "success" then - log:verbose("Response:"..utils.dump(self.body.status).." ("..utils.dump(self.body.msg)..")") + log:verbose(MOD_ABBR, "Response:"..utils.dump(self.body.status).." ("..utils.dump(self.body.msg)..")") end end diff --git a/src/script/d3d-updater.lua b/src/script/d3d-updater.lua index 9cdb0ff..7bb678d 100644 --- a/src/script/d3d-updater.lua +++ b/src/script/d3d-updater.lua @@ -24,6 +24,8 @@ -- note: take care not to print any text in module functions, as this breaks http responses -- change representation of sysupgrade/factory info in versionInfo? (and also in image index?) <- create api call to get all info on all versions? +local MOD_ABBR = "UPDA" + local M = {} --- Possible states the updater can be in, they are stored in @{STATE_FILE}. @@ -82,8 +84,8 @@ local baseUrl = M.DEFAULT_BASE_URL -- default, can be overwritten by M.setBaseUr -- @string msg The message to log. local function P(lvl, msg) if log then - if lvl == -1 then log:verbose(msg) - elseif lvl == 0 or lvl == 1 then log:info(msg) + if lvl == -1 then log:verbose(MOD_ABBR, msg) + elseif lvl == 0 or lvl == 1 then log:info(MOD_ABBR, msg) end else if (-lvl <= verbosity) then print(msg) end @@ -99,7 +101,7 @@ local function D(msg) P(-1, (log and msg or "(DBG) " .. msg)) end -- Messages will be written to [stderr](http://www.cplusplus.com/reference/cstdio/stderr/), or logged using the logger set with @{setLogger}. -- @string msg The message to log. local function E(msg) - if log then log:error(msg) + if log then log:error(MOD_ABBR, msg) else io.stderr:write(msg .. '\n') end end @@ -792,7 +794,7 @@ end -- @treturn bool|nil True on success (with the 'exception' as noted above) or nil on error. -- @treturn ?string|number (optional) Descriptive message or sysupgrade exit status on error. function M.flashImageVersion(versionEntry, noRetain, devType, isFactory) - if log then log:info("flashImageVersion") end + if log then log:info(MOD_ABBR, "flashImageVersion") end local imgName = M.constructImageFilename(versionEntry.version, devType, isFactory) local cmd = noRetain and 'sysupgrade -n ' or 'sysupgrade ' cmd = cmd .. cachePath .. '/' .. imgName diff --git a/src/util/access.lua b/src/util/access.lua index b7da5c6..cf2ac64 100644 --- a/src/util/access.lua +++ b/src/util/access.lua @@ -10,38 +10,40 @@ local log = require('util.logger') local utils = require('util.utils') local printerUtils = require('util.printer') +local MOD_ABBR = "UACS" + local M = {} function M.hasControl(ip) local controllerIP = M.getController() - + -- no controller stored? we have control if controllerIP == "" then return true end; - + -- controller stored is same as our (requesting) ip? we have control if(controllerIP == ip) then return true end; - + -- no printer connected? we have control local printer,msg = printerUtils.createPrinterOrFail() - if not printer or not printer:hasSocket() then + if not printer or not printer:hasSocket() then M.setController("") -- clear the controller return true end - + -- printer is idle (done printing)? we have control local state = printer:getState() if state == "idle" then -- TODO: define in constants somewhere M.setController("") -- clear controller return true end - + return false end function M.getController() local file, error = io.open('/tmp/controller.txt','r') if file == nil then - --log:error("Util:Access:Can't read controller file. Error: "..error) + --log:error(MOD_ABBR, "Util:Access:Can't read controller file. Error: "..error) return "" else controllerIP = file:read('*a') diff --git a/src/util/logger.lua b/src/util/logger.lua index 39ac173..3ce3af4 100644 --- a/src/util/logger.lua +++ b/src/util/logger.lua @@ -32,7 +32,7 @@ for i,v in ipairs(M.LEVEL) do end -local function log(level, msg, verboseFmt) +local function log(level, module, msg, verboseFmt) if level <= logLevel then local now = os.date('%m-%d %H:%M:%S') local i = debug.getinfo(3) --the stack frame just above the logger call @@ -41,9 +41,10 @@ local function log(level, msg, verboseFmt) local name = i.name or "(nil)" local vVal = 'nil' local m = (type(msg) == 'string') and msg or utils.dump(msg) + if module == nil then module = "LUA " end - if v then logStream:write(now .. " (" .. M.LEVEL[level] .. ") " .. m .. " [" .. name .. "@" .. i.short_src .. ":" .. i.linedefined .. "]\n") - else logStream:write(now .. " (" .. M.LEVEL[level] .. ") " .. m .. "\n") end + if v then logStream:write(now .. " [" .. module .. "] (" .. M.LEVEL[level] .. "): " .. m .. " [" .. name .. "@" .. i.short_src .. ":" .. i.linedefined .. "]\n") + else logStream:write(now .. " [" .. module .. "] (" .. M.LEVEL[level] .. "): " .. m .. "\n") end logStream:flush() end @@ -52,11 +53,11 @@ end --- Initializes the logger. -- @tparam @{util.logger.LEVEL} level Minimum level of messages to log. --- @tparam bool verbose Write verbose log messages (include file/line inforomation). +-- @tparam bool verbose Write verbose log messages (include file/line information). function M:init(level, verboseFmt) logLevel = level or M.LEVEL.warning logVerboseFmt = verboseFmt or false - logStream = stream or io.stdout + --logStream = stream or io.stdout end function M:setLevel(level, verboseFmt) @@ -73,10 +74,14 @@ function M:getLevel() return logLevel, logVerboseFmt end -function M:error(msg, verboseFmt) log(M.LEVEL.error, msg, verboseFmt); return false end -function M:warning(msg, verboseFmt) log(M.LEVEL.warning, msg, verboseFmt); return true end -function M:info(msg, verboseFmt) log(M.LEVEL.info, msg, verboseFmt); return true end -function M:verbose(msg, verboseFmt) log(M.LEVEL.verbose, msg, verboseFmt); return true end -function M:bulk(msg, verboseFmt) log(M.LEVEL.bulk, msg, verboseFmt); return true end +function M:getStream() + return logStream +end + +function M:error(module, msg, verboseFmt) log(M.LEVEL.error, module, msg, verboseFmt); return false end +function M:warning(module, msg, verboseFmt) log(M.LEVEL.warning, module, msg, verboseFmt); return true end +function M:info(module, msg, verboseFmt) log(M.LEVEL.info, module, msg, verboseFmt); return true end +function M:verbose(module, msg, verboseFmt) log(M.LEVEL.verbose, module, msg, verboseFmt); return true end +function M:bulk(module, msg, verboseFmt) log(M.LEVEL.bulk, module, msg, verboseFmt); return true end return M diff --git a/src/util/printer.lua b/src/util/printer.lua index 13ec827..a61781f 100644 --- a/src/util/printer.lua +++ b/src/util/printer.lua @@ -10,6 +10,8 @@ local log = require('util.logger') local utils = require('util.utils') local printDriver = require('print3d') +local MOD_ABBR = "UPRN" + local SUPPORTED_PRINTERS = { rigidbot = "Rigidbot", ultimaker = "Ultimaker", @@ -81,8 +83,8 @@ end --returns a printer instance or nil (and sets error state on response in the latter case) function M.createPrinterOrFail(deviceId, response) - --log:verbose("API:printer:createPrinterOrFail: "..utils.dump(deviceId)) - local msg,printer = nil, nil + --log:verbose(MOD_ABBR, "API:printer:createPrinterOrFail: "..utils.dump(deviceId)) + local rv,msg,printer = nil, nil, nil if deviceId == nil or deviceId == "" then printer,msg = printDriver.getPrinter() @@ -98,7 +100,17 @@ function M.createPrinterOrFail(deviceId, response) return nil end - printer:setLocalLogLevel(log:getLevel()) + -- only log these log setup errors, do not let them prevent further request handling + + rv,msg = printer:setLocalLogStream(log:getStream()) + if not rv then + log:error(MOD_ABBR, "could not set log stream in Lua binding (" .. msg .. ")") + end + + rv,msg = printer:setLocalLogLevel(log:getLevel()) + if not rv then + log:error(MOD_ABBR, "could not set log level '" .. log:getLevel() .. "' in Lua binding (" .. msg .. ")") + end return printer end diff --git a/src/util/settings.lua b/src/util/settings.lua index a01693e..b9d14e3 100644 --- a/src/util/settings.lua +++ b/src/util/settings.lua @@ -19,6 +19,8 @@ local baseconfig = require('conf_defaults') local utils = require('util.utils') local log = require('util.logger') +local MOD_ABBR = "USET" + local M = {} --- UCI config name (i.e., file under `/etc/config`) @@ -159,7 +161,7 @@ end]]-- -- @return The associated value, beware (!) that this may be boolean false for keys of 'bool' type, or nil if the key could not be read because of a UCI error. -- @treturn string Message in case of error. function M.get(key) - --log:info("settings:get: "..utils.dump(key)) + --log:info(MOD_ABBR, "settings:get: "..utils.dump(key)) key = replaceDots(key) -- retrieve settings's base settings from conf_defaults.lua @@ -177,7 +179,7 @@ function M.get(key) local uciV,msg = uci:get(UCI_CONFIG_NAME, section, key) if not uciV and msg ~= nil then local errorMSG = "Issue reading setting '"..utils.dump(key).."': "..utils.dump(msg); - log:info(errorMSG) + log:info(MOD_ABBR, errorMSG) return nil, errorMSG; end @@ -244,21 +246,21 @@ end -- @treturn bool|nil True if everything went well, false if validation error, nil in case of error. -- @treturn ?string Error message in case first return value is nil (invalid key). function M.set(key, value, noCommit) - log:info("settings:set: "..utils.dump(key).." to: "..utils.dump(value)) + log:info(MOD_ABBR, "settings:set: "..utils.dump(key).." to: "..utils.dump(value)) key = replaceDots(key) local r = utils.create(UCI_CONFIG_FILE) local rv, msg = uci:set(UCI_CONFIG_NAME, UCI_CONFIG_SECTION, UCI_CONFIG_TYPE) if not rv and msg ~= nil then local errorMSG = "Issue creating section '"..utils.dump(UCI_CONFIG_SECTION).."': "..utils.dump(msg); - log:info(errorMSG) + log:info(MOD_ABBR, errorMSG) return nil, errorMSG; end local base = getBaseKeyTable(key) if not base then return false,ERR_NO_SUCH_KEY end - --log:info(" base.type: "..utils.dump(base.type)) + --log:info(MOD_ABBR, " base.type: "..utils.dump(base.type)) if base.type == 'bool' then if value ~= "" then value = utils.toboolean(value) @@ -271,7 +273,7 @@ function M.set(key, value, noCommit) return false,"Value isn't a valid int or float" end end - + local valid,m = isValid(value, base) if not valid then return false,m @@ -283,7 +285,7 @@ function M.set(key, value, noCommit) local rv, msg = uci:set(UCI_CONFIG_NAME, section, UCI_CONFIG_TYPE) if not rv and msg ~= nil then local errorMSG = "Issue getting subsection '"..utils.dump(base.subSection).."': "..utils.dump(msg); - log:info(errorMSG) + log:info(MOD_ABBR, errorMSG) return nil, errorMSG; end end @@ -292,14 +294,14 @@ function M.set(key, value, noCommit) local rv, msg = uci:set(UCI_CONFIG_NAME, section, key, toUciValue(value, base.type)) if not rv and msg ~= nil then local errorMSG = "Issue setting setting '"..utils.dump(key).."' in section '"..utils.dump(section).."': "..utils.dump(msg); - log:info(errorMSG) + log:info(MOD_ABBR, errorMSG) return nil, errorMSG; end else local rv, msg = uci:delete(UCI_CONFIG_NAME, section, key) if not rv and msg ~= nil then local errorMSG = "Issue deleting setting '"..utils.dump(key).."' in section '"..utils.dump(section).."': "..utils.dump(msg); - log:info(errorMSG) + log:info(MOD_ABBR, errorMSG) return nil, errorMSG; end end @@ -318,35 +320,35 @@ end -- @string key The key to set. -- @treturn bool|nil True if everything went well, nil in case of error. function M.resetAll() - log:info("settings:resetAll") + log:info(MOD_ABBR, "settings:resetAll") -- find all sections local allSections, msg = uci:get_all(UCI_CONFIG_NAME) if not allSections and msg ~= nil then local errorMSG = "Issue reading all settings: "..utils.dump(msg); - log:info(errorMSG) + log:info(MOD_ABBR, errorMSG) return nil, errorMSG; end - + -- delete all uci sections but system for key,value in pairs(allSections) do if key ~= "system" and not key:match('^[A-Z_]*$') then --TEMP: skip 'constants', which should be moved anyway local rv, msg = uci:delete(UCI_CONFIG_NAME,key) if not rv and msg ~= nil then local errorMSG = "Issue deleting setting '"..utils.dump(key).."': "..utils.dump(msg); - log:info(errorMSG) + log:info(MOD_ABBR, errorMSG) return nil, errorMSG; end end end - + -- reset all to defaults for k,_ in pairs(baseconfig) do if not k:match('^[A-Z_]*$') then --TEMP: skip 'constants', which should be moved anyway M.reset(k,true) end end - + M.commit() return true end @@ -356,7 +358,7 @@ end -- @p[opt=nil] noCommit If true, do not commit the uci configuration; this is more efficient when resetting multiple values -- @treturn bool|nil True if everything went well, nil in case of error. function M.reset(key, noCommit) - log:info("settings:reset: "..utils.dump(key)) + log:info(MOD_ABBR, "settings:reset: "..utils.dump(key)) -- delete key = replaceDots(key) @@ -365,13 +367,13 @@ function M.reset(key, noCommit) local section = UCI_CONFIG_SECTION; if base.subSection ~= nil then section = M.get(base.subSection) - end + end local rv, msg = uci:delete(UCI_CONFIG_NAME, section, key) - -- we can't respond to errors in general here because when a key isn't found + -- we can't respond to errors in general here because when a key isn't found -- (which always happens when reset is used in resetall) it will also generate a error --if not rv and msg ~= nil then -- local errorMSG = "Issue deleting setting '"..utils.dump(key).."' in section '"..section.."': "..utils.dump(msg); - -- log:info(errorMSG) + -- log:info(MOD_ABBR, errorMSG) -- return nil, errorMSG; --end diff --git a/src/util/status.lua b/src/util/status.lua index 3f91736..6d38149 100644 --- a/src/util/status.lua +++ b/src/util/status.lua @@ -9,13 +9,15 @@ local log = require('util.logger') local utils = require('util.utils') +local MOD_ABBR = "USTS" + local M = {} local FOLDER = "/tmp/" local FILE_PREFIX = "d3d-" local FILE_EXTENSION = ".txt" -function getPath(fileName) +function getPath(fileName) return FOLDER..FILE_PREFIX..fileName..FILE_EXTENSION end @@ -23,7 +25,7 @@ function M.get(fileName) local path = getPath(fileName) local file, error = io.open(path,'r') if file == nil then - --log:error("Util:Access:Can't read controller file. Error: "..error) + --log:error(MOD_ABBR, "Util:Access:Can't read controller file. Error: "..error) return "","" else local status = file:read('*a') @@ -35,7 +37,7 @@ function M.get(fileName) end function M.set(fileName,code,msg) - --log:info("setStatus: "..code.." | "..msg) + --log:info(MOD_ABBR, "setStatus: "..code.." | "..msg) local path = getPath(fileName) local file = io.open(path,'w') file:write(code.."|"..msg) From 1439bd0434a44bddd4439f5c4de3fc6ef04c7365 Mon Sep 17 00:00:00 2001 From: Wouter R Date: Mon, 12 May 2014 12:22:16 +0200 Subject: [PATCH 03/62] Change levels and improve consistency in log statements. --- src/main.lua | 19 ++++++++++--------- src/network/netconfig.lua | 25 +++++++++++++------------ src/network/signin.lua | 6 +++--- src/network/wlanconfig.lua | 15 +++++++++------ src/rest/api/api_config.lua | 10 ++++++---- src/rest/api/api_network.lua | 6 +++--- src/rest/api/api_printer.lua | 3 ++- src/rest/api/api_sketch.lua | 2 +- 8 files changed, 47 insertions(+), 39 deletions(-) diff --git a/src/main.lua b/src/main.lua index c481a2b..368b284 100644 --- a/src/main.lua +++ b/src/main.lua @@ -227,31 +227,32 @@ local function main(environment) local version = updater.formatVersion(updater.getCurrentVersion()); log:info(MOD_ABBR, "Doodle3D version: "..util.dump(version)) - log:info(MOD_ABBR, "running in autowifi mode") + log:info(MOD_ABBR, "Running in autowifi mode") local rv,msg = setupAutoWifiMode() if rv then - log:info(MOD_ABBR, "autowifi setup done (" .. msg .. ")") + log:info(MOD_ABBR, "Autowifi setup done (" .. msg .. ")") else - log:error(MOD_ABBR, "autowifi setup failed (" .. msg .. ")") + log:error(MOD_ABBR, "Autowifi setup failed (" .. msg .. ")") end elseif rq:getRequestMethod() == 'CMDLINE' and rq:get('signin') ~= nil then - log:info(MOD_ABBR, "running in signin mode") + log:info(MOD_ABBR, "Running in signin mode") local ds = wifi.getDeviceState() - log:info(MOD_ABBR, " ds.mode: "..util.dump(ds.mode)) + log:info(MOD_ABBR, " wifi deviceState.mode: "..util.dump(ds.mode)) if ds.mode == "sta" then log:info(MOD_ABBR, " attempting signin") local success,msg = Signin.signin() if success then log:info(MOD_ABBR, "Signin successful") else - log:info(MOD_ABBR, "Signin failed: "..util.dump(msg)) + log:warning(MOD_ABBR, "Signin failed: "..util.dump(msg)) end end elseif rq:getRequestMethod() ~= 'CMDLINE' or confDefaults.DEBUG_API then - -- log:info(MOD_ABBR, "received request of type " .. rq:getRequestMethod() .. " for " .. (rq:getRequestedApiModule() or "") - -- .. "/" .. (rq:getRealApiFunctionName() or "") .. " with arguments: " .. util.dump(rq:getAll())) + -- Note: the commented log statement will print too many data if it's for instance dumping a gcode add request + --log:info(MOD_ABBR, "received request of type " .. rq:getRequestMethod() .. " for " .. (rq:getRequestedApiModule() or "") + -- .. "/" .. (rq:getRealApiFunctionName() or "") .. " with arguments: " .. util.dump(rq:getAll())) log:info(MOD_ABBR, "received request of type " .. rq:getRequestMethod() .. " for " .. (rq:getRequestedApiModule() or "") .. "/" .. (rq:getRealApiFunctionName() or "")) if rq:getRequestMethod() ~= 'CMDLINE' then @@ -287,7 +288,7 @@ function handle_request(env) resp:setError("initialization failed" .. errSuffix) resp:send() - log:error(MOD_ABBR, "initialization failed" .. errSuffix) --NOTE: this assumes the logger has been initialized properly, despite init() having failed + log:error(MOD_ABBR, "Initialization failed" .. errSuffix) --NOTE: this assumes the logger has been initialized properly, despite init() having failed return 1 else diff --git a/src/network/netconfig.lua b/src/network/netconfig.lua index 14fe69b..7863377 100644 --- a/src/network/netconfig.lua +++ b/src/network/netconfig.lua @@ -64,7 +64,7 @@ function M.switchConfiguration(components) for k,v in pairs(components) do local fname = k .. '_' .. v if type(reconf[fname]) == 'function' then - log:verbose(MOD_ABBR, "reconfiguring component '" .. k .. "' (" .. v .. ")") + log:info(MOD_ABBR, "reconfiguring component '" .. k .. "' (" .. v .. ")") reconf[fname](dirtyList) else log:warning(MOD_ABBR, "unknown component or action '" .. fname .. "' skipped") @@ -94,7 +94,7 @@ function M.reloadComponent(c, silent) os.execute(cmd) else rv = utils.captureCommandOutput(cmd) - log:verbose(MOD_ABBR, " result reloading component '" .. c .. "' (cmd: '"..cmd.."'): \n"..utils.dump(rv)) + log:info(MOD_ABBR, " result reloading component '" .. c .. "' (cmd: '"..cmd.."'): \n"..utils.dump(rv)) end end @@ -215,7 +215,7 @@ function reconf.dnsredir_add(dirtyList) local redirText = '/#/' .. settings.get('network.ap.address') local sname = utils.getUciSectionName('dhcp', 'dnsmasq') if sname == nil then return log:error(MOD_ABBR, "dhcp config does not contain a dnsmasq section") end - if uci:get('dhcp', sname, 'address') ~= nil then return log:verbose(MOD_ABBR, "DNS address redirection already in place, not re-adding", false) end + if uci:get('dhcp', sname, 'address') ~= nil then return log:info(MOD_ABBR, "DNS address redirection already in place, not re-adding", false) end uci:set('dhcp', sname, 'address', {redirText}) commitBit(dirtyList, 'dhcp'); reloadBit(dirtyList, 'dnsmasq') @@ -232,7 +232,7 @@ end --TODO: handle os.rename() return values (nil+msg on error) function reconf.wwwcaptive_add(dirtyList) if utils.exists(M.WWW_CAPTIVE_INDICATOR) then - return log:verbose(MOD_ABBR, "WWW captive directory already in place, not redoing", false) + return log:info(MOD_ABBR, "WWW captive directory already in place, not redoing", false) end local rv,reason = os.rename('/www', M.WWW_RENAME_NAME) if rv == true then @@ -243,7 +243,7 @@ function reconf.wwwcaptive_add(dirtyList) end end function reconf.wwwcaptive_rm(dirtyList) - if not utils.exists(M.WWW_CAPTIVE_INDICATOR) then return log:verbose(MOD_ABBR, "WWW captive directory not in place, not undoing", false) end + if not utils.exists(M.WWW_CAPTIVE_INDICATOR) then return log:info(MOD_ABBR, "WWW captive directory not in place, not undoing", false) end os.remove('/www') if os.rename(M.WWW_RENAME_NAME, '/www') ~= true then return log:error(MOD_ABBR, "Could not rename " .. M.WWW_RENAME_NAME .. " to /www") @@ -290,7 +290,8 @@ function M.setupAccessPoint(ssid) local ds = wifi.getDeviceState() --log:info(MOD_ABBR, " network/status: ") log:info(MOD_ABBR, " ssid: ".. utils.dump(ds.ssid)) - --[[log:info(MOD_ABBR, " bssid: ".. utils.dump(ds.bssid)) + --[[ + log:info(MOD_ABBR, " bssid: ".. utils.dump(ds.bssid)) log:info(MOD_ABBR, " channel: ".. utils.dump(ds.channel)) log:info(MOD_ABBR, " mode: ".. utils.dump(ds.mode)) log:info(MOD_ABBR, " encryption: ".. utils.dump(ds.encryption)) @@ -302,7 +303,8 @@ function M.setupAccessPoint(ssid) log:info(MOD_ABBR, " raw: ".. utils.dump(ds)) local localip = wifi.getLocalIP() - log:info(MOD_ABBR, " localip: "..utils.dump(localip))]]-- + log:info(MOD_ABBR, " localip: "..utils.dump(localip)) + --]] return true end @@ -313,13 +315,13 @@ end -- @tparam string ssid The SSID to use for the access point. -- @return True on success or nil+msg on error. function M.enableAccessPoint(ssid) - log:verbose(MOD_ABBR, "enableAccessPoint ssid: ".. utils.dump(ssid)) + log:info(MOD_ABBR, "enableAccessPoint ssid: ".. utils.dump(ssid)) M.switchConfiguration{apnet="add_noreload"} wifi.activateConfig(ssid) local ds = wifi.getDeviceState() - log:verbose(MOD_ABBR, " ssid: ".. utils.dump(ds.ssid)) + log:info(MOD_ABBR, " deviceState.ssid: ".. utils.dump(ds.ssid)) return true end @@ -392,6 +394,7 @@ function M.associateSsid(ssid, passphrase, recreate) end end end + log:info(MOD_ABBR, "Network associated") -- signin to connect.doodle3d.com local success, output = signin.signin() @@ -426,11 +429,9 @@ function M.getStatus() return "","" else local status = file:read('*a') - --log:info(MOD_ABBR, " status: "..utils.dump(status)) file:close() local code, msg = string.match(status, '([^|]+)|+(.*)') - --log:info(MOD_ABBR, " code: "..utils.dump(code)) - --log:info(MOD_ABBR, " msg: "..utils.dump(msg)) + log:verbose(MOD_ABBR, " raw status: "..utils.dump(status).." (code: "..utils.dump(code)..", msg: "..utils.dump(msg)..")") return code,msg end end diff --git a/src/network/signin.lua b/src/network/signin.lua index 6d8a8cf..6576d95 100644 --- a/src/network/signin.lua +++ b/src/network/signin.lua @@ -54,7 +54,7 @@ function M.signin() local signinResponse = "" while true do if os.time() > nextAttemptTime then - log:verbose(MOD_ABBR, "signin attempt "..utils.dump(attempt).."/"..utils.dump(maxAttempts)) + log:verbose(MOD_ABBR, " signin attempt "..utils.dump(attempt).."/"..utils.dump(maxAttempts)) local signedin = false local localip = wifi.getLocalIP(); --log:verbose(MOD_ABBR, " localip: "..utils.dump(localip)) @@ -71,10 +71,10 @@ function M.signin() if success ~= nil then signedin = true else - log:warning(MOD_ABBR, "signin failed request failed (response: "..utils.dump(signinResponse)..")") + log:warning(MOD_ABBR, "signin failed, request failed (response: "..utils.dump(signinResponse)..")") end else - log:warning(MOD_ABBR, "signin failed no local ip found (attempt: "..utils.dump(attempt).."/"..utils.dump(maxAttempts)..")") + log:warning(MOD_ABBR, "signin failed, no local ip found (attempt: "..utils.dump(attempt).."/"..utils.dump(maxAttempts)..")") end if signedin then diff --git a/src/network/wlanconfig.lua b/src/network/wlanconfig.lua index 0a63573..39f7fd3 100644 --- a/src/network/wlanconfig.lua +++ b/src/network/wlanconfig.lua @@ -128,7 +128,7 @@ end --returns the wireless local ip address function M.getLocalIP() local ifconfig, rv = utils.captureCommandOutput("ifconfig wlan0"); - --log:verbose(MOD_ABBR, " ifconfig: \n"..utils.dump(ifconfig)); + log:bulk(MOD_ABBR, " ifconfig: \n"..utils.dump(ifconfig)); local localip = ifconfig:match('inet addr:([%d%.]+)') return localip @@ -189,12 +189,12 @@ end --- Activate wireless section for given SSID and disable all others -- @param ssid SSID of config to enable, or nil to disable all network configs function M.activateConfig(ssid) - --log:info(MOD_ABBR, "wlanconfig.activateConfig: "..ssid); + log:verbose(MOD_ABBR, "wlanconfig.activateConfig: "..ssid); -- make sure only one is enabled uci:foreach('wireless', 'wifi-iface', function(s) local disabled = s.ssid ~= ssid and '1' or '0' - --log:info(MOD_ABBR, " "..utils.dump(s.ssid).." disable: "..utils.dump(disabled)) + log:verbose(MOD_ABBR, " ssid: "..utils.dump(s.ssid).." disabled: "..utils.dump(disabled)) uci:set('wireless', s['.name'], 'disabled', disabled) end) @@ -212,11 +212,14 @@ function M.activateConfig(ssid) return false end end) - --[[log:info(MOD_ABBR, " result:"); + + --[[ + log:verbose(MOD_ABBR, " wifi reorder result:"); uci:foreach('wireless', 'wifi-iface', function(s) local disabled = s.ssid ~= ssid and '1' or '0' - log:info(MOD_ABBR, " "..utils.dump(s.ssid).." disable: "..utils.dump(disabled)) - end)]]-- + log:verbose(MOD_ABBR, " ssid: "..utils.dump(s.ssid).." disabled: "..utils.dump(disabled)) + end) + --]] uci:commit('wireless') end diff --git a/src/rest/api/api_config.lua b/src/rest/api/api_config.lua index 9527919..45de490 100644 --- a/src/rest/api/api_config.lua +++ b/src/rest/api/api_config.lua @@ -59,7 +59,7 @@ end -- returns substituted_wifiboxid (since version 0.10.2) -- returns substituted_ssid (since version 0.9.1) function M._global_POST(request, response) - --log:info(MOD_ABBR, "API:config:set") + log:verbose(MOD_ABBR, "API:config(set)") if not operationsAccessOrFail(request, response) then return end @@ -67,14 +67,14 @@ function M._global_POST(request, response) local validation = {} for k,v in pairs(request:getAll()) do - --log:info(MOD_ABBR, " "..k..": "..v); + log:verbose(MOD_ABBR, " about to set '"..k.."' -> '"..v.."'"); local r,m = settings.set(k, v, true) if r then validation[k] = "ok" elseif r == false then validation[k] = "could not save setting ('" .. m .. "')" - log:info(MOD_ABBR, " m: "..utils.dump(m)) + log:info(MOD_ABBR, " failed to set '"..k.."' ("..utils.dump(m)..")") elseif r == nil then settings.commit() response:setError(m) @@ -111,7 +111,7 @@ end -- and printer.type is set to 'ultimaker' then -- only the printer.startcode under the ultimaker subsection is removed. function M.reset_POST(request, response) - --log:info(MOD_ABBR, "API:reset"); + --log:verbose(MOD_ABBR, "API:config/reset") if not operationsAccessOrFail(request, response) then return end response:setSuccess() @@ -120,9 +120,11 @@ function M.reset_POST(request, response) local r,m = settings.reset(k); if r ~= nil then response:addData(k, "ok") + log:verbose(MOD_ABBR, " reset " .. k) else response:addData(k, "could not reset key ('" .. m .. "')") response:setError(m) + log:verbose(MOD_ABBR, " could not reset key " .. k .. "(" .. m .. ")") return end end diff --git a/src/rest/api/api_network.lua b/src/rest/api/api_network.lua index 4e234bc..c34c697 100644 --- a/src/rest/api/api_network.lua +++ b/src/rest/api/api_network.lua @@ -203,14 +203,14 @@ function M.remove_POST(request, response) end function M.signin(request, response) - log:info(MOD_ABBR, "API:Network:signin") + log:info(MOD_ABBR, "API:network/signin") local success, output = signin.signin() if success then - log:info(MOD_ABBR, "API:Network:signed in") + log:info(MOD_ABBR, " signed in") response:setSuccess("API:Network:signed in") response:addData("response", output) else - log:info(MOD_ABBR, "API:Network:Signing in failed") + log:info(MOD_ABBR, "signing in failed") response:setError("Signing in failed") end end diff --git a/src/rest/api/api_printer.lua b/src/rest/api/api_printer.lua index c90c542..9fc693b 100644 --- a/src/rest/api/api_printer.lua +++ b/src/rest/api/api_printer.lua @@ -166,6 +166,7 @@ end --accepts: first(bool) (chunks will be concatenated but output file will be cleared first if this argument is true) --accepts: start(bool) (only when this argument is true will printing be started) function M.print_POST(request, response) + log:info(MOD_ABBR, "API:printer/print") local controllerIP = accessManager.getController() local hasControl = false @@ -198,7 +199,7 @@ function M.print_POST(request, response) end if argIsFirst == true then - log:verbose(MOD_ABBR, "clearing all gcode for " .. printer:getId()) + log:verbose(MOD_ABBR, " clearing all gcode for " .. printer:getId()) response:addData('gcode_clear',true) local rv,msg = printer:clearGcode() diff --git a/src/rest/api/api_sketch.lua b/src/rest/api/api_sketch.lua index ae71ce2..1cb32c8 100644 --- a/src/rest/api/api_sketch.lua +++ b/src/rest/api/api_sketch.lua @@ -31,7 +31,7 @@ local SKETCH_FILE_EXTENSION = 'svg' -- creates sketch directory, and sets response to error state on failure local function createSketchDirectory() if os.execute('mkdir -p ' .. M.SKETCH_DIR) ~= 0 then - log:error(MOD_ABBR, "Error: could not create sketch directory '" .. M.SKETCH_DIR .. "'") + log:error(MOD_ABBR, "could not create sketch directory '" .. M.SKETCH_DIR .. "'") response:setError("could not create sketch directory") return false end From b29eb5289ec8b8ccc9c233614f7f287c1f4d0bdb Mon Sep 17 00:00:00 2001 From: Wouter R Date: Mon, 12 May 2014 12:33:00 +0200 Subject: [PATCH 04/62] Initial version of log filter/highlight script. --- src/script/loglite-filters.lua | 20 ++++ src/script/loglite.lua | 183 +++++++++++++++++++++++++++++++++ 2 files changed, 203 insertions(+) create mode 100644 src/script/loglite-filters.lua create mode 100644 src/script/loglite.lua diff --git a/src/script/loglite-filters.lua b/src/script/loglite-filters.lua new file mode 100644 index 0000000..d0171b0 --- /dev/null +++ b/src/script/loglite-filters.lua @@ -0,0 +1,20 @@ +local M = {} + +M.printstart = { + ['options'] = { ['mode'] = 'delete' }, + ['patterns'] = { + ['print started'] = '_uppercase,bwhite' + } +} + +M.test = { + ['options'] = { 'delete_mode' }, + ['patterns'] = { + ['(verbose)'] = 'underline,cyan,_delete' + ['(info)'] = 'magenta', + ['ABSD'] = '_nodelete', + ['SE?RV?'] = 'bblue' + } +} + +return M diff --git a/src/script/loglite.lua b/src/script/loglite.lua new file mode 100644 index 0000000..f9034ce --- /dev/null +++ b/src/script/loglite.lua @@ -0,0 +1,183 @@ +-- Notes +-- * directives: either a color, a color prefixed by 'b' or one of: _delete, _nodelete, [_matchonly] +-- * pattern rules are matched top to bottom, the last one encountered overriding any previous conflicting directive +-- +-- TODO: +-- * move script to firmware repo (since it's shared between that and print3d) and remove commit from print3d +-- * pre-split keyword lists for efficiency? +-- * keep formats separate and only concat in the end, so things like upperasing can work properly +-- * add more directives like uppercase, prefix/suffix? +-- * options: en/dis total count, en/dis match count (how to deal with multiple matches?), en/dis keep_mode / delete_mode/ +-- * create named sets of options+patterns to allow for task-specific filter sets - choose/parse options and includes (pcall require()d) in main and pass on to workhorse function +-- * add specialized patterns for levels/modules? +-- +-- FIXME: +-- * with deleteMode enabled, multiple matches and _nodelete in a later match, previous directives are ignored + +--[[ +-- * https://stackoverflow.com/questions/17363973/how-can-i-tail-f-a-log-filetruncate-aware-in-lua +-- * http://pueblo.sourceforge.net/doc/manual/ansi_color_codes.html +]]-- + + +--[[========================================================================]]-- + +local ANSI_COLORS = { + ['blink'] = 5, -- no dice on osx/iterm2 + ['underline'] = 24, -- no dice on osx/iterm2 + ['black'] = 30, + ['red'] = 31, + ['green'] = 32, + ['yellow'] = 33, + ['blue'] = 34, + ['magenta'] = 35, + ['cyan'] = 36, + ['white'] = 37, + ['bblack'] = 40, + ['bred'] = 41, + ['bgreen'] = 42, + ['byellow'] = 43, + ['bblue'] = 44, + ['bmagenta'] = 45, + ['bcyan'] = 46, + ['bwhite'] = 47 +} + +local ESCAPE_STR = string.char(27) .. "[" +local RESET_CODE = ESCAPE_STR .. "m" +local DFL_FILTERSET_FILE = "loglite-filters.lua" + +local DEFAULT_FILTERSET = { + ['options'] = { 'default_enabled', 'keep_mode' }, + ['patterns'] = { + ['(error)'] = 'red', + ['(warning)'] = 'yellow', + ['(bulk)'] = 'gray' + } +} + + +--- Stringifies the given object. +-- From util/utils.lua +-- Note that self-referencing objects will cause an endless loop with the current implementation. +-- @param o The object to convert. +-- @treturn string Stringified version of o. +local function dump(o) + if type(o) == 'table' then + local s = '{ ' + for k,v in pairs(o) do + if type(k) ~= 'number' then k = '"'..k..'"' end + s = s .. '['..k..'] = ' .. dump(v) .. ',' + end + return s .. '} ' + else + return tostring(o) + end +end + +--- Splits a string on a given divider character. +-- From util/utils.lua +-- @string[opt=':'] div The divider character to use. +-- @return An array containing the resultant substrings. +-- @usage local str = "a,b,c"; local parts = str:split(',') +function string:split(div) + local div, pos, arr = div or ':', 0, {} + for st,sp in function() return self:find(div, pos, true) end do + table.insert(arr, self:sub(pos, st - 1)) + pos = sp + 1 + end + table.insert(arr, self:sub(pos)) + return arr +end + +local function makeAnsiCode(key) + if not ANSI_COLORS[key] then return nil end + return ESCAPE_STR .. ANSI_COLORS[key] .. 'm' +end + +local function hasValue(t, needle) + for k,v in pairs(t) do + if needle == v then return k end + end + return nil +end + + + +--[[========================================================================]]-- + +local function tailStream(stream, patterns, options) + local c = 0 + for line in stream:lines() do + --c = c + 1 -- Note: this would also count deleted lines + local embellished = line + local keepLine = (options.mode == 'keep') + local keepLineOverridden = false + + -- look for a pattern matching this line + for p,c in pairs(patterns) do + if line:match(p) then +-- print("+matched rule '" .. p .. "'/'" .. c .. "' against '" .. line .. "'") + local kws = c:split(',') + + if hasValue(kws, '_delete') then keepLine = false; keepLineOverridden = true + elseif hasValue(kws, '_nodelete') then keepLine = true; keepLineOverridden = true + end + + if keepLine then + -- first collect formatting sequences + local fmt = '' + for _,kw in ipairs(kws) do + local code = makeAnsiCode(kw) + if code then fmt = fmt .. code end + end + + -- then wrap the line in formatting, if any + if fmt:len() > 0 then embellished = fmt .. embellished .. RESET_CODE end + else + -- Note: break out of loop and stop processing when line should be deleted _if_ the default has been overridden to do so + if keepLineOverridden then + embellished = nil + break + end + end + + --break -- Note: don't break, allow multiple matches per line, e.g. to mix and match fg and bg colors + end + end + + if embellished and keepLine then + c = c + 1 + + if options.count == 'all' then print(c, embellished) + else print(embellished) end + else +-- print("-skipped '"..line.."'") + end + + --c = line:match 'truncated' and 0 or c -- from tail on stderr apparently + end +end + +local function main() + if #arg > 0 and arg[1] == "-h" or arg[1] == "--help" then + print("Usage: either pass file to tail as argument, or pipe through stdin.") + os.exit(0) + end + + local followFile = #arg > 0 and arg[1] or nil + + --print("following file: '" .. (followFile and followFile or "") .. "'.") + + + --local tailin = io.popen('tail -F '..(...)..' 2>&1', 'r') + local tailin = followFile and io.popen('tail -f ' .. followFile, 'r') or io.stdin + + local patterns = DEFAULT_FILTERSET.patterns + local options = { ['mode'] = 'keep', count = 'all' } + + pcall(tailStream, tailin, patterns, options) -- Note: protected call to suppress interrupt error thrown by lines iterator +end + +main() +os.exit(0) From bca5228b734a521f92d2ccaa105e66e902d8df09 Mon Sep 17 00:00:00 2001 From: Wouter R Date: Mon, 12 May 2014 13:46:38 +0200 Subject: [PATCH 05/62] More changes to log messages. --- src/rest/response.lua | 10 +++++----- src/util/access.lua | 2 +- src/util/printer.lua | 2 +- src/util/settings.lua | 18 +++++++++--------- src/util/status.lua | 2 +- 5 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/rest/response.lua b/src/rest/response.lua index 159e825..e1588f0 100644 --- a/src/rest/response.lua +++ b/src/rest/response.lua @@ -130,11 +130,11 @@ function M:addPostResponseFunction(fn) table.insert(self.postResponseQueue, fn) end ---- Call all function on the post-response queue, see @{M:addPostResponseFunction} for details and a side-note. +--- Call all functions on the post-response queue, see @{M:addPostResponseFunction} for details and a side-note. function M:executePostResponseQueue() - --log:info(MOD_ABBR, "Response:executePostResponseQueue: " .. utils.dump(self.postResponseQueue)) + log:verbose(MOD_ABBR, "Response:executePostResponseQueue: " .. utils.dump(self.postResponseQueue)) - for i,fn in ipairs(self.postResponseQueue) do fn() end + for i,fn in ipairs(self.postResponseQueue) do fn() end end --- Returns an API url pointing to @{conf_defaults.API_BASE_URL_PATH}, which is quite useless. @@ -153,7 +153,7 @@ function M:serializeAsJson() return JSON:encode(self.body) end ---- Writes HTTP headers, followed by an HTTP body containing JSON data to stdout. +--- Writes HTTP headers to stdout, followed by an HTTP body containing either JSON data or a file attachment. function M:send() printHeaderLine("Status", self.httpStatusCode .. " " .. self.httpStatusText) printHeaderLine("Content-Type", self.contentType) @@ -170,7 +170,7 @@ function M:send() end if self.body.status ~= "success" then - log:verbose(MOD_ABBR, "Response:"..utils.dump(self.body.status).." ("..utils.dump(self.body.msg)..")") + log:verbose(MOD_ABBR, "Response: "..utils.dump(self.body.status).." ("..utils.dump(self.body.msg)..")") end end diff --git a/src/util/access.lua b/src/util/access.lua index cf2ac64..73acbb5 100644 --- a/src/util/access.lua +++ b/src/util/access.lua @@ -43,7 +43,7 @@ end function M.getController() local file, error = io.open('/tmp/controller.txt','r') if file == nil then - --log:error(MOD_ABBR, "Util:Access:Can't read controller file. Error: "..error) + --log:error(MOD_ABBR, "Util:Access: Can't read controller file. Error: "..error) return "" else controllerIP = file:read('*a') diff --git a/src/util/printer.lua b/src/util/printer.lua index a61781f..48ce912 100644 --- a/src/util/printer.lua +++ b/src/util/printer.lua @@ -83,7 +83,7 @@ end --returns a printer instance or nil (and sets error state on response in the latter case) function M.createPrinterOrFail(deviceId, response) - --log:verbose(MOD_ABBR, "API:printer:createPrinterOrFail: "..utils.dump(deviceId)) + --log:info(MOD_ABBR, "API:printer:createPrinterOrFail: "..utils.dump(deviceId)) local rv,msg,printer = nil, nil, nil if deviceId == nil or deviceId == "" then diff --git a/src/util/settings.lua b/src/util/settings.lua index b9d14e3..d8c1d8f 100644 --- a/src/util/settings.lua +++ b/src/util/settings.lua @@ -179,7 +179,7 @@ function M.get(key) local uciV,msg = uci:get(UCI_CONFIG_NAME, section, key) if not uciV and msg ~= nil then local errorMSG = "Issue reading setting '"..utils.dump(key).."': "..utils.dump(msg); - log:info(MOD_ABBR, errorMSG) + log:warning(MOD_ABBR, errorMSG) return nil, errorMSG; end @@ -246,14 +246,14 @@ end -- @treturn bool|nil True if everything went well, false if validation error, nil in case of error. -- @treturn ?string Error message in case first return value is nil (invalid key). function M.set(key, value, noCommit) - log:info(MOD_ABBR, "settings:set: "..utils.dump(key).." to: "..utils.dump(value)) + --log:info(MOD_ABBR, "settings:set: "..utils.dump(key).." to: "..utils.dump(value)) key = replaceDots(key) local r = utils.create(UCI_CONFIG_FILE) local rv, msg = uci:set(UCI_CONFIG_NAME, UCI_CONFIG_SECTION, UCI_CONFIG_TYPE) if not rv and msg ~= nil then local errorMSG = "Issue creating section '"..utils.dump(UCI_CONFIG_SECTION).."': "..utils.dump(msg); - log:info(MOD_ABBR, errorMSG) + log:warning(MOD_ABBR, errorMSG) return nil, errorMSG; end @@ -285,7 +285,7 @@ function M.set(key, value, noCommit) local rv, msg = uci:set(UCI_CONFIG_NAME, section, UCI_CONFIG_TYPE) if not rv and msg ~= nil then local errorMSG = "Issue getting subsection '"..utils.dump(base.subSection).."': "..utils.dump(msg); - log:info(MOD_ABBR, errorMSG) + log:warning(MOD_ABBR, errorMSG) return nil, errorMSG; end end @@ -294,14 +294,14 @@ function M.set(key, value, noCommit) local rv, msg = uci:set(UCI_CONFIG_NAME, section, key, toUciValue(value, base.type)) if not rv and msg ~= nil then local errorMSG = "Issue setting setting '"..utils.dump(key).."' in section '"..utils.dump(section).."': "..utils.dump(msg); - log:info(MOD_ABBR, errorMSG) + log:warning(MOD_ABBR, errorMSG) return nil, errorMSG; end else local rv, msg = uci:delete(UCI_CONFIG_NAME, section, key) if not rv and msg ~= nil then local errorMSG = "Issue deleting setting '"..utils.dump(key).."' in section '"..utils.dump(section).."': "..utils.dump(msg); - log:info(MOD_ABBR, errorMSG) + log:warning(MOD_ABBR, errorMSG) return nil, errorMSG; end end @@ -326,7 +326,7 @@ function M.resetAll() local allSections, msg = uci:get_all(UCI_CONFIG_NAME) if not allSections and msg ~= nil then local errorMSG = "Issue reading all settings: "..utils.dump(msg); - log:info(MOD_ABBR, errorMSG) + log:warning(MOD_ABBR, errorMSG) return nil, errorMSG; end @@ -336,7 +336,7 @@ function M.resetAll() local rv, msg = uci:delete(UCI_CONFIG_NAME,key) if not rv and msg ~= nil then local errorMSG = "Issue deleting setting '"..utils.dump(key).."': "..utils.dump(msg); - log:info(MOD_ABBR, errorMSG) + log:warning(MOD_ABBR, errorMSG) return nil, errorMSG; end end @@ -373,7 +373,7 @@ function M.reset(key, noCommit) -- (which always happens when reset is used in resetall) it will also generate a error --if not rv and msg ~= nil then -- local errorMSG = "Issue deleting setting '"..utils.dump(key).."' in section '"..section.."': "..utils.dump(msg); - -- log:info(MOD_ABBR, errorMSG) + -- log:warning(MOD_ABBR, errorMSG) -- return nil, errorMSG; --end diff --git a/src/util/status.lua b/src/util/status.lua index 6d38149..557ebe7 100644 --- a/src/util/status.lua +++ b/src/util/status.lua @@ -25,7 +25,7 @@ function M.get(fileName) local path = getPath(fileName) local file, error = io.open(path,'r') if file == nil then - --log:error(MOD_ABBR, "Util:Access:Can't read controller file. Error: "..error) + --log:warning(MOD_ABBR, "Util:Access:Can't read controller file. Error: "..error) return "","" else local status = file:read('*a') From 52c94355345d5ce0c1f05415c823ec9c44f9062e Mon Sep 17 00:00:00 2001 From: Wouter R Date: Wed, 4 Jun 2014 23:19:59 +0200 Subject: [PATCH 06/62] Fixups after cherry-picking commits from unify-logging+chunk-checking branch: - add missing log module arguments, - remove temporary log level mapping. --- src/main.lua | 9 ++------- src/network/wlanconfig.lua | 12 ++++++------ 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/src/main.lua b/src/main.lua index 368b284..c4b38bc 100644 --- a/src/main.lua +++ b/src/main.lua @@ -77,9 +77,9 @@ local function setupAutoWifiMode() -- signin to connect.doodle3d.com local success, output = Signin.signin() if success then - log:info("Signed in") + log:info(MOD_ABBR, "Signed in") else - log:info("Signing in failed") + log:info(MOD_ABBR, "Signing in failed") end -- report we are connected after signin attempt netconf.setStatus(netconf.CONNECTED,"Connected"); @@ -131,11 +131,6 @@ local function setupLogger() local logTargetSetting = settings.getSystemKey('logfile') local logLevelSetting = settings.get('system.log.level') local logTargetError, logLevelError = nil, nil - - -- TEMP: for now, translate print3d log level to firmware level, these will be unfiied in the future - -- we get (print3d): quiet,error,warning,info,verbose,bulk -- and we need (firmware): debug,info,warn,error,fatal - logLevelMapping = { quiet='fatal', error='error', warning='warn', info='warn', verbose='info', bulk='debug' } - logLevelSetting = logLevelMapping[logLevelSetting] if type(logTargetSetting) == 'string' then local specialTarget = logTargetSetting:match('^<(.*)>$') diff --git a/src/network/wlanconfig.lua b/src/network/wlanconfig.lua index 39f7fd3..5f626c4 100644 --- a/src/network/wlanconfig.lua +++ b/src/network/wlanconfig.lua @@ -27,12 +27,12 @@ local deviceName, deviceApi local cachedApSsid, baseApSsid = nil, nil function M.getSubstitutedSsid(unformattedSsid) - log:debug("getSubstitutedSsid unformattedSsid:'" .. (unformattedSsid or "nil") .. "' baseApSsid:'" .. (baseApSsid or "nil") .. "' cachedApSsid:'" .. (cachedApSsid or "nil")) + log:debug(MOD_ABBR, "getSubstitutedSsid unformattedSsid:'" .. (unformattedSsid or "nil") .. "' baseApSsid:'" .. (baseApSsid or "nil") .. "' cachedApSsid:'" .. (cachedApSsid or "nil")) if unformattedSsid == baseApSsid and cachedApSsid ~= nil then return cachedApSsid end if not unformattedSsid or type(unformattedSsid) ~= 'string' then return nil end local macTail = M.getMacAddress():sub(7) - log:debug(" macTail:'" .. macTail) + log:debug(MOD_ABBR, " macTail:'" .. macTail) baseApSsid = unformattedSsid cachedApSsid = unformattedSsid:gsub('%%%%MAC_ADDR_TAIL%%%%', macTail) return cachedApSsid @@ -109,9 +109,9 @@ end --returns the wireless device's MAC address (as string, without colons) --(lua numbers on openWrt seem to be 32bit so they cannot represent a MAC address as one number) function M.getMacAddress() - log:debug("getMacAddress") + log:debug(MOD_ABBR, "getMacAddress") local macText = utils.readFile('/sys/class/net/' .. deviceName .. '/address') - log:debug(" macText: '" .. (macText or "nil") .. "'") + log:debug(MOD_ABBR, " macText: '" .. (macText or "nil") .. "'") local out = '' -- Hack to prevent failure in case the MAC address could not be obtained. @@ -143,9 +143,9 @@ end -- @return data for all or requested network; false+error on failure or nil when requested network not found function M.getScanInfo(ssid) local iw = iwinfo[deviceApi] - log:info("start wifi scan") + log:info(MOD_ABBR, "start wifi scan") local sr = iw.scanlist(deviceName) - log:info("wifi scan done") + log:info(MOD_ABBR, "wifi scan done") local si, se if ssid == nil then From 759c0a3007bc145fca914e19e2aef99541ff942f Mon Sep 17 00:00:00 2001 From: Wouter R Date: Mon, 19 May 2014 21:39:59 +0200 Subject: [PATCH 07/62] Install loglite filter to wifibox. Small fixes to loglite filter. Set dummy host/port/agent info on command-line API call. Remove non-functional version info from shell banner. New todo item in release script. Conflicts: extra/scripts/publish-wifibox-release.lua --- Makefile | 4 ++++ extra/scripts/publish-wifibox-release.lua | 4 ++-- post-install.sh | 2 +- src/rest/request.lua | 4 ++++ src/script/loglite-filters.lua | 2 +- src/script/loglite.lua | 10 ++++++---- 6 files changed, 18 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index 63cdfae..c0f17e2 100644 --- a/Makefile +++ b/Makefile @@ -88,6 +88,7 @@ define Package/wifibox/install #$(INSTALL_DIR) $(1)/etc $(INSTALL_DIR) $(1)/etc/init.d $(INSTALL_DIR) $(1)/etc/config + $(INSTALL_DIR) $(1)/root/ #$(INSTALL_DIR) $(1)/www $(INSTALL_DIR) $(1)/www/cgi-bin @@ -102,6 +103,9 @@ define Package/wifibox/install $(INSTALL_BIN) $(WIFIBOX_BASE_DIR)/script/d3d-updater.lua $(1)/$(TGT_LUA_DIR_SUFFIX)/script $(LN) -s /$(TGT_LUA_DIR_SUFFIX)/script/d3d-updater.lua $(1)/bin/d3d-updater + $(CP) $(WIFIBOX_BASE_DIR)/script/loglite-filters.lua $(1)/root/ + $(INSTALL_BIN) $(WIFIBOX_BASE_DIR)/script/loglite.lua $(1)/$(TGT_LUA_DIR_SUFFIX)/script + $(LN) -s /$(TGT_LUA_DIR_SUFFIX)/script/loglite.lua $(1)/bin/loglite $(INSTALL_BIN) $(WIFIBOX_BASE_DIR)/script/wifibox_init $(1)/etc/init.d/wifibox $(INSTALL_BIN) $(WIFIBOX_BASE_DIR)/script/dhcpcheck_init $(1)/etc/init.d/dhcpcheck $(INSTALL_BIN) $(WIFIBOX_BASE_DIR)/script/d3dapi $(1)/$(TGT_LUA_DIR_SUFFIX)/script diff --git a/extra/scripts/publish-wifibox-release.lua b/extra/scripts/publish-wifibox-release.lua index 6cbcacd..1154304 100755 --- a/extra/scripts/publish-wifibox-release.lua +++ b/extra/scripts/publish-wifibox-release.lua @@ -27,8 +27,8 @@ -- HostName ftp.greenhost.nl -- Some basic sanity checks are built in (unique version, updated release notes, 'clean' openwrt config) -- but lots others are still missing (mainly: clean git repo's, freshly built images). --- The script must be run from within the openwrt build root. So it's handy to create a symlink --- to this file. You could to something like from the build root: +-- The script must be run from within the openwrt build root. So it's handy to create a symlink +-- to this file. You could to something like from the build root: -- ln -s ~/wrt-wifibox-feed/doodle3d-firmware/extra/scripts/publish-wifibox-release.lua . -- Then you can start with: -- cd trunk ../publish-wifibox-release.lua diff --git a/post-install.sh b/post-install.sh index b85d63c..8f5865a 100644 --- a/post-install.sh +++ b/post-install.sh @@ -38,7 +38,7 @@ if [ ! -f $IPKG_INSTROOT/etc/banner.default ]; then mv $IPKG_INSTROOT/etc/banner $IPKG_INSTROOT/etc/banner.default cat <<-'EOM' > $IPKG_INSTROOT/etc/banner ........D o o d l e 3 D - .......________ _____ _____ v \$(PACKAGE_VERSION) + .......________ _____ _____ ....../ / / |__ / __/ / - /___ __ ...../ / / /--// _|-// --| . /v / ..../________/__//__/__//____/___/_^_\ diff --git a/src/rest/request.lua b/src/rest/request.lua index 5c52c8f..00ad3e4 100644 --- a/src/rest/request.lua +++ b/src/rest/request.lua @@ -181,6 +181,10 @@ function M.new(environment, postData, debugEnabled) self.remoteAddress = environment['REMOTE_ADDR'] else self.requestMethod = 'CMDLINE' + self.remoteHost = "cmdline" + self.remotePort = "0" + self.userAgent = "shell" + self.remoteAddress = "0.0.0.0" end self.cmdLineArgs = kvTableFromArray(arg) diff --git a/src/script/loglite-filters.lua b/src/script/loglite-filters.lua index d0171b0..6d74ade 100644 --- a/src/script/loglite-filters.lua +++ b/src/script/loglite-filters.lua @@ -10,7 +10,7 @@ M.printstart = { M.test = { ['options'] = { 'delete_mode' }, ['patterns'] = { - ['(verbose)'] = 'underline,cyan,_delete' + ['(verbose)'] = 'underline,cyan,_delete', ['(info)'] = 'magenta', ['ABSD'] = '_nodelete', ['SE?RV?'] = 'bblue' diff --git a/src/script/loglite.lua b/src/script/loglite.lua index f9034ce..4e88d9b 100644 --- a/src/script/loglite.lua +++ b/src/script/loglite.lua @@ -1,3 +1,5 @@ +#!/usr/bin/env lua + -- Notes -- * directives: either a color, a color prefixed by 'b' or one of: _delete, _nodelete, [_matchonly] -- * pattern rules are matched top to bottom, the last one encountered overriding any previous conflicting directive @@ -50,9 +52,9 @@ local DFL_FILTERSET_FILE = "loglite-filters.lua" local DEFAULT_FILTERSET = { ['options'] = { 'default_enabled', 'keep_mode' }, ['patterns'] = { - ['(error)'] = 'red', - ['(warning)'] = 'yellow', - ['(bulk)'] = 'gray' + ['%(error%)'] = 'red', + ['%(warning%)'] = 'yellow', + ['%(bulk%)'] = 'gray' } } @@ -174,7 +176,7 @@ local function main() local tailin = followFile and io.popen('tail -f ' .. followFile, 'r') or io.stdin local patterns = DEFAULT_FILTERSET.patterns - local options = { ['mode'] = 'keep', count = 'all' } + local options = { ['mode'] = 'keep', count = 'none' } pcall(tailStream, tailin, patterns, options) -- Note: protected call to suppress interrupt error thrown by lines iterator end From 4f84e1060bdacec344fe1f05612533116b29a353 Mon Sep 17 00:00:00 2001 From: Wouter R Date: Wed, 28 May 2014 09:51:27 +0200 Subject: [PATCH 08/62] Add padding to level names in log messages. Other minor changes. --- src/rest/response.lua | 2 +- src/script/loglite.lua | 3 ++- src/util/logger.lua | 18 +++++++++++++++--- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/rest/response.lua b/src/rest/response.lua index e1588f0..e740341 100644 --- a/src/rest/response.lua +++ b/src/rest/response.lua @@ -170,7 +170,7 @@ function M:send() end if self.body.status ~= "success" then - log:verbose(MOD_ABBR, "Response: "..utils.dump(self.body.status).." ("..utils.dump(self.body.msg)..")") + log:warning(MOD_ABBR, "Response status: "..utils.dump(self.body.status).." ("..utils.dump(self.body.msg)..")") end end diff --git a/src/script/loglite.lua b/src/script/loglite.lua index 4e88d9b..52ff68d 100644 --- a/src/script/loglite.lua +++ b/src/script/loglite.lua @@ -54,7 +54,8 @@ local DEFAULT_FILTERSET = { ['patterns'] = { ['%(error%)'] = 'red', ['%(warning%)'] = 'yellow', - ['%(bulk%)'] = 'gray' + ['%(bulk%)'] = 'gray', + ['setState%(%)'] = 'bblue' } } diff --git a/src/util/logger.lua b/src/util/logger.lua index 3ce3af4..5ce3d1a 100644 --- a/src/util/logger.lua +++ b/src/util/logger.lua @@ -15,6 +15,8 @@ local M = {} local logLevel, logVerboseFmt, logStream +local LONGEST_LEVEL_NAME = -1 + --- Available log levels (starting at 1) -- @table LEVEL M.LEVEL = { @@ -26,12 +28,19 @@ M.LEVEL = { 'bulk' -- debug information (in large amounts) } --- M.LEVEL already has idx=>name entries, now create name=>idx entries so it can be indexed both ways + +--[[== module initialization code ==]]-- + +-- M.LEVEL already has idx=>name entries, now create name=>idx entries so it can be indexed both ways, and init LONGEST_LEVEL_NAME for i,v in ipairs(M.LEVEL) do M.LEVEL[v] = i + if v:len() > LONGEST_LEVEL_NAME then LONGEST_LEVEL_NAME = v:len() end end +--[[================================]]-- + + local function log(level, module, msg, verboseFmt) if level <= logLevel then local now = os.date('%m-%d %H:%M:%S') @@ -43,8 +52,11 @@ local function log(level, module, msg, verboseFmt) local m = (type(msg) == 'string') and msg or utils.dump(msg) if module == nil then module = "LUA " end - if v then logStream:write(now .. " [" .. module .. "] (" .. M.LEVEL[level] .. "): " .. m .. " [" .. name .. "@" .. i.short_src .. ":" .. i.linedefined .. "]\n") - else logStream:write(now .. " [" .. module .. "] (" .. M.LEVEL[level] .. "): " .. m .. "\n") end + local levelName = M.LEVEL[level] + local padding = string.rep(' ', LONGEST_LEVEL_NAME - levelName:len()) + + if v then logStream:write(now .. " [" .. module .. "] (" .. levelName .. ")" .. padding .. ": " .. m .. " [" .. name .. "@" .. i.short_src .. ":" .. i.linedefined .. "]\n") + else logStream:write(now .. " [" .. module .. "] (" .. levelName .. ")" .. padding .. ": " .. m .. "\n") end logStream:flush() end From af76720e54b12e0d07269857987230cfba3ab26f Mon Sep 17 00:00:00 2001 From: Wouter R Date: Wed, 4 Jun 2014 23:19:59 +0200 Subject: [PATCH 09/62] Prepare log subsystem to be consistent with changes in print3d. --- src/main.lua | 49 +++++++++++++++++++++++++---------- src/rest/api/api_info.lua | 13 ++++++---- src/script/wifibox.uci.config | 5 +++- src/util/logger.lua | 4 +++ 4 files changed, 51 insertions(+), 20 deletions(-) diff --git a/src/main.lua b/src/main.lua index c4b38bc..4701e8e 100644 --- a/src/main.lua +++ b/src/main.lua @@ -120,36 +120,57 @@ local function setupAutoWifiMode() end --- Initializes the logging system to use the file and level defined in the system settings. --- The settings used are `logfile` and `loglevel`. The former may either be a --- reular file path, or `` or ``. +-- The settings used are `log_path`, `api_log_filename` from the system section and +-- `system_log_level` from the general section. The filename may either be a regular filename +-- (with an absolute log_path), or `` or ``. +-- TODO: also support backticks (see Logger.cpp in print3d)-- -- @see util.settings.getSystemKey -- @treturn bool True on success, false on error. local function setupLogger() local logStream = io.stderr -- use stderr as hard-coded default target local logLevel = log.LEVEL.verbose -- use verbose logging as hard-coded default level - local logTargetSetting = settings.getSystemKey('logfile') - local logLevelSetting = settings.get('system.log.level') + local logPathSetting = settings.getSystemKey('log_path') + local logTargetSetting = settings.getSystemKey('api_log_filename') + local logLevelSetting = settings.get('system_log_level') local logTargetError, logLevelError = nil, nil if type(logTargetSetting) == 'string' then - local specialTarget = logTargetSetting:match('^<(.*)>$') - if specialTarget then - if specialTarget == 'stdout' then logStream = io.stdout - elseif specialTarget == 'stderr' then logStream = io.stderr + local streamTarget = logTargetSetting:match('^<(.*)>$') + local popenTarget = logTargetSetting:match('^`(.*)`$') + if streamTarget then + if streamTarget:lower() == 'stdout' then logStream = io.stdout + elseif streamTarget:lower() == 'stderr' then logStream = io.stderr end - elseif logTargetSetting:sub(1, 1) == '/' then - local f,msg = io.open(logTargetSetting, 'a+') + elseif popenTarget then + local f,msg = io.popen(popenTarget, 'w') if f then logStream = f else logTargetError = msg end + elseif logPathSetting:sub(1, 1) == '/' then + local path = logPathSetting .. '/' .. logTargetSetting + local f,msg = io.open(path, 'a+') + + if f then + logStream = f + log:setLogFilePath(path) + else + logTargetError = msg + end + else + logTargetError = "log file path is not absolute" end else -- if uci config not available, fallback to /tmp/wifibox.log - local f,msg = io.open('/tmp/wifibox.log', 'a+') - if f then logStream = f - else logTargetError = msg + local path = '/tmp/wifibox.log' + local f,msg = io.open(path, 'a+') + + if f then + logStream = f + log:setLogFilePath(path) + else + logTargetError = msg end end @@ -169,7 +190,7 @@ local function setupLogger() local rv = true if logTargetError then - log:error(MOD_ABBR, "could not open logfile '" .. logTargetSetting .. "', using stderr as fallback (" .. logTargetError .. ")") + log:error(MOD_ABBR, "could not open logfile '" .. logPathSetting .. '/' .. logTargetSetting .. "', using stderr as fallback (" .. logTargetError .. ")") rv = false end diff --git a/src/rest/api/api_info.lua b/src/rest/api/api_info.lua index 947a996..2a57df3 100644 --- a/src/rest/api/api_info.lua +++ b/src/rest/api/api_info.lua @@ -19,8 +19,8 @@ local settings = require('util.settings') local TMP_DIR = '/tmp' local LOG_COLLECT_DIRNAME = 'wifibox-logs' local LOG_COLLECT_DIR = TMP_DIR .. '/' .. LOG_COLLECT_DIRNAME -local WIFIBOX_LOG_FILENAME = 'wifibox.log' -local WIFIBOX_LOG_FILE = TMP_DIR .. '/' .. WIFIBOX_LOG_FILENAME +local DEFAULT_WIFIBOX_LOG_FILENAME = 'wifibox.log' +local DEFAULT_WIFIBOX_LOG_FILE = TMP_DIR .. '/' .. DEFAULT_WIFIBOX_LOG_FILENAME local MOD_ABBR = "AINF" local SYSLOG_FILENAME = 'syslog' @@ -35,7 +35,7 @@ local USB_DIRTREE_COMMAND = "ls -R /sys/devices/platform/ehci-platform/usb1 | gr local USB_DIRTREE_FILENAME = 'sys_devices_platform_ehci-platform_usb1.tree' local PRINT3D_BASEPATH = '/tmp' -local PRINT3D_LOG_FILENAME_PREFIX = 'print3d-' +local PRINT3D_LOG_FILENAME_PREFIX = 'print3d.' local PRINT3D_LOG_FILENAME_SUFFIX = '.log' local LOG_COLLECT_ARCHIVE_FILENAME = LOG_COLLECT_DIRNAME .. '.tgz' local LOG_COLLECT_ARCHIVE_FILE = TMP_DIR .. '/' .. LOG_COLLECT_ARCHIVE_FILENAME @@ -64,11 +64,14 @@ function M.logfiles(request, response) rv,msg = lfs.mkdir(LOG_COLLECT_DIR) rv,msg = lfs.chdir(TMP_DIR) + local wifiboxLogFilePath = log:getLogFilePath() + local wifiboxLogFileName = wifiboxLogFilePath and wifiboxLogFilePath:match('.*/(.*)') + --[[ create temporary files ]]-- -- copy wifibox API-script log - rv,sig,code = redirectedExecute('cp ' .. WIFIBOX_LOG_FILE .. ' ' .. LOG_COLLECT_DIR) + rv,sig,code = redirectedExecute('cp ' .. wifiboxLogFilePath .. ' ' .. LOG_COLLECT_DIR) -- capture syslog rv,sig,code = os.execute('logread > ' .. LOG_COLLECT_DIR .. '/' .. SYSLOG_FILENAME) @@ -140,7 +143,7 @@ function M.logfiles(request, response) rv,sig,code = redirectedExecute('rm ' .. LOG_COLLECT_DIR .. '/' .. MEMINFO_FILENAME) rv,sig,code = redirectedExecute('rm ' .. LOG_COLLECT_DIR .. '/' .. PROCESS_LIST_FILENAME) rv,sig,code = redirectedExecute('rm ' .. LOG_COLLECT_DIR .. '/' .. SYSLOG_FILENAME) - rv,sig,code = redirectedExecute('rm ' .. LOG_COLLECT_DIR .. '/' .. WIFIBOX_LOG_FILENAME) + rv,sig,code = redirectedExecute('rm ' .. LOG_COLLECT_DIR .. '/' .. wifiboxLogFileName) rv,msg = lfs.rmdir(LOG_COLLECT_DIR) diff --git a/src/script/wifibox.uci.config b/src/script/wifibox.uci.config index 0194e07..f025c08 100644 --- a/src/script/wifibox.uci.config +++ b/src/script/wifibox.uci.config @@ -1,5 +1,8 @@ config settings 'system' - option logfile '/tmp/wifibox.log' + #Note that using an SSH tunnel without public key auth will need interaction + option log_path '/tmp' + option api_log_filename 'wifibox.log' + option p3d_log_basename 'print3d' #or: '', or: '`ssh USER@HOST "cat > /tmp/test.log"`' config settings 'general' option printer_type 'ultimaker' diff --git a/src/util/logger.lua b/src/util/logger.lua index 5ce3d1a..80604e9 100644 --- a/src/util/logger.lua +++ b/src/util/logger.lua @@ -14,6 +14,7 @@ local utils = require('util.utils') local M = {} local logLevel, logVerboseFmt, logStream +local logFilePath = nil -- only used for reference local LONGEST_LEVEL_NAME = -1 @@ -90,6 +91,9 @@ function M:getStream() return logStream end +function M:setLogFilePath(path) logFilePath = path end +function M:getLogFilePath() return logFilePath end + function M:error(module, msg, verboseFmt) log(M.LEVEL.error, module, msg, verboseFmt); return false end function M:warning(module, msg, verboseFmt) log(M.LEVEL.warning, module, msg, verboseFmt); return true end function M:info(module, msg, verboseFmt) log(M.LEVEL.info, module, msg, verboseFmt); return true end From 9fd96740a3ab20247a1984b85ca79da067b931b4 Mon Sep 17 00:00:00 2001 From: Wouter R Date: Mon, 16 Jun 2014 14:19:29 +0200 Subject: [PATCH 10/62] Remove unnecessary require. Change backup log file name in d3dapi script. --- src/rest/api/api_printer.lua | 2 +- src/script/d3dapi | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/rest/api/api_printer.lua b/src/rest/api/api_printer.lua index 9fc693b..40704ca 100644 --- a/src/rest/api/api_printer.lua +++ b/src/rest/api/api_printer.lua @@ -10,7 +10,6 @@ local lfs = require('lfs') local log = require('util.logger') local utils = require('util.utils') local settings = require('util.settings') -local printDriver = require('print3d') local printerUtils = require('util.printer') local accessManager = require('util.access') @@ -165,6 +164,7 @@ end --accepts: id(string) (the printer ID to append to) --accepts: first(bool) (chunks will be concatenated but output file will be cleared first if this argument is true) --accepts: start(bool) (only when this argument is true will printing be started) +--returns: when the gcode buffer cannot accept the gcode, or the IPC transaction fails, a fail with a (formal, i.e., parseable) status argument will be returned function M.print_POST(request, response) log:info(MOD_ABBR, "API:printer/print") diff --git a/src/script/d3dapi b/src/script/d3dapi index 3dbe552..c97fab5 100644 --- a/src/script/d3dapi +++ b/src/script/d3dapi @@ -10,15 +10,16 @@ LUA=lua SCRIPT_PATH=/usr/share/lua/wifibox -LOG_FILE=/tmp/wifibox.log +LOG_FILE=/tmp/wifibox.cgi-fallback.log cd $SCRIPT_PATH -echo "CGI invocation" >> $LOG_FILE +#echo "CGI invocation" >> $LOG_FILE #strace -o /tmp/trace-$$.log -ff $LUA ./cmdmain.lua $@ 2>> $LOG_FILE $LUA ./cmdmain.lua $@ 2>> $LOG_FILE exit $? + # Code below is for debugging incoming CGI data read -n $CONTENT_LENGTH POSTDATA echo -e "Content-type: text/plain\r\n\r\n" From c3756f53487376f3d415b9f9d47b666d7c42b5d1 Mon Sep 17 00:00:00 2001 From: Wouter R Date: Mon, 19 May 2014 21:37:17 +0200 Subject: [PATCH 11/62] Fix return values in printer api. Pass along chunk metadata in printer api. Move wifi station mode check from main.lua to signin.lua so it is always run past. Only log postresponsequeue if it is not empty. Conflicts: src/rest/api/api_printer.lua --- src/main.lua | 19 ++++----- src/network/netconfig.lua | 1 + src/network/signin.lua | 12 ++++-- src/rest/api/api_info.lua | 2 +- src/rest/api/api_printer.lua | 75 ++++++++++++++++++++++++------------ src/rest/response.lua | 2 +- 6 files changed, 70 insertions(+), 41 deletions(-) diff --git a/src/main.lua b/src/main.lua index 4701e8e..501b753 100644 --- a/src/main.lua +++ b/src/main.lua @@ -254,16 +254,12 @@ local function main(environment) elseif rq:getRequestMethod() == 'CMDLINE' and rq:get('signin') ~= nil then log:info(MOD_ABBR, "Running in signin mode") - local ds = wifi.getDeviceState() - log:info(MOD_ABBR, " wifi deviceState.mode: "..util.dump(ds.mode)) - if ds.mode == "sta" then - log:info(MOD_ABBR, " attempting signin") - local success,msg = Signin.signin() - if success then - log:info(MOD_ABBR, "Signin successful") - else - log:warning(MOD_ABBR, "Signin failed: "..util.dump(msg)) - end + log:info(MOD_ABBR, " attempting signin") + local success,msg = Signin.signin() + if success then + log:info(MOD_ABBR, "Signin successful") + else + log:warning(MOD_ABBR, "Signin failed: "..util.dump(msg)) end elseif rq:getRequestMethod() ~= 'CMDLINE' or confDefaults.DEBUG_API then -- Note: the commented log statement will print too many data if it's for instance dumping a gcode add request @@ -280,8 +276,7 @@ local function main(environment) if err ~= nil then log:error(MOD_ABBR, err) end response:send() - - response:executePostResponseQueue() + response:executePostResponseQueue() else log:info(MOD_ABBR, "Nothing to do...bye.\n") end diff --git a/src/network/netconfig.lua b/src/network/netconfig.lua index 7863377..f9f7ff8 100644 --- a/src/network/netconfig.lua +++ b/src/network/netconfig.lua @@ -409,6 +409,7 @@ function M.associateSsid(ssid, passphrase, recreate) return true end + --- Disassociate wlan device as client from all SSID's. -- Note: this function might belong in the wlanconfig module but that would introduce -- a circular dependency, easiest solution is to place the function here. diff --git a/src/network/signin.lua b/src/network/signin.lua index 6576d95..4ed7b93 100644 --- a/src/network/signin.lua +++ b/src/network/signin.lua @@ -28,15 +28,21 @@ local SIGNING_IN_STATUS = 2 --- Signin to connect.doodle3d.com server -- function M.signin() - - --log:verbose(MOD_ABBR, "signin:signin"); + log:verbose(MOD_ABBR, "signin:signin"); local code, msg = M.getStatus() --log:verbose(MOD_ABBR, " status: "..utils.dump(code).." "..utils.dump(msg)); -- if we are already signin in, skip if(code == SIGNING_IN_STATUS) then - log:verbose(MOD_ABBR, " skipping signin") + log:verbose(MOD_ABBR, " already signing in, skipping") + return + end + + local ds = wifi.getDeviceState() + log:verbose(MOD_ABBR, " wifi deviceState.mode: "..utils.dump(ds.mode)) + if ds.mode ~= "sta" then + log:verbose(MOD_ABBR, " wifi not in station mode, skipping signin") return end diff --git a/src/rest/api/api_info.lua b/src/rest/api/api_info.lua index 2a57df3..5e07d37 100644 --- a/src/rest/api/api_info.lua +++ b/src/rest/api/api_info.lua @@ -160,7 +160,7 @@ function M.access(request, response) response:setSuccess() response:addData('has_control', hasControl) - return true + return end function M.status(request, response) diff --git a/src/rest/api/api_printer.lua b/src/rest/api/api_printer.lua index 40704ca..faafad8 100644 --- a/src/rest/api/api_printer.lua +++ b/src/rest/api/api_printer.lua @@ -5,6 +5,7 @@ -- @license This software is licensed under the terms of the GNU GPL v2 or later. -- See file LICENSE.txt or visit http://www.gnu.org/licenses/gpl.html for full license details. +-- TODO: return errors like in print_POST (error message in a 'msg' key instead of directly in the response) if this does not break API compatibility local lfs = require('lfs') local log = require('util.logger') @@ -28,7 +29,7 @@ end function M.temperature(request, response) local argId = request:get("id") local printer,msg = printerUtils.createPrinterOrFail(argId, response) - if not printer or not printer:hasSocket() then return false end + if not printer or not printer:hasSocket() then return end local temperatures,msg = printer:getTemperatures() @@ -39,18 +40,18 @@ function M.temperature(request, response) response:addData('hotend_target', temperatures.hotend_target) response:addData('bed', temperatures.bed) response:addData('bed_target', temperatures.bed_target) + elseif temperatures == false then + response:addData('status', msg) + response:setFail() else response:setError(msg) - return false; end - - return true; end function M.progress(request, response) local argId = request:get("id") local printer,msg = printerUtils.createPrinterOrFail(argId, response) - if not printer or not printer:hasSocket() then return false end + if not printer or not printer:hasSocket() then return end -- NOTE: despite their names, `currentLine` is still the error indicator and `bufferedLines` the message in such case. local currentLine,bufferedLines,totalLines = printer:getProgress() @@ -61,15 +62,16 @@ function M.progress(request, response) response:addData('current_line', currentLine) response:addData('buffered_lines', bufferedLines) response:addData('total_lines', totalLines) + elseif progress == false then + response:addData('status', bufferedLines) + response:setFail() else response:setError(bufferedLines) - return false end - - return true; end --- NOTE: onlyReturnState is optional and prevents response from being modified, used when calling from within other api call +-- Note: onlyReturnState is optional and prevents response from being modified, used when calling from within other api call +-- Note: unlike regular API-functions, this one returns either true+state or false function M.state(request, response, onlyReturnState) local argId = request:get("id") if not onlyReturnState then response:addData('id', argId) end @@ -98,19 +100,19 @@ function M.state(request, response, onlyReturnState) response:addData('state', rv) end return true, rv - else + else -- Note: do not differentiate between false and nil here, false should never be returned if not onlyReturnState then response:setError(msg) end return false end end - return true + + --this point cannot be reached, no return necessary end -- retrieve a list of 3D printers currently supported function M.listall(request, response) response:setSuccess() response:addData('printers', printerUtils.supportedPrinters()) - return true end @@ -130,13 +132,17 @@ function M.heatup_POST(request, response) local rv,msg = printer:heatup(temperature) response:addData('id', argId) - if rv then response:setSuccess() - else response:setFail(msg) + if rv then + response:setSuccess() + elseif rv == false then + response:addData('status', msg) + response:setFail() + else + response:setError(msg) end end function M.stop_POST(request, response) - log:info(MOD_ABBR, "API:printer/stop") if not accessManager.hasControl(request.remoteAddress) then @@ -147,7 +153,7 @@ function M.stop_POST(request, response) local argId = request:get("id") local argGcode = request:get("gcode") local printer,msg = printerUtils.createPrinterOrFail(argId, response) - if not printer or not printer:hasSocket() then return false end + if not printer or not printer:hasSocket() then return end if(argGcode == nil) then argGcode = "" @@ -155,8 +161,13 @@ function M.stop_POST(request, response) local rv,msg = printer:stopPrint(argGcode) response:addData('id', argId) - if rv then response:setSuccess() - else response:setError(msg) + if rv then + response:setSuccess() + elseif rv == false then + response:addData('status', msg) + response:setFail() + else + response:setError(msg) end end @@ -164,6 +175,8 @@ end --accepts: id(string) (the printer ID to append to) --accepts: first(bool) (chunks will be concatenated but output file will be cleared first if this argument is true) --accepts: start(bool) (only when this argument is true will printing be started) +--accepts: seq_number(int) (sequence number of the chunk, must be given until clear() after given once, and incremented each time) +--accepts: seq_total(int) (total number of gcode chunks to be appended, must be given until clear() after given once, and stay the same) --returns: when the gcode buffer cannot accept the gcode, or the IPC transaction fails, a fail with a (formal, i.e., parseable) status argument will be returned function M.print_POST(request, response) log:info(MOD_ABBR, "API:printer/print") @@ -187,9 +200,12 @@ function M.print_POST(request, response) local argGcode = request:get("gcode") local argIsFirst = utils.toboolean(request:get("first")) local argStart = utils.toboolean(request:get("start")) + local argSeqNumber = request:get("seq_number") or -1 + local argSeqTotal = request:get("seq_total") or -1 + local remoteHost = request:getRemoteHost() local printer,msg = printerUtils.createPrinterOrFail(argId, response) - if not printer or not printer:hasSocket() then return false end + if not printer or not printer:hasSocket() then return end response:addData('id', argId) @@ -203,7 +219,10 @@ function M.print_POST(request, response) response:addData('gcode_clear',true) local rv,msg = printer:clearGcode() - if not rv then + if rv == false then + response:addData('status', msg) + response:setFail("could not clear gcode") + elseif rv == nil then response:setError(msg) return end @@ -211,14 +230,17 @@ function M.print_POST(request, response) local rv,msg - -- TODO: return errors with a separate argument like here in the rest of the code (this is how we designed the API right?) - rv,msg = printer:appendGcode(argGcode) + rv,msg = printer:appendGcode(argGcode, { seq_number = argSeqNumber, seq_total = argSeqTotal, source = remoteHost }) if rv then --NOTE: this does not report the number of lines, but only the block which has just been added response:addData('gcode_append',argGcode:len()) + elseif rv == false then + response:addData('status', msg) + response:setFail("could not add gcode") + return else - response:setError("could not add gcode") response:addData('msg', msg) + response:setError("could not add gcode") return end @@ -228,9 +250,14 @@ function M.print_POST(request, response) if rv then response:setSuccess() response:addData('gcode_print',true) + elseif rv == false then + response:addData('status', msg) + response:setFail("could not send gcode") + return else - response:setError("could not send gcode") response:addData('msg', msg) + response:setError("could not send gcode") + return end else response:setSuccess() diff --git a/src/rest/response.lua b/src/rest/response.lua index e740341..4f71c43 100644 --- a/src/rest/response.lua +++ b/src/rest/response.lua @@ -132,7 +132,7 @@ end --- Call all functions on the post-response queue, see @{M:addPostResponseFunction} for details and a side-note. function M:executePostResponseQueue() - log:verbose(MOD_ABBR, "Response:executePostResponseQueue: " .. utils.dump(self.postResponseQueue)) + if #self.postResponseQueue > 0 then log:verbose(MOD_ABBR, "Response:executePostResponseQueue: " .. utils.dump(self.postResponseQueue)) end for i,fn in ipairs(self.postResponseQueue) do fn() end end From 1c3f35c84f144e0700bd9174f17c1353cbf433b3 Mon Sep 17 00:00:00 2001 From: Wouter R Date: Fri, 8 Jan 2016 22:46:17 +0100 Subject: [PATCH 12/62] Update comment on possible log targets. Update license-spec. --- license-spec.lua | 2 +- src/script/wifibox.uci.config | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/license-spec.lua b/license-spec.lua index 3f451d5..7ce5907 100644 --- a/license-spec.lua +++ b/license-spec.lua @@ -1,6 +1,6 @@ local M = { BASE_PATH = 'src', - EXCLUDE_FILES = { 'src/util/JSON.lua', 'src/util/urlcode.lua' }, + EXCLUDE_FILES = { 'src/util/JSON.lua', 'src/util/urlcode.lua', 'src/script/loglite%-filters.lua' }, PROCESS_FILES = { ['src/[^/]*%.lua'] = 'lua', ['src/network/[^/]*%.lua'] = 'lua', diff --git a/src/script/wifibox.uci.config b/src/script/wifibox.uci.config index f025c08..dd8146e 100644 --- a/src/script/wifibox.uci.config +++ b/src/script/wifibox.uci.config @@ -1,8 +1,12 @@ config settings 'system' + #In addition to regular text, api_log_filename and p3d_log_basename may also be + #a stream enclose in angle brackets (e.g. '') or + #a command to send output to in back quotes (e.g. '`ssh USER@HOST "cat > /tmp/test.log"`'). #Note that using an SSH tunnel without public key auth will need interaction + option log_path '/tmp' option api_log_filename 'wifibox.log' - option p3d_log_basename 'print3d' #or: '', or: '`ssh USER@HOST "cat > /tmp/test.log"`' + option p3d_log_basename 'print3d' config settings 'general' option printer_type 'ultimaker' From 6d001f4894b0d68f34ec488056934243bc0090c6 Mon Sep 17 00:00:00 2001 From: Wouter R Date: Fri, 5 Feb 2016 15:16:36 +0100 Subject: [PATCH 13/62] Extra description about debugging Lua issues in readme. --- README.md | 11 +++++++++++ src/conf_defaults.lua | 2 ++ 2 files changed, 13 insertions(+) diff --git a/README.md b/README.md index b96cfa0..5e1513d 100644 --- a/README.md +++ b/README.md @@ -10,3 +10,14 @@ The script 'doxify.sh' generates HTML documentation of the source code in the di Make sure the 'ldoc' program is installed on your machine and the LDOC variable in the script points there. On OSX, this can be accomplished by installing it through luarocks (run `sudo luarocks install ldoc`). Luarocks can be installed using [MacPorts](http://www.macports.org/). After installing that, the command would be `sudo port install luarocks`. + + +Debugging Lua +------------- + +Syntax errors in Lua can lead to tricky issues since they might only surface when the faulty code is actually being run. + +One countermeasure for this is to use [pcall](http://www.lua.org/pil/8.4.html) instead of regular calls in many cases. To let the error 'happen' (which in turn gives information in the form of stack traces), tell the code to use regular calls by setting 'M.DEBUG_PCALLS' to 'true' in `conf_defaults.lua`. + +A second way of debugging is to take uhttpd out of the loop and invoke the Lua code from command-line. To do this, set 'M.DEBUG_API' to 'true' in `conf_defaults.lua`. Then invoke the API using the command `d3dapi p=/mod/func r=POST` where `p=` is followed by the API path and `r=` followed by either 'GET' or 'POST'. +Be aware though, that this script redirects output streams to a fallback log file where stack traces will end up, this file is `/tmp/wifibox.cgi-fallback.log`. diff --git a/src/conf_defaults.lua b/src/conf_defaults.lua index 2487b57..5dd9093 100644 --- a/src/conf_defaults.lua +++ b/src/conf_defaults.lua @@ -36,6 +36,8 @@ M.DEBUG_PCALLS = false --- This constant enables debugging of the REST API from the command-line by emulating GET/POST requests. -- Specify the path and optionally the request method as follows: `d3dapi p=/mod/func r=POST`. +-- Note that the command-line script redirects the output streams to '/tmp/wifibox.cgi-fallback.log' +-- meaning that any stack traces can be found there. M.DEBUG_API = true --- If enabled, REST responses will contain 'module' and 'function' keys describing what was requested. From b6ab64d1f23612f081068b8b7ff73be9c393326b Mon Sep 17 00:00:00 2001 From: Wouter R Date: Fri, 5 Feb 2016 15:23:40 +0100 Subject: [PATCH 14/62] Fix invalid log function calls. --- src/network/wlanconfig.lua | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/network/wlanconfig.lua b/src/network/wlanconfig.lua index 5f626c4..b7bff30 100644 --- a/src/network/wlanconfig.lua +++ b/src/network/wlanconfig.lua @@ -27,12 +27,12 @@ local deviceName, deviceApi local cachedApSsid, baseApSsid = nil, nil function M.getSubstitutedSsid(unformattedSsid) - log:debug(MOD_ABBR, "getSubstitutedSsid unformattedSsid:'" .. (unformattedSsid or "nil") .. "' baseApSsid:'" .. (baseApSsid or "nil") .. "' cachedApSsid:'" .. (cachedApSsid or "nil")) + log:verbose(MOD_ABBR, "getSubstitutedSsid unformattedSsid:'" .. (unformattedSsid or "nil") .. "' baseApSsid:'" .. (baseApSsid or "nil") .. "' cachedApSsid:'" .. (cachedApSsid or "nil")) if unformattedSsid == baseApSsid and cachedApSsid ~= nil then return cachedApSsid end if not unformattedSsid or type(unformattedSsid) ~= 'string' then return nil end local macTail = M.getMacAddress():sub(7) - log:debug(MOD_ABBR, " macTail:'" .. macTail) + log:verbose(MOD_ABBR, " macTail:'" .. macTail) baseApSsid = unformattedSsid cachedApSsid = unformattedSsid:gsub('%%%%MAC_ADDR_TAIL%%%%', macTail) return cachedApSsid @@ -109,9 +109,9 @@ end --returns the wireless device's MAC address (as string, without colons) --(lua numbers on openWrt seem to be 32bit so they cannot represent a MAC address as one number) function M.getMacAddress() - log:debug(MOD_ABBR, "getMacAddress") + log:verbose(MOD_ABBR, "getMacAddress") local macText = utils.readFile('/sys/class/net/' .. deviceName .. '/address') - log:debug(MOD_ABBR, " macText: '" .. (macText or "nil") .. "'") + log:verbose(MOD_ABBR, " macText: '" .. (macText or "nil") .. "'") local out = '' -- Hack to prevent failure in case the MAC address could not be obtained. From f3e9ebf26e482d7f8fbd7044f9f105476ca2d62d Mon Sep 17 00:00:00 2001 From: Wouter R Date: Fri, 12 Feb 2016 15:40:07 +0100 Subject: [PATCH 15/62] Report current and maximum size in bytes of gcode buffer in getProgress functions. --- src/rest/api/api_printer.lua | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/rest/api/api_printer.lua b/src/rest/api/api_printer.lua index faafad8..d7927c9 100644 --- a/src/rest/api/api_printer.lua +++ b/src/rest/api/api_printer.lua @@ -54,7 +54,7 @@ function M.progress(request, response) if not printer or not printer:hasSocket() then return end -- NOTE: despite their names, `currentLine` is still the error indicator and `bufferedLines` the message in such case. - local currentLine,bufferedLines,totalLines = printer:getProgress() + local currentLine,bufferedLines,totalLines,bufferSize,maxBufferSize = printer:getProgress() response:addData('id', argId) if currentLine then @@ -62,6 +62,8 @@ function M.progress(request, response) response:addData('current_line', currentLine) response:addData('buffered_lines', bufferedLines) response:addData('total_lines', totalLines) + response:addData('buffer_size', bufferSize) + response:addData('max_buffer_size', maxBufferSize) elseif progress == false then response:addData('status', bufferedLines) response:setFail() From de16721b854a193e4652c433354745b69f78d8e7 Mon Sep 17 00:00:00 2001 From: Wouter R Date: Sat, 13 Feb 2016 17:17:57 +0100 Subject: [PATCH 16/62] Accept argument 'total_lines' in printer/print API request. --- src/rest/api/api_printer.lua | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/rest/api/api_printer.lua b/src/rest/api/api_printer.lua index d7927c9..42e8fce 100644 --- a/src/rest/api/api_printer.lua +++ b/src/rest/api/api_printer.lua @@ -177,6 +177,7 @@ end --accepts: id(string) (the printer ID to append to) --accepts: first(bool) (chunks will be concatenated but output file will be cleared first if this argument is true) --accepts: start(bool) (only when this argument is true will printing be started) +--accepts: total_lines(int) (the total number of lines that is going to be sent, will be used only for reporting progress) --accepts: seq_number(int) (sequence number of the chunk, must be given until clear() after given once, and incremented each time) --accepts: seq_total(int) (total number of gcode chunks to be appended, must be given until clear() after given once, and stay the same) --returns: when the gcode buffer cannot accept the gcode, or the IPC transaction fails, a fail with a (formal, i.e., parseable) status argument will be returned @@ -202,6 +203,7 @@ function M.print_POST(request, response) local argGcode = request:get("gcode") local argIsFirst = utils.toboolean(request:get("first")) local argStart = utils.toboolean(request:get("start")) + local argTotalLines = request:get("total_lines") or -1 local argSeqNumber = request:get("seq_number") or -1 local argSeqTotal = request:get("seq_total") or -1 local remoteHost = request:getRemoteHost() @@ -232,7 +234,7 @@ function M.print_POST(request, response) local rv,msg - rv,msg = printer:appendGcode(argGcode, { seq_number = argSeqNumber, seq_total = argSeqTotal, source = remoteHost }) + rv,msg = printer:appendGcode(argGcode, argTotalLines, { seq_number = argSeqNumber, seq_total = argSeqTotal, source = remoteHost }) if rv then --NOTE: this does not report the number of lines, but only the block which has just been added response:addData('gcode_append',argGcode:len()) From 03e34f2676b042eed26ae5b3de51595dba8d617c Mon Sep 17 00:00:00 2001 From: Wouter R Date: Sun, 14 Feb 2016 00:38:19 +0100 Subject: [PATCH 17/62] 'printer/print' API call: change 'first' parameter to 'clear' and deprecate old name. --- src/rest/api/api_printer.lua | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/rest/api/api_printer.lua b/src/rest/api/api_printer.lua index 42e8fce..c2d43f4 100644 --- a/src/rest/api/api_printer.lua +++ b/src/rest/api/api_printer.lua @@ -175,7 +175,8 @@ end --requires: gcode(string) (the gcode to be appended) --accepts: id(string) (the printer ID to append to) ---accepts: first(bool) (chunks will be concatenated but output file will be cleared first if this argument is true) +--accepts: clear(bool) (chunks will be concatenated but output file will be cleared first if this argument is true) +--accepts: first(deprecated) (an alias for 'clear') --accepts: start(bool) (only when this argument is true will printing be started) --accepts: total_lines(int) (the total number of lines that is going to be sent, will be used only for reporting progress) --accepts: seq_number(int) (sequence number of the chunk, must be given until clear() after given once, and incremented each time) @@ -201,7 +202,8 @@ function M.print_POST(request, response) local argId = request:get("id") local argGcode = request:get("gcode") - local argIsFirst = utils.toboolean(request:get("first")) + local argClear = utils.toboolean(request:get("clear")) + local argIsFirst = utils.toboolean(request:get("first")) -- deprecated local argStart = utils.toboolean(request:get("start")) local argTotalLines = request:get("total_lines") or -1 local argSeqNumber = request:get("seq_number") or -1 @@ -218,7 +220,7 @@ function M.print_POST(request, response) return end - if argIsFirst == true then + if argClear == true or argIsFirst == true then log:verbose(MOD_ABBR, " clearing all gcode for " .. printer:getId()) response:addData('gcode_clear',true) local rv,msg = printer:clearGcode() From 97612c8a74455168c24c1f057ed7a8832779d38c Mon Sep 17 00:00:00 2001 From: Wouter R Date: Tue, 23 Feb 2016 13:47:17 +0100 Subject: [PATCH 18/62] Reduce logging on start of request handling: - change level of stasrtup message from info to verbose; - Combine two request information log messages into one. --- src/main.lua | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/main.lua b/src/main.lua index 501b753..6836e1d 100644 --- a/src/main.lua +++ b/src/main.lua @@ -215,7 +215,7 @@ local function init(environment) end if dbgText ~= "" then dbgText = " (" .. dbgText .. " debugging)" end - log:info(MOD_ABBR, "=======rest api" .. dbgText .. "=======") + log:verbose(MOD_ABBR, "=======rest api" .. dbgText .. "=======") if (environment['REQUEST_METHOD'] == 'POST') then local n = tonumber(environment['CONTENT_LENGTH']) @@ -262,15 +262,16 @@ local function main(environment) log:warning(MOD_ABBR, "Signin failed: "..util.dump(msg)) end elseif rq:getRequestMethod() ~= 'CMDLINE' or confDefaults.DEBUG_API then - -- Note: the commented log statement will print too many data if it's for instance dumping a gcode add request - --log:info(MOD_ABBR, "received request of type " .. rq:getRequestMethod() .. " for " .. (rq:getRequestedApiModule() or "") - -- .. "/" .. (rq:getRealApiFunctionName() or "") .. " with arguments: " .. util.dump(rq:getAll())) - log:info(MOD_ABBR, "received request of type " .. rq:getRequestMethod() .. " for " .. (rq:getRequestedApiModule() or "") - .. "/" .. (rq:getRealApiFunctionName() or "")) + -- Note: the commented log message will print too many data if it's for instance dumping a gcode add request + --logMessage = "received request of type " .. rq:getRequestMethod() .. " for " .. (rq:getRequestedApiModule() or "") + -- .. "/" .. (rq:getRealApiFunctionName() or "") .. " with arguments: " .. util.dump(rq:getAll()) + logMessage = rq:getRequestMethod() .. " request for " .. (rq:getRequestedApiModule() or "") + .. "/" .. (rq:getRealApiFunctionName() or "") if rq:getRequestMethod() ~= 'CMDLINE' then - log:info(MOD_ABBR, "remote IP/port: " .. rq:getRemoteHost() .. "/" .. rq:getRemotePort()) - --log:verbose(MOD_ABBR, "user agent: " .. rq:getUserAgent()) + logMessage = logMessage .. " (remote IP/port: " .. rq:getRemoteHost() .. "/" .. rq:getRemotePort() .. ")" + --logMessage = logMessage .. " (user agent: " .. rq:getUserAgent() .. ")" end + log:info(MOD_ABBR, logMessage) local response, err = rq:handle() From b4aa1a9746cadb616652482e260e0a87c4d32a53 Mon Sep 17 00:00:00 2001 From: Wouter R Date: Tue, 23 Feb 2016 16:18:42 +0100 Subject: [PATCH 19/62] Wifibox stdout/stderr log file: - rename 'wifibox.cgi-fallback.log' to 'wifibox.stdout.log' - extend description of Lua debugging (and the fallback log) - send fallback log along with info/logfiles request --- README.md | 8 ++++++-- src/conf_defaults.lua | 2 +- src/rest/api/api_info.lua | 5 +++++ src/script/d3dapi | 2 +- 4 files changed, 13 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 5e1513d..5638ba3 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,7 @@ WiFi box OpenWRT firmware package General documentation can be found on the wiki: . Source code documentation can be generated, see below. + Documentation ------------- @@ -19,5 +20,8 @@ Syntax errors in Lua can lead to tricky issues since they might only surface whe One countermeasure for this is to use [pcall](http://www.lua.org/pil/8.4.html) instead of regular calls in many cases. To let the error 'happen' (which in turn gives information in the form of stack traces), tell the code to use regular calls by setting 'M.DEBUG_PCALLS' to 'true' in `conf_defaults.lua`. -A second way of debugging is to take uhttpd out of the loop and invoke the Lua code from command-line. To do this, set 'M.DEBUG_API' to 'true' in `conf_defaults.lua`. Then invoke the API using the command `d3dapi p=/mod/func r=POST` where `p=` is followed by the API path and `r=` followed by either 'GET' or 'POST'. -Be aware though, that this script redirects output streams to a fallback log file where stack traces will end up, this file is `/tmp/wifibox.cgi-fallback.log`. +Additionally, when the pcall setting is not enough, a second way of debugging is to take uhttpd out of the loop and invoke the Lua code from command-line. To do this, set 'M.DEBUG_API' to 'true' in `conf_defaults.lua`. Then invoke the API using the command `d3dapi p=/mod/func r=POST` where `p=` is followed by the API path and `r=` followed by either 'GET' or 'POST'. + +Note that because stack traces are sent to `stderr`, this script redirects output streams to a separate log file: `/tmp/wifibox.stdout.log`; apart from these traces, the file should not contain any data. + +Finally, the `CGI` gateway of uhttpd also uses this script, so the API can also be used to obtain stack traces by calling it as `/cgi-bin/d3dapi/` (note the extra `cgi-bin`). diff --git a/src/conf_defaults.lua b/src/conf_defaults.lua index 5dd9093..95f5113 100644 --- a/src/conf_defaults.lua +++ b/src/conf_defaults.lua @@ -36,7 +36,7 @@ M.DEBUG_PCALLS = false --- This constant enables debugging of the REST API from the command-line by emulating GET/POST requests. -- Specify the path and optionally the request method as follows: `d3dapi p=/mod/func r=POST`. --- Note that the command-line script redirects the output streams to '/tmp/wifibox.cgi-fallback.log' +-- Note that the command-line script redirects the output streams to '/tmp/wifibox.stdout.log', -- meaning that any stack traces can be found there. M.DEBUG_API = true diff --git a/src/rest/api/api_info.lua b/src/rest/api/api_info.lua index 5e07d37..f5283c1 100644 --- a/src/rest/api/api_info.lua +++ b/src/rest/api/api_info.lua @@ -21,6 +21,8 @@ local LOG_COLLECT_DIRNAME = 'wifibox-logs' local LOG_COLLECT_DIR = TMP_DIR .. '/' .. LOG_COLLECT_DIRNAME local DEFAULT_WIFIBOX_LOG_FILENAME = 'wifibox.log' local DEFAULT_WIFIBOX_LOG_FILE = TMP_DIR .. '/' .. DEFAULT_WIFIBOX_LOG_FILENAME +local WIFIBOX_STDOUT_LOG_FILENAME = 'wifibox.stdout.log' +local WIFIBOX_STDOUT_LOG_FILE = TMP_DIR .. '/' .. WIFIBOX_STDOUT_LOG_FILENAME local MOD_ABBR = "AINF" local SYSLOG_FILENAME = 'syslog' @@ -73,6 +75,9 @@ function M.logfiles(request, response) -- copy wifibox API-script log rv,sig,code = redirectedExecute('cp ' .. wifiboxLogFilePath .. ' ' .. LOG_COLLECT_DIR) + -- copy d3dapi script stdout/stderr log + rv,sig,code = redirectedExecute('cp ' .. WIFIBOX_STDOUT_LOG_FILE .. ' ' .. LOG_COLLECT_DIR) + -- capture syslog rv,sig,code = os.execute('logread > ' .. LOG_COLLECT_DIR .. '/' .. SYSLOG_FILENAME) diff --git a/src/script/d3dapi b/src/script/d3dapi index c97fab5..948c22f 100644 --- a/src/script/d3dapi +++ b/src/script/d3dapi @@ -10,7 +10,7 @@ LUA=lua SCRIPT_PATH=/usr/share/lua/wifibox -LOG_FILE=/tmp/wifibox.cgi-fallback.log +LOG_FILE=/tmp/wifibox.stdout.log cd $SCRIPT_PATH #echo "CGI invocation" >> $LOG_FILE From 65062207ffe55d44673c83189726ec62d657f5aa Mon Sep 17 00:00:00 2001 From: Wouter R Date: Tue, 23 Feb 2016 17:36:17 +0100 Subject: [PATCH 20/62] Logging: - log only one line when signing in; - change level of UCI/init.d changes from info to verbose; - change log level of network:getStatus message from info to verbose. --- src/network/netconfig.lua | 10 +++++----- src/network/signin.lua | 2 +- src/rest/api/api_network.lua | 5 ++--- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/network/netconfig.lua b/src/network/netconfig.lua index f9f7ff8..60bb5fb 100644 --- a/src/network/netconfig.lua +++ b/src/network/netconfig.lua @@ -64,7 +64,7 @@ function M.switchConfiguration(components) for k,v in pairs(components) do local fname = k .. '_' .. v if type(reconf[fname]) == 'function' then - log:info(MOD_ABBR, "reconfiguring component '" .. k .. "' (" .. v .. ")") + log:verbose(MOD_ABBR, "reconfiguring component '" .. k .. "' (" .. v .. ")") reconf[fname](dirtyList) else log:warning(MOD_ABBR, "unknown component or action '" .. fname .. "' skipped") @@ -81,12 +81,12 @@ function M.switchConfiguration(components) end function M.commitComponent(c) - log:info(MOD_ABBR, "committing component '" .. c .. "'") + log:verbose(MOD_ABBR, "committing component '" .. c .. "'") uci:commit(c) end function M.reloadComponent(c, silent) - log:info(MOD_ABBR, "reloading component '" .. c .. "'") + log:verbose(MOD_ABBR, "reloading component '" .. c .. "'") local command = 'reload' local cmd = '/etc/init.d/' .. c .. ' '..command if silent ~= nil and silent then @@ -94,7 +94,7 @@ function M.reloadComponent(c, silent) os.execute(cmd) else rv = utils.captureCommandOutput(cmd) - log:info(MOD_ABBR, " result reloading component '" .. c .. "' (cmd: '"..cmd.."'): \n"..utils.dump(rv)) + log:verbose(MOD_ABBR, " result reloading component '" .. c .. "' (cmd: '"..cmd.."'): \n"..utils.dump(rv)) end end @@ -423,7 +423,7 @@ function M.disassociate() end function M.getStatus() - log:info(MOD_ABBR, "network:getStatus") + log:verbose(MOD_ABBR, "network:getStatus") local file, error = io.open('/tmp/networkstatus.txt','r') if file == nil then --log:error(MOD_ABBR, "Util:Access:Can't read controller file. Error: "..error) diff --git a/src/network/signin.lua b/src/network/signin.lua index 4ed7b93..b662577 100644 --- a/src/network/signin.lua +++ b/src/network/signin.lua @@ -107,7 +107,7 @@ function M.getStatus() end function M.setStatus(code,msg) - log:info(MOD_ABBR, "signin:setStatus: "..code.." | "..msg) + log:verbose(MOD_ABBR, "signin:setStatus: " .. code .. " (" .. msg .. ")") status.set(STATUS_FILE,code,msg); end diff --git a/src/rest/api/api_network.lua b/src/rest/api/api_network.lua index c34c697..2c4992a 100644 --- a/src/rest/api/api_network.lua +++ b/src/rest/api/api_network.lua @@ -203,14 +203,13 @@ function M.remove_POST(request, response) end function M.signin(request, response) - log:info(MOD_ABBR, "API:network/signin") local success, output = signin.signin() if success then - log:info(MOD_ABBR, " signed in") + log:info(MOD_ABBR, "signed in") response:setSuccess("API:Network:signed in") response:addData("response", output) else - log:info(MOD_ABBR, "signing in failed") + log:warning(MOD_ABBR, "signing in failed") response:setError("Signing in failed") end end From 1cb130965977ecd2de5beb1de226a40b287769bd Mon Sep 17 00:00:00 2001 From: peteruithoven Date: Wed, 24 Feb 2016 11:44:12 +0100 Subject: [PATCH 21/62] Craftbot PLUS --- src/conf_defaults.lua | 4 ++++ src/util/printer.lua | 2 ++ 2 files changed, 6 insertions(+) diff --git a/src/conf_defaults.lua b/src/conf_defaults.lua index 95f5113..9d56de2 100644 --- a/src/conf_defaults.lua +++ b/src/conf_defaults.lua @@ -133,6 +133,7 @@ M.printer_dimensions_x = { default_colido_2_0_plus = 230, default_colido_x3045 = 300, default_colido_compact = 130, + default_craftbot_plus = 250, subSection = 'printer_type', type = 'int', description = '', @@ -184,6 +185,7 @@ M.printer_heatedbed = { default_colido_2_0_plus = true, default_colido_m2020 = true, default_colido_x3045 = true, + default_craftbot_plus = true, subSection = 'printer_type', type = 'bool', description = 'Printer has heated bed', @@ -192,6 +194,7 @@ M.printer_filamentThickness = { default = 2.89, default_doodle_dream = 1.75, default_wanhao_duplicator4 = 1.75, + default_craftbot_plus = 1.75, type = 'float', description = '', min = 0.0, @@ -235,6 +238,7 @@ M.printer_endcode = { default_delta_rostockmax = default_deltabot_endcode, default_deltamaker = default_deltabot_endcode, default_kossel = default_deltabot_endcode, + default_craftbot_plus = 'M107 ;fan off\nG91 ;relative positioning\nG1 E-1 F300 ;retract the filament a bit before lifting the nozzle, to release some of the pressure\nG1 Z+0.5 E-5 X-20 Y-20 F9000 ;move Z up a bit and retract filament even more\nG28 X0 Y0 ;move X/Y to min endstops, so the head is out of the way\nM84 ;disable axes / steppers\nG90 ;absolute positioning\nM109 S{preheatTemp}\n{if heatedBed}M140 S{preheatBedTemp}\nM117 Done ;display message (20 characters to clear whole screen)', type = 'string', subSection = 'printer_type', description = '' diff --git a/src/util/printer.lua b/src/util/printer.lua index 48ce912..78e7e73 100644 --- a/src/util/printer.lua +++ b/src/util/printer.lua @@ -62,6 +62,8 @@ local SUPPORTED_PRINTERS = { colido_x3045 = "ColiDo X3045", colido_compact = "ColiDo Compact", colido_diy = "ColiDo DIY" + colido_diy = "ColiDo DIY" + craftbot_plus = "CraftBot PLUS" } local SUPPORTED_BAUDRATES = { ["115200"] = "115200 bps", From 7b88ca4489baaa09d22c5b1c6540bf880d6750a3 Mon Sep 17 00:00:00 2001 From: peteruithoven Date: Wed, 24 Feb 2016 12:00:19 +0100 Subject: [PATCH 22/62] 0.10.10-a version & releasenotes --- ReleaseNotes.md | 15 +++++++++++++++ src/FIRMWARE-VERSION | 2 +- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/ReleaseNotes.md b/ReleaseNotes.md index 43512f5..6f8dd56 100644 --- a/ReleaseNotes.md +++ b/ReleaseNotes.md @@ -1,4 +1,19 @@ Changelog +# 0.10.10-a (24 feb 2016) +- Max buffer size check: the WiFi-Box can now indicate through the API that its buffer is full and it won't accept new gcode. +- Buffer information is communicated through the API so all clients can handle this properly. The Doodle3D client will stop sending and waits until it drops below 75%. It will check this every 30 seconds. +- Improved progress indication: when sending a print the total number of lines can be added, causing the total lines reported through the API to be correct, instead of being approximated to be total lines in buffer, which keeps increasing while a print is still being sent. +- When sending print parts, sequence numbers can be added which will be checked for correctness. +- The stop button now stays enabled while still sending a print. +- Made firmware log format consistent with print3d's. +- Fixed local IP parsing pattern in firmware. +- Improved log messages and log messages formatting. +- Added loglite utility which enables filtering and coloring of logs. +- Won't attempt sign-in when in access point mode. +- More consistent error reporting from API. +- API d3dapi/printer/print's 'first' parameter has been deprecated in favor of a more descriptive 'clear' parameter. +- Support for CraftBot PLUS printer. + # 0.10.9 (11 jan 2016) # 0.10.8 (11 jan 2016) # 0.10.8-b (7 jan 2016) diff --git a/src/FIRMWARE-VERSION b/src/FIRMWARE-VERSION index f314d02..8869810 100644 --- a/src/FIRMWARE-VERSION +++ b/src/FIRMWARE-VERSION @@ -1 +1 @@ -0.10.9 +0.10.10-a From 7e0d5cabee045050b40e4ee34f1133332d016f93 Mon Sep 17 00:00:00 2001 From: peteruithoven Date: Wed, 24 Feb 2016 12:02:45 +0100 Subject: [PATCH 23/62] Removed duplicate Colido DIY printer --- src/util/printer.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/src/util/printer.lua b/src/util/printer.lua index 78e7e73..ea25d48 100644 --- a/src/util/printer.lua +++ b/src/util/printer.lua @@ -62,7 +62,6 @@ local SUPPORTED_PRINTERS = { colido_x3045 = "ColiDo X3045", colido_compact = "ColiDo Compact", colido_diy = "ColiDo DIY" - colido_diy = "ColiDo DIY" craftbot_plus = "CraftBot PLUS" } local SUPPORTED_BAUDRATES = { From 2f53a5e8b062ef02194e8d9c7342d4c9df8ff416 Mon Sep 17 00:00:00 2001 From: peteruithoven Date: Wed, 24 Feb 2016 15:26:06 +0100 Subject: [PATCH 24/62] printer.lua syntax error Remember: test before commit/push --- src/util/printer.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/util/printer.lua b/src/util/printer.lua index ea25d48..da54774 100644 --- a/src/util/printer.lua +++ b/src/util/printer.lua @@ -61,7 +61,7 @@ local SUPPORTED_PRINTERS = { colido_m2020 = "ColiDo M2020", colido_x3045 = "ColiDo X3045", colido_compact = "ColiDo Compact", - colido_diy = "ColiDo DIY" + colido_diy = "ColiDo DIY", craftbot_plus = "CraftBot PLUS" } local SUPPORTED_BAUDRATES = { From 9b8831350628fde0ca73d30adb6d3a83bbdf8f8c Mon Sep 17 00:00:00 2001 From: peteruithoven Date: Wed, 24 Feb 2016 17:12:15 +0100 Subject: [PATCH 25/62] Release script Linux support --- extra/scripts/publish-wifibox-release.lua | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/extra/scripts/publish-wifibox-release.lua b/extra/scripts/publish-wifibox-release.lua index c47fa0b..9ee6386 100755 --- a/extra/scripts/publish-wifibox-release.lua +++ b/extra/scripts/publish-wifibox-release.lua @@ -129,9 +129,15 @@ local function quit(ev) end local function md5sum(file) + -- try OS X specific md5 utility local rv,_,sum = pl.utils.executeex('md5 -q "' .. file .. '"') - - return rv and sum:sub(1, -2) or nil + if rv then + return rv and sum:sub(1, -2) or nil + end + -- try Linux md5sum utility + local rv,_,output = pl.utils.executeex('md5sum "' .. file .. '"') + local md5 = string.match(output, "[^%s]+") + return md5 end local function getYesNo(question) From fc026f1b3894a3070b497915afa92aa46571f68c Mon Sep 17 00:00:00 2001 From: Wouter R Date: Thu, 25 Feb 2016 10:29:02 +0100 Subject: [PATCH 26/62] loglite enhancements: - Attempt reading named filter sets from $HOME/loglite-filters.lua - Slightly improved debug printing --- src/script/loglite-filters.lua | 7 +++++ src/script/loglite.lua | 47 +++++++++++++++++++++++++++++----- 2 files changed, 47 insertions(+), 7 deletions(-) diff --git a/src/script/loglite-filters.lua b/src/script/loglite-filters.lua index 6d74ade..c4b43eb 100644 --- a/src/script/loglite-filters.lua +++ b/src/script/loglite-filters.lua @@ -1,5 +1,12 @@ local M = {} +M.default = { -- TEST set + ['options'] = { ['mode'] = 'keep', count = 'all' }, + ['patterns'] = { + ['%(info%)'] = 'magenta' + } +} + M.printstart = { ['options'] = { ['mode'] = 'delete' }, ['patterns'] = { diff --git a/src/script/loglite.lua b/src/script/loglite.lua index 52ff68d..b4dfe3d 100644 --- a/src/script/loglite.lua +++ b/src/script/loglite.lua @@ -105,11 +105,22 @@ local function hasValue(t, needle) return nil end +--- Determines if filename exists and can be opened for reading. +-- From http://stackoverflow.com/a/4991602 +-- @string filename The file to test. +-- @return True if the file exists and is readable, false otherwise. +function fileExists(filename) + local f = io.open(filename, "r") + if f ~= nil then io.close(f) return true else return false end +end + --[[========================================================================]]-- -local function tailStream(stream, patterns, options) +local function tailStream(stream, filterSet) + patterns = filterSet.patterns + options = filterSet.options local c = 0 for line in stream:lines() do --c = c + 1 -- Note: this would also count deleted lines @@ -120,7 +131,7 @@ local function tailStream(stream, patterns, options) -- look for a pattern matching this line for p,c in pairs(patterns) do if line:match(p) then --- print("+matched rule '" .. p .. "'/'" .. c .. "' against '" .. line .. "'") +-- print("[DEBUG] +matched rule '" .. p .. "'/'" .. c .. "' against '" .. line .. "'") local kws = c:split(',') if hasValue(kws, '_delete') then keepLine = false; keepLineOverridden = true @@ -155,13 +166,26 @@ local function tailStream(stream, patterns, options) if options.count == 'all' then print(c, embellished) else print(embellished) end else --- print("-skipped '"..line.."'") +-- print("[DEBUG] -skipped '"..line.."'") end --c = line:match 'truncated' and 0 or c -- from tail on stderr apparently end end +--TODO: could be extended to look for multiple filenames in multiple paths +local function readConfigFile(filename, searchPath) + fullPath = searchPath .. '/' .. filename + if not fileExists(fullPath) then +-- print("[DEBUG] config file '" .. fullPath .. "' not found") + return nil + end + +-- print("[DEBUG] using config file '" .. fullPath .. "'") + -- require does not accept full paths? also, pcall does not help with dofile + return dofile(fullPath) +end + local function main() if #arg > 0 and arg[1] == "-h" or arg[1] == "--help" then print("Usage: either pass file to tail as argument, or pipe through stdin.") @@ -169,17 +193,26 @@ local function main() end local followFile = #arg > 0 and arg[1] or nil + local filterSetName = 'default' -- TODO: parse from options and leave at 'default' if not specified - --print("following file: '" .. (followFile and followFile or "") .. "'.") + --print("[DEBUG] following file: '" .. (followFile and followFile or "") .. "'.") --local tailin = io.popen('tail -F '..(...)..' 2>&1', 'r') local tailin = followFile and io.popen('tail -f ' .. followFile, 'r') or io.stdin - local patterns = DEFAULT_FILTERSET.patterns - local options = { ['mode'] = 'keep', count = 'none' } + local filterSet = DEFAULT_FILTERSET + + configSets = readConfigFile(DFL_FILTERSET_FILE, os.getenv('HOME')) + for k,_ in pairs(configSets) do + if k == filterSetName then + filterSet = configSets[filterSetName] +-- print("[DEBUG] using filter set '" .. filterSetName .. "' from config") + break + end + end - pcall(tailStream, tailin, patterns, options) -- Note: protected call to suppress interrupt error thrown by lines iterator + pcall(tailStream, tailin, filterSet) -- Note: protected call to suppress interrupt error thrown by lines iterator end main() From 8a07d972dd149c884a5953d5585556352dc707bd Mon Sep 17 00:00:00 2001 From: Wouter R Date: Thu, 25 Feb 2016 23:23:50 +0100 Subject: [PATCH 27/62] Create debug print to list available filter sets (to be used in help output later). --- src/script/loglite.lua | 50 ++++++++++++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 14 deletions(-) diff --git a/src/script/loglite.lua b/src/script/loglite.lua index b4dfe3d..0d295c5 100644 --- a/src/script/loglite.lua +++ b/src/script/loglite.lua @@ -5,12 +5,10 @@ -- * pattern rules are matched top to bottom, the last one encountered overriding any previous conflicting directive -- -- TODO: --- * move script to firmware repo (since it's shared between that and print3d) and remove commit from print3d -- * pre-split keyword lists for efficiency? --- * keep formats separate and only concat in the end, so things like upperasing can work properly +-- * keep formats separate and only concat in the end, so things like uppercasing can work properly -- * add more directives like uppercase, prefix/suffix? -- * options: en/dis total count, en/dis match count (how to deal with multiple matches?), en/dis keep_mode / delete_mode/ --- * create named sets of options+patterns to allow for task-specific filter sets - choose/parse options and includes (pcall require()d) in main and pass on to workhorse function -- * add specialized patterns for levels/modules? -- -- FIXME: @@ -50,7 +48,7 @@ local RESET_CODE = ESCAPE_STR .. "m" local DFL_FILTERSET_FILE = "loglite-filters.lua" local DEFAULT_FILTERSET = { - ['options'] = { 'default_enabled', 'keep_mode' }, + ['options'] = { ['mode'] = 'keep', count = 'none' }, ['patterns'] = { ['%(error%)'] = 'red', ['%(warning%)'] = 'yellow', @@ -93,9 +91,30 @@ function string:split(div) return arr end -local function makeAnsiCode(key) - if not ANSI_COLORS[key] then return nil end - return ESCAPE_STR .. ANSI_COLORS[key] .. 'm' +--- Determines if filename exists and can be opened for reading. +-- From http://stackoverflow.com/a/4991602 +-- @string filename The file to test. +-- @return True if the file exists and is readable, false otherwise. +function fileExists(filename) + local f = io.open(filename, "r") + if f ~= nil then io.close(f) return true else return false end +end + +--- Converts keys of a table into a string. +-- Adapted from http://stackoverflow.com/a/12674376. +-- @string tbl A key/value table. +-- @string[opt=','] sep Separator to use between items. +-- @boolean[opt=false] sort Whether or not to sort the resulting list. +-- @return A string with all keys from the given table. +local function keysToString(tbl, sep, sort) + local sep, sort = sep or ',', sort or false + local keyset, n = {}, 0 + for k,_ in pairs(tbl) do + n = n + 1 + keyset[n] = k + end + if sort then table.sort(keyset) end + return table.concat(keyset, sep) end local function hasValue(t, needle) @@ -105,13 +124,9 @@ local function hasValue(t, needle) return nil end ---- Determines if filename exists and can be opened for reading. --- From http://stackoverflow.com/a/4991602 --- @string filename The file to test. --- @return True if the file exists and is readable, false otherwise. -function fileExists(filename) - local f = io.open(filename, "r") - if f ~= nil then io.close(f) return true else return false end +local function makeAnsiCode(key) + if not ANSI_COLORS[key] then return nil end + return ESCAPE_STR .. ANSI_COLORS[key] .. 'm' end @@ -212,6 +227,13 @@ local function main() end end + -- TODO: add commandline option to enable this flag + if listAvailableFilterSets == true then + print(" Available filter sets in " .. DFL_FILTERSET_FILE .. ": " .. keysToString(configSets, ', ')) + os.exit(0) + end + + pcall(tailStream, tailin, filterSet) -- Note: protected call to suppress interrupt error thrown by lines iterator end From 1db8bf3119a9a60fba9225a17bd15405f826ea5b Mon Sep 17 00:00:00 2001 From: Wouter R Date: Thu, 25 Feb 2016 23:39:23 +0100 Subject: [PATCH 28/62] Accept second command-line argument as filter set name. Fixes #55. --- src/script/loglite.lua | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/script/loglite.lua b/src/script/loglite.lua index 0d295c5..30611fc 100644 --- a/src/script/loglite.lua +++ b/src/script/loglite.lua @@ -201,16 +201,21 @@ local function readConfigFile(filename, searchPath) return dofile(fullPath) end +--NOTE: if command-line options get any more complex, switch to a lightweight +-- getopt like this one? https://attractivechaos.wordpress.com/2011/04/07/getopt-for-lua/ local function main() if #arg > 0 and arg[1] == "-h" or arg[1] == "--help" then - print("Usage: either pass file to tail as argument, or pipe through stdin.") + print("Usage: loglite.lua [file-to-tail] [filter-set]") + print(" If no arguments are supplied, or if the first one is `-', stdin is used as input.") + print(" If no filter set is supplied, a set named `default' will be looked for.") + print(" Filter sets can be defined in a file `loglite-filters.lua' in your home directory.") os.exit(0) end - local followFile = #arg > 0 and arg[1] or nil - local filterSetName = 'default' -- TODO: parse from options and leave at 'default' if not specified + local followFile = #arg > 0 and arg[1] ~= '-' and arg[1] or nil + local filterSetName = #arg > 1 and arg[2] or 'default' - --print("[DEBUG] following file: '" .. (followFile and followFile or "") .. "'.") + --print("[DEBUG] following file: '" .. (followFile and followFile or "") .. "', with filter set '" .. filterSetName .. "'.") --local tailin = io.popen('tail -F '..(...)..' 2>&1', 'r') From c8bea605f71fb7c8b359cee4b2c44d1705a08ec9 Mon Sep 17 00:00:00 2001 From: Wouter R Date: Fri, 26 Feb 2016 00:23:19 +0100 Subject: [PATCH 29/62] Move default filter set to config file. Show available filter sets in help output. --- src/script/loglite-filters.lua | 24 ++++++------ src/script/loglite.lua | 70 +++++++++++++++++----------------- 2 files changed, 46 insertions(+), 48 deletions(-) diff --git a/src/script/loglite-filters.lua b/src/script/loglite-filters.lua index c4b43eb..30b2f3f 100644 --- a/src/script/loglite-filters.lua +++ b/src/script/loglite-filters.lua @@ -1,9 +1,19 @@ local M = {} -M.default = { -- TEST set +M.default = { + ['options'] = { ['mode'] = 'keep', count = 'none' }, + ['patterns'] = { + ['%(error%)'] = 'red', + ['%(warning%)'] = 'yellow', + ['%(bulk%)'] = 'gray', + ['setState%(%)'] = 'bblue' + } +} + +M.test = { -- TEST set ['options'] = { ['mode'] = 'keep', count = 'all' }, ['patterns'] = { - ['%(info%)'] = 'magenta' + ['%(info%)'] = 'yellow' } } @@ -14,14 +24,4 @@ M.printstart = { } } -M.test = { - ['options'] = { 'delete_mode' }, - ['patterns'] = { - ['(verbose)'] = 'underline,cyan,_delete', - ['(info)'] = 'magenta', - ['ABSD'] = '_nodelete', - ['SE?RV?'] = 'bblue' - } -} - return M diff --git a/src/script/loglite.lua b/src/script/loglite.lua index 30611fc..f5913c0 100644 --- a/src/script/loglite.lua +++ b/src/script/loglite.lua @@ -45,18 +45,11 @@ local ANSI_COLORS = { local ESCAPE_STR = string.char(27) .. "[" local RESET_CODE = ESCAPE_STR .. "m" + local DFL_FILTERSET_FILE = "loglite-filters.lua" -local DEFAULT_FILTERSET = { - ['options'] = { ['mode'] = 'keep', count = 'none' }, - ['patterns'] = { - ['%(error%)'] = 'red', - ['%(warning%)'] = 'yellow', - ['%(bulk%)'] = 'gray', - ['setState%(%)'] = 'bblue' - } -} +--[[========================================================================]]-- --- Stringifies the given object. -- From util/utils.lua @@ -134,9 +127,10 @@ end --[[========================================================================]]-- local function tailStream(stream, filterSet) - patterns = filterSet.patterns - options = filterSet.options + patterns = filterSet and filterSet.patterns or {} + options = filterSet and filterSet.options or { ['mode'] = 'keep' } local c = 0 + for line in stream:lines() do --c = c + 1 -- Note: this would also count deleted lines local embellished = line @@ -146,7 +140,7 @@ local function tailStream(stream, filterSet) -- look for a pattern matching this line for p,c in pairs(patterns) do if line:match(p) then --- print("[DEBUG] +matched rule '" .. p .. "'/'" .. c .. "' against '" .. line .. "'") + --print("[DEBUG] +matched rule '" .. p .. "'/'" .. c .. "' against '" .. line .. "'") local kws = c:split(',') if hasValue(kws, '_delete') then keepLine = false; keepLineOverridden = true @@ -181,7 +175,7 @@ local function tailStream(stream, filterSet) if options.count == 'all' then print(c, embellished) else print(embellished) end else --- print("[DEBUG] -skipped '"..line.."'") + --print("[DEBUG] -skipped '"..line.."'") end --c = line:match 'truncated' and 0 or c -- from tail on stderr apparently @@ -192,11 +186,11 @@ end local function readConfigFile(filename, searchPath) fullPath = searchPath .. '/' .. filename if not fileExists(fullPath) then --- print("[DEBUG] config file '" .. fullPath .. "' not found") + --print("[DEBUG] config file '" .. fullPath .. "' not found") return nil end --- print("[DEBUG] using config file '" .. fullPath .. "'") + --print("[DEBUG] using config file '" .. fullPath .. "'") -- require does not accept full paths? also, pcall does not help with dofile return dofile(fullPath) end @@ -204,41 +198,45 @@ end --NOTE: if command-line options get any more complex, switch to a lightweight -- getopt like this one? https://attractivechaos.wordpress.com/2011/04/07/getopt-for-lua/ local function main() + -- handle command-line arguments + local showHelp, followFile, filterSetName = false, nil, 'default' if #arg > 0 and arg[1] == "-h" or arg[1] == "--help" then - print("Usage: loglite.lua [file-to-tail] [filter-set]") - print(" If no arguments are supplied, or if the first one is `-', stdin is used as input.") - print(" If no filter set is supplied, a set named `default' will be looked for.") - print(" Filter sets can be defined in a file `loglite-filters.lua' in your home directory.") - os.exit(0) + showHelp = true + else + if #arg > 0 and arg[1] ~= '-' then followFile = arg[1] end + if #arg > 1 then filterSetName = arg[2] end end - local followFile = #arg > 0 and arg[1] ~= '-' and arg[1] or nil - local filterSetName = #arg > 1 and arg[2] or 'default' - - --print("[DEBUG] following file: '" .. (followFile and followFile or "") .. "', with filter set '" .. filterSetName .. "'.") - - - --local tailin = io.popen('tail -F '..(...)..' 2>&1', 'r') - local tailin = followFile and io.popen('tail -f ' .. followFile, 'r') or io.stdin - - local filterSet = DEFAULT_FILTERSET - - configSets = readConfigFile(DFL_FILTERSET_FILE, os.getenv('HOME')) + -- read filter set file if available + local filterSet = nil + configSets = readConfigFile(DFL_FILTERSET_FILE, os.getenv('HOME')) or {} for k,_ in pairs(configSets) do if k == filterSetName then filterSet = configSets[filterSetName] --- print("[DEBUG] using filter set '" .. filterSetName .. "' from config") + --print("[DEBUG] using filter set '" .. filterSetName .. "' from config") break end end - -- TODO: add commandline option to enable this flag - if listAvailableFilterSets == true then - print(" Available filter sets in " .. DFL_FILTERSET_FILE .. ": " .. keysToString(configSets, ', ')) + -- if requested, display help and exit + if showHelp and showHelp == true then + print("Usage: loglite.lua [file-to-tail] [filter-set]") + print(" If no arguments are supplied, or if the first one is `-', stdin is used as input.") + print(" If no filter set is supplied, a set named `default' will be looked for.") + print(" Filter sets can be defined in a file `loglite-filters.lua' in your home directory.") + print() + print(" Available filter sets in " .. os.getenv('HOME') .. "/" .. DFL_FILTERSET_FILE .. ": " .. keysToString(configSets, ', ', true)) os.exit(0) end + ------------------------- + + --print("[DEBUG] following file: '" .. (followFile and followFile or "") .. "', with filter set '" .. filterSetName .. "'.") + + --local tailin = io.popen('tail -F '..(...)..' 2>&1', 'r') + local tailin = followFile and io.popen('tail -f ' .. followFile, 'r') or io.stdin + pcall(tailStream, tailin, filterSet) -- Note: protected call to suppress interrupt error thrown by lines iterator end From 9f95b8c537501ea5e3b3a2c72fab270c37503b7d Mon Sep 17 00:00:00 2001 From: Olaf van der Kruk Date: Mon, 29 Feb 2016 10:48:11 +0100 Subject: [PATCH 30/62] Added little readme --- src/script/loglite.lua | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/script/loglite.lua b/src/script/loglite.lua index 52ff68d..29ba313 100644 --- a/src/script/loglite.lua +++ b/src/script/loglite.lua @@ -4,6 +4,12 @@ -- * directives: either a color, a color prefixed by 'b' or one of: _delete, _nodelete, [_matchonly] -- * pattern rules are matched top to bottom, the last one encountered overriding any previous conflicting directive -- +-- EXAMPLE USAGE: +-- to be able to run print3d and at the same time color the logging you can use the pipe (|) operator. +-- use 2>&1 to redirect the stderr to stdout. +-- eg: +-- ./print3d -S -V -F -p marlin_generic 2>&1 | lua ./loglite.lua +-- -- TODO: -- * move script to firmware repo (since it's shared between that and print3d) and remove commit from print3d -- * pre-split keyword lists for efficiency? From 540651c72fabae5dc9a620f691d8ca3989851c33 Mon Sep 17 00:00:00 2001 From: Wouter R Date: Mon, 29 Feb 2016 16:13:41 +0100 Subject: [PATCH 31/62] Create `/root/sketches` on package installation. Fixes #56. --- Makefile | 1 + 1 file changed, 1 insertion(+) diff --git a/Makefile b/Makefile index c0f17e2..b2cdac2 100644 --- a/Makefile +++ b/Makefile @@ -89,6 +89,7 @@ define Package/wifibox/install $(INSTALL_DIR) $(1)/etc/init.d $(INSTALL_DIR) $(1)/etc/config $(INSTALL_DIR) $(1)/root/ + $(INSTALL_DIR) $(1)/root/sketches #$(INSTALL_DIR) $(1)/www $(INSTALL_DIR) $(1)/www/cgi-bin From b27506d236f5c269e4e4d9a62882b17f6f5eb5d4 Mon Sep 17 00:00:00 2001 From: Wouter R Date: Mon, 29 Feb 2016 17:35:17 +0100 Subject: [PATCH 32/62] Create convenience aliases (tailfw & tailp3d) for loglite. --- post-install.sh | 16 +++++++++++----- src/script/loglite-filters.lua | 15 +++++++++++++++ 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/post-install.sh b/post-install.sh index 8f5865a..ba70915 100644 --- a/post-install.sh +++ b/post-install.sh @@ -36,7 +36,7 @@ addFirewallNet() { ### Replace the banner with a custom one if [ ! -f $IPKG_INSTROOT/etc/banner.default ]; then mv $IPKG_INSTROOT/etc/banner $IPKG_INSTROOT/etc/banner.default - cat <<-'EOM' > $IPKG_INSTROOT/etc/banner + cat <<-\EOM > $IPKG_INSTROOT/etc/banner ........D o o d l e 3 D .......________ _____ _____ ....../ / / |__ / __/ / - /___ __ @@ -52,17 +52,23 @@ fi mkdir -p $IPKG_INSTROOT/root grep '^# DO NOT MODIFY.*wifibox package.$' $IPKG_INSTROOT/root/.profile >/dev/null 2>&1 if [ $? -gt 0 ]; then - cat <<-EOM >> $IPKG_INSTROOT/root/.profile - + cat <<-\EOM >> $IPKG_INSTROOT/root/.profile # DO NOT MODIFY - this block of lines has been added by the wifibox package. alias d='ls -la' alias d3dapi='/usr/share/lua/wifibox/script/d3dapi' alias encore='ulimit -c unlimited' alias wopkg='opkg -f /usr/share/lua/wifibox/opkg.conf' + alias tailfw='loglite /tmp/wifibox.log firmware' + tailp3d() { + logfile=/tmp/print3d-ttyACM0.log + if [ $# -gt 0 ]; then logfile=$1; fi + loglite "$logfile" print3d + } + loop() { - if [ \$# -lt 2 ]; then echo "Please supply a delay and a command."; return 1; fi - DELAY=\$1; shift; while true; do \$@; sleep \$DELAY; done + if [ $# -lt 2 ]; then echo "Please supply a delay and a command."; return 1; fi + DELAY=$1; shift; while true; do $@; sleep $DELAY; done } EOM fi diff --git a/src/script/loglite-filters.lua b/src/script/loglite-filters.lua index 30b2f3f..33ead2f 100644 --- a/src/script/loglite-filters.lua +++ b/src/script/loglite-filters.lua @@ -10,6 +10,21 @@ M.default = { } } +-- filter rules for firmware log (/tmp/wifibox.log) +M.firmware = { + ['options'] = { ['mode'] = 'keep', count = 'none' }, + ['patterns'] = { + } +} + +-- filter rules for print3d log (/tmp/print3d-*.log) +M.print3d = { + ['options'] = { ['mode'] = 'keep', count = 'none' }, + ['patterns'] = { + } +} + + M.test = { -- TEST set ['options'] = { ['mode'] = 'keep', count = 'all' }, ['patterns'] = { From 332d185b50df0b421412db50a30688b1309a6348 Mon Sep 17 00:00:00 2001 From: Wouter R Date: Thu, 3 Mar 2016 22:40:23 +0100 Subject: [PATCH 33/62] Loglite: add inheritance support for filter sets. --- src/script/loglite-filters.lua | 4 +-- src/script/loglite.lua | 62 ++++++++++++++++++++++++++-------- 2 files changed, 49 insertions(+), 17 deletions(-) diff --git a/src/script/loglite-filters.lua b/src/script/loglite-filters.lua index 33ead2f..ca7b300 100644 --- a/src/script/loglite-filters.lua +++ b/src/script/loglite-filters.lua @@ -12,14 +12,14 @@ M.default = { -- filter rules for firmware log (/tmp/wifibox.log) M.firmware = { - ['options'] = { ['mode'] = 'keep', count = 'none' }, + ['parent'] = 'default', ['patterns'] = { } } -- filter rules for print3d log (/tmp/print3d-*.log) M.print3d = { - ['options'] = { ['mode'] = 'keep', count = 'none' }, + ['parent'] = 'default', ['patterns'] = { } } diff --git a/src/script/loglite.lua b/src/script/loglite.lua index 20b3c61..8193583 100644 --- a/src/script/loglite.lua +++ b/src/script/loglite.lua @@ -1,15 +1,14 @@ #!/usr/bin/env lua +-- EXAMPLE USAGE: +-- To be able to run print3d and at the same time color the logging you can use +-- the pipe (|) operator. Use 2>&1 to redirect the stderr to stdout, e.g.: +-- ./print3d -S -V -F -p marlin_generic 2>&1 | ./loglite.lua +-- -- Notes -- * directives: either a color, a color prefixed by 'b' or one of: _delete, _nodelete, [_matchonly] -- * pattern rules are matched top to bottom, the last one encountered overriding any previous conflicting directive -- --- EXAMPLE USAGE: --- to be able to run print3d and at the same time color the logging you can use the pipe (|) operator. --- use 2>&1 to redirect the stderr to stdout. --- eg: --- ./print3d -S -V -F -p marlin_generic 2>&1 | lua ./loglite.lua --- -- TODO: -- * pre-split keyword lists for efficiency? -- * keep formats separate and only concat in the end, so things like uppercasing can work properly @@ -55,6 +54,7 @@ local RESET_CODE = ESCAPE_STR .. "m" local DFL_FILTERSET_FILE = "loglite-filters.lua" + --[[========================================================================]]-- --- Stringifies the given object. @@ -116,6 +116,26 @@ local function keysToString(tbl, sep, sort) return table.concat(keyset, sep) end +--- Merge two tables recursively (i.e., subtables also get merged). +-- from: http://stackoverflow.com/a/1283608 +-- @table t1 Table to merge into. +-- @table t2 Table to merge into t1. +-- @return The combined table (actually t1). +function mergeTables(t1, t2) + for k,v in pairs(t2) do + if type(v) == "table" then + if type(t1[k] or false) == "table" then + mergeTables(t1[k] or {}, t2[k] or {}) + else + t1[k] = v + end + else + t1[k] = v + end + end + return t1 +end + local function hasValue(t, needle) for k,v in pairs(t) do if needle == v then return k end @@ -201,6 +221,24 @@ local function readConfigFile(filename, searchPath) return dofile(fullPath) end +--- Load filter set with given name from configSets, with inheritance as specified. +local function readFilterSet(configSets, setName) + local result = {} + for k,_ in pairs(configSets) do + if k == setName then + parent = configSets[setName]['parent'] + if parent ~= nil then + --print("[DEBUG] recursing for filter set '" .. parent .. "' from config") + result = mergeTables(result, readFilterSet(configSets, parent)) + end + --print("[DEBUG] using/merging filter set '" .. setName .. "' from config") + result = mergeTables(result, configSets[setName]) + break + end + end + return result +end + --NOTE: if command-line options get any more complex, switch to a lightweight -- getopt like this one? https://attractivechaos.wordpress.com/2011/04/07/getopt-for-lua/ local function main() @@ -214,15 +252,9 @@ local function main() end -- read filter set file if available - local filterSet = nil - configSets = readConfigFile(DFL_FILTERSET_FILE, os.getenv('HOME')) or {} - for k,_ in pairs(configSets) do - if k == filterSetName then - filterSet = configSets[filterSetName] - --print("[DEBUG] using filter set '" .. filterSetName .. "' from config") - break - end - end + local configSets = readConfigFile(DFL_FILTERSET_FILE, os.getenv('HOME')) or {} + local filterSet = readFilterSet(configSets, filterSetName) + --print("[DEBUG] final filter set for '" .. filterSetName .. "' from config: " .. dump(filterSet)) -- if requested, display help and exit if showHelp and showHelp == true then From a9e17e515141ff5bad08701a8899ed5c8886214a Mon Sep 17 00:00:00 2001 From: Wouter R Date: Fri, 4 Mar 2016 17:24:30 +0100 Subject: [PATCH 34/62] Loglite: - README.md with documentation on usage and creating filter sets (implements #54). - small improvements to default filter set. --- src/script/README-loglite.md | 60 ++++++++++++++++++++++++++++++++++ src/script/loglite-filters.lua | 6 ++-- src/script/loglite.lua | 33 +++++++------------ 3 files changed, 75 insertions(+), 24 deletions(-) create mode 100644 src/script/README-loglite.md mode change 100644 => 100755 src/script/loglite.lua diff --git a/src/script/README-loglite.md b/src/script/README-loglite.md new file mode 100644 index 0000000..c20fa4e --- /dev/null +++ b/src/script/README-loglite.md @@ -0,0 +1,60 @@ +## Loglite + +The loglite script allows coloring and filtering of log files by specifying certain patterns and associating directives to them. These mainly specify colors but additionally, (non-)matched lines can be deleted from output and also all output lines can be numbered. + + +### Usage + +The script can follow an existing log file (comparable to `tail -f`), or it can follow its standard input. A file to follow is always specified as the first argument and a filter set name as the second (use '-' as file name to read from standard input). Details on filter sets can be found below. If no filter set is mentioned on the command-line, the script will attempt to use one named 'default'. + +* Example following an existing log file using a filter set named 'example': `./loglite.lua print3d.log example`. +* Example using standard input, to capture both output streams from `print3d`, with a filter set named 'example' (note the '-' as file name): `./print3d -V 2>&1 | ./loglite.lua - example`. + + +### Filter sets + +The script looks for filter sets in the file '$HOME/loglite-filters.lua'. It looks like this: + + local M = {} + + M.default = { + ['options'] = { mode = 'keep', count = 'none' }, + ['patterns'] = { + ['%(error%)'] = 'red', + ['%(warning%)'] = 'yellow', + ['%(bulk%)'] = 'gray' + } + } + + M.specialization = { + ['parent'] = 'default', + ['options'] = { mode = 'delete' } + ['patterns'] = { + ['setState%(%)'] = 'bblue,_nodelete' + } + } + + return M + +Here, the declaration and returning of `M` is required for the loglite script to be able to cleanly import the file. In `M.default`, 'default' is the name of a filter set being defined (similar for 'specialization'). Definitions can contain three so-called keys: 'parent' specifies a filter set to inherit from in order to reduce code duplication, 'options' and 'patterns' are described below. + +Inheritance can be used to set new keys or to override keys from the parent set. Previously set keys cannot be removed, but they can be set to a non-existing directive (e.g., Lua's 'false' keyword) to achieve the same effect. Note that directives in inheriting sets are currently not combined with previous ones, so for instance overriding `['test'] = 'red, _delete'` with `['test'] = 'blue'` will result in only the directive 'blue' to be applied. + +#### Options + +Two options are currently available: + +* `mode`, which specifies whether to keep log lines (`keep`, the default) or to drop them (`delete`). For specific lines this can then be overriden, see 'Patterns' below. +* `count`, which can be set to `all` to prefix log lines with a counter, or `none` (default) to leave them as is. + +#### Patterns + +Pattern specifications are patterns as used in Lua: [Lua documentation on patterns](http://www.lua.org/pil/20.2.html). +The following directives can be associated with a pattern: + +* A foreground color, one of: black, red, green, yellow, blue, magenta, cyan or white. +* A background color, like foreground colors but prefixed with 'b'. +* `_delete` or `_nodelete` to override the active mode specified in the 'options' above. +* Also available are `blink` and `underline` but they do currently not work in all terminal programs. + +Directives can be combined with ',' (e.g.: `'red,_nodelete'`). Finally, in any filter set, pattern rules are matched from top to bottom, the last one encountered overriding any previous conflicting directive. diff --git a/src/script/loglite-filters.lua b/src/script/loglite-filters.lua index ca7b300..b8040e0 100644 --- a/src/script/loglite-filters.lua +++ b/src/script/loglite-filters.lua @@ -1,7 +1,7 @@ local M = {} M.default = { - ['options'] = { ['mode'] = 'keep', count = 'none' }, + ['options'] = { mode = 'keep', count = 'none' }, ['patterns'] = { ['%(error%)'] = 'red', ['%(warning%)'] = 'yellow', @@ -26,14 +26,14 @@ M.print3d = { M.test = { -- TEST set - ['options'] = { ['mode'] = 'keep', count = 'all' }, + ['options'] = { mode = 'keep', count = 'all' }, ['patterns'] = { ['%(info%)'] = 'yellow' } } M.printstart = { - ['options'] = { ['mode'] = 'delete' }, + ['options'] = { mode = 'delete' }, ['patterns'] = { ['print started'] = '_uppercase,bwhite' } diff --git a/src/script/loglite.lua b/src/script/loglite.lua old mode 100644 new mode 100755 index 8193583..fc3114e --- a/src/script/loglite.lua +++ b/src/script/loglite.lua @@ -1,32 +1,22 @@ #!/usr/bin/env lua --- EXAMPLE USAGE: --- To be able to run print3d and at the same time color the logging you can use --- the pipe (|) operator. Use 2>&1 to redirect the stderr to stdout, e.g.: --- ./print3d -S -V -F -p marlin_generic 2>&1 | ./loglite.lua --- --- Notes --- * directives: either a color, a color prefixed by 'b' or one of: _delete, _nodelete, [_matchonly] --- * pattern rules are matched top to bottom, the last one encountered overriding any previous conflicting directive --- --- TODO: --- * pre-split keyword lists for efficiency? --- * keep formats separate and only concat in the end, so things like uppercasing can work properly --- * add more directives like uppercase, prefix/suffix? --- * options: en/dis total count, en/dis match count (how to deal with multiple matches?), en/dis keep_mode / delete_mode/ --- * add specialized patterns for levels/modules? --- --- FIXME: --- * with deleteMode enabled, multiple matches and _nodelete in a later match, previous directives are ignored - --[[ --- * https://stackoverflow.com/questions/17363973/how-can-i-tail-f-a-log-filetruncate-aware-in-lua --- * http://pueblo.sourceforge.net/doc/manual/ansi_color_codes.html +For documentation on this script, see README-loglite.md. + +Ideas for improvement: +* add more directives like uppercase, prefix/suffix? +* create separate package for this script: a) since it is useful for any log file, b) this file is getting somewhat long +* for broader terminal support: detect `tput` and use it if available (http://wiki.bash-hackers.org/scripting/terminalcodes) +* pre-split keyword lists for efficiency instead of redoing this at every new line? + +FIXME: +* with deleteMode enabled, multiple matches and _nodelete in a later match, previous directives are ignored ]]-- --[[========================================================================]]-- +--Note: overview of ANSI escape codes: http://ascii-table.com/ansi-escape-sequences.php (support varies per terminal/termtype) local ANSI_COLORS = { ['blink'] = 5, -- no dice on osx/iterm2 ['underline'] = 24, -- no dice on osx/iterm2 @@ -272,6 +262,7 @@ local function main() --print("[DEBUG] following file: '" .. (followFile and followFile or "") .. "', with filter set '" .. filterSetName .. "'.") + --Info on tailing a file: https://stackoverflow.com/questions/17363973/how-can-i-tail-f-a-log-filetruncate-aware-in-lua --local tailin = io.popen('tail -F '..(...)..' 2>&1', 'r') local tailin = followFile and io.popen('tail -f ' .. followFile, 'r') or io.stdin From 7a346d6cf132a61415cd55dfe12861688dfa1606 Mon Sep 17 00:00:00 2001 From: Wouter R Date: Fri, 4 Mar 2016 18:12:28 +0100 Subject: [PATCH 35/62] Loglite: several new filter directives. --- src/script/README-loglite.md | 4 +++- src/script/loglite.lua | 6 ++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/script/README-loglite.md b/src/script/README-loglite.md index c20fa4e..508f467 100644 --- a/src/script/README-loglite.md +++ b/src/script/README-loglite.md @@ -54,7 +54,9 @@ The following directives can be associated with a pattern: * A foreground color, one of: black, red, green, yellow, blue, magenta, cyan or white. * A background color, like foreground colors but prefixed with 'b'. +* `bold`, which usually has the effect of rendering a bright variant of the foreground color (note that `bold,black` renders as dark gray). +* `reverse` will reverse fore- and background colors. +* Also available are `blink` and `underscore` but they do currently not work in all terminal programs or might need to be enabled in the preferences. * `_delete` or `_nodelete` to override the active mode specified in the 'options' above. -* Also available are `blink` and `underline` but they do currently not work in all terminal programs. Directives can be combined with ',' (e.g.: `'red,_nodelete'`). Finally, in any filter set, pattern rules are matched from top to bottom, the last one encountered overriding any previous conflicting directive. diff --git a/src/script/loglite.lua b/src/script/loglite.lua index fc3114e..9effc09 100755 --- a/src/script/loglite.lua +++ b/src/script/loglite.lua @@ -18,8 +18,10 @@ FIXME: --Note: overview of ANSI escape codes: http://ascii-table.com/ansi-escape-sequences.php (support varies per terminal/termtype) local ANSI_COLORS = { - ['blink'] = 5, -- no dice on osx/iterm2 - ['underline'] = 24, -- no dice on osx/iterm2 + ['bold'] = 1, + ['underscore'] = 4, + ['blink'] = 5, -- on osx/iterm2, this has to be enabled in preferences + ['reverse'] = 7, ['black'] = 30, ['red'] = 31, ['green'] = 32, From f0c839ffdc10c97991b51cf07f4801ca3b005826 Mon Sep 17 00:00:00 2001 From: Wouter R Date: Fri, 4 Mar 2016 20:29:37 +0100 Subject: [PATCH 36/62] Loglite: fix non-existing directive in example. --- src/script/README-loglite.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/script/README-loglite.md b/src/script/README-loglite.md index 508f467..4f210a8 100644 --- a/src/script/README-loglite.md +++ b/src/script/README-loglite.md @@ -22,7 +22,7 @@ The script looks for filter sets in the file '$HOME/loglite-filters.lua'. It loo ['patterns'] = { ['%(error%)'] = 'red', ['%(warning%)'] = 'yellow', - ['%(bulk%)'] = 'gray' + ['%(bulk%)'] = 'bold,black' } } From 260fac9cd45403ec8f6316ef657f7c49b11d0923 Mon Sep 17 00:00:00 2001 From: Wouter R Date: Fri, 11 Mar 2016 15:58:35 +0100 Subject: [PATCH 37/62] Log more information when printer/* endpoint operations fail. --- src/rest/api/api_printer.lua | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/rest/api/api_printer.lua b/src/rest/api/api_printer.lua index c2d43f4..c3a08d6 100644 --- a/src/rest/api/api_printer.lua +++ b/src/rest/api/api_printer.lua @@ -42,7 +42,7 @@ function M.temperature(request, response) response:addData('bed_target', temperatures.bed_target) elseif temperatures == false then response:addData('status', msg) - response:setFail() + response:setFail("could not get temperature information (" .. msg .. ")") else response:setError(msg) end @@ -66,7 +66,7 @@ function M.progress(request, response) response:addData('max_buffer_size', maxBufferSize) elseif progress == false then response:addData('status', bufferedLines) - response:setFail() + response:setFail("could not get progress information (" .. bufferedLines .. ")") else response:setError(bufferedLines) end @@ -138,7 +138,7 @@ function M.heatup_POST(request, response) response:setSuccess() elseif rv == false then response:addData('status', msg) - response:setFail() + response:setFail("could not start heatup (" .. msg .. ")") else response:setError(msg) end @@ -167,7 +167,7 @@ function M.stop_POST(request, response) response:setSuccess() elseif rv == false then response:addData('status', msg) - response:setFail() + response:setFail("could not stop print (" .. msg .. ")") else response:setError(msg) end @@ -227,7 +227,7 @@ function M.print_POST(request, response) if rv == false then response:addData('status', msg) - response:setFail("could not clear gcode") + response:setFail("could not clear gcode (" .. msg .. ")") elseif rv == nil then response:setError(msg) return @@ -242,11 +242,11 @@ function M.print_POST(request, response) response:addData('gcode_append',argGcode:len()) elseif rv == false then response:addData('status', msg) - response:setFail("could not add gcode") + response:setFail("could not add gcode (" .. msg .. ")") return else response:addData('msg', msg) - response:setError("could not add gcode") + response:setError("could not add gcode (" .. msg .. ")") return end @@ -258,11 +258,11 @@ function M.print_POST(request, response) response:addData('gcode_print',true) elseif rv == false then response:addData('status', msg) - response:setFail("could not send gcode") + response:setFail("could not send gcode (" .. msg .. ")") return else response:addData('msg', msg) - response:setError("could not send gcode") + response:setError("could not send gcode (" .. msg .. ")") return end else From 589d2c705e33e2c5bc389b0bfa5bf14dfdf89fea Mon Sep 17 00:00:00 2001 From: Wouter R Date: Fri, 11 Mar 2016 19:52:43 +0100 Subject: [PATCH 38/62] Include logrotate (+deps) in openwrt diffconfig. --- extra/openwrt-build/openwrt-diffconfig-extramini | 2 ++ extra/openwrt-build/openwrt-diffconfig-mini | 2 ++ extra/openwrt-build/openwrt-diffconfig-regular | 2 ++ 3 files changed, 6 insertions(+) diff --git a/extra/openwrt-build/openwrt-diffconfig-extramini b/extra/openwrt-build/openwrt-diffconfig-extramini index fe0a28d..e0fda86 100644 --- a/extra/openwrt-build/openwrt-diffconfig-extramini +++ b/extra/openwrt-build/openwrt-diffconfig-extramini @@ -22,8 +22,10 @@ CONFIG_PACKAGE_kmod-usb-serial-ftdi=y # CONFIG_PACKAGE_libip6tc is not set CONFIG_PACKAGE_libiwinfo=y CONFIG_PACKAGE_libiwinfo-lua=y +CONFIG_PACKAGE_libpopt=y CONFIG_PACKAGE_liblua=y CONFIG_PACKAGE_libuci-lua=y +CONFIG_PACKAGE_logrotate=y CONFIG_PACKAGE_lua=y CONFIG_PACKAGE_luafilesystem=y # CONFIG_PACKAGE_odhcp6c is not set diff --git a/extra/openwrt-build/openwrt-diffconfig-mini b/extra/openwrt-build/openwrt-diffconfig-mini index cd9460d..f65648e 100644 --- a/extra/openwrt-build/openwrt-diffconfig-mini +++ b/extra/openwrt-build/openwrt-diffconfig-mini @@ -20,8 +20,10 @@ CONFIG_PACKAGE_kmod-usb-serial-ftdi=y # CONFIG_PACKAGE_libip6tc is not set CONFIG_PACKAGE_libiwinfo=y CONFIG_PACKAGE_libiwinfo-lua=y +CONFIG_PACKAGE_libpopt=y CONFIG_PACKAGE_liblua=y CONFIG_PACKAGE_libuci-lua=y +CONFIG_PACKAGE_logrotate=y CONFIG_PACKAGE_lua=y CONFIG_PACKAGE_luafilesystem=y # CONFIG_PACKAGE_odhcp6c is not set diff --git a/extra/openwrt-build/openwrt-diffconfig-regular b/extra/openwrt-build/openwrt-diffconfig-regular index 6e01360..af76be8 100644 --- a/extra/openwrt-build/openwrt-diffconfig-regular +++ b/extra/openwrt-build/openwrt-diffconfig-regular @@ -9,8 +9,10 @@ CONFIG_PACKAGE_kmod-usb-serial=y CONFIG_PACKAGE_kmod-usb-serial-ftdi=y CONFIG_PACKAGE_libiwinfo=y CONFIG_PACKAGE_libiwinfo-lua=y +CONFIG_PACKAGE_libpopt=y CONFIG_PACKAGE_liblua=y CONFIG_PACKAGE_libuci-lua=y +CONFIG_PACKAGE_logrotate=y CONFIG_PACKAGE_lua=y CONFIG_PACKAGE_luafilesystem=y CONFIG_PACKAGE_print3d=y From ae65e0841c060229644ed95e1754106db5770ecd Mon Sep 17 00:00:00 2001 From: peteruithoven Date: Wed, 16 Mar 2016 16:58:32 +0100 Subject: [PATCH 39/62] Better print3d and new serial loglite filters --- src/script/loglite-filters.lua | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/script/loglite-filters.lua b/src/script/loglite-filters.lua index b8040e0..3809a40 100644 --- a/src/script/loglite-filters.lua +++ b/src/script/loglite-filters.lua @@ -21,6 +21,25 @@ M.firmware = { M.print3d = { ['parent'] = 'default', ['patterns'] = { + ['Print 3D server'] = 'byellow', + ['sendCode%(%)'] = 'green', + ['readCode%(%)'] = 'blue', + ['readResponseCode%(%)'] = 'blue' + } +} + +-- filter rules for serial communcation of print3d +M.serial = { + ['options'] = { mode = 'delete', count = 'none' }, + ['patterns'] = { + ['Print 3D server'] = 'byellow,_nodelete', + ['sendCode%(%)'] = 'green,_nodelete', + ['readCode%(%)'] = 'blue,_nodelete', + ['readResponseCode%(%)'] = 'blue,_nodelete', + ['setState%(%)'] = 'bblue,_nodelete', + ['%[ABSD%]'] = 'gray,_nodelete', -- 0.10.10 + ['%[ABD%]'] = 'gray,_nodelete', -- 0.10.9 + ['%(info%)'] = 'gray,_nodelete' -- 0.10.10 } } From eebd00c32f14a449278de898bf86c46296660fda Mon Sep 17 00:00:00 2001 From: peteruithoven Date: Wed, 16 Mar 2016 17:05:58 +0100 Subject: [PATCH 40/62] loglite readme: code highlighting --- src/script/README-loglite.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/script/README-loglite.md b/src/script/README-loglite.md index 4f210a8..971a6b8 100644 --- a/src/script/README-loglite.md +++ b/src/script/README-loglite.md @@ -15,6 +15,7 @@ The script can follow an existing log file (comparable to `tail -f`), or it can The script looks for filter sets in the file '$HOME/loglite-filters.lua'. It looks like this: +``` lua local M = {} M.default = { @@ -35,6 +36,7 @@ The script looks for filter sets in the file '$HOME/loglite-filters.lua'. It loo } return M +``` Here, the declaration and returning of `M` is required for the loglite script to be able to cleanly import the file. In `M.default`, 'default' is the name of a filter set being defined (similar for 'specialization'). Definitions can contain three so-called keys: 'parent' specifies a filter set to inherit from in order to reduce code duplication, 'options' and 'patterns' are described below. From f06e3da46be68c285833d2697271c74bb75f7531 Mon Sep 17 00:00:00 2001 From: peteruithoven Date: Wed, 16 Mar 2016 17:06:31 +0100 Subject: [PATCH 41/62] loglite readme: filter whole log file example --- src/script/README-loglite.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/script/README-loglite.md b/src/script/README-loglite.md index 971a6b8..64c3098 100644 --- a/src/script/README-loglite.md +++ b/src/script/README-loglite.md @@ -7,8 +7,11 @@ The loglite script allows coloring and filtering of log files by specifying cert The script can follow an existing log file (comparable to `tail -f`), or it can follow its standard input. A file to follow is always specified as the first argument and a filter set name as the second (use '-' as file name to read from standard input). Details on filter sets can be found below. If no filter set is mentioned on the command-line, the script will attempt to use one named 'default'. -* Example following an existing log file using a filter set named 'example': `./loglite.lua print3d.log example`. -* Example using standard input, to capture both output streams from `print3d`, with a filter set named 'example' (note the '-' as file name): `./print3d -V 2>&1 | ./loglite.lua - example`. +* Example following an existing log file using a filter set named 'example': `./loglite.lua print3d.log example`. +* Example using standard input, to filter/view a whole log file, with a filter set named 'serial' (note the '-' as file name): +`cat print3d-ttyACM0.log | ./loglite.lua - serial` +* Example using standard input, to capture both output streams from `print3d`, with a filter set named 'example' (note the '-' as file name): +`./print3d -V 2>&1 | ./loglite.lua - example`. ### Filter sets From 4db83c98c5b512c74adeda49bbc9c0253b85ff1b Mon Sep 17 00:00:00 2001 From: peteruithoven Date: Wed, 16 Mar 2016 17:36:41 +0100 Subject: [PATCH 42/62] loglite readme additions like installation --- src/script/README-loglite.md | 21 +++++++++++++++++++++ src/script/loglite.lua | 12 ++++++------ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/script/README-loglite.md b/src/script/README-loglite.md index 64c3098..3457f94 100644 --- a/src/script/README-loglite.md +++ b/src/script/README-loglite.md @@ -65,3 +65,24 @@ The following directives can be associated with a pattern: * `_delete` or `_nodelete` to override the active mode specified in the 'options' above. Directives can be combined with ',' (e.g.: `'red,_nodelete'`). Finally, in any filter set, pattern rules are matched from top to bottom, the last one encountered overriding any previous conflicting directive. + +### Installation +Note: Loglite is already installed on the WiFi-Box since version 0.10.10. + +Install Lua. See: +http://lua-users.org/wiki/LuaBinaries +It's tested in Lua 5.1 and Lua 5.2. + +Loglite will check for a `loglite-filters.lua` file in your home directory. It's recommended to create a symbolic link to the latest version. +On OS X / Linux: +``` +cd +ln -s [absolute path to file]/loglite-filters.lua loglite-filters.lua +``` + +It's recommended to create a symbolic link in one of your PATH directories (`echo $PATH`) to the loglite.lua file. This allows you to run `loglite` from any directory. +On OS X / Linux: +``` +cd /usr/local/bin +ln -s [absolute path to file]/loglite.lua loglite.lua +``` diff --git a/src/script/loglite.lua b/src/script/loglite.lua index 9effc09..0fdbbd2 100755 --- a/src/script/loglite.lua +++ b/src/script/loglite.lua @@ -5,7 +5,7 @@ For documentation on this script, see README-loglite.md. Ideas for improvement: * add more directives like uppercase, prefix/suffix? -* create separate package for this script: a) since it is useful for any log file, b) this file is getting somewhat long +* create separate package for this script: a) since it is useful for any log file, b) this file is getting somewhat long * for broader terminal support: detect `tput` and use it if available (http://wiki.bash-hackers.org/scripting/terminalcodes) * pre-split keyword lists for efficiency instead of redoing this at every new line? @@ -158,7 +158,7 @@ local function tailStream(stream, filterSet) -- look for a pattern matching this line for p,c in pairs(patterns) do if line:match(p) then - --print("[DEBUG] +matched rule '" .. p .. "'/'" .. c .. "' against '" .. line .. "'") + -- print("[DEBUG] +matched rule '" .. p .. "'/'" .. c .. "' against '" .. line .. "'") local kws = c:split(',') if hasValue(kws, '_delete') then keepLine = false; keepLineOverridden = true @@ -193,7 +193,7 @@ local function tailStream(stream, filterSet) if options.count == 'all' then print(c, embellished) else print(embellished) end else - --print("[DEBUG] -skipped '"..line.."'") + -- print("[DEBUG] -skipped '"..line.."'") end --c = line:match 'truncated' and 0 or c -- from tail on stderr apparently @@ -207,9 +207,9 @@ local function readConfigFile(filename, searchPath) --print("[DEBUG] config file '" .. fullPath .. "' not found") return nil end - + --print("[DEBUG] using config file '" .. fullPath .. "'") - -- require does not accept full paths? also, pcall does not help with dofile + -- require does not accept full paths? also, pcall does not help with dofile return dofile(fullPath) end @@ -246,7 +246,7 @@ local function main() -- read filter set file if available local configSets = readConfigFile(DFL_FILTERSET_FILE, os.getenv('HOME')) or {} local filterSet = readFilterSet(configSets, filterSetName) - --print("[DEBUG] final filter set for '" .. filterSetName .. "' from config: " .. dump(filterSet)) + -- print("[DEBUG] final filter set for '" .. filterSetName .. "' from config: " .. dump(filterSet)) -- if requested, display help and exit if showHelp and showHelp == true then From 5d22267ae3abfa03ddcb8c53b3e24474275d5eb7 Mon Sep 17 00:00:00 2001 From: peteruithoven Date: Wed, 16 Mar 2016 18:40:31 +0100 Subject: [PATCH 43/62] Loglite readme fixes --- src/script/README-loglite.md | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/src/script/README-loglite.md b/src/script/README-loglite.md index 3457f94..8922714 100644 --- a/src/script/README-loglite.md +++ b/src/script/README-loglite.md @@ -7,38 +7,42 @@ The loglite script allows coloring and filtering of log files by specifying cert The script can follow an existing log file (comparable to `tail -f`), or it can follow its standard input. A file to follow is always specified as the first argument and a filter set name as the second (use '-' as file name to read from standard input). Details on filter sets can be found below. If no filter set is mentioned on the command-line, the script will attempt to use one named 'default'. -* Example following an existing log file using a filter set named 'example': `./loglite.lua print3d.log example`. +* Example following an existing log file using a filter set named 'example': +`./loglite.lua print3d.log example`. * Example using standard input, to filter/view a whole log file, with a filter set named 'serial' (note the '-' as file name): `cat print3d-ttyACM0.log | ./loglite.lua - serial` * Example using standard input, to capture both output streams from `print3d`, with a filter set named 'example' (note the '-' as file name): `./print3d -V 2>&1 | ./loglite.lua - example`. +#### On WiFi-Box +Loglite is already installed since version 0.10.10 as `loglite`. +Check `/root/.profile` for handy aliases like `tailfw` and `tailp3d`. ### Filter sets The script looks for filter sets in the file '$HOME/loglite-filters.lua'. It looks like this: ``` lua - local M = {} +local M = {} - M.default = { - ['options'] = { mode = 'keep', count = 'none' }, - ['patterns'] = { - ['%(error%)'] = 'red', - ['%(warning%)'] = 'yellow', - ['%(bulk%)'] = 'bold,black' - } +M.default = { + ['options'] = { mode = 'keep', count = 'none' }, + ['patterns'] = { + ['%(error%)'] = 'red', + ['%(warning%)'] = 'yellow', + ['%(bulk%)'] = 'bold,black' } +} - M.specialization = { - ['parent'] = 'default', - ['options'] = { mode = 'delete' } - ['patterns'] = { - ['setState%(%)'] = 'bblue,_nodelete' - } +M.specialization = { + ['parent'] = 'default', + ['options'] = { mode = 'delete' } + ['patterns'] = { + ['setState%(%)'] = 'bblue,_nodelete' } +} - return M +return M ``` Here, the declaration and returning of `M` is required for the loglite script to be able to cleanly import the file. In `M.default`, 'default' is the name of a filter set being defined (similar for 'specialization'). Definitions can contain three so-called keys: 'parent' specifies a filter set to inherit from in order to reduce code duplication, 'options' and 'patterns' are described below. From 61fcb7ecb61c6c27c77f057109ace98a4ac6edac Mon Sep 17 00:00:00 2001 From: Wouter R Date: Wed, 16 Mar 2016 22:30:39 +0100 Subject: [PATCH 44/62] Makefile/opkg: register configuration file as such. --- Makefile | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Makefile b/Makefile index b2cdac2..e0361dc 100644 --- a/Makefile +++ b/Makefile @@ -66,6 +66,12 @@ define Build/Compile directives # no compilation necessary (although possible with luac?) endef +# This information is contained within the ipk file and (at least) used by +# sysupgrade to determine which files to keep (see `opkg list-changed-conffiles`). +define Package/wifibox/conffiles +/etc/config/wifibox +endef + # The $(1) variable represents the root directory on the router running # OpenWrt. The $(INSTALL_DIR) variable contains a command to prepare the install # directory if it does not already exist. Likewise $(INSTALL_BIN) contains the From 453ec4294f1548a40e71a2d4a42c5860a01a0a24 Mon Sep 17 00:00:00 2001 From: Wouter R Date: Wed, 16 Mar 2016 22:33:38 +0100 Subject: [PATCH 45/62] Log rotation: - install logrotate wifibox.conf file, - create directories required for log rotation on boot. --- Makefile | 3 +++ src/script/logrotate-wifibox.conf | 13 +++++++++++++ src/script/wifibox_init | 6 ++++-- 3 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 src/script/logrotate-wifibox.conf diff --git a/Makefile b/Makefile index e0361dc..abb567e 100644 --- a/Makefile +++ b/Makefile @@ -70,6 +70,7 @@ endef # sysupgrade to determine which files to keep (see `opkg list-changed-conffiles`). define Package/wifibox/conffiles /etc/config/wifibox +/etc/logrotate.d/wifibox.conf endef # The $(1) variable represents the root directory on the router running @@ -94,6 +95,7 @@ define Package/wifibox/install #$(INSTALL_DIR) $(1)/etc $(INSTALL_DIR) $(1)/etc/init.d $(INSTALL_DIR) $(1)/etc/config + $(INSTALL_DIR) $(1)/etc/logrotate.d $(INSTALL_DIR) $(1)/root/ $(INSTALL_DIR) $(1)/root/sketches #$(INSTALL_DIR) $(1)/www @@ -117,6 +119,7 @@ define Package/wifibox/install $(INSTALL_BIN) $(WIFIBOX_BASE_DIR)/script/dhcpcheck_init $(1)/etc/init.d/dhcpcheck $(INSTALL_BIN) $(WIFIBOX_BASE_DIR)/script/d3dapi $(1)/$(TGT_LUA_DIR_SUFFIX)/script $(INSTALL_BIN) $(WIFIBOX_BASE_DIR)/script/signin.sh $(1)/$(TGT_LUA_DIR_SUFFIX)/script + $(CP) $(WIFIBOX_BASE_DIR)/script/logrotate-wifibox.conf $(1)/etc/logrotate.d/wifibox.conf $(CP) $(WIFIBOX_BASE_DIR)/script/wifibox.uci.config $(1)/etc/config/wifibox # copy base configuration to uci config dir $(CP) $(WIFIBOX_BASE_DIR)/FIRMWARE-VERSION $(1)/etc/wifibox-version diff --git a/src/script/logrotate-wifibox.conf b/src/script/logrotate-wifibox.conf new file mode 100644 index 0000000..edc6176 --- /dev/null +++ b/src/script/logrotate-wifibox.conf @@ -0,0 +1,13 @@ +# Doodle3D logrotate configuration + +compress +su +create + +/tmp/wifibox.log /tmp/print3d-*.log { + rotate 2 + size 1000k + missingok + olddir /tmp/wifibox-rotated + copytruncate +} diff --git a/src/script/wifibox_init b/src/script/wifibox_init index 903ad60..4123d1f 100644 --- a/src/script/wifibox_init +++ b/src/script/wifibox_init @@ -17,9 +17,11 @@ boot() { $LOGGER "Invoking Doodle3D WiFi box network auto-initialization..." /usr/share/lua/wifibox/script/d3dapi autowifi - $LOGGER "Start signing in..." - /usr/share/lua/wifibox/script/signin.sh > /dev/null 2> /dev/null & + $LOGGER "Start signing in..." + /usr/share/lua/wifibox/script/signin.sh > /dev/null 2> /dev/null & + mkdir -p /var/lib # required by logrotate for logrotate.status + mkdir -p /tmp/wifibox-rotated # this is where rotated wifibox logs are placed } start() { From 19eef4a334ebad46b8b3467ff1e4e224f0917dbc Mon Sep 17 00:00:00 2001 From: Wouter R Date: Wed, 16 Mar 2016 22:54:21 +0100 Subject: [PATCH 46/62] Include rotated log files in 'info/logfiles' API endpoint. --- src/rest/api/api_info.lua | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/rest/api/api_info.lua b/src/rest/api/api_info.lua index f5283c1..f0281a9 100644 --- a/src/rest/api/api_info.lua +++ b/src/rest/api/api_info.lua @@ -23,6 +23,8 @@ local DEFAULT_WIFIBOX_LOG_FILENAME = 'wifibox.log' local DEFAULT_WIFIBOX_LOG_FILE = TMP_DIR .. '/' .. DEFAULT_WIFIBOX_LOG_FILENAME local WIFIBOX_STDOUT_LOG_FILENAME = 'wifibox.stdout.log' local WIFIBOX_STDOUT_LOG_FILE = TMP_DIR .. '/' .. WIFIBOX_STDOUT_LOG_FILENAME +local ROTATED_LOGS_DIRNAME = 'wifibox-rotated' +local ROTATED_LOGS_DIR = TMP_DIR .. '/' .. ROTATED_LOGS_DIRNAME local MOD_ABBR = "AINF" local SYSLOG_FILENAME = 'syslog' @@ -72,12 +74,15 @@ function M.logfiles(request, response) --[[ create temporary files ]]-- - -- copy wifibox API-script log + -- copy wifibox API-script (firmware) log rv,sig,code = redirectedExecute('cp ' .. wifiboxLogFilePath .. ' ' .. LOG_COLLECT_DIR) - -- copy d3dapi script stdout/stderr log + -- copy d3dapi script stdout/stderr (fallback) log rv,sig,code = redirectedExecute('cp ' .. WIFIBOX_STDOUT_LOG_FILE .. ' ' .. LOG_COLLECT_DIR) + -- copy rotated firmware and print3d logs + rv,sig,code = redirectedExecute('cp -r ' .. ROTATED_LOGS_DIR .. ' ' .. LOG_COLLECT_DIR) + -- capture syslog rv,sig,code = os.execute('logread > ' .. LOG_COLLECT_DIR .. '/' .. SYSLOG_FILENAME) @@ -142,12 +147,17 @@ function M.logfiles(request, response) rv,sig,code = redirectedExecute('rm ' .. LOG_COLLECT_DIR .. '/config/*') rv,msg = lfs.rmdir(LOG_COLLECT_DIR .. '/config') + -- Note: this assumes the rotated logs directory does not contain subdirectories + rv,sig,code = redirectedExecute('rm ' .. LOG_COLLECT_DIR .. '/' .. ROTATED_LOGS_DIRNAME .. '/*') + rv,msg = lfs.rmdir(LOG_COLLECT_DIR .. '/' .. ROTATED_LOGS_DIRNAME) + rv,sig,code = redirectedExecute('rm ' .. LOG_COLLECT_DIR .. '/' .. USB_DIRTREE_FILENAME) rv,sig,code = redirectedExecute('rm ' .. LOG_COLLECT_DIR .. '/' .. DISKFREE_FILENAME) rv,sig,code = redirectedExecute('rm ' .. LOG_COLLECT_DIR .. '/' .. MOUNTS_FILENAME) rv,sig,code = redirectedExecute('rm ' .. LOG_COLLECT_DIR .. '/' .. MEMINFO_FILENAME) rv,sig,code = redirectedExecute('rm ' .. LOG_COLLECT_DIR .. '/' .. PROCESS_LIST_FILENAME) rv,sig,code = redirectedExecute('rm ' .. LOG_COLLECT_DIR .. '/' .. SYSLOG_FILENAME) + rv,sig,code = redirectedExecute('rm ' .. LOG_COLLECT_DIR .. '/' .. WIFIBOX_STDOUT_LOG_FILENAME) rv,sig,code = redirectedExecute('rm ' .. LOG_COLLECT_DIR .. '/' .. wifiboxLogFileName) rv,msg = lfs.rmdir(LOG_COLLECT_DIR) From 4165d3b46d88bd1292b97824b980661e4848f23b Mon Sep 17 00:00:00 2001 From: Wouter R Date: Fri, 18 Mar 2016 09:08:15 +0100 Subject: [PATCH 47/62] When preparing archive for 'info/logfiles' endpoint, create hardlinks to log files instead of copies to require less temporary space. --- src/rest/api/api_info.lua | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/src/rest/api/api_info.lua b/src/rest/api/api_info.lua index f0281a9..4e00994 100644 --- a/src/rest/api/api_info.lua +++ b/src/rest/api/api_info.lua @@ -75,13 +75,27 @@ function M.logfiles(request, response) --[[ create temporary files ]]-- -- copy wifibox API-script (firmware) log - rv,sig,code = redirectedExecute('cp ' .. wifiboxLogFilePath .. ' ' .. LOG_COLLECT_DIR) + lfs.link(wifiboxLogFilePath, LOG_COLLECT_DIR .. '/' .. wifiboxLogFileName) -- copy d3dapi script stdout/stderr (fallback) log - rv,sig,code = redirectedExecute('cp ' .. WIFIBOX_STDOUT_LOG_FILE .. ' ' .. LOG_COLLECT_DIR) + lfs.link(WIFIBOX_STDOUT_LOG_FILE, LOG_COLLECT_DIR .. '/' .. WIFIBOX_STDOUT_LOG_FILENAME) + + -- collect and copy print3d server logs + for file in lfs.dir(PRINT3D_BASEPATH) do + if file:find(PRINT3D_LOG_FILENAME_PREFIX) == 1 and file:find(PRINT3D_LOG_FILENAME_SUFFIX) ~= nil then + local srcLogFile = PRINT3D_BASEPATH .. '/' .. file + local tgtLogFile = LOG_COLLECT_DIR .. '/' .. file + lfs.link(srcLogFile, tgtLogFile) + end + end -- copy rotated firmware and print3d logs - rv,sig,code = redirectedExecute('cp -r ' .. ROTATED_LOGS_DIR .. ' ' .. LOG_COLLECT_DIR) + rv,msg = lfs.mkdir(LOG_COLLECT_DIR .. '/' .. ROTATED_LOGS_DIRNAME) + for file in lfs.dir(ROTATED_LOGS_DIR) do + local srcLogFile = ROTATED_LOGS_DIR .. '/' .. file + local tgtLogFile = LOG_COLLECT_DIR .. '/' .. ROTATED_LOGS_DIRNAME .. '/' .. file + lfs.link(srcLogFile, tgtLogFile) + end -- capture syslog rv,sig,code = os.execute('logread > ' .. LOG_COLLECT_DIR .. '/' .. SYSLOG_FILENAME) @@ -102,6 +116,7 @@ function M.logfiles(request, response) rv,sig,code = os.execute(USB_DIRTREE_COMMAND .. ' > ' .. LOG_COLLECT_DIR .. '/' .. USB_DIRTREE_FILENAME) -- copy relevant openwrt configuration files + -- Note: we cannot link them because that would require the link to span over filesystems rv,msg = lfs.mkdir(LOG_COLLECT_DIR .. '/config') for _,v in pairs(UCI_CONFIG_FILES_TO_SAVE) do local srcFile = '/etc/config/' .. v @@ -109,19 +124,12 @@ function M.logfiles(request, response) if v ~= 'wireless' then rv,sig,code = redirectedExecute('cp ' .. srcFile .. ' ' .. tgtFile) else + -- replace WiFi passwords with '...' rv,sig,code = os.execute("sed \"s/option key '.*'/option key '...'/g\" " .. srcFile .. " > " .. tgtFile) end end - -- collect and copy print3d server logs - for file in lfs.dir(PRINT3D_BASEPATH) do - if file:find(PRINT3D_LOG_FILENAME_PREFIX) == 1 and file:find(PRINT3D_LOG_FILENAME_SUFFIX) ~= nil then - local srcLogFile = PRINT3D_BASEPATH .. '/' .. file - local tgtLogFile = LOG_COLLECT_DIR .. '/' .. file - rv,sig,code = redirectedExecute('cp ' .. srcLogFile .. ' ' .. tgtLogFile) - end - end - + -- create tar.gz archive of the files/data we collected rv,sig,code = redirectedExecute('tar czf ' .. LOG_COLLECT_ARCHIVE_FILE .. ' ' .. LOG_COLLECT_DIRNAME) --returns 0 success, 1 error From 584a5bfce2574f6217aa0fb9b74b7fdd4916ff7a Mon Sep 17 00:00:00 2001 From: Wouter R Date: Fri, 18 Mar 2016 11:31:07 +0100 Subject: [PATCH 48/62] Log rotation: - create cron job in postinst; - start+enable cron in postinst. --- post-install.sh | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/post-install.sh b/post-install.sh index ba70915..62d43bb 100644 --- a/post-install.sh +++ b/post-install.sh @@ -76,11 +76,11 @@ fi #preserve saved sketches during firmware update echo "/root/sketches" >> $IPKG_INSTROOT/etc/sysupgrade.conf + + ### Finally make sure basic configuration is set correctly -$IPKG_INSTROOT/etc/init.d/wifibox enable -$IPKG_INSTROOT/etc/init.d/wifibox start -$IPKG_INSTROOT/etc/init.d/dhcpcheck enable +LOGROTATE_CRON_LINE="*/3 * * * * /usr/sbin/logrotate /etc/logrotate.conf" if [ -z "$IPKG_INSTROOT" ]; then # No installation root, we are being installed on a live box so run uci commands directly. @@ -105,6 +105,13 @@ if [ -z "$IPKG_INSTROOT" ]; then uci -q delete wifibox.system.loglevel # remove key used in older versions (<=0.10.8a) if it exists uci commit wifibox + crontab -l 2> /dev/null | grep logrotate\.conf > /dev/null + if [ $? -ne 0 ]; then + # add line, method from http://askubuntu.com/a/58582 + # Note: `crontab -l` will throw an error to stderr because the file does not exist, but that does not matter + (crontab -l 2> /dev/null; echo "$LOGROTATE_CRON_LINE" ) | crontab - + fi + else # Create a script to setup the system as wifibox, it will be deleted after it has been run, except if it returns > 0. @@ -127,10 +134,23 @@ else uci set wifibox.general.system_log_level='info' uci -q delete wifibox.system.loglevel # remove key used in older versions (<=0.10.8a) if it exists + crontab -l 2> /dev/null | grep logrotate\.conf > /dev/null + if [ $? -ne 0 ]; then + # add line, method from http://askubuntu.com/a/58582 + # Note: `crontab -l` will throw an error to stderr because the file does not exist, but that does not matter + (crontab -l 2> /dev/null; echo "$LOGROTATE_CRON_LINE" ) | crontab - + fi + exit 0 EOM echo "WARNING: WiFiBox network configuration can only be fully prepared when installing on real device" fi +$IPKG_INSTROOT/etc/init.d/wifibox enable +$IPKG_INSTROOT/etc/init.d/wifibox start +$IPKG_INSTROOT/etc/init.d/dhcpcheck enable +$IPKG_INSTROOT/etc/init.d/cron enable +$IPKG_INSTROOT/etc/init.d/cron start + exit 0 From 00d77c491db0ae007b70b18f0b19086ff42830a2 Mon Sep 17 00:00:00 2001 From: Wouter R Date: Fri, 18 Mar 2016 16:28:21 +0100 Subject: [PATCH 49/62] Log rotation: store first 500 lines of print3d and firmware logs separately to prevent them from being rotated. --- src/script/logrotate-wifibox.conf | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/script/logrotate-wifibox.conf b/src/script/logrotate-wifibox.conf index edc6176..4c5a672 100644 --- a/src/script/logrotate-wifibox.conf +++ b/src/script/logrotate-wifibox.conf @@ -9,5 +9,28 @@ create size 1000k missingok olddir /tmp/wifibox-rotated - copytruncate + + # as alternative to copytruncate (which *might* miss lines) we could implement + # SIGUSR1 in print3d and firmware to reopen their logs + copytruncate # 'create' is ignored with copytruncate, as the file stays in place + + prerotate + FIRST_N_LINES=500 + PRINT3D_LOGHEAD_FILE=print3d-loghead + WIFIBOX_LOGHEAD_FILE=wifibox-loghead + ROTATED_FILES_PATH=/tmp/wifibox-rotated + + #Note: by looking at creation date of the print3d socket and comparing against that + # of print3d-loghead we could detect a server restart (e.g. due to reconnected printer) + + echo "$1" | grep -q "print3d-.*\.log" + if [ $? -eq 0 -a ! -f "$ROTATED_FILES_PATH/$PRINT3D_LOGHEAD_FILE" ]; then + head -q -n$FIRST_N_LINES "$1" > "$ROTATED_FILES_PATH/$PRINT3D_LOGHEAD_FILE" + fi + + echo "$1" | grep -q "wifibox.log" + if [ $? -eq 0 -a ! -f "$ROTATED_FILES_PATH/$WIFIBOX_LOGHEAD_FILE" ]; then + head -q -n$FIRST_N_LINES "$1" > "$ROTATED_FILES_PATH/$WIFIBOX_LOGHEAD_FILE" + fi + endscript } From ca45f151510a907f0fd18ed3f05d97ee8444368a Mon Sep 17 00:00:00 2001 From: Wouter R Date: Fri, 18 Mar 2016 17:04:20 +0100 Subject: [PATCH 50/62] Improve printer/print logging, partial fix for https://github.com/Doodle3D/doodle3d-firmware/issues/58. --- src/rest/api/api_printer.lua | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/rest/api/api_printer.lua b/src/rest/api/api_printer.lua index c3a08d6..1bfed2c 100644 --- a/src/rest/api/api_printer.lua +++ b/src/rest/api/api_printer.lua @@ -120,7 +120,6 @@ end function M.heatup_POST(request, response) - if not accessManager.hasControl(request.remoteAddress) then response:setFail("No control access") return @@ -145,8 +144,6 @@ function M.heatup_POST(request, response) end function M.stop_POST(request, response) - log:info(MOD_ABBR, "API:printer/stop") - if not accessManager.hasControl(request.remoteAddress) then response:setFail("No control access") return @@ -183,8 +180,6 @@ end --accepts: seq_total(int) (total number of gcode chunks to be appended, must be given until clear() after given once, and stay the same) --returns: when the gcode buffer cannot accept the gcode, or the IPC transaction fails, a fail with a (formal, i.e., parseable) status argument will be returned function M.print_POST(request, response) - log:info(MOD_ABBR, "API:printer/print") - local controllerIP = accessManager.getController() local hasControl = false if controllerIP == "" then @@ -194,7 +189,6 @@ function M.print_POST(request, response) hasControl = true end - log:info(MOD_ABBR, " hasControl: "..utils.dump(hasControl)) if not hasControl then response:setFail("No control access") return @@ -209,6 +203,8 @@ function M.print_POST(request, response) local argSeqNumber = request:get("seq_number") or -1 local argSeqTotal = request:get("seq_total") or -1 local remoteHost = request:getRemoteHost() + + log:info(MOD_ABBR, "print chunk metadata: total_lines=" .. argTotalLines .. ", seq_number=" .. argSeqNumber .. ", seq_total=" .. argSeqTotal) local printer,msg = printerUtils.createPrinterOrFail(argId, response) if not printer or not printer:hasSocket() then return end From 8fb0eba9e650f26a82e5ab9d7af1c2da111e163c Mon Sep 17 00:00:00 2001 From: peteruithoven Date: Mon, 21 Mar 2016 12:45:41 +0100 Subject: [PATCH 51/62] 0.10.10-b version & releasenotes --- ReleaseNotes.md | 6 ++++++ src/FIRMWARE-VERSION | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/ReleaseNotes.md b/ReleaseNotes.md index 6f8dd56..c5a8157 100644 --- a/ReleaseNotes.md +++ b/ReleaseNotes.md @@ -1,4 +1,10 @@ Changelog +# 0.10.10-b (21 mrt 2016) +- Logrotation (making sure older logs are removed) +- Improved logging. +- Always log initial printer communication +- loglite improvements, added readme + # 0.10.10-a (24 feb 2016) - Max buffer size check: the WiFi-Box can now indicate through the API that its buffer is full and it won't accept new gcode. - Buffer information is communicated through the API so all clients can handle this properly. The Doodle3D client will stop sending and waits until it drops below 75%. It will check this every 30 seconds. diff --git a/src/FIRMWARE-VERSION b/src/FIRMWARE-VERSION index 8869810..2c3ec4c 100644 --- a/src/FIRMWARE-VERSION +++ b/src/FIRMWARE-VERSION @@ -1 +1 @@ -0.10.10-a +0.10.10-b From 35bbac596b75f2b92aeec8b4b19ad781914dc366 Mon Sep 17 00:00:00 2001 From: Wouter R Date: Wed, 23 Mar 2016 17:08:29 +0100 Subject: [PATCH 52/62] Fix day/month order in firmware log timestamp. --- src/util/logger.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/util/logger.lua b/src/util/logger.lua index 80604e9..b1ddc16 100644 --- a/src/util/logger.lua +++ b/src/util/logger.lua @@ -44,7 +44,7 @@ end local function log(level, module, msg, verboseFmt) if level <= logLevel then - local now = os.date('%m-%d %H:%M:%S') + local now = os.date('%d-%m %H:%M:%S') local i = debug.getinfo(3) --the stack frame just above the logger call local v = verboseFmt if v == nil then v = logVerboseFmt end From ad048349b3a960743a5fb949410d420fb1f3953c Mon Sep 17 00:00:00 2001 From: Wouter R Date: Tue, 29 Mar 2016 14:34:58 +0200 Subject: [PATCH 53/62] Lower logrotate cron interval from 3 to 2 minutes. --- post-install.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/post-install.sh b/post-install.sh index 62d43bb..2f4282f 100644 --- a/post-install.sh +++ b/post-install.sh @@ -80,7 +80,7 @@ echo "/root/sketches" >> $IPKG_INSTROOT/etc/sysupgrade.conf ### Finally make sure basic configuration is set correctly -LOGROTATE_CRON_LINE="*/3 * * * * /usr/sbin/logrotate /etc/logrotate.conf" +LOGROTATE_CRON_LINE="*/2 * * * * /usr/sbin/logrotate /etc/logrotate.conf" if [ -z "$IPKG_INSTROOT" ]; then # No installation root, we are being installed on a live box so run uci commands directly. From 0bdaca49713fb776d56b5f84da69f731d329c759 Mon Sep 17 00:00:00 2001 From: peteruithoven Date: Fri, 1 Apr 2016 11:45:07 +0200 Subject: [PATCH 54/62] 0.10.10-c version & releasenotes --- ReleaseNotes.md | 3 +++ src/FIRMWARE-VERSION | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/ReleaseNotes.md b/ReleaseNotes.md index c5a8157..57dda2d 100644 --- a/ReleaseNotes.md +++ b/ReleaseNotes.md @@ -1,4 +1,7 @@ Changelog +# 0.10.10-c (1 api 2016) +- Quicker log rotation + # 0.10.10-b (21 mrt 2016) - Logrotation (making sure older logs are removed) - Improved logging. diff --git a/src/FIRMWARE-VERSION b/src/FIRMWARE-VERSION index 2c3ec4c..f2f011d 100644 --- a/src/FIRMWARE-VERSION +++ b/src/FIRMWARE-VERSION @@ -1 +1 @@ -0.10.10-b +0.10.10-c From 6a26cf3005e5076479f1e1de8815cf530c9b63ba Mon Sep 17 00:00:00 2001 From: Wouter R Date: Tue, 19 Apr 2016 00:27:10 +0200 Subject: [PATCH 55/62] Include sequence_number and sequence_total in printer/progress as well as printer/print (to improve client recovery on unstable networks). --- src/rest/api/api_printer.lua | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/rest/api/api_printer.lua b/src/rest/api/api_printer.lua index 1bfed2c..6086f33 100644 --- a/src/rest/api/api_printer.lua +++ b/src/rest/api/api_printer.lua @@ -54,7 +54,7 @@ function M.progress(request, response) if not printer or not printer:hasSocket() then return end -- NOTE: despite their names, `currentLine` is still the error indicator and `bufferedLines` the message in such case. - local currentLine,bufferedLines,totalLines,bufferSize,maxBufferSize = printer:getProgress() + local currentLine,bufferedLines,totalLines,bufferSize,maxBufferSize,seqNumber,seqTotal = printer:getProgress() response:addData('id', argId) if currentLine then @@ -64,6 +64,8 @@ function M.progress(request, response) response:addData('total_lines', totalLines) response:addData('buffer_size', bufferSize) response:addData('max_buffer_size', maxBufferSize) + response:addData('sequence_number', seqNumber) + response:addData('sequence_total', seqTotal) elseif progress == false then response:addData('status', bufferedLines) response:setFail("could not get progress information (" .. bufferedLines .. ")") @@ -170,6 +172,19 @@ function M.stop_POST(request, response) end end +-- Used only in print_POST(); not nested for performance reasons +local function addSequenceNumbering(printer, response) + -- NOTE: despite their names, `currentLine` is still the error indicator and `bufferedLines` the message in such case. + local currentLine,bufferedLines,totalLines,bufferSize,maxBufferSize,seqNumber,seqTotal = printer:getProgress() + if currentLine then + response:addData('sequence_number', seqNumber) + response:addData('sequence_total', seqTotal) + --else + --Note: getProgress failure is ignored (unlikely to happen if the other calls work, and also not really fatal here). + -- Alternatively, we could still add the fields with a special value (NaN is not supported by json, so perhaps -2?) + end +end + --requires: gcode(string) (the gcode to be appended) --accepts: id(string) (the printer ID to append to) --accepts: clear(bool) (chunks will be concatenated but output file will be cleared first if this argument is true) @@ -178,7 +193,9 @@ end --accepts: total_lines(int) (the total number of lines that is going to be sent, will be used only for reporting progress) --accepts: seq_number(int) (sequence number of the chunk, must be given until clear() after given once, and incremented each time) --accepts: seq_total(int) (total number of gcode chunks to be appended, must be given until clear() after given once, and stay the same) ---returns: when the gcode buffer cannot accept the gcode, or the IPC transaction fails, a fail with a (formal, i.e., parseable) status argument will be returned +--returns: when the gcode buffer cannot accept the gcode, or the IPC transaction fails, +-- a fail with a (formal, i.e., parseable) status argument will be returned; +-- additionally, current sequence number and total will be returned (both are -1 if they have not been set) function M.print_POST(request, response) local controllerIP = accessManager.getController() local hasControl = false @@ -234,13 +251,16 @@ function M.print_POST(request, response) rv,msg = printer:appendGcode(argGcode, argTotalLines, { seq_number = argSeqNumber, seq_total = argSeqTotal, source = remoteHost }) if rv then + addSequenceNumbering(printer, response) --NOTE: this does not report the number of lines, but only the block which has just been added response:addData('gcode_append',argGcode:len()) elseif rv == false then + addSequenceNumbering(printer, response) response:addData('status', msg) response:setFail("could not add gcode (" .. msg .. ")") return else + addSequenceNumbering(printer, response) response:addData('msg', msg) response:setError("could not add gcode (" .. msg .. ")") return From 7cc345d729d50ebccba18507b5ecf6b441f977ee Mon Sep 17 00:00:00 2001 From: Wouter R Date: Tue, 19 Apr 2016 12:10:40 +0200 Subject: [PATCH 56/62] Rename sequence_* in API responses to seq_* to be consistent with printer/print POST arguments. --- src/rest/api/api_printer.lua | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/rest/api/api_printer.lua b/src/rest/api/api_printer.lua index 6086f33..194ad82 100644 --- a/src/rest/api/api_printer.lua +++ b/src/rest/api/api_printer.lua @@ -64,8 +64,8 @@ function M.progress(request, response) response:addData('total_lines', totalLines) response:addData('buffer_size', bufferSize) response:addData('max_buffer_size', maxBufferSize) - response:addData('sequence_number', seqNumber) - response:addData('sequence_total', seqTotal) + response:addData('seq_number', seqNumber) + response:addData('seq_total', seqTotal) elseif progress == false then response:addData('status', bufferedLines) response:setFail("could not get progress information (" .. bufferedLines .. ")") @@ -177,8 +177,8 @@ local function addSequenceNumbering(printer, response) -- NOTE: despite their names, `currentLine` is still the error indicator and `bufferedLines` the message in such case. local currentLine,bufferedLines,totalLines,bufferSize,maxBufferSize,seqNumber,seqTotal = printer:getProgress() if currentLine then - response:addData('sequence_number', seqNumber) - response:addData('sequence_total', seqTotal) + response:addData('seq_number', seqNumber) + response:addData('seq_total', seqTotal) --else --Note: getProgress failure is ignored (unlikely to happen if the other calls work, and also not really fatal here). -- Alternatively, we could still add the fields with a special value (NaN is not supported by json, so perhaps -2?) From 64f1ab47ec1e3b2af518128ff681b971bf9ef611 Mon Sep 17 00:00:00 2001 From: peteruithoven Date: Wed, 20 Apr 2016 13:26:52 +0200 Subject: [PATCH 57/62] 0.10.10-d releasenotes & version update --- ReleaseNotes.md | 5 +++++ src/FIRMWARE-VERSION | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/ReleaseNotes.md b/ReleaseNotes.md index 57dda2d..e889946 100644 --- a/ReleaseNotes.md +++ b/ReleaseNotes.md @@ -1,4 +1,9 @@ Changelog +# 0.10.10-d (20 api 2016) +- Allow print part sequence numbers to be retrieved +- Sequence numbers are send back when sending print parts +- When WiFi-Box responds that print part was already received the client will send the next part. + # 0.10.10-c (1 api 2016) - Quicker log rotation diff --git a/src/FIRMWARE-VERSION b/src/FIRMWARE-VERSION index f2f011d..4bc44d5 100644 --- a/src/FIRMWARE-VERSION +++ b/src/FIRMWARE-VERSION @@ -1 +1 @@ -0.10.10-c +0.10.10-d From 65dd10b43cebc5e45b2de8796fc485d6415724ee Mon Sep 17 00:00:00 2001 From: peteruithoven Date: Wed, 20 Apr 2016 13:55:30 +0200 Subject: [PATCH 58/62] Removed some end gcode parts for Makerbot Fixes #40 --- src/conf_defaults.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/conf_defaults.lua b/src/conf_defaults.lua index 9d56de2..6f4327b 100644 --- a/src/conf_defaults.lua +++ b/src/conf_defaults.lua @@ -222,7 +222,7 @@ M.printer_startcode = { description = '' } -local default_makerbot_endcode = 'M73 P100\nG92 A0 B0 ;reset extruder position to prevent retraction\nM18 A B(Turn off A and B Steppers)\nG162 Z F900\nG162 X Y F2000\nM18 X Y Z(Turn off steppers after a build)\n{if heatedBed}M140 S{preheatBedTemp} T0\nM104 S{preheatTemp} T0\nM73 P100 (end build progress )\nM72 P1 ( Play Ta-Da song )\nM137 (build end notification)' +local default_makerbot_endcode = 'G92 A0 B0 ;reset extruder position to prevent retraction\nM18 A B(Turn off A and B Steppers)\nG162 Z F900\nG162 X Y F2000\nM18 X Y Z(Turn off steppers after a build)\n{if heatedBed}M140 S{preheatBedTemp} T0\nM104 S{preheatTemp} T0\nM72 P1 ( Play Ta-Da song )\nM137 (build end notification)' local default_deltabot_endcode = 'M107 ;fan offG91 ;relative positioningG1 E-1 F300 ;retract the filament a bit before lifting the nozzle, to release some of the pressureG1 Z+0.5 E-5 X-20 Y-20 F9000 ;move Z up a bit and retract filament even moreG28 ;move to homeM84 ;disable axes / steppersG90 ;absolute positioningM109 S0 ; hot end off{if heatedBed}M140 S{preheatBedTemp}M117 Done ;display message (20 characters to clear whole screen)' local default_ultimaker2_endcode = 'M107 ;fan off\nG91 ;relative positioning\nG1 E-1 F300 ;retract the filament a bit before lifting the nozzle, to release some of the pressure\nG1 Z+5.5 E-5 X-20 Y-20 F9000 ;move Z up a bit and retract filament even more\nG28 ;home the printer\nM84 ;disable axes / steppers\nG90 ;absolute positioning\nM104 S{preheatTemp}\n{if heatedBed}M140 S{preheatBedTemp}\nM117 Done ;display message (20 characters to clear whole screen)' M.printer_endcode = { From 0f822b99cd8f762f15686305da913471bf1179e2 Mon Sep 17 00:00:00 2001 From: peteruithoven Date: Thu, 21 Apr 2016 19:58:15 +0200 Subject: [PATCH 59/62] 0.10.10-e releasenotes & version update --- ReleaseNotes.md | 3 +++ src/FIRMWARE-VERSION | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/ReleaseNotes.md b/ReleaseNotes.md index e889946..5b30a21 100644 --- a/ReleaseNotes.md +++ b/ReleaseNotes.md @@ -1,4 +1,7 @@ Changelog +# 0.10.10-e (21 api 2016) +- Fixed Makerbot end gcode that prevented prints or stopping prints to finish. + # 0.10.10-d (20 api 2016) - Allow print part sequence numbers to be retrieved - Sequence numbers are send back when sending print parts diff --git a/src/FIRMWARE-VERSION b/src/FIRMWARE-VERSION index 4bc44d5..9c3cf51 100644 --- a/src/FIRMWARE-VERSION +++ b/src/FIRMWARE-VERSION @@ -1 +1 @@ -0.10.10-d +0.10.10-e From c6668958424f164ed6e1b37e69e43f8208d0b409 Mon Sep 17 00:00:00 2001 From: Wouter R Date: Sun, 24 Apr 2016 11:33:34 +0200 Subject: [PATCH 60/62] Include /etc/wifibox-version in info/logfiles endpoint (#305). --- src/rest/api/api_info.lua | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/rest/api/api_info.lua b/src/rest/api/api_info.lua index 4e00994..7374c27 100644 --- a/src/rest/api/api_info.lua +++ b/src/rest/api/api_info.lua @@ -16,6 +16,8 @@ local printerAPI = require('rest.api.api_printer') local wifi = require('network.wlanconfig') local settings = require('util.settings') +local MOD_ABBR = "AINF" + local TMP_DIR = '/tmp' local LOG_COLLECT_DIRNAME = 'wifibox-logs' local LOG_COLLECT_DIR = TMP_DIR .. '/' .. LOG_COLLECT_DIRNAME @@ -23,9 +25,10 @@ local DEFAULT_WIFIBOX_LOG_FILENAME = 'wifibox.log' local DEFAULT_WIFIBOX_LOG_FILE = TMP_DIR .. '/' .. DEFAULT_WIFIBOX_LOG_FILENAME local WIFIBOX_STDOUT_LOG_FILENAME = 'wifibox.stdout.log' local WIFIBOX_STDOUT_LOG_FILE = TMP_DIR .. '/' .. WIFIBOX_STDOUT_LOG_FILENAME +local WIFIBOX_VERSION_FILENAME = 'wifibox-version' +local WIFIBOX_VERSION_FILE = '/etc/' .. WIFIBOX_VERSION_FILENAME local ROTATED_LOGS_DIRNAME = 'wifibox-rotated' local ROTATED_LOGS_DIR = TMP_DIR .. '/' .. ROTATED_LOGS_DIRNAME -local MOD_ABBR = "AINF" local SYSLOG_FILENAME = 'syslog' local PROCESS_LIST_FILENAME = 'processes' @@ -115,6 +118,8 @@ function M.logfiles(request, response) -- list directory structure for primary USB controller rv,sig,code = os.execute(USB_DIRTREE_COMMAND .. ' > ' .. LOG_COLLECT_DIR .. '/' .. USB_DIRTREE_FILENAME) + rv,sig,code = redirectedExecute('cp ' .. WIFIBOX_VERSION_FILE .. ' ' .. LOG_COLLECT_DIR) + -- copy relevant openwrt configuration files -- Note: we cannot link them because that would require the link to span over filesystems rv,msg = lfs.mkdir(LOG_COLLECT_DIR .. '/config') @@ -159,6 +164,7 @@ function M.logfiles(request, response) rv,sig,code = redirectedExecute('rm ' .. LOG_COLLECT_DIR .. '/' .. ROTATED_LOGS_DIRNAME .. '/*') rv,msg = lfs.rmdir(LOG_COLLECT_DIR .. '/' .. ROTATED_LOGS_DIRNAME) + rv,sig,code = redirectedExecute('rm ' .. LOG_COLLECT_DIR .. '/' .. WIFIBOX_VERSION_FILENAME) rv,sig,code = redirectedExecute('rm ' .. LOG_COLLECT_DIR .. '/' .. USB_DIRTREE_FILENAME) rv,sig,code = redirectedExecute('rm ' .. LOG_COLLECT_DIR .. '/' .. DISKFREE_FILENAME) rv,sig,code = redirectedExecute('rm ' .. LOG_COLLECT_DIR .. '/' .. MOUNTS_FILENAME) From efd2b6aacb53017c4e2b7fd51ffb9a3cbe29e788 Mon Sep 17 00:00:00 2001 From: Wouter R Date: Sat, 7 May 2016 22:54:45 +0200 Subject: [PATCH 61/62] Log PID at start of each request and elapsed time when it finishes. --- src/main.lua | 33 +++++++++++++++++++++------------ src/script/README-loglite.md | 2 +- src/script/loglite-filters.lua | 2 ++ 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/src/main.lua b/src/main.lua index 6836e1d..89f61fa 100644 --- a/src/main.lua +++ b/src/main.lua @@ -22,6 +22,8 @@ local netconf = require('network.netconfig') local RequestClass = require('rest.request') local ResponseClass = require('rest.response') local Signin = require('network.signin') +local osUtils = require('os_utils') + -- NOTE: the updater module 'detects' command-line invocation by existence of 'arg', so we have to make sure it is not defined. argStash = arg @@ -206,17 +208,6 @@ end -- The logger is set up, any POST data is read and several other subsystems are initialized. -- @tparam table environment The 'shell' environment containing all CGI variables. Note that @{cmdmain} simulates this. local function init(environment) - setupLogger() - - local dbgText = "" - if confDefaults.DEBUG_API and confDefaults.DEBUG_PCALLS then dbgText = "pcall+api" - elseif confDefaults.DEBUG_API then dbgText = "api" - elseif confDefaults.DEBUG_PCALL then dbgText = "pcall" - end - - if dbgText ~= "" then dbgText = " (" .. dbgText .. " debugging)" end - log:verbose(MOD_ABBR, "=======rest api" .. dbgText .. "=======") - if (environment['REQUEST_METHOD'] == 'POST') then local n = tonumber(environment['CONTENT_LENGTH']) postData = io.read(n) @@ -292,6 +283,22 @@ end -- @tparam table env The CGI environment table. -- @treturn number A Z+ return value suitable to return from wrapper script. Note that this value is ignored by uhttpd-mod-lua. function handle_request(env) + local function constructDebugText() + local dbgText = "" + if confDefaults.DEBUG_API and confDefaults.DEBUG_PCALLS then dbgText = "pcall+api" + elseif confDefaults.DEBUG_API then dbgText = "api" + elseif confDefaults.DEBUG_PCALL then dbgText = "pcall" + end + if dbgText ~= "" then dbgText = " (" .. dbgText .. " debugging)" end + return dbgText + end + + setupLogger() + + local pid = osUtils.getPID() + local beginSecs, beginUSecs = osUtils.getTime() + + log:verbose(MOD_ABBR, "START-RQ with PID=" .. pid .. constructDebugText()) local s, msg = init(env) if s == false then @@ -300,11 +307,13 @@ function handle_request(env) resp:setError("initialization failed" .. errSuffix) resp:send() - log:error(MOD_ABBR, "Initialization failed" .. errSuffix) --NOTE: this assumes the logger has been initialized properly, despite init() having failed + log:error(MOD_ABBR, "Initialization failed" .. errSuffix) return 1 else main(env) + local elapsed,msg = osUtils.getElapsedTime(beginSecs, beginUSecs) + log:bulk(MOD_ABBR, "END-RQ with PID=" .. pid .. " completed in " .. elapsed .. " msec") return 0 end end diff --git a/src/script/README-loglite.md b/src/script/README-loglite.md index 8922714..ed2b1c6 100644 --- a/src/script/README-loglite.md +++ b/src/script/README-loglite.md @@ -53,7 +53,7 @@ Inheritance can be used to set new keys or to override keys from the parent set. Two options are currently available: -* `mode`, which specifies whether to keep log lines (`keep`, the default) or to drop them (`delete`). For specific lines this can then be overriden, see 'Patterns' below. +* `mode`, which specifies whether to keep log lines (`keep`, the default) or to drop them (`delete`). For specific lines this can then be overridden, see 'Patterns' below. * `count`, which can be set to `all` to prefix log lines with a counter, or `none` (default) to leave them as is. #### Patterns diff --git a/src/script/loglite-filters.lua b/src/script/loglite-filters.lua index 3809a40..2830da8 100644 --- a/src/script/loglite-filters.lua +++ b/src/script/loglite-filters.lua @@ -14,6 +14,8 @@ M.default = { M.firmware = { ['parent'] = 'default', ['patterns'] = { + ['START%-RQ'] = 'bblue', + ['END%-RQ'] = 'blue' } } From 1c88c17390409170a3c375b092155f11e586a218 Mon Sep 17 00:00:00 2001 From: peteruithoven Date: Wed, 11 May 2016 15:21:39 +0200 Subject: [PATCH 62/62] 0.10.10 version & releasenotes --- ReleaseNotes.md | 13 ++++++++++--- src/FIRMWARE-VERSION | 2 +- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/ReleaseNotes.md b/ReleaseNotes.md index 5b30a21..0483b36 100644 --- a/ReleaseNotes.md +++ b/ReleaseNotes.md @@ -1,13 +1,20 @@ Changelog -# 0.10.10-e (21 api 2016) +# 0.10.10 (11 may 2016) +- Logging client version info to console. +- Logging less when printing to makerbot/wanhao +- Logging API time info +- Included version number file in logfiles +- Fixed stopping while sending + +# 0.10.10-e (21 apr 2016) - Fixed Makerbot end gcode that prevented prints or stopping prints to finish. -# 0.10.10-d (20 api 2016) +# 0.10.10-d (20 apr 2016) - Allow print part sequence numbers to be retrieved - Sequence numbers are send back when sending print parts - When WiFi-Box responds that print part was already received the client will send the next part. -# 0.10.10-c (1 api 2016) +# 0.10.10-c (1 apr 2016) - Quicker log rotation # 0.10.10-b (21 mrt 2016) diff --git a/src/FIRMWARE-VERSION b/src/FIRMWARE-VERSION index 9c3cf51..ddf1d4a 100644 --- a/src/FIRMWARE-VERSION +++ b/src/FIRMWARE-VERSION @@ -1 +1 @@ -0.10.10-e +0.10.10