elua: start binding Eina
authorDaniel Kolesa <quaker66@gmail.com>
Fri, 4 Apr 2014 14:09:58 +0000 (15:09 +0100)
committerDaniel Kolesa <d.kolesa@samsung.com>
Tue, 10 Jun 2014 14:48:46 +0000 (15:48 +0100)
Added initial eina logging module, added library handling utility funcs in util.lua, added .gitignores

src/Makefile_Elua.am
src/bin/elua/.gitignore [new file with mode: 0644]
src/bin/elua/core/util.lua
src/bindings/luajit/eina/log.lua [new file with mode: 0644]
src/examples/elua/.gitignore [new file with mode: 0644]
src/examples/elua/elm_test.lua

index 4e3a140..251c88a 100644 (file)
@@ -3,8 +3,8 @@ if HAVE_ELUA
 bin_PROGRAMS += bin/elua/elua
 
 bin_elua_elua_SOURCES = \
-                bin/elua/cache.c \
-                bin/elua/main.c
+    bin/elua/cache.c \
+    bin/elua/main.c
 
 bin_elua_elua_CPPFLAGS = -I$(top_builddir)/src/lib/efl \
     -DELUA_DATA_DIR="\"$(datadir)/elua\"" @ELUA_CFLAGS@
@@ -12,11 +12,25 @@ bin_elua_elua_LDADD = @ELUA_LIBS@
 bin_elua_elua_DEPENDENCIES = @ELUA_INTERNAL_LIBS@
 
 eluamodulesdir = $(datadir)/elua/modules
-eluamodules_DATA = bin/elua/elm.lua
+
+eluamodules_DATA = \
+    bin/elua/modules/elm.lua
+
 EXTRA_DIST += $(eluamodules_DATA)
 
+eluaeinadir = $(eluamodulesdir)/eina
+
+eluaeina_DATA = \
+    bindings/luajit/eina/log.lua
+
+EXTRA_DIST += $(eluaeina_DATA)
+
 eluacoredir = $(datadir)/elua/core
-eluacore_DATA = bin/elua/module.lua
+
+eluacore_DATA = \
+    bin/elua/core/module.lua \
+    bin/elua/core/util.lua
+
 EXTRA_DIST += $(eluacore_DATA)
 
 endif
diff --git a/src/bin/elua/.gitignore b/src/bin/elua/.gitignore
new file mode 100644 (file)
index 0000000..e5f2dce
--- /dev/null
@@ -0,0 +1,2 @@
+elua
+*.luac
index 1ae0796..93c2152 100644 (file)
@@ -1,5 +1,7 @@
 -- elua core utilities used in other modules
 
