From f55800ff061ac6f28135a9db30fb058c15b5674b Mon Sep 17 00:00:00 2001 From: barbieri Date: Sat, 3 Sep 2011 05:31:25 +0000 Subject: [PATCH] eina_prefix: use log infrastructure. use eina_log instead of weirdo printfs... multiple levels, greatness everywhere... helped me debug emotion's warning. It is always a pain to try to use library log and notice they are not using their own standards :-/ export EINA_LOG_LEVELS=eina_prefix:4 git-svn-id: svn+ssh://svn.enlightenment.org/var/svn/e/trunk/eina@63108 7cbeb6ba-43b4-40fd-8cce-4c39aea84d33 --- src/lib/eina_main.c | 4 +- src/lib/eina_prefix.c | 140 +++++++++++++++++++++++++++++++++----------------- 2 files changed, 97 insertions(+), 47 deletions(-) diff --git a/src/lib/eina_main.c b/src/lib/eina_main.c index 5e28885..f7d2559 100644 --- a/src/lib/eina_main.c +++ b/src/lib/eina_main.c @@ -129,6 +129,7 @@ EAPI Eina_Inlist *_eina_tracking = NULL; S(quadtree); S(simple_xml); S(file); + S(prefix); #undef S struct eina_desc_setup @@ -162,7 +163,8 @@ static const struct eina_desc_setup _eina_desc_setup[] = { S(ustrbuf), S(quadtree), S(simple_xml), - S(file) + S(file), + S(prefix) #undef S }; static const size_t _eina_desc_setup_len = sizeof(_eina_desc_setup) / diff --git a/src/lib/eina_prefix.c b/src/lib/eina_prefix.c index 84ba5bb..b609b22 100644 --- a/src/lib/eina_prefix.c +++ b/src/lib/eina_prefix.c @@ -105,7 +105,32 @@ struct _Eina_Prefix #define STRDUP_REP(x, y) do { if (x) free(x); x = strdup(y); } while (0) #define IF_FREE_NULL(p) do { if (p) { free(p); p = NULL; } } while (0) -#define DBG(fmt, args...) do { if (getenv("EINA_PREFIX_DEBUG")) fprintf(stderr, fmt, ##args); } while (0) + +#ifndef EINA_LOG_COLOR_DEFAULT +#define EINA_LOG_COLOR_DEFAULT EINA_COLOR_CYAN +#endif + +#ifdef ERR +#undef ERR +#endif +#define ERR(...) EINA_LOG_DOM_ERR(_eina_prefix_log_dom, __VA_ARGS__) + +#ifdef WRN +#undef WRN +#endif +#define WRN(...) EINA_LOG_DOM_WARN(_eina_prefix_log_dom, __VA_ARGS__) + +#ifdef INF +#undef INF +#endif +#define INF(...) EINA_LOG_DOM_INFO(_eina_prefix_log_dom, __VA_ARGS__) + +#ifdef DBG +#undef DBG +#endif +#define DBG(...) EINA_LOG_DOM_DBG(_eina_prefix_log_dom, __VA_ARGS__) + +static int _eina_prefix_log_dom = -1; static int _fallback(Eina_Prefix *pfx, const char *pkg_bin, const char *pkg_lib, @@ -151,10 +176,10 @@ _try_proc(Eina_Prefix *pfx, void *symbol) FILE *f; char buf[4096]; - DBG("Try /proc/self/maps\n"); + DBG("Try /proc/self/maps"); f = fopen("/proc/self/maps", "rb"); if (!f) return 0; - DBG("Exists /proc/self/maps\n"); + DBG("Exists /proc/self/maps"); while (fgets(buf, sizeof(buf), f)) { int len; @@ -173,18 +198,18 @@ _try_proc(Eina_Prefix *pfx, void *symbol) { if (((void *)ptr1 <= symbol) && (symbol < (void *)ptr2)) { - DBG("Found in /proc/self/maps: %s\n", buf); + DBG("Found in /proc/self/maps: %s", buf); p = strchr(buf, '/'); if (p) { - DBG("Found in /proc/self/maps: found last /\n"); + DBG("Found in /proc/self/maps: found last /"); if (len > 10) { if (!strcmp(buf + len - 10, " (deleted)")) buf[len - 10] = 0; } STRDUP_REP(pfx->exe_path, p); - DBG("Found in /proc/self/maps: guess exe path is %s\n", pfx->exe_path); + INF("Found in /proc/self/maps: guess exe path is %s", pfx->exe_path); fclose(f); return 1; } @@ -194,7 +219,7 @@ _try_proc(Eina_Prefix *pfx, void *symbol) } } fclose(f); - DBG("Failed in /proc/self/maps\n"); + WRN("Failed in /proc/self/maps"); return 0; } #endif @@ -206,7 +231,7 @@ _try_argv(Eina_Prefix *pfx, const char *argv0) int len, lenexe; char buf[PATH_MAX], buf2[PATH_MAX], buf3[PATH_MAX]; - DBG("Try argv0 = %s\n", argv0); + DBG("Try argv0 = %s", argv0); /* 1. is argv0 abs path? */ #ifdef _WIN32 if (argv0[0] && (argv0[1] == ':')) @@ -214,41 +239,41 @@ _try_argv(Eina_Prefix *pfx, const char *argv0) if (argv0[0] == DSEP_C) #endif { - DBG("Match arvg0 is full path: %s\n", argv0); + DBG("Match arvg0 is full path: %s", argv0); STRDUP_REP(pfx->exe_path, argv0); if (access(pfx->exe_path, X_OK) == 0) { - DBG("Executable argv0 = %s\n", argv0); + INF("Executable argv0 = %s", argv0); return 1; } IF_FREE_NULL(pfx->exe_path); - DBG("Non existent argv0: %s\n", argv0); + DBG("Non existent argv0: %s", argv0); return 0; } /* 2. relative path */ if (strchr(argv0, DSEP_C)) { - DBG("Relative path argv0: %s\n", argv0); + DBG("Relative path argv0: %s", argv0); if (getcwd(buf3, sizeof(buf3))) { snprintf(buf2, sizeof(buf2), "%s" DSEP_S "%s", buf3, argv0); - DBG("Relative to CWD: %s\n", buf2); + DBG("Relative to CWD: %s", buf2); if (realpath(buf2, buf)) { - DBG("Realpath is: %s\n", buf); + DBG("Realpath is: %s", buf); STRDUP_REP(pfx->exe_path, buf); if (access(pfx->exe_path, X_OK) == 0) { - DBG("Path %s is executable\n", pfx->exe_path); + INF("Path %s is executable", pfx->exe_path); return 1; } - DBG("Fail check for executable: %s\n", pfx->exe_path); + DBG("Fail check for executable: %s", pfx->exe_path); IF_FREE_NULL(pfx->exe_path); } } } /* 3. argv0 no path - look in PATH */ - DBG("Look for argv0=%s in $PATH\n", argv0); + DBG("Look for argv0=%s in $PATH", argv0); path = getenv("PATH"); if (!path) return 0; p = path; @@ -263,14 +288,14 @@ _try_argv(Eina_Prefix *pfx, const char *argv0) strncpy(s, cp, len); s[len] = DSEP_C; strcpy(s + len + 1, argv0); - DBG("Try path: %s\n", s); + DBG("Try path: %s", s); if (realpath(s, buf)) { - DBG("Realpath is: %s\n", buf); + DBG("Realpath is: %s", buf); if (access(buf, X_OK) == 0) { STRDUP_REP(pfx->exe_path, buf); - DBG("Path %s is executable\n", pfx->exe_path); + INF("Path %s is executable", pfx->exe_path); free(s); return 1; } @@ -289,17 +314,17 @@ _get_env_var(char **var, const char *env, const char *prefix, const char *dir) char buf[PATH_MAX]; const char *s = getenv(env); - DBG("Try env var %s\n", env); + DBG("Try env var %s", env); if (s) { - DBG("Have env %s = %s\n", env, s); + INF("Have env %s = %s", env, s); STRDUP_REP(*var, s); return 1; } else if (prefix) { snprintf(buf, sizeof(buf), "%s" DSEP_S "%s", prefix, dir); - DBG("Have prefix %s = %s\n", prefix, buf); + INF("Have prefix %s = %s", prefix, buf); STRDUP_REP(*var, buf); return 1; } @@ -365,7 +390,8 @@ eina_prefix_new(const char *argv0, void *symbol, const char *envprefix, const char *datadir = "share"; const char *localedir = "share"; - DBG("EINA PREFIX: argv0=%s, symbol=%p, magicsharefile=%s, envprefix=%s\n", argv0, symbol, magicsharefile, envprefix); + DBG("EINA PREFIX: argv0=%s, symbol=%p, magicsharefile=%s, envprefix=%s", + argv0, symbol, magicsharefile, envprefix); pfx = calloc(1, sizeof(Eina_Prefix)); if (!pfx) return NULL; @@ -465,10 +491,10 @@ eina_prefix_new(const char *argv0, void *symbol, const char *envprefix, libdir = pkg_lib_p; datadir = pkg_data_p; localedir = pkg_locale_p; - DBG("Prefix relative bindir = %s\n", bindir); - DBG("Prefix relative libdir = %s\n", libdir); - DBG("Prefix relative datadir = %s\n", datadir); - DBG("Prefix relative localedir = %s\n", localedir); + DBG("Prefix relative bindir = %s", bindir); + DBG("Prefix relative libdir = %s", libdir); + DBG("Prefix relative datadir = %s", datadir); + DBG("Prefix relative localedir = %s", localedir); } /* 3. some galoot thought it awesome not to give us a common prefix at compile time * so fall back to the compile time directories. we are no longer relocatable */ @@ -479,7 +505,7 @@ eina_prefix_new(const char *argv0, void *symbol, const char *envprefix, STRDUP_REP(pfx->prefix_path_data, pkg_data); STRDUP_REP(pfx->prefix_path_locale, pkg_locale); pfx->no_common_prefix = 1; - DBG("Can't work out a common prefix - compiled in fallback\n"); + DBG("Can't work out a common prefix - compiled in fallback"); } /* if user provides env vars - then use that or also more specific sub @@ -492,24 +518,24 @@ eina_prefix_new(const char *argv0, void *symbol, const char *envprefix, } #ifdef HAVE_DLADDR - DBG("Try dladdr on %p\n", symbol); + DBG("Try dladdr on %p", symbol); if (symbol) { Dl_info info_dl; if (dladdr(symbol, &info_dl)) { - DBG("Dlinfo worked\n"); + DBG("Dlinfo worked"); if (info_dl.dli_fname) { - DBG("Dlinfo dli_fname = %s\n", info_dl.dli_fname); + DBG("Dlinfo dli_fname = %s", info_dl.dli_fname); # ifdef _WIN32 if (info_dl.dli_fname[0] && (info_dl.dli_fname[1] == ':')) # else if (info_dl.dli_fname[0] == DSEP_C) # endif { - DBG("Dlsym gave full path.\n"); + INF("Dlsym gave full path = %s", info_dl.dli_fname); STRDUP_REP(pfx->exe_path, info_dl.dli_fname); } } @@ -535,7 +561,7 @@ eina_prefix_new(const char *argv0, void *symbol, const char *envprefix, } if (!pfx->exe_path) { - DBG("Fallback - nothing found\n"); + WRN("Fallback - nothing found"); _fallback(pfx, pkg_bin, pkg_lib, pkg_data, pkg_locale, envprefix); return pfx; } @@ -550,7 +576,7 @@ eina_prefix_new(const char *argv0, void *symbol, const char *envprefix, * data_dir = /blah/whatever/share/enlightenment * lib_dir = /blah/whatever/lib */ - DBG("From exe %s figure out the rest\n", pfx->exe_path); + DBG("From exe %s figure out the rest", pfx->exe_path); p = strrchr(pfx->exe_path, DSEP_C); if (p) { @@ -565,38 +591,38 @@ eina_prefix_new(const char *argv0, void *symbol, const char *envprefix, strncpy(pfx->prefix_path, pfx->exe_path, p - pfx->exe_path); pfx->prefix_path[p - pfx->exe_path] = 0; - DBG("Have prefix = %s\n", pfx->prefix_path); + DBG("Have prefix = %s", pfx->prefix_path); /* bin */ snprintf(buf, sizeof(buf), "%s" DSEP_S "%s", pfx->prefix_path, bindir); STRDUP_REP(pfx->prefix_path_bin, buf); - DBG("Have bin = %s\n", pfx->prefix_path_bin); + DBG("Have bin = %s", pfx->prefix_path_bin); /* lib */ snprintf(buf, sizeof(buf), "%s" DSEP_S "%s", pfx->prefix_path, libdir); STRDUP_REP(pfx->prefix_path_lib, buf); - DBG("Have lib = %s\n", pfx->prefix_path_lib); + DBG("Have lib = %s", pfx->prefix_path_lib); /* locale */ snprintf(buf, sizeof(buf), "%s" DSEP_S "%s", pfx->prefix_path, localedir); STRDUP_REP(pfx->prefix_path_locale, buf); - DBG("Have locale = %s\n", pfx->prefix_path_locale); + DBG("Have locale = %s", pfx->prefix_path_locale); /* check if magic file is there - then our guess is right */ if (magic) { - DBG("Magic = %s\n", magic); + DBG("Magic = %s", magic); snprintf(buf, sizeof(buf), "%s" DSEP_S "%s" DSEP_S "%s", pfx->prefix_path, datadir, magic); - DBG("Checck in %s\n", buf); + DBG("Check in %s", buf); } if ((!magic) || (stat(buf, &st) == 0)) { if (buf[0]) - DBG("Magic path %s stat passed\n", buf); + DBG("Magic path %s stat passed", buf); else - DBG("No magic file\n"); + DBG("No magic file"); snprintf(buf, sizeof(buf), "%s" DSEP_S "%s", pfx->prefix_path, datadir); STRDUP_REP(pfx->prefix_path_data, buf); @@ -604,14 +630,14 @@ eina_prefix_new(const char *argv0, void *symbol, const char *envprefix, /* magic file not there. time to start hunting! */ else { - DBG("Magic faile\n"); + WRN("Magic failed"); _fallback(pfx, pkg_bin, pkg_lib, pkg_data, pkg_locale, envprefix); } } else { - DBG("No Prefix path (alloc fail)\n"); + WRN("No Prefix path (alloc fail)"); _fallback(pfx, pkg_bin, pkg_lib, pkg_data, pkg_locale, envprefix); } @@ -620,7 +646,7 @@ eina_prefix_new(const char *argv0, void *symbol, const char *envprefix, p--; } } - DBG("Final fallback\n"); + WRN("Final fallback"); _fallback(pfx, pkg_bin, pkg_lib, pkg_data, pkg_locale, envprefix); return pfx; } @@ -673,3 +699,25 @@ eina_prefix_locale_get(Eina_Prefix *pfx) if (!pfx) return ""; return pfx->prefix_path_locale; } + +Eina_Bool +eina_prefix_init(void) +{ + _eina_prefix_log_dom = eina_log_domain_register("eina_prefix", + EINA_LOG_COLOR_DEFAULT); + if (_eina_prefix_log_dom < 0) + { + EINA_LOG_ERR("Could not register log domain: eina_prefix"); + return EINA_FALSE; + } + + return EINA_TRUE; +} + +Eina_Bool +eina_prefix_shutdown(void) +{ + eina_log_domain_unregister(_eina_prefix_log_dom); + _eina_prefix_log_dom = -1; + return EINA_TRUE; +} -- 2.7.4