diff mbox series

Lua: make logging macro wrappers more printf()-like

Message ID 20180201145522.4621-1-christian.storm@siemens.com
State Accepted
Headers show
Series Lua: make logging macro wrappers more printf()-like | expand

Commit Message

Storm, Christian Feb. 1, 2018, 2:55 p.m. UTC
Currently, the Lua wrapper methods for calling the macros
wrapping swupdate_notify(), i.e., INFO, ERROR, ..., are limited
to accept a single parameter as message being the string handed
over to swupdate_notify().

In order to have a printf()-like behavior in the Lua realm as
it's available in the C realm, the parameter to those macros has
to be processed by string.format() beforehand, i.e., a formatted
error message from the Lua realm is issued like

    swupdate.error(string.format("message was: %s", msg))

Hence, modify the logging macro Lua wrapper methods so that
string.format() is implicitly called in the C realm prior to
handing over the processed string to the respective logging
macro. This way, formatted messages from the Lua realm can be
issued in a more printf()-like style like

    swupdate.error("message was: %s", msg)

While at it, introduce Lua wrapper methods for the missing WARN
and DEBUG macros.

This change is backwards-compatible and doesn't require any
modification to existing code. The only impact is more
convenience and one call to string.format() per log message
issued from the Lua realm, even if not required.

Signed-off-by: Christian Storm <christian.storm@siemens.com>
---
 corelib/lua_interface.c | 65 +++++++++++++++++++++++++++++++++++--------------
 1 file changed, 47 insertions(+), 18 deletions(-)

Comments

Stefano Babic Feb. 1, 2018, 9:25 p.m. UTC | #1
On 01/02/2018 15:55, Christian Storm wrote:
> Currently, the Lua wrapper methods for calling the macros
> wrapping swupdate_notify(), i.e., INFO, ERROR, ..., are limited
> to accept a single parameter as message being the string handed
> over to swupdate_notify().
> 
> In order to have a printf()-like behavior in the Lua realm as
> it's available in the C realm, the parameter to those macros has
> to be processed by string.format() beforehand, i.e., a formatted
> error message from the Lua realm is issued like
> 
>     swupdate.error(string.format("message was: %s", msg))
> 
> Hence, modify the logging macro Lua wrapper methods so that
> string.format() is implicitly called in the C realm prior to
> handing over the processed string to the respective logging
> macro. This way, formatted messages from the Lua realm can be
> issued in a more printf()-like style like
> 
>     swupdate.error("message was: %s", msg)

Agree, this is a much more elegant solution, thanks !

> 
> While at it, introduce Lua wrapper methods for the missing WARN
> and DEBUG macros.
> 
> This change is backwards-compatible and doesn't require any
> modification to existing code. The only impact is more
> convenience and one call to string.format() per log message
> issued from the Lua realm, even if not required.
> 
> Signed-off-by: Christian Storm <christian.storm@siemens.com>
> ---
>  corelib/lua_interface.c | 65 +++++++++++++++++++++++++++++++++++--------------
>  1 file changed, 47 insertions(+), 18 deletions(-)
> 
> diff --git a/corelib/lua_interface.c b/corelib/lua_interface.c
> index ed0849a..eb11978 100644
> --- a/corelib/lua_interface.c
> +++ b/corelib/lua_interface.c
> @@ -621,34 +621,61 @@ static int l_notify (lua_State *L) {
>  	return 0;
>  }
>  
> -static int l_trace(lua_State *L) {
> -	const char *msg = luaL_checkstring (L, 1);
> -
> -	if (msg && strlen(msg))
> -		TRACE("%s", msg);
> -
> +static int notify_helper(lua_State *L, LOGLEVEL level)
> +{
> +	luaL_checktype(L, 1, LUA_TSTRING);
> +	lua_getglobal(L, "string");
> +	lua_pushliteral(L, "format");
> +	lua_gettable(L, -2);
> +	lua_insert(L, 1);
>  	lua_pop(L, 1);
> +	if (lua_pcall(L, lua_gettop(L) - 1, 1, 0) != LUA_OK) {
> +		ERROR("error while notify call: %s", lua_tostring(L, -1));
> +	} else {
> +		switch (level) {
> +		case ERRORLEVEL:
> +			ERROR("%s", lua_tostring(L, -1));
> +			break;
> +		case WARNLEVEL:
> +			WARN("%s", lua_tostring(L, -1));
> +			break;
> +		case INFOLEVEL:
> +			INFO("%s", lua_tostring(L, -1));
> +			break;
> +		case DEBUGLEVEL:
> +			DEBUG("%s", lua_tostring(L, -1));
> +			break;
> +		case TRACELEVEL:
> +			TRACE("%s", lua_tostring(L, -1));
> +			break;
> +		case OFF:
> +			break;
> +		}
> +	}
> + 	lua_pop(L, 1);
>  	return 0;
>  }
>  
> -static int l_error(lua_State *L) {
> -	const char *msg = luaL_checkstring (L, 1);
> -
> -	if (msg && strlen(msg))
> -		ERROR("%s", msg);
> +static int l_trace(lua_State *L) {
> +	return notify_helper(L, TRACELEVEL);
> +}
>  
> -	lua_pop(L, 1);
> -	return 0;
> +static int l_error(lua_State *L) {
> +	return notify_helper(L, ERRORLEVEL);
>  }
>  
>  static int l_info(lua_State *L) {
> -	const char *msg = luaL_checkstring (L, 1);
> +	return notify_helper(L, INFOLEVEL);
> +}
>  
> -	if (msg && strlen(msg))
> -		INFO("%s", msg);
> +static int l_warn(lua_State *L)
> +{
> +	return notify_helper(L, WARNLEVEL);
> +}
>  
> -	lua_pop(L, 1);
> -	return 0;
> +static int l_debug(lua_State *L)
> +{
> +	return notify_helper(L, DEBUGLEVEL);
>  }
>  
>  static int l_mount(lua_State *L) {
> @@ -759,6 +786,8 @@ static const luaL_Reg l_swupdate[] = {
>          { "error", l_error },
>          { "trace", l_trace },
>          { "info", l_info },
> +        { "warn", l_warn },
> +        { "debug", l_debug },
>          { "mount", l_mount },
>          { "umount", l_umount },
>          { NULL, NULL }
> 

Acked-by: Stefano Babic <sbabic@denx.de>

Best regards,
Stefano Babic
Stefano Babic Feb. 2, 2018, 12:16 p.m. UTC | #2
On 01/02/2018 15:55, Christian Storm wrote:
> Currently, the Lua wrapper methods for calling the macros
> wrapping swupdate_notify(), i.e., INFO, ERROR, ..., are limited
> to accept a single parameter as message being the string handed
> over to swupdate_notify().
> 
> In order to have a printf()-like behavior in the Lua realm as
> it's available in the C realm, the parameter to those macros has
> to be processed by string.format() beforehand, i.e., a formatted
> error message from the Lua realm is issued like
> 
>     swupdate.error(string.format("message was: %s", msg))
> 
> Hence, modify the logging macro Lua wrapper methods so that
> string.format() is implicitly called in the C realm prior to
> handing over the processed string to the respective logging
> macro. This way, formatted messages from the Lua realm can be
> issued in a more printf()-like style like
> 
>     swupdate.error("message was: %s", msg)
> 
> While at it, introduce Lua wrapper methods for the missing WARN
> and DEBUG macros.
> 
> This change is backwards-compatible and doesn't require any
> modification to existing code. The only impact is more
> convenience and one call to string.format() per log message
> issued from the Lua realm, even if not required.
> 
> Signed-off-by: Christian Storm <christian.storm@siemens.com>
> ---
>  corelib/lua_interface.c | 65 +++++++++++++++++++++++++++++++++++--------------
>  1 file changed, 47 insertions(+), 18 deletions(-)
> 
> diff --git a/corelib/lua_interface.c b/corelib/lua_interface.c
> index ed0849a..eb11978 100644
> --- a/corelib/lua_interface.c
> +++ b/corelib/lua_interface.c
> @@ -621,34 +621,61 @@ static int l_notify (lua_State *L) {
>  	return 0;
>  }
>  
> -static int l_trace(lua_State *L) {
> -	const char *msg = luaL_checkstring (L, 1);
> -
> -	if (msg && strlen(msg))
> -		TRACE("%s", msg);
> -
> +static int notify_helper(lua_State *L, LOGLEVEL level)
> +{
> +	luaL_checktype(L, 1, LUA_TSTRING);
> +	lua_getglobal(L, "string");
> +	lua_pushliteral(L, "format");
> +	lua_gettable(L, -2);
> +	lua_insert(L, 1);
>  	lua_pop(L, 1);
> +	if (lua_pcall(L, lua_gettop(L) - 1, 1, 0) != LUA_OK) {
> +		ERROR("error while notify call: %s", lua_tostring(L, -1));
> +	} else {
> +		switch (level) {
> +		case ERRORLEVEL:
> +			ERROR("%s", lua_tostring(L, -1));
> +			break;
> +		case WARNLEVEL:
> +			WARN("%s", lua_tostring(L, -1));
> +			break;
> +		case INFOLEVEL:
> +			INFO("%s", lua_tostring(L, -1));
> +			break;
> +		case DEBUGLEVEL:
> +			DEBUG("%s", lua_tostring(L, -1));
> +			break;
> +		case TRACELEVEL:
> +			TRACE("%s", lua_tostring(L, -1));
> +			break;
> +		case OFF:
> +			break;
> +		}
> +	}
> + 	lua_pop(L, 1);
>  	return 0;
>  }
>  
> -static int l_error(lua_State *L) {
> -	const char *msg = luaL_checkstring (L, 1);
> -
> -	if (msg && strlen(msg))
> -		ERROR("%s", msg);
> +static int l_trace(lua_State *L) {
> +	return notify_helper(L, TRACELEVEL);
> +}
>  
> -	lua_pop(L, 1);
> -	return 0;
> +static int l_error(lua_State *L) {
> +	return notify_helper(L, ERRORLEVEL);
>  }
>  
>  static int l_info(lua_State *L) {
> -	const char *msg = luaL_checkstring (L, 1);
> +	return notify_helper(L, INFOLEVEL);
> +}
>  
> -	if (msg && strlen(msg))
> -		INFO("%s", msg);
> +static int l_warn(lua_State *L)
> +{
> +	return notify_helper(L, WARNLEVEL);
> +}
>  
> -	lua_pop(L, 1);
> -	return 0;
> +static int l_debug(lua_State *L)
> +{
> +	return notify_helper(L, DEBUGLEVEL);
>  }
>  
>  static int l_mount(lua_State *L) {
> @@ -759,6 +786,8 @@ static const luaL_Reg l_swupdate[] = {
>          { "error", l_error },
>          { "trace", l_trace },
>          { "info", l_info },
> +        { "warn", l_warn },
> +        { "debug", l_debug },
>          { "mount", l_mount },
>          { "umount", l_umount },
>          { NULL, NULL }
> 

Applied to -master, thanks !

Best regards,
Stefano Babic
diff mbox series

Patch

diff --git a/corelib/lua_interface.c b/corelib/lua_interface.c
index ed0849a..eb11978 100644
--- a/corelib/lua_interface.c
+++ b/corelib/lua_interface.c
@@ -621,34 +621,61 @@  static int l_notify (lua_State *L) {
 	return 0;
 }
 
-static int l_trace(lua_State *L) {
-	const char *msg = luaL_checkstring (L, 1);
-
-	if (msg && strlen(msg))
-		TRACE("%s", msg);
-
+static int notify_helper(lua_State *L, LOGLEVEL level)
+{
+	luaL_checktype(L, 1, LUA_TSTRING);
+	lua_getglobal(L, "string");
+	lua_pushliteral(L, "format");
+	lua_gettable(L, -2);
+	lua_insert(L, 1);
 	lua_pop(L, 1);
+	if (lua_pcall(L, lua_gettop(L) - 1, 1, 0) != LUA_OK) {
+		ERROR("error while notify call: %s", lua_tostring(L, -1));
+	} else {
+		switch (level) {
+		case ERRORLEVEL:
+			ERROR("%s", lua_tostring(L, -1));
+			break;
+		case WARNLEVEL:
+			WARN("%s", lua_tostring(L, -1));
+			break;
+		case INFOLEVEL:
+			INFO("%s", lua_tostring(L, -1));
+			break;
+		case DEBUGLEVEL:
+			DEBUG("%s", lua_tostring(L, -1));
+			break;
+		case TRACELEVEL:
+			TRACE("%s", lua_tostring(L, -1));
+			break;
+		case OFF:
+			break;
+		}
+	}
+ 	lua_pop(L, 1);
 	return 0;
 }
 
-static int l_error(lua_State *L) {
-	const char *msg = luaL_checkstring (L, 1);
-
-	if (msg && strlen(msg))
-		ERROR("%s", msg);
+static int l_trace(lua_State *L) {
+	return notify_helper(L, TRACELEVEL);
+}
 
-	lua_pop(L, 1);
-	return 0;
+static int l_error(lua_State *L) {
+	return notify_helper(L, ERRORLEVEL);
 }
 
 static int l_info(lua_State *L) {
-	const char *msg = luaL_checkstring (L, 1);
+	return notify_helper(L, INFOLEVEL);
+}
 
-	if (msg && strlen(msg))
-		INFO("%s", msg);
+static int l_warn(lua_State *L)
+{
+	return notify_helper(L, WARNLEVEL);
+}
 
-	lua_pop(L, 1);
-	return 0;
+static int l_debug(lua_State *L)
+{
+	return notify_helper(L, DEBUGLEVEL);
 }
 
 static int l_mount(lua_State *L) {
@@ -759,6 +786,8 @@  static const luaL_Reg l_swupdate[] = {
         { "error", l_error },
         { "trace", l_trace },
         { "info", l_info },
+        { "warn", l_warn },
+        { "debug", l_debug },
         { "mount", l_mount },
         { "umount", l_umount },
         { NULL, NULL }