aboutsummaryrefslogtreecommitdiffstats
path: root/reference
diff options
context:
space:
mode:
authorAndy Williams <andy@andywilliams.me>2017-12-05 16:38:06 +0000
committerAndy Williams <andy@andywilliams.me>2017-12-05 16:38:06 +0000
commit4aa1b33ff2d265a5dfdd45b3ea4d89f6269dd2a9 (patch)
tree625ecfe2121919b59817fdaabdd4a85263b962af /reference
parentcore: Add an events reference example (diff)
downloadexamples-4aa1b33ff2d265a5dfdd45b3ea4d89f6269dd2a9.tar.gz
core: Add a logging example
Diffstat (limited to 'reference')
-rw-r--r--reference/c/core/src/core_log.c83
-rw-r--r--reference/c/core/src/meson.build7
2 files changed, 90 insertions, 0 deletions
diff --git a/reference/c/core/src/core_log.c b/reference/c/core/src/core_log.c
new file mode 100644
index 00000000..01f9dac2
--- /dev/null
+++ b/reference/c/core/src/core_log.c
@@ -0,0 +1,83 @@
+#define EFL_EO_API_SUPPORT 1
+#define EFL_BETA_API_SUPPORT 1
+
+#include <stdio.h>
+
+#include <Eina.h>
+#include <Efl_Core.h>
+
+/*
+ * Efl Core Log examples.
+ *
+ * This demo shows how to log at various levels and to change what log is shown.
+ * You can also use a custom log printer in your app as shown in _log_custom.
+ */
+
+static double
+_divide(int num, int denom)
+{
+ if (denom == 0)
+ EINA_LOG_CRIT("Attempt to divide by 0\n");
+ else
+ {
+ if (denom < 0)
+ EINA_LOG_WARN("Possible undesirable effect, divide by negative number");
+
+ double ret = ((double) num / denom);
+ EINA_LOG_INFO("%d / %d = %f\n", num, denom, ret);
+ return ret;
+ }
+
+ return -1;
+}
+
+static void
+_divides()
+{
+ _divide(5, 1);
+ _divide(5, -1);
+ _divide(5, 0);
+}
+
+static void
+_log_levels()
+{
+ printf("Executing with default logging\n");
+ _divides();
+
+ eina_log_level_set(EINA_LOG_LEVEL_WARN);
+ printf("Executing with WARN level\n"); // same as EINA_LOG_LEVEL = 2
+ _divides();
+
+ eina_log_level_set(EINA_LOG_LEVEL_INFO);
+ printf("Executing with INFO on\n"); // same as EINA_LOG_LEVEL = 3
+ _divides();
+}
+
+void _print_cb(const Eina_Log_Domain *domain EINA_UNUSED, Eina_Log_Level level,
+ const char *file, const char *fnc, int line,
+ const char *fmt, void *data EINA_UNUSED, va_list args)
+{
+ fprintf(stdout, "LOG %d <%s (%s:%d)> ", level, fnc, file, line);
+ vfprintf(stdout, fmt, args);
+ putc('\n', stdout);
+}
+
+static void
+_log_custom()
+{
+ printf("Executing with custom log printer\n");
+ eina_log_print_cb_set(_print_cb, NULL);
+ _divides();
+}
+
+EAPI_MAIN void
+efl_main(void *data EINA_UNUSED, const Efl_Event *ev EINA_UNUSED)
+{
+ _log_levels();
+ _log_custom();
+
+ efl_exit(0);
+}
+EFL_MAIN()
+
diff --git a/reference/c/core/src/meson.build b/reference/c/core/src/meson.build
index 26085f6d..67214f55 100644
--- a/reference/c/core/src/meson.build
+++ b/reference/c/core/src/meson.build
@@ -35,3 +35,10 @@ executable('efl_reference_core_poll',
install : true
)
+executable('efl_reference_core_log',
+ files(['core_log.c']),
+ dependencies : deps,
+ include_directories : inc,
+ install : true
+)
+