From af76720e54b12e0d07269857987230cfba3ab26f Mon Sep 17 00:00:00 2001 From: Wouter R Date: Wed, 4 Jun 2014 23:19:59 +0200 Subject: [PATCH] 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