1 /*
2 * Copyright (C) 2015-2019 Alibaba Group Holding Limited
3 */
4
5 #include "aos_system.h"
6 #include "board_config.h"
7 #include "py/builtin.h"
8 #include "py/obj.h"
9 #include "py/runtime.h"
10 #include "py/stackctrl.h"
11 #include "py_defines.h"
12 #include "ulog/ulog.h"
13
14 #define MOD_STR "LOG"
15 #define SCRIPT "PYLOG"
16 #define LOG_LEVEL_DEBUG "debug"
17 #define LOG_LEVEL_INFO "info"
18 #define LOG_LEVEL_WARN "warning"
19 #define LOG_LEVEL_ERROR "error"
20 #define LOG_LEVEL_FATAL "fatal"
21 #define LOG_LEVEL_NONE "none"
22
set_stdlog_level(mp_obj_t loglevel)23 static mp_obj_t set_stdlog_level(mp_obj_t loglevel)
24 {
25 int ret = 0;
26 aos_log_level_t log_level;
27 log_level = mp_obj_get_int(loglevel);
28 ret = aos_set_log_level(log_level);
29 return mp_obj_new_int(ret);
30 }
31 MP_DEFINE_CONST_FUN_OBJ_1(native_set_stdlog_level, set_stdlog_level);
32
set_popcloud_log_level(mp_obj_t loglevel)33 static mp_obj_t set_popcloud_log_level(mp_obj_t loglevel)
34 {
35 int ret = 0;
36 aos_log_level_t log_level;
37 log_level = mp_obj_get_int(loglevel);
38 ret = aos_set_popcloud_log_level(log_level);
39 return mp_obj_new_int(ret);
40 }
41 MP_DEFINE_CONST_FUN_OBJ_1(native_set_popcloud_log_level, set_popcloud_log_level);
42
set_popfs_log_level(mp_obj_t loglevel)43 static mp_obj_t set_popfs_log_level(mp_obj_t loglevel)
44 {
45 int ret = 0;
46 aos_log_level_t log_level;
47 log_level = mp_obj_get_int(loglevel);
48 ret = aos_set_popfs_log_level(log_level);
49 return mp_obj_new_int(ret);
50 }
51 MP_DEFINE_CONST_FUN_OBJ_1(native_set_popfs_log_level, set_popfs_log_level);
52
set_log_file_path(mp_obj_t logpath)53 static mp_obj_t set_log_file_path(mp_obj_t logpath)
54 {
55 int ret = 0;
56
57 const char *path = mp_obj_str_get_str(logpath);
58 if (NULL == path) {
59 amp_warn(MOD_STR, "invalid parameter\n");
60 return mp_obj_new_int(-1);
61 }
62
63 ret = ulog_fs_log_file_path(path);
64 if (ret) {
65 amp_warn(MOD_STR, "fail to set log file path %s\n", path);
66 return mp_obj_new_int(-1);
67 }
68 return mp_obj_new_int(0);
69 }
70 MP_DEFINE_CONST_FUN_OBJ_1(native_set_log_file_path, set_log_file_path);
71
set_log_file_size(mp_obj_t logsize)72 static mp_obj_t set_log_file_size(mp_obj_t logsize)
73 {
74 int ret = -1;
75 unsigned int filesize = 0;
76
77 filesize = mp_obj_get_int(logsize);
78
79 ret = ulog_fs_log_file_size(filesize);
80 if (ret) {
81 amp_warn(MOD_STR, "fail to set log file size %d\n", filesize);
82 return mp_obj_new_int(-1);
83 }
84
85 return mp_obj_new_int(0);
86 }
87 MP_DEFINE_CONST_FUN_OBJ_1(native_set_log_file_size, set_log_file_size);
88
debug_log_out(mp_obj_t log_str,mp_obj_t log)89 static mp_obj_t debug_log_out(mp_obj_t log_str, mp_obj_t log)
90 {
91 const char *msg = mp_obj_str_get_str(log);
92 const char *tag = mp_obj_str_get_str(log_str);
93
94 if (NULL == msg) {
95 amp_error(MOD_STR, "ulog fail to get output format msg");
96 return mp_obj_new_int(-1);
97 }
98
99 ulog(AOS_LL_DEBUG, tag, NULL, 0, msg);
100
101 return mp_obj_new_int(0);
102 }
103 MP_DEFINE_CONST_FUN_OBJ_2(native_debug_log_out, debug_log_out);
104
info_log_out(mp_obj_t log_str,mp_obj_t log)105 static mp_obj_t info_log_out(mp_obj_t log_str, mp_obj_t log)
106 {
107 const char *msg = mp_obj_str_get_str(log);
108 const char *tag = mp_obj_str_get_str(log_str);
109
110 if (NULL == msg) {
111 amp_error(MOD_STR, "ulog fail to get output format msg");
112 return mp_obj_new_int(-1);
113 }
114
115 ulog(AOS_LL_INFO, tag, NULL, 0, msg);
116
117 return mp_obj_new_int(0);
118 }
119 MP_DEFINE_CONST_FUN_OBJ_2(native_info_log_out, info_log_out);
120
warn_log_out(mp_obj_t log_str,mp_obj_t log)121 static mp_obj_t warn_log_out(mp_obj_t log_str, mp_obj_t log)
122 {
123 const char *msg = mp_obj_str_get_str(log);
124 const char *tag = mp_obj_str_get_str(log_str);
125
126 if (NULL == msg) {
127 amp_error(MOD_STR, "ulog fail to get output format msg");
128 return mp_obj_new_int(-1);
129 }
130
131 ulog(AOS_LL_WARN, tag, NULL, 0, msg);
132
133 return mp_obj_new_int(0);
134 }
135 MP_DEFINE_CONST_FUN_OBJ_2(native_warn_log_out, warn_log_out);
136
error_log_out(mp_obj_t log_str,mp_obj_t log)137 static mp_obj_t error_log_out(mp_obj_t log_str, mp_obj_t log)
138 {
139 const char *msg = mp_obj_str_get_str(log);
140 const char *tag = mp_obj_str_get_str(log_str);
141
142 if (NULL == msg) {
143 amp_error(MOD_STR, "ulog fail to get output format msg");
144 return mp_obj_new_int(-1);
145 }
146
147 ulog(AOS_LL_ERROR, tag, NULL, 0, msg);
148
149 return mp_obj_new_int(0);
150 }
151 MP_DEFINE_CONST_FUN_OBJ_2(native_error_log_out, error_log_out);
152
fatal_log_out(mp_obj_t log_str,mp_obj_t log)153 static mp_obj_t fatal_log_out(mp_obj_t log_str, mp_obj_t log)
154 {
155 const char *msg = mp_obj_str_get_str(log);
156 const char *tag = mp_obj_str_get_str(log_str);
157
158 if (NULL == msg) {
159 amp_error(MOD_STR, "ulog fail to get output format msg");
160 return mp_obj_new_int(-1);
161 }
162
163 ulog(AOS_LL_FATAL, tag, NULL, 0, msg);
164
165 return mp_obj_new_int(0);
166 }
167 MP_DEFINE_CONST_FUN_OBJ_2(native_fatal_log_out, fatal_log_out);
168
169 STATIC const mp_rom_map_elem_t ulog_module_globals_table[] = {
170 { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_ulog) },
171 { MP_OBJ_NEW_QSTR(MP_QSTR_debug), MP_ROM_PTR(&native_debug_log_out) },
172 { MP_OBJ_NEW_QSTR(MP_QSTR_info), MP_ROM_PTR(&native_info_log_out) },
173 { MP_OBJ_NEW_QSTR(MP_QSTR_warn), MP_ROM_PTR(&native_warn_log_out) },
174 { MP_OBJ_NEW_QSTR(MP_QSTR_error), MP_ROM_PTR(&native_error_log_out) },
175 { MP_OBJ_NEW_QSTR(MP_QSTR_fatal), MP_ROM_PTR(&native_fatal_log_out) },
176 { MP_OBJ_NEW_QSTR(MP_QSTR_stdloglevel), MP_ROM_PTR(&native_set_stdlog_level) },
177 { MP_OBJ_NEW_QSTR(MP_QSTR_cloudloglevel), MP_ROM_PTR(&native_set_popcloud_log_level) },
178 { MP_OBJ_NEW_QSTR(MP_QSTR_fsloglevel), MP_ROM_PTR(&native_set_popfs_log_level) },
179 { MP_OBJ_NEW_QSTR(MP_QSTR_setlogfilepath), MP_ROM_PTR(&native_set_log_file_path) },
180 { MP_OBJ_NEW_QSTR(MP_QSTR_setlogfilesize), MP_ROM_PTR(&native_set_log_file_size) },
181 { MP_OBJ_NEW_QSTR(MP_QSTR_LOG_DEBUG), MP_ROM_INT(AOS_LL_DEBUG) },
182 { MP_OBJ_NEW_QSTR(MP_QSTR_LOG_INFO), MP_ROM_INT(AOS_LL_INFO) },
183 { MP_OBJ_NEW_QSTR(MP_QSTR_LOG_WARN), MP_ROM_INT(AOS_LL_WARN) },
184 { MP_OBJ_NEW_QSTR(MP_QSTR_LOG_ERROR), MP_ROM_INT(AOS_LL_ERROR) },
185 { MP_OBJ_NEW_QSTR(MP_QSTR_LOG_FATAL), MP_ROM_INT(AOS_LL_FATAL) },
186
187 };
188
189 STATIC MP_DEFINE_CONST_DICT(ulog_module_globals, ulog_module_globals_table);
190
191 const mp_obj_module_t ulog_module = {
192 .base = { &mp_type_module },
193 .globals = (mp_obj_dict_t *)&ulog_module_globals,
194 };
195