Imported Upstream version 0.9.1
[platform/upstream/iotivity.git] / resource / csdk / connectivity / common / src / oic_logger.c
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #include "oic_logger.h"
22
23 #include <string.h>
24 #include <stdlib.h>
25
26 oic_log_ctx_t *oic_log_make_ctx(void *world, const oic_log_level level, oic_log_init_t init,
27                                 oic_log_destroy_t destroy, oic_log_flush_t flush,
28                                 oic_log_set_level_t set_level,oic_log_write_level_t write_level,
29                                 oic_log_set_module_t set_module)
30 {
31     oic_log_ctx_t *log_ctx;
32
33     if (0 == init || 0 == destroy || 0 == flush || 0 == set_level || 0 == write_level
34         || 0 == set_module)
35         return 0;
36
37     if (__OIC_LOG_MIN__ > level || __OIC_LOG_MAX__ < level)
38         return 0;
39
40     log_ctx = (oic_log_ctx_t *) malloc(sizeof(oic_log_ctx_t));
41
42     if (0 == log_ctx)
43         return 0;
44
45     log_ctx->ctx = 0; /* we'll get to this in a sec... */
46     log_ctx->log_level = level;
47     log_ctx->module_name = 0;
48     log_ctx->init = init;
49     log_ctx->destroy = destroy;
50     log_ctx->flush = flush;
51     log_ctx->set_level = set_level;
52     log_ctx->set_module = set_module;
53
54     log_ctx->write_level = write_level;
55
56     if (0 == log_ctx->init(log_ctx, world))
57     {
58         free(log_ctx);
59         return 0;
60     }
61
62     return log_ctx;
63 }
64
65 void oic_log_destroy(oic_log_ctx_t *ctx)
66 {
67     if (0 == ctx)
68         return;
69
70     ctx->destroy(ctx);
71
72     if (0 != ctx->module_name)
73         free(ctx->module_name);
74
75     free(ctx);
76 }
77
78 int oic_log_init(oic_log_ctx_t *ctx, void *world)
79 {
80     if (0 == ctx)
81         return 0;
82
83     return ctx->init(ctx, world);
84 }
85
86 void oic_log_flush(oic_log_ctx_t *ctx)
87 {
88     if (0 == ctx)
89     {
90         return;
91     }
92     ctx->flush(ctx);
93 }
94
95 void oic_log_set_level(oic_log_ctx_t *ctx, const oic_log_level ll)
96 {
97     if (0 == ctx)
98     {
99         return;
100     }
101     ctx->set_level(ctx, ll);
102 }
103
104 size_t oic_log_write(oic_log_ctx_t *ctx, const char *msg)
105 {
106     if (0 == ctx)
107         return 0;
108
109     return oic_log_write_level(ctx, ctx->log_level, msg);
110 }
111
112 size_t oic_log_write_level(oic_log_ctx_t *ctx, const oic_log_level ll, const char *msg)
113 {
114     if (0 == ctx)
115         return 0;
116
117     ctx->log_level = ll;
118
119     /* Notify: */
120     return ctx->write_level(ctx, ll, msg);
121 }
122
123 int oic_log_set_module(oic_log_ctx_t *ctx, const char *module_name)
124 {
125     char *mn;
126     size_t l;
127
128     if (0 == ctx)
129         return 0;
130
131     /* Swap pointers so that module data's not erased in the event of failure: */
132     l = strlen(module_name);
133
134     mn = (char *) malloc(1 + l);
135
136     if (0 == mn)
137     {
138         if (0 != ctx->module_name)
139             free(ctx->module_name);
140         return 0;
141     }
142
143     memcpy(mn, module_name, 1 + l);
144
145     if (0 != ctx->module_name)
146         free(ctx->module_name);
147
148     ctx->module_name = mn;
149
150     /* Notify: */
151     return ctx->set_module(ctx, ctx->module_name);
152 }
153
154