Motr  M0
logger.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2017-2020 Seagate Technology LLC and/or its Affiliates
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * For any questions about this software or licensing,
18  * please email opensource@seagate.com or cortx-questions@seagate.com.
19  *
20  */
21 
22 
23 #include "motr/m0crate/logger.h"
24 #include <string.h>
25 
28 
29 struct {
30  const char *name;
31 } level_str[] = {
32  [CLL_ERROR] = {"error"},
33  [CLL_WARN] = {"warning"},
34  [CLL_INFO] = {"info"},
35  [CLL_TRACE] = {"trace"},
36  [CLL_DEBUG] = {"dbg"},
37 };
38 
39 void cr_log(enum cr_log_level lev, const char *fmt, ...)
40 {
41  va_list va;
42  va_start(va, fmt);
43  cr_vlog(lev, fmt, va);
44  va_end(va);
45 
46 }
47 
48 void cr_vlog(enum cr_log_level lev, const char *fmt, va_list args)
49 {
50  if (lev == CLL_SAME) {
51  if (prev_level <= log_level) {
52  (void) vfprintf(stderr, fmt, args);
53  }
54  } else {
55  if (lev <= log_level) {
56  (void) fprintf(stderr, "%s: ", level_str[lev].name);
57  (void) vfprintf(stderr, fmt, args);
58  }
59  prev_level = lev;
60  }
61 }
62 
64 {
65  log_level = level;
66  prev_level = level;
67 }
68 
69 void cr_log_ex(enum cr_log_level lev,
70  const char *pre,
71  const char *post,
72  const char *fmt, ...)
73 {
74  va_list va;
75  cr_log(lev, "%s", pre);
76  va_start(va, fmt);
77  cr_vlog(CLL_SAME, fmt, va);
78  va_end(va);
79  cr_log(CLL_SAME, "%s", post);
80 }
81 
82 /*
83  * Local variables:
84  * c-indentation-style: "K&R"
85  * c-basic-offset: 8
86  * tab-width: 8
87  * fill-column: 80
88  * scroll-step: 1
89  * End:
90  */
91 /*
92  * vim: tabstop=8 shiftwidth=8 noexpandtab textwidth=80 nowrap
93  */
const char * name
Definition: logger.c:30
enum m0_trace_level level
Definition: trace.c:111
enum cr_log_level prev_level
Definition: logger.c:27
cr_log_level
Definition: logger.h:37
enum cr_log_level log_level
Definition: logger.c:26
Definition: ub.c:49
void cr_set_debug_level(enum cr_log_level level)
Definition: logger.c:63
char * fmt(const char *format,...) __attribute__((format(printf
void cr_log(enum cr_log_level lev, const char *fmt,...)
Definition: logger.c:39
void cr_vlog(enum cr_log_level lev, const char *fmt, va_list args)
Definition: logger.c:48
struct @326 level_str[]
void cr_log_ex(enum cr_log_level lev, const char *pre, const char *post, const char *fmt,...)
Definition: logger.c:69