1 /*
2 * Copyright 2015 Rockchip Electronics Co. LTD
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #define MODULE_TAG "mpp_log"
18
19 #include <stdio.h>
20 #include <stdarg.h>
21 #include <string.h>
22
23 #include "mpp_env.h"
24 #include "mpp_debug.h"
25 #include "mpp_common.h"
26
27 #include "os_log.h"
28
29 #define MPP_LOG_MAX_LEN 256
30
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34
35 RK_U32 mpp_debug = 0;
36
37 // TODO: add log timing information and switch flag
38 static const char *msg_log_warning = "log message is long\n";
39 static const char *msg_log_nothing = "\n";
40 static int mpp_log_level = MPP_LOG_INFO;
41
__mpp_log(os_log_callback func,const char * tag,const char * fmt,const char * fname,va_list args)42 static void __mpp_log(os_log_callback func, const char *tag, const char *fmt,
43 const char *fname, va_list args)
44 {
45 char msg[MPP_LOG_MAX_LEN + 1];
46 char *tmp = msg;
47 const char *buf = fmt;
48 size_t len_fmt = strnlen(fmt, MPP_LOG_MAX_LEN);
49 size_t len_name = (fname) ? (strnlen(fname, MPP_LOG_MAX_LEN)) : (0);
50 size_t buf_left = MPP_LOG_MAX_LEN;
51 size_t len_all = len_fmt + len_name;
52
53 if (NULL == tag)
54 tag = MODULE_TAG;
55
56 if (len_name) {
57 buf = msg;
58 buf_left -= snprintf(msg, buf_left, "%s ", fname);
59 tmp += len_name + 1;
60 }
61
62 if (len_all == 0) {
63 buf = msg_log_nothing;
64 } else if (len_all >= MPP_LOG_MAX_LEN) {
65 buf_left -= snprintf(tmp, buf_left, "%s", msg_log_warning);
66 buf = msg;
67 } else {
68 snprintf(tmp, buf_left, "%s", fmt);
69 if (fmt[len_fmt - 1] != '\n') {
70 tmp[len_fmt] = '\n';
71 tmp[len_fmt + 1] = '\0';
72 }
73 buf = msg;
74 }
75
76 func(tag, buf, args);
77 }
78
_mpp_log(const char * tag,const char * fmt,const char * fname,...)79 void _mpp_log(const char *tag, const char *fmt, const char *fname, ...)
80 {
81 va_list args;
82
83 mpp_logw("warning: use new logx function\n");
84
85 va_start(args, fname);
86 __mpp_log(os_log_info, tag, fmt, fname, args);
87 va_end(args);
88 }
89
_mpp_err(const char * tag,const char * fmt,const char * fname,...)90 void _mpp_err(const char *tag, const char *fmt, const char *fname, ...)
91 {
92 va_list args;
93
94 mpp_logw("warning: use new logx function\n");
95
96 va_start(args, fname);
97 __mpp_log(os_log_error, tag, fmt, fname, args);
98 va_end(args);
99 }
100
_mpp_log_l(int level,const char * tag,const char * fmt,const char * fname,...)101 void _mpp_log_l(int level, const char *tag, const char *fmt, const char *fname, ...)
102 {
103 static os_log_callback log_func[] = {
104 NULL, /* MPP_LOG_DEFAULT */
105 os_log_fatal, /* MPP_LOG_FATAL */
106 os_log_error, /* MPP_LOG_ERROR */
107 os_log_warn, /* MPP_LOG_WARN */
108 os_log_info, /* MPP_LOG_INFO */
109 os_log_debug, /* MPP_LOG_DEBUG */
110 os_log_trace, /* MPP_LOG_VERBOSE */
111 os_log_info, /* MPP_LOG_DEFAULT */
112 };
113
114 va_list args;
115 int log_level;
116
117 if (level <= MPP_LOG_UNKNOWN || level >= MPP_LOG_SILENT)
118 return;
119
120 log_level = mpp_log_level;
121 if (log_level >= MPP_LOG_SILENT)
122 return;
123
124 if (level > log_level)
125 return;
126
127 va_start(args, fname);
128 __mpp_log(log_func[level], tag, fmt, fname, args);
129 va_end(args);
130 }
131
mpp_set_log_level(int level)132 void mpp_set_log_level(int level)
133 {
134 if (level <= MPP_LOG_UNKNOWN || level > MPP_LOG_SILENT) {
135 mpp_logw("log level should in range [%d : %d] invalid intput %d\n",
136 MPP_LOG_FATAL, MPP_LOG_SILENT, level);
137 level = MPP_LOG_INFO;
138 }
139
140 mpp_log_level = level;
141 }
142
mpp_get_log_level(void)143 int mpp_get_log_level(void)
144 {
145 int level;
146
147 mpp_env_get_u32("mpp_log_level", (RK_U32 *)&level, mpp_log_level);
148
149 if (level <= MPP_LOG_UNKNOWN || level > MPP_LOG_SILENT)
150 level = MPP_LOG_INFO;
151
152 mpp_log_level = level;
153
154 return level;
155 }
156
157 #ifdef __cplusplus
158 }
159 #endif
160