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 "oc_logger.h"
22 #include "oic_string.h"
27 oc_log_ctx_t *oc_log_make_ctx(
29 const oc_log_level level,
31 oc_log_destroy_t destroy,
33 oc_log_set_level_t set_level,
34 oc_log_write_level_t write_level,
35 oc_log_set_module_t set_module
38 oc_log_ctx_t *log_ctx;
50 if(OC_LOG_MIN_VAL__ >= level || OC_LOG_MAX_VAL__ <= level)
55 log_ctx = (oc_log_ctx_t *)malloc(sizeof(oc_log_ctx_t));
62 log_ctx->ctx = 0; /* we'll get to this in a sec... */
63 log_ctx->log_level = level;
64 log_ctx->module_name = 0;
66 log_ctx->destroy = destroy;
67 log_ctx->flush = flush;
68 log_ctx->set_level = set_level;
69 log_ctx->set_module = set_module;
71 log_ctx->write_level = write_level;
73 if(!log_ctx->init(log_ctx, world))
82 void oc_log_destroy(oc_log_ctx_t *ctx)
91 if(0 != ctx->module_name)
93 free(ctx->module_name);
99 int oc_log_init(oc_log_ctx_t *ctx, void *world)
106 return ctx->init(ctx, world);
109 void oc_log_flush(oc_log_ctx_t *ctx)
118 void oc_log_set_level(oc_log_ctx_t *ctx, const oc_log_level loglevel)
124 ctx->set_level(ctx, loglevel);
127 size_t oc_log_write(oc_log_ctx_t *ctx, const char *msg)
134 return oc_log_write_level(ctx, ctx->log_level, msg);
137 size_t oc_log_write_level(oc_log_ctx_t *ctx, const oc_log_level loglevel, const char *msg)
144 ctx->log_level = loglevel;
147 return ctx->write_level(ctx, loglevel, msg);
150 int oc_log_set_module(oc_log_ctx_t *ctx, const char *module_name)
154 if(!ctx || !module_name)
159 /* Swap pointers so that module data's not erased in the event of failure: */
160 mn = OICStrdup(module_name);
167 if(!ctx->module_name)
169 free(ctx->module_name);
172 ctx->module_name = mn;
175 return ctx->set_module(ctx, ctx->module_name);