+local ffi = require("ffi")
+
 local M = {}
 
 M.Object = {
@@ -39,4 +41,30 @@ M.Object = {
     end
 }
 
+local loaded_libs = {}
+local loaded_libc = {}
+
+-- makes sure we only keep one handle for each lib
+-- reference counted
+M.lib_load = function(libname)
+    local  lib = loaded_libs[libname]
+    if not lib then
+        lib = ffi.load(libname)
+        loaded_libs[libname] = lib
+        loaded_libc[libname] = 0
+    end
+    loaded_libc[libname] = loaded_libc[libname] + 1
+    return lib
+end
+
+M.lib_unload = function(libname)
+    local  cnt = loaded_libc[libname]
+    if not cnt then return end
+    if cnt == 1 then
+        loaded_libs[libname], loaded_libc[libname] = nil, nil
+    else
+        loaded_libc[libname] = cnt - 1
+    end
+end
+
 return M
\ No newline at end of file
diff --git a/src/bindings/luajit/eina/log.lua b/src/bindings/luajit/eina/log.lua
new file mode 100644 (file)
index 0000000..7e8fa07
--- /dev/null
@@ -0,0 +1,154 @@
+-- EFL LuaJIT bindings: Eina (log module)
+-- For use with Elua
+
+local ffi = require("ffi")
+local C = ffi.C
+
+ffi.cdef [[
+    typedef enum _Eina_Log_Level {
+        EINA_LOG_LEVEL_CRITICAL,
+        EINA_LOG_LEVEL_ERR,
+        EINA_LOG_LEVEL_WARN,
+        EINA_LOG_LEVEL_INFO,
+        EINA_LOG_LEVEL_DBG,
+        EINA_LOG_LEVELS,
+        EINA_LOG_LEVEL_UNKNOWN = (-2147483647 - 1)
+    } Eina_Log_Level;
+
+    extern int EINA_LOG_DOMAIN_GLOBAL;
+
+    int  eina_log_domain_register  (const char *name, const char *color);
+    void eina_log_domain_unregister(int domain);
+
+    int  eina_log_domain_registered_level_get(int domain);
+    void eina_log_domain_registered_level_set(int domain, int level);
+
+    void eina_log_print(int            domain,
+                        Eina_Log_Level level,
+                        const char    *file,
+                        const char    *function,
+                        int            line,
+                        const char    *fmt,
+                        ...);
+]]
+
+local cutil = require("cutil")
+local util  = require("util")
+
+local M = {}
+
+local eina
+local default_domain
+
+local init = function()
+    eina = util.lib_load("eina")
+    default_domain = eina.EINA_LOG_DOMAIN_GLOBAL
+end
+
+local shutdown = function()
+    util.lib_unload("eina")
+    default_domain = nil
+end
+
+cutil.init_module(init, shutdown)
+
+M.level = {
+    CRITICAL = C.EINA_LOG_LEVEL_CRITICAL,
+    ERR      = C.EINA_LOG_LEVEL_ERR,
+    WARN     = C.EINA_LOG_LEVEL_WARN,
+    INFO     = C.EINA_LOG_LEVEL_INFO,
+    DBG      = C.EINA_LOG_LEVEL_DBG,
+    UNKNOWN  = C.EINA_LOG_LEVEL_UNKNOWN,
+    levels   = C.EINA_LOG_LEVELS
+}
+
+M.color = {
+    LIGHTRED  = "\x1B[31;1m",
+    RED       = "\x1B[31m",
+    LIGHTBLUE = "\x1B[34;1m",
+    BLUE      = "\x1B[34m",
+    GREEN     = "\x1B[32;1m",
+    YELLOW    = "\x1B[33;1m",
+    ORANGE    = "\x1B[0;33m",
+    WHITE     = "\x1B[37;1m",
+    LIGHTCYAN = "\x1B[36;1m",
+    CYAN      = "\x1B[36m",
+    RESET     = "\x1B[0m",
+    HIGH      = "\x1B[1m"
+}
+
+local log_full = function(dom, level, file, func, line, msg)
+    dom = dom:get_domain()
+    if dom == -1 then return end
+    eina.eina_log_print(dom, level, file, func, line, msg)
+end
+M.log_full = log_full
+
+local getinfo = debug.getinfo
+
+local log = function(dom, level, msg, loff)
+    local info = getinfo(2 + (loff or 0), "nlSf")
+    log_full(dom, level, info.source,
+        info.name or "<" .. tostring(info.func) .. ">", info.currentline, msg)
+end
+M.log = log
+
+M.Domain_Base = util.Object:clone {
+    set_level = function(self, level)
+        local dom = self:get_domain()
+        if dom == -1 then return end
+        eina.eina_log_domain_registered_level_set(dom, level)
+    end,
+
+    get_level = function(self)
+        local dom = self:get_domain()
+        if dom == -1 then return -1 end
+        return eina.eina_log_domain_registered_level_get(dom)
+    end,
+
+    get_domain = function(self)
+        return -1
+    end,
+
+    log = function(self, level, msg, loff)
+        log(self, level, msg, (loff or 0) + 1)
+    end
+}
+
+M.Domain_Global = M.Domain_Base:clone {
+    get_domain = function(self)
+        return eina.EINA_LOG_DOMAIN_GLOBAL
+    end
+}
+
+M.global_domain = M.Domain_Global
+
+M.Domain_Default = M.Domain_Base:clone {
+    get_domain = function(self)
+        return default_domain
+    end
+}
+
+M.default_domain = M.Domain_Default
+
+M.Domain = M.Domain_Base:clone {
+    __ctor = function(self, name, color)
+        self.__domain = eina.eina_log_domain_register(name, color)
+        self.__gc = newproxy(true)
+        getmetatable(self.__gc).__gc = function()
+            self:unregister()
+        end
+    end,
+
+    unregister = function(self)
+        if self.__domain == -1 then return end
+        eina.eina_log_domain_unregister(self.__domain)
+        self.__domain = -1
+    end,
+
+    get_domain = function(self)
+        return self.__domain
+    end
+}
+
+return M
\ No newline at end of file
diff --git a/src/examples/elua/.gitignore b/src/examples/elua/.gitignore
new file mode 100644 (file)
index 0000000..31b688b
--- /dev/null
@@ -0,0 +1 @@
+*.luac
index 77076d4..95f362b 100644 (file)
@@ -1,5 +1,12 @@
+local log = require("eina.log")
+
+local dom = log.Domain("elm_test", log.color.LIGHTRED)
+
 local win = elm.Window("test", "Hello World")
 
+dom:log(log.level.INFO, "created window")
+dom:log(log.level.ERR, "error test!")
+
 win:smart_callback_add("delete,request", function()
     elm.exit()
 end)
@@ -35,4 +42,7 @@ end)
 btn:show()
 
 win:resize(360, 360)
-win:show()
\ No newline at end of file
+win:show()
+
+dom:log(log.level.INFO, "done initing")
+dom:unregister()
\ No newline at end of file