Imported Upstream version 0.9.2
[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 #include "oic_string.h"
23
24 #include <string.h>
25 #include <stdlib.h>
26
27 oic_log_ctx_t *oic_log_make_ctx(void *world, const oic_log_level level, oic_log_init_t init,
28                                 oic_log_destroy_t destroy, oic_log_flush_t flush,
29                                 oic_log_set_level_t set_level,oic_log_write_level_t write_level,
30                                 oic_log_set_module_t set_module)
31 {
32     oic_log_ctx_t *log_ctx;
33
34     if (0 == init || 0 == destroy || 0 == flush || 0 == set_level || 0 == write_level
35         || 0 == set_module)
36         return 0;
37
38     if (__OIC_LOG_MIN__ > level || __OIC_LOG_MAX__ < level)
39         return 0;
40
41     log_ctx = (oic_log_ctx_t *) malloc(sizeof(oic_log_ctx_t));
42
43     if (0 == log_ctx)
44         return 0;
45
46     log_ctx->ctx = 0; /* we'll get to this in a sec... */
47     log_ctx->log_level = level;
48     log_ctx->module_name = 0;
49     log_ctx->init = init;
50     log_ctx->destroy = destroy;
51     log_ctx->flush = flush;
52     log_ctx->set_level = set_level;
53     log_ctx->set_module = set_module;
54
55     log_ctx->write_level = write_level;
56
57     if (0 == log_ctx->init(log_ctx, world))
58     {
59         free(log_ctx);
60         return 0;
61     }
62
63     return log_ctx;
64 }
65
66 void oic_log_destroy(oic_log_ctx_t *ctx)
67 {
68     if (0 == ctx)
69         return;
70
71     ctx->destroy(ctx);
72
73     if (0 != ctx->module_name)
74         free(ctx->module_name);
75
76     free(ctx);
77 }
78
79 int oic_log_init(oic_log_ctx_t *ctx, void *world)
80 {
81     if (0 == ctx)
82         return 0;
83
84     return ctx->init(ctx, world);
85 }
86
87 void oic_log_flush(oic_log_ctx_t *ctx)
88 {
89     if (0 == ctx)
90     {
91         return;
92     }
93     ctx->flush(ctx);
94 }
95
96 void oic_log_set_level(oic_log_ctx_t *ctx, const oic_log_level ll)
97 {
98     if (0 == ctx)
99     {
100         return;
101     }
102     ctx->set_level(ctx, ll);
103 }
104
105 size_t oic_log_write(oic_log_ctx_t *ctx, const char *msg)
106 {
107     if (0 == ctx)
108         return 0;
109
110     return oic_log_write_level(ctx, ctx->log_level, msg);
111 }
112
113 size_t oic_log_write_level(oic_log_ctx_t *ctx, const oic_log_level ll, const char *msg)
114 {
115     if (0 == ctx)
116         return 0;
117
118     ctx->log_level = ll;
119
120     /* Notify: */
121     return ctx->write_level(ctx, ll, msg);
122 }
123
124 int oic_log_set_module(oic_log_ctx_t *ctx, const char *module_name)
125 {
126     char *mn;
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     mn = OICStrdup(module_name);
133     if (0 == mn)
134     {
135         if (0 != ctx->module_name)
136             free(ctx->module_name);
137         return 0;
138     }
139
140     if (0 != ctx->module_name)
141         free(ctx->module_name);
142
143     ctx->module_name = mn;
144
145     /* Notify: */
146     return ctx->set_module(ctx, ctx->module_name);
147 }
148
149