5e2015b4cdace1c2afc8e1c3994c471c70cfab9b
[platform/upstream/iotivity.git] / resource / oc_logger / c / oc_console_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 "targets/oc_console_logger.h"
23
24 #include <stdio.h>
25 #include <stdlib.h>
26
27 typedef struct
28 {
29  FILE *out;
30 } oc_console_logger_ctx;
31
32 oc_log_ctx_t *oc_make_console_logger()
33 {
34  return oc_log_make_ctx(
35                         NULL,
36             OC_LOG_ALL,
37             oc_console_logger_init,
38             oc_console_logger_destroy,
39             oc_console_logger_flush,
40             oc_console_logger_set_level,
41             oc_console_logger_write,
42                         oc_console_logger_set_module
43         );
44 }
45
46 int oc_console_logger_init(oc_log_ctx_t *ctx, void *world)
47 {
48  (void)world;
49  oc_console_logger_ctx *my_ctx;
50
51  my_ctx = (oc_console_logger_ctx *)malloc(sizeof(oc_console_logger_ctx));
52
53  if(0 == my_ctx)
54   return 0;
55
56  my_ctx->out = stderr;
57
58  ctx->ctx = (void *)my_ctx;
59
60  return 1;
61 }
62
63 void oc_console_logger_destroy(oc_log_ctx_t *ctx)
64 {
65  oc_console_logger_ctx *lctx = (oc_console_logger_ctx *)ctx->ctx;
66
67  fflush(lctx->out);
68
69  free(lctx);
70 }
71
72 void oc_console_logger_flush(oc_log_ctx_t *ctx)
73 {
74  oc_console_logger_ctx *lctx = (oc_console_logger_ctx *)ctx->ctx;
75
76  fflush(lctx->out);
77 }
78
79 void oc_console_logger_set_level(oc_log_ctx_t *ctx, const int level)
80 {
81  (void)ctx;
82  (void)level;
83  /* We don't have any special thing we need to do when a log level changes. */
84  return;
85 }
86
87 size_t oc_console_logger_write(oc_log_ctx_t *ctx, const int level, const char *msg)
88 {
89  oc_console_logger_ctx *lctx = (oc_console_logger_ctx *)ctx->ctx;
90
91  /* A "real" implementation might want to replace the loglevel with a mnemonic: */
92
93  if(0 == ctx->module_name)
94   return 1 + fprintf(lctx->out, "%d: %s\n", level, msg);
95
96  return 1 + fprintf(lctx->out, "%d: [%s]: %s\n", level, ctx->module_name, msg);
97 }
98
99 int oc_console_logger_set_module(oc_log_ctx_t *ctx, const char *module_name)
100 {
101  (void)ctx;
102  (void)module_name;
103  /* We don't do anything special when the module name changes: */
104  return 1;
105 }