1 //******************************************************************
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include "oic_logger.h"
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)
31 oic_log_ctx_t *log_ctx;
33 if (0 == init || 0 == destroy || 0 == flush || 0 == set_level || 0 == write_level
37 if (__OIC_LOG_MIN__ > level || __OIC_LOG_MAX__ < level)
40 log_ctx = (oic_log_ctx_t *) malloc(sizeof(oic_log_ctx_t));
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;
49 log_ctx->destroy = destroy;
50 log_ctx->flush = flush;
51 log_ctx->set_level = set_level;
52 log_ctx->set_module = set_module;
54 log_ctx->write_level = write_level;
56 if (0 == log_ctx->init(log_ctx, world))
65 void oic_log_destroy(oic_log_ctx_t *ctx)
72 if (0 != ctx->module_name)
73 free(ctx->module_name);
78 int oic_log_init(oic_log_ctx_t *ctx, void *world)
83 return ctx->init(ctx, world);
86 void oic_log_flush(oic_log_ctx_t *ctx)
95 void oic_log_set_level(oic_log_ctx_t *ctx, const oic_log_level ll)
101 ctx->set_level(ctx, ll);
104 size_t oic_log_write(oic_log_ctx_t *ctx, const char *msg)
109 return oic_log_write_level(ctx, ctx->log_level, msg);
112 size_t oic_log_write_level(oic_log_ctx_t *ctx, const oic_log_level ll, const char *msg)
120 return ctx->write_level(ctx, ll, msg);
123 int oic_log_set_module(oic_log_ctx_t *ctx, const char *module_name)
131 /* Swap pointers so that module data's not erased in the event of failure: */
132 l = strlen(module_name);
134 mn = (char *) malloc(1 + l);
138 if (0 != ctx->module_name)
139 free(ctx->module_name);
143 memcpy(mn, module_name, 1 + l);
145 if (0 != ctx->module_name)
146 free(ctx->module_name);
148 ctx->module_name = mn;
151 return ctx->set_module(ctx, ctx->module_name);