Skip to content

Commit

Permalink
Refactor logging
Browse files Browse the repository at this point in the history
- Add warning log level
- Change debug_log_level setting to enumeration string
- Map Irrlicht log events to MT log events
- Encapsulate log_* functions and global variables into a class, Logger
- Unify dstream with standard logging mechanism
- Unify core.debug() with standard core.log() script API
  • Loading branch information
ShadowNinja authored and kwolekr committed Oct 14, 2015
1 parent e0b57c1 commit 2139d7d
Show file tree
Hide file tree
Showing 25 changed files with 574 additions and 627 deletions.
14 changes: 4 additions & 10 deletions builtin/common/strict.lua
Expand Up @@ -9,11 +9,6 @@ function core.global_exists(name)
end


local function warn(message)
print(os.date("%H:%M:%S: WARNING: ")..message)
end


local meta = {}
local declared = {}
-- Key is source file, line, and variable name; seperated by NULs
Expand All @@ -27,17 +22,16 @@ function meta:__newindex(name, value)
info.currentline, name)
if not warned[warn_key] and info.what ~= "main" and
info.what ~= "C" then
warn(("Assignment to undeclared "..
core.log("warning", ("Assignment to undeclared "..
"global %q inside a function at %s.")
:format(name, desc))
warned[warn_key] = true
end
declared[name] = true
end
-- Ignore mod namespaces
if WARN_INIT and (not core.get_current_modname or
name ~= core.get_current_modname()) then
warn(("Global variable %q created at %s.")
if WARN_INIT and name ~= core.get_current_modname() then
core.log("warning", ("Global variable %q created at %s.")
:format(name, desc))
end
rawset(self, name, value)
Expand All @@ -48,7 +42,7 @@ function meta:__index(name)
local info = debug.getinfo(2, "Sl")
local warn_key = ("%s\0%d\0%s"):format(info.source, info.currentline, name)
if not declared[name] and not warned[warn_key] and info.what ~= "C" then
warn(("Undeclared global variable %q accessed at %s:%s")
core.log("warning", ("Undeclared global variable %q accessed at %s:%s")
:format(name, info.short_src, info.currentline))
warned[warn_key] = true
end
Expand Down
6 changes: 4 additions & 2 deletions builtin/fstk/ui.lua
Expand Up @@ -127,11 +127,13 @@ function ui.update()
end

if (active_toplevel_ui_elements > 1) then
print("WARNING: ui manager detected more then one active ui element, self most likely isn't intended")
core.log("warning", "more than one active ui "..
"element, self most likely isn't intended")
end

if (active_toplevel_ui_elements == 0) then
print("WARNING: not a single toplevel ui element active switching to default")
core.log("warning", "no toplevel ui element "..
"active; switching to default")
ui.childlist[ui.default]:show()
formspec = ui.childlist[ui.default]:get_formspec()
end
Expand Down
13 changes: 6 additions & 7 deletions builtin/game/deprecated.lua
Expand Up @@ -4,8 +4,7 @@
-- Default material types
--
local function digprop_err()
core.log("info", debug.traceback())
core.log("info", "WARNING: The core.digprop_* functions are obsolete and need to be replaced by item groups.")
core.log("deprecated", "The core.digprop_* functions are obsolete and need to be replaced by item groups.")
end

core.digprop_constanttime = digprop_err
Expand All @@ -16,12 +15,12 @@ core.digprop_woodlike = digprop_err
core.digprop_leaveslike = digprop_err
core.digprop_glasslike = digprop_err

core.node_metadata_inventory_move_allow_all = function()
core.log("info", "WARNING: core.node_metadata_inventory_move_allow_all is obsolete and does nothing.")
function core.node_metadata_inventory_move_allow_all()
core.log("deprecated", "core.node_metadata_inventory_move_allow_all is obsolete and does nothing.")
end

core.add_to_creative_inventory = function(itemstring)
core.log('info', "WARNING: core.add_to_creative_inventory: This function is deprecated and does nothing.")
function core.add_to_creative_inventory(itemstring)
core.log("deprecated", "core.add_to_creative_inventory: This function is deprecated and does nothing.")
end

--
Expand All @@ -32,7 +31,7 @@ local envref_deprecation_message_printed = false
setmetatable(core.env, {
__index = function(table, key)
if not envref_deprecation_message_printed then
core.log("info", "WARNING: core.env:[...] is deprecated and should be replaced with core.[...]")
core.log("deprecated", "core.env:[...] is deprecated and should be replaced with core.[...]")
envref_deprecation_message_printed = true
end
local func = core[key]
Expand Down
2 changes: 1 addition & 1 deletion builtin/game/register.lua
Expand Up @@ -221,7 +221,7 @@ function core.register_alias(name, convert_to)
error("Unable to register alias: Name is forbidden: " .. name)
end
if core.registered_items[name] ~= nil then
core.log("WARNING: Not registering alias, item with same name" ..
core.log("warning", "Not registering alias, item with same name" ..
" is already defined: " .. name .. " -> " .. convert_to)
else
--core.log("Registering alias: " .. name .. " -> " .. convert_to)
Expand Down
2 changes: 1 addition & 1 deletion builtin/init.lua
Expand Up @@ -6,7 +6,7 @@
--

-- Initialize some very basic things
print = core.debug
function core.debug(...) core.log(table.concat({...}, "\t")) end
math.randomseed(os.time())
os.setlocale("C", "numeric")
minetest = core
Expand Down
10 changes: 5 additions & 5 deletions doc/lua_api.txt
Expand Up @@ -1774,11 +1774,11 @@ Helper functions
* false: return only file names.

### Logging
* `minetest.debug(line)`
* Always printed to `stderr` and logfile (`print()` is redirected here)
* `minetest.log(line)`
* `minetest.log(loglevel, line)`
* `loglevel` is one of `"error"`, `"action"`, `"info"`, `"verbose"`
* `minetest.debug(...)`
* Equivalent to `minetest.log(table.concat({...}, "\t"))`
* `minetest.log([level,] text)`
* `level` is one of `"none"`, `"error"`, `"warning"`, `"action"`,
`"info"`, or `"verbose"`. Default is `"none"`.

### Registration functions
Call these functions only at load time!
Expand Down
5 changes: 3 additions & 2 deletions minetest.conf.example
Expand Up @@ -612,8 +612,9 @@
#remote_media =

# Level of logging to be written to debug.txt:
# 0 = none, 1 = errors and debug, 2 = action, 3 = info, 4 = verbose.
#debug_log_level = 2
# <nothing> (no logging), none (messages with no level), error,
# warning, action, info, or verbose.
#debug_log_level = action

# Maximum number of blocks that can be queued for loading
#emergequeue_limit_total = 256
Expand Down
26 changes: 5 additions & 21 deletions src/client/clientlauncher.cpp
Expand Up @@ -89,7 +89,7 @@ bool ClientLauncher::run(GameParams &game_params, const Settings &cmd_args)
if (list_video_modes)
return print_video_modes();

if (!init_engine(game_params.log_level)) {
if (!init_engine()) {
errorstream << "Could not initialize game engine." << std::endl;
return false;
}
Expand Down Expand Up @@ -321,10 +321,10 @@ void ClientLauncher::init_args(GameParams &game_params, const Settings &cmd_args
|| cmd_args.getFlag("random-input");
}

bool ClientLauncher::init_engine(int log_level)
bool ClientLauncher::init_engine()
{
receiver = new MyEventReceiver();
create_engine_device(log_level);
create_engine_device();
return device != NULL;
}

Expand Down Expand Up @@ -455,7 +455,7 @@ bool ClientLauncher::launch_game(std::string &error_message,

if (game_params.game_spec.isValid() &&
game_params.game_spec.id != worldspec.gameid) {
errorstream << "WARNING: Overriding gamespec from \""
warningstream << "Overriding gamespec from \""
<< worldspec.gameid << "\" to \""
<< game_params.game_spec.id << "\"" << std::endl;
gamespec = game_params.game_spec;
Expand Down Expand Up @@ -500,20 +500,8 @@ void ClientLauncher::main_menu(MainMenuData *menudata)
smgr->clear(); /* leave scene manager in a clean state */
}

bool ClientLauncher::create_engine_device(int log_level)
bool ClientLauncher::create_engine_device()
{
static const irr::ELOG_LEVEL irr_log_level[5] = {
ELL_NONE,
ELL_ERROR,
ELL_WARNING,
ELL_INFORMATION,
#if (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 8)
ELL_INFORMATION
#else
ELL_DEBUG
#endif
};

// Resolution selection
bool fullscreen = g_settings->getBool("fullscreen");
u16 screenW = g_settings->getU16("screenW");
Expand Down Expand Up @@ -561,10 +549,6 @@ bool ClientLauncher::create_engine_device(int log_level)
device = createDeviceEx(params);

if (device) {
// Map our log level to irrlicht engine one.
ILogger* irr_logger = device->getLogger();
irr_logger->setLogLevel(irr_log_level[log_level]);

porting::initIrrlicht(device);
}

Expand Down
4 changes: 2 additions & 2 deletions src/client/clientlauncher.h
Expand Up @@ -90,13 +90,13 @@ class ClientLauncher

protected:
void init_args(GameParams &game_params, const Settings &cmd_args);
bool init_engine(int log_level);
bool init_engine();

bool launch_game(std::string &error_message, bool reconnect_requested,
GameParams &game_params, const Settings &cmd_args);

void main_menu(MainMenuData *menudata);
bool create_engine_device(int log_level);
bool create_engine_device();

void speed_tests();
bool print_video_modes();
Expand Down
20 changes: 10 additions & 10 deletions src/client/inputhandler.h
Expand Up @@ -17,8 +17,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#ifndef __INPUT_HANDLER_H__
#define __INPUT_HANDLER_H__
#ifndef INPUT_HANDLER_H
#define INPUT_HANDLER_H

#include "irrlichttypes_extrabloated.h"

Expand Down Expand Up @@ -86,16 +86,16 @@ class MyEventReceiver : public IEventReceiver
}
}
} else if (event.EventType == irr::EET_LOG_TEXT_EVENT) {
static const enum LogMessageLevel irr_loglev_conv[] = {
LMT_VERBOSE, // ELL_DEBUG
LMT_INFO, // ELL_INFORMATION
LMT_ACTION, // ELL_WARNING
LMT_ERROR, // ELL_ERROR
LMT_ERROR, // ELL_NONE
static const LogLevel irr_loglev_conv[] = {
LL_VERBOSE, // ELL_DEBUG
LL_INFO, // ELL_INFORMATION
LL_WARNING, // ELL_WARNING
LL_ERROR, // ELL_ERROR
LL_NONE, // ELL_NONE
};
assert(event.LogEvent.Level < ARRLEN(irr_loglev_conv));
log_printline(irr_loglev_conv[event.LogEvent.Level],
std::string("Irrlicht: ") + (const char *)event.LogEvent.Text);
g_logger.log(irr_loglev_conv[event.LogEvent.Level],
std::string("Irrlicht: ") + (const char*) event.LogEvent.Text);
return true;
}
/* always return false in order to continue processing events */
Expand Down

0 comments on commit 2139d7d

Please sign in to comment.