Browse Source

cleanup the debug message pipeline in C and Lua

should also remove the memory manager output when debug=0

fix #95 point 2.
master
Jaromil 3 years ago
parent
commit
c40e8a2e65
  1. 49
      src/jutils.c
  2. 1
      src/lua/init.lua
  3. 6
      src/lua/zencode_data.lua
  4. 37
      src/lua/zencode_debug.lua
  5. 1
      src/lua/zencode_when.lua
  6. 7
      src/lua/zenroom_common.lua
  7. 1
      src/stb_sprintf.h
  8. 1
      src/zen_config.c
  9. 4
      src/zenroom.c

49
src/jutils.c

@ -48,52 +48,44 @@ const char* ANSI_MAGENTA = "\x1b[35m";
const char* ANSI_CYAN = "\x1b[36m";
const char* ANSI_RESET = "\x1b[0m";
#define MAX_DEBUG 2
// #define MAX_STRING 1024
#define FUNC 2 /* se il debug level e' questo
ci sono le funzioni chiamate */
#define WARN 1 /* ... blkbblbl */
extern zenroom_t *Z;
char pfx[MAX_STRING];
extern int zen_write_err_va(const char *fmt, va_list va);
static int verbosity = 1;
int get_debug() { return(verbosity); }
void set_debug(int lev) {
lev = lev<0 ? 0 : lev;
lev = lev>MAX_DEBUG ? MAX_DEBUG : lev;
lev = lev>3 ? 3 : lev;
verbosity = lev;
}
static int color = 0;
void set_color(int on) { color = on; }
int get_debug() {
return(verbosity);
}
void notice(lua_State *L, const char *format, ...) {
(void)L;
va_list arg;
if(color)
z_snprintf(pfx, MAX_STRING-1, "%s[*]%s %s\n",ANSI_GREEN,ANSI_RESET,format);
else
z_snprintf(pfx, MAX_STRING-1, "[*] %s\n",format);
va_start(arg, format);
zen_write_err_va(pfx, arg);
va_end(arg);
if(!verbosity) return;
va_list arg;
if(color)
z_snprintf(pfx, MAX_STRING-1, "%s[*]%s %s\n",ANSI_GREEN,ANSI_RESET,format);
else
z_snprintf(pfx, MAX_STRING-1, "[*] %s\n",format);
va_start(arg, format);
zen_write_err_va(pfx, arg);
va_end(arg);
}
void func(void *L, const char *format, ...) {
(void)L;
if(verbosity>=FUNC) {
va_list arg;
z_snprintf(pfx, MAX_STRING-1, "[D] %s\n",format);
va_start(arg, format);
zen_write_err_va(pfx, arg);
va_end(arg);
}
if(verbosity<3) return;
va_list arg;
z_snprintf(pfx, MAX_STRING-1, "[D] %s\n",format);
va_start(arg, format);
zen_write_err_va(pfx, arg);
va_end(arg);
}
void error(lua_State *L, const char *format, ...) {
@ -113,6 +105,7 @@ void error(lua_State *L, const char *format, ...) {
void act(lua_State *L, const char *format, ...) {
(void)L;
if(!verbosity) return;
va_list arg;
z_snprintf(pfx, MAX_STRING-1, " . %s\n",format);
va_start(arg, format);
@ -121,7 +114,9 @@ void act(lua_State *L, const char *format, ...) {
}
void warning(lua_State *L, const char *format, ...) {
(void) L;
(void) L;
if(verbosity<2) return;
va_list arg;
if(color)
z_snprintf(pfx, MAX_STRING-1, "%s[W]%s %s\n",ANSI_YELLOW,ANSI_RESET,format);

1
src/lua/init.lua

@ -76,6 +76,7 @@ require('zencode_given')
require('zencode_when')
require('zencode_then')
require('zencode_eval')
if DEBUG > 0 then require('zencode_debug') end
-- scenario are loaded on-demand
-- scenarios can only implement "When ..." steps

6
src/lua/zencode_data.lua

@ -64,12 +64,6 @@ function Then(text, fn)
ZEN.then_steps[text] = fn
end
-- debug functions
Given("debug", function() ZEN.debug() end)
When("debug", function() ZEN.debug() end)
Then("debug", function() ZEN.debug() end)
-- the main security concern in this Zencode module is that no data
-- passes without validation from IN to ACK or from inline input.

37
src/lua/zencode_debug.lua

@ -0,0 +1,37 @@
-- This file is part of Zenroom (https://zenroom.dyne.org)
--
-- Copyright (C) 2018-2020 Dyne.org foundation
-- designed, written and maintained by Denis Roio <jaromil@dyne.org>
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU Affero General Public License as
-- published by the Free Software Foundation, either version 3 of the
-- License, or (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU Affero General Public License for more details.
--
-- You should have received a copy of the GNU Affero General Public License
-- along with this program. If not, see <https://www.gnu.org/licenses/>.
-- debug functions
local function debug_traceback()
I.print(ZEN_traceback)
end
local function debug_heap_dump()
I.print(HEAP)
end
local function debug_heap_schema()
-- print only keys without values
end
-- local function debug_obj_dump()
-- local function debug_obj_schema()
Given("debug", function() ZEN.debug() end)
When("debug", function() ZEN.debug() end)
Then("debug", function() ZEN.debug() end)

1
src/lua/zencode_when.lua

@ -166,7 +166,6 @@ When("I create the aggregation of ''", function(arr)
local count = isarray(A)
ZEN.assert( count > 0, "Object is not an array: "..arr)
if type(A[1]) == 'zenroom.ecp' then -- TODO: check all elements
xxx(3, "Computing sum of "..count.." ECP")
ACK.aggregation = ECP.generator()
for k,v in next,A,nil do
if not ACK.aggregation then ACK.aggregation = v

7
src/lua/zenroom_common.lua

@ -111,8 +111,11 @@ function get_format(what)
end
-- debugging facility
function xxx(n,s)
if CONF.verbosity or CONF.debug >= n then act(s) end
function xxx(s, n)
n = n or 3
if DEBUG >= n then
print("LUA "..s)
end
end
function content(var)

1
src/stb_sprintf.h

@ -1373,6 +1373,7 @@ static char *stbsp__clamp_callback(char *buf, void *user, int len)
static char * stbsp__count_clamp_callback( char * buf, void * user, int len )
{
(void)buf;
stbsp__context * c = (stbsp__context*)user;
c->count += len;

1
src/zen_config.c

@ -158,7 +158,6 @@ int zen_conf_parse(const char *configuration) {
warning(NULL,"undefined config variable");
break; }
if(lex.token == '=' && curconf != NIL) break; // OK
if(lex.token == '"' && curconf != NIL) break; // OK
error(NULL,"%s: invalid string in configuration: %c",__func__, lex.token);
free(lexbuf);
return 0;

4
src/zenroom.c

@ -179,6 +179,10 @@ zenroom_t *zen_init(const char *conf, char *keys, char *data) {
return NULL;
}
// expose the debug level
lua_pushinteger(Z->lua, Z->errorlevel);
lua_setglobal (Z->lua, "DEBUG");
lua_atpanic(Z->lua, &zen_lua_panic); // as done in lauxlib luaL_newstate
lua_pushcfunction(Z->lua, &zen_init_pmain); /* to call in protected mode */
// lua_pushinteger(Z->lua, 0); /* 1st argument */

Loading…
Cancel
Save