Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / resource / oc_logger / c / oc_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 "oc_logger.h"
22 #include "oic_string.h"
23
24 #include <string.h>
25 #include <stdlib.h>
26
27 oc_log_ctx_t *oc_log_make_ctx(
28                             void*                 world,
29                             const oc_log_level    level,
30                             oc_log_init_t         init,
31                             oc_log_destroy_t      destroy,
32                             oc_log_flush_t        flush,
33                             oc_log_set_level_t    set_level,
34                             oc_log_write_level_t  write_level,
35                             oc_log_set_module_t   set_module
36                            )
37 {
38  oc_log_ctx_t *log_ctx;
39
40  if(0 == init ||
41     0 == destroy ||
42     0 == flush ||
43     0 == set_level ||
44     0 == write_level ||
45     0 == set_module)
46  {
47   return 0;
48  }
49
50  if(OC_LOG_MIN_VAL__ >= level || OC_LOG_MAX_VAL__ <= level)
51  {
52      return 0;
53  }
54
55  log_ctx = (oc_log_ctx_t *)malloc(sizeof(oc_log_ctx_t));
56
57  if(!log_ctx)
58  {
59      return 0;
60  }
61
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;
65  log_ctx->init          = init;
66  log_ctx->destroy       = destroy;
67  log_ctx->flush         = flush;
68  log_ctx->set_level     = set_level;
69  log_ctx->set_module    = set_module;
70
71  log_ctx->write_level   = write_level;
72
73  if(!log_ctx->init(log_ctx, world))
74   {
75     free(log_ctx);
76     return 0;
77   }
78
79  return log_ctx;
80 }
81
82 void oc_log_destroy(oc_log_ctx_t *ctx)
83 {
84  if(!ctx)
85  {
86      return;
87  }
88
89  ctx->destroy(ctx);
90
91  if(0 != ctx->module_name)
92  {
93      free(ctx->module_name);
94  }
95
96  free(ctx);
97 }
98
99 int oc_log_init(oc_log_ctx_t *ctx, void *world)
100 {
101  if(!ctx)
102  {
103      return 0;
104  }
105
106  return ctx->init(ctx, world);
107 }
108
109 void oc_log_flush(oc_log_ctx_t *ctx)
110 {
111     if(!ctx)
112     {
113         return;
114     }
115     ctx->flush(ctx);
116 }
117
118 void oc_log_set_level(oc_log_ctx_t *ctx, const oc_log_level loglevel)
119 {
120     if(!ctx)
121     {
122         return;
123     }
124     ctx->set_level(ctx, loglevel);
125 }
126
127 size_t oc_log_write(oc_log_ctx_t *ctx, const char *msg)
128 {
129     if(!ctx)
130     {
131         return 0;
132     }
133
134  return oc_log_write_level(ctx, ctx->log_level, msg);
135 }
136
137 size_t oc_log_write_level(oc_log_ctx_t *ctx, const oc_log_level loglevel, const char *msg)
138 {
139  if(!ctx)
140  {
141      return 0;
142  }
143
144  ctx->log_level = loglevel;
145
146  /* Notify: */
147  return ctx->write_level(ctx, loglevel, msg);
148 }
149
150 int oc_log_set_module(oc_log_ctx_t *ctx, const char *module_name)
151 {
152  char *mn = NULL;
153
154  if(!ctx || !module_name)
155  {
156      return 0;
157  }
158
159  /* Swap pointers so that module data's not erased in the event of failure: */
160  mn = OICStrdup(module_name);
161
162  if(!mn)
163  {
164      return 0;
165  }
166
167  if(!ctx->module_name)
168  {
169      free(ctx->module_name);
170  }
171
172  ctx->module_name = mn;
173
174  /* Notify: */
175  return ctx->set_module(ctx, ctx->module_name);
176 }
177
178