Improve log messages
[src/app-framework-binder.git] / src / afb-ditf.c
1 /*
2  * Copyright (C) 2016, 2017 "IoT.bzh"
3  * Author José Bollo <jose.bollo@iot.bzh>
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #define _GNU_SOURCE
19 #define NO_BINDING_VERBOSE_MACRO
20
21 #include <string.h>
22 #include <errno.h>
23
24 #include <afb/afb-binding.h>
25
26 #include "afb-ditf.h"
27 #include "afb-evt.h"
28 #include "afb-common.h"
29 #include "verbose.h"
30
31
32 static void vverbose_cb(void *closure, int level, const char *file, int line, const char *function, const char *fmt, va_list args)
33 {
34         char *p;
35         struct afb_ditf *ditf = closure;
36
37         if (vasprintf(&p, fmt, args) < 0)
38                 vverbose(level, file, line, function, fmt, args);
39         else {
40                 verbose(level, file, line, function, "%s {binding %s}", p, ditf->prefix);
41                 free(p);
42         }
43 }
44
45 static void old_vverbose_cb(void *closure, int level, const char *file, int line, const char *fmt, va_list args)
46 {
47         vverbose_cb(closure, level, file, line, "?", fmt, args);
48 }
49
50 static struct afb_event event_make_cb(void *closure, const char *name)
51 {
52         size_t plen, nlen;
53         char *event;
54         struct afb_ditf *ditf = closure;
55
56         /* makes the event name */
57         plen = strlen(ditf->prefix);
58         nlen = strlen(name);
59         event = alloca(nlen + plen + 2);
60         memcpy(event, ditf->prefix, plen);
61         event[plen] = '/';
62         memcpy(event + plen + 1, name, nlen + 1);
63
64         /* create the event */
65         return afb_evt_create_event(event);
66 }
67
68 static int event_broadcast_cb(void *closure, const char *name, struct json_object *object)
69 {
70         size_t plen, nlen;
71         char *event;
72         struct afb_ditf *ditf = closure;
73
74         /* makes the event name */
75         plen = strlen(ditf->prefix);
76         nlen = strlen(name);
77         event = alloca(nlen + plen + 2);
78         memcpy(event, ditf->prefix, plen);
79         event[plen] = '/';
80         memcpy(event + plen + 1, name, nlen + 1);
81
82         /* broadcast the event */
83         return afb_evt_broadcast(event, object);
84 }
85
86 static int rootdir_open_locale_cb(void *closure, const char *filename, int flags, const char *locale)
87 {
88         return afb_common_rootdir_open_locale(filename, flags, locale);
89 }
90
91 static const struct afb_daemon_itf daemon_itf = {
92         .vverbose = old_vverbose_cb,
93         .event_make = event_make_cb,
94         .event_broadcast = event_broadcast_cb,
95         .get_event_loop = afb_common_get_event_loop,
96         .get_user_bus = afb_common_get_user_bus,
97         .get_system_bus = afb_common_get_system_bus,
98         .rootdir_get_fd = afb_common_rootdir_get_fd,
99         .rootdir_open_locale = rootdir_open_locale_cb
100 };
101
102 void afb_ditf_init(struct afb_ditf *ditf, const char *prefix)
103 {
104         ditf->interface.verbosity = verbosity;
105         ditf->interface.mode = AFB_MODE_LOCAL;
106         ditf->interface.daemon.itf = &daemon_itf;
107         ditf->interface.daemon.closure = ditf;
108         afb_ditf_rename(ditf, prefix);
109 }
110
111 void afb_ditf_rename(struct afb_ditf *ditf, const char *prefix)
112 {
113         ditf->prefix = prefix;
114 }
115