Repo Merge: Moving resource API down a directory
[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  oc_console_logger_ctx *my_ctx;
49
50  my_ctx = (oc_console_logger_ctx *)malloc(sizeof(oc_console_logger_ctx));
51
52  if(0 == my_ctx)
53   return 0;
54
55  my_ctx->out = stderr;
56
57  ctx->ctx = (void *)my_ctx;
58
59  return 1;
60 }
61
62 void oc_console_logger_destroy(oc_log_ctx_t *ctx)
63 {
64  oc_console_logger_ctx *lctx = (oc_console_logger_ctx *)ctx->ctx;
65
66  fflush(lctx->out);
67
68  free(lctx);
69 }
70
71 void oc_console_logger_flush(oc_log_ctx_t *ctx)
72 {
73  oc_console_logger_ctx *lctx = (oc_console_logger_ctx *)ctx->ctx;
74
75  fflush(lctx->out);
76 }
77
78 void oc_console_logger_set_level(oc_log_ctx_t *ctx, const int level)
79 {
80  /* We don't have any special thing we need to do when a log level changes. */
81  return;
82 }
83
84 size_t oc_console_logger_write(oc_log_ctx_t *ctx, const int level, const char *msg)
85 {
86  oc_console_logger_ctx *lctx = (oc_console_logger_ctx *)ctx->ctx;
87
88  /* A "real" implementation might want to replace the loglevel with a mnemonic: */
89
90  if(0 == ctx->module_name)
91   return 1 + fprintf(lctx->out, "%d: %s\n", level, msg);
92
93  return 1 + fprintf(lctx->out, "%d: [%s]: %s\n", level, ctx->module_name, msg);
94 }
95
96 int oc_console_logger_set_module(oc_log_ctx_t *ctx, const char *module_name)
97 {
98  /* We don't do anything special when the module name changes: */
99  return 1;
100 }