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"
22 #include "oic_string.h"
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)
32 oic_log_ctx_t *log_ctx;
34 if (0 == init || 0 == destroy || 0 == flush || 0 == set_level || 0 == write_level
38 if (__OIC_LOG_MIN__ > level || __OIC_LOG_MAX__ < level)
41 log_ctx = (oic_log_ctx_t *) malloc(sizeof(oic_log_ctx_t));
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;
50 log_ctx->destroy = destroy;
51 log_ctx->flush = flush;
52 log_ctx->set_level = set_level;
53 log_ctx->set_module = set_module;
55 log_ctx->write_level = write_level;
57 if (0 == log_ctx->init(log_ctx, world))
66 void oic_log_destroy(oic_log_ctx_t *ctx)
73 if (0 != ctx->module_name)
74 free(ctx->module_name);
79 int oic_log_init(oic_log_ctx_t *ctx, void *world)
84 return ctx->init(ctx, world);
87 void oic_log_flush(oic_log_ctx_t *ctx)
96 void oic_log_set_level(oic_log_ctx_t *ctx, const oic_log_level ll)
102 ctx->set_level(ctx, ll);
105 size_t oic_log_write(oic_log_ctx_t *ctx, const char *msg)
110 return oic_log_write_level(ctx, ctx->log_level, msg);
113 size_t oic_log_write_level(oic_log_ctx_t *ctx, const oic_log_level ll, const char *msg)
121 return ctx->write_level(ctx, ll, msg);
124 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 mn = OICStrdup(module_name);
135 if (0 != ctx->module_name)
136 free(ctx->module_name);
140 if (0 != ctx->module_name)
141 free(ctx->module_name);
143 ctx->module_name = mn;
146 return ctx->set_module(ctx, ctx->module_name);