1 /* 2 * (C) COPYRIGHT RockChip Limited. All rights reserved. 3 * 4 * This program is free software and is provided to you under the terms of the 5 * GNU General Public License version 2 as published by the Free Software 6 * Foundation, and any use by you of this program is subject to the terms 7 * of such GNU licence. 8 */ 9 10 #ifndef __CUSTOM_LOG_H__ 11 #define __CUSTOM_LOG_H__ 12 13 #ifdef __cplusplus 14 extern "C" { 15 #endif 16 17 /* ----------------------------------------------------------------------------- 18 * Include Files 19 * ----------------------------------------------------------------------------- 20 */ 21 #include <linux/kernel.h> 22 #include <linux/printk.h> 23 24 /* ----------------------------------------------------------------------------- 25 * Macros Definition 26 * ----------------------------------------------------------------------------- 27 */ 28 29 /** 若下列 macro 有被定义, 才 使能 log 输出. */ 30 /* #define ENABLE_DEBUG_LOG */ 31 32 /*----------------------------------------------------------------------------*/ 33 34 #ifdef ENABLE_VERBOSE_LOG 35 /** Verbose log. */ 36 #define V(fmt, args...) \ 37 pr_debug("V : [File] : %s; [Line] : %d; [Func] : %s(); " fmt \ 38 "\n", \ 39 __FILE__, \ 40 __LINE__, \ 41 __func__, \ 42 ## args) 43 #else 44 #define V(...) ((void)0) 45 #endif 46 47 #ifdef ENABLE_DEBUG_LOG 48 /** Debug log. */ 49 #define D(fmt, args...) \ 50 pr_info("D : [File] : %s; [Line] : %d; [Func] : %s(); " fmt \ 51 "\n", \ 52 __FILE__, \ 53 __LINE__, \ 54 __func__, \ 55 ## args) 56 #else 57 #define D(...) ((void)0) 58 #endif 59 60 #define I(fmt, args...) \ 61 pr_info("I : [File] : %s; [Line] : %d; [Func] : %s(); " fmt \ 62 "\n", \ 63 __FILE__, \ 64 __LINE__, \ 65 __func__, \ 66 ## args) 67 68 #define W(fmt, args...) \ 69 pr_warn("W : [File] : %s; [Line] : %d; [Func] : %s(); " \ 70 fmt "\n", \ 71 __FILE__, \ 72 __LINE__, \ 73 __func__, \ 74 ## args) 75 76 #define E(fmt, args...) \ 77 pr_err("E : [File] : %s; [Line] : %d; [Func] : %s(); " fmt \ 78 "\n", \ 79 __FILE__, \ 80 __LINE__, \ 81 __func__, \ 82 ## args) 83 84 /*-------------------------------------------------------*/ 85 86 /** 使用 D(), 以十进制的形式打印变量 'var' 的 value. */ 87 #define D_DEC(var) D(#var " = %d.", var) 88 89 #define E_DEC(var) E(#var " = %d.", var) 90 91 /** 使用 D(), 以十六进制的形式打印变量 'var' 的 value. */ 92 #define D_HEX(var) D(#var " = 0x%x.", var) 93 94 #define E_HEX(var) E(#var " = 0x%x.", var) 95 96 /** 97 * 使用 D(), 以十六进制的形式, 98 * 打印指针类型变量 'ptr' 的 value. 99 */ 100 #define D_PTR(ptr) D(#ptr " = %p.", ptr) 101 102 #define E_PTR(ptr) E(#ptr " = %p.", ptr) 103 104 /** 使用 D(), 打印 char 字串. */ 105 #define D_STR(p_str) \ 106 do { \ 107 if (!p_str) { \ 108 D(#p_str " = NULL."); \ 109 else \ 110 D(#p_str " = '%s'.", p_str); \ 111 } while (0) 112 113 #define E_STR(p_str) \ 114 do { \ 115 if (!p_str) \ 116 E(#p_str " = NULL."); \ 117 else \ 118 E(#p_str " = '%s'.", p_str); \ 119 } while (0) 120 121 #ifdef ENABLE_DEBUG_LOG 122 /** 123 * log 从 'p_start' 地址开始的 'len' 个字节的数据. 124 */ 125 #define D_MEM(p_start, len) \ 126 do { \ 127 int i = 0; \ 128 char *p = (char *)(p_start); \ 129 D("dump memory from addr of '" #p_start "', from %p, length %d' : ", \ 130 (p_start), \ 131 (len)); \ 132 pr_debug("\t\t"); \ 133 for (i = 0; i < (len); i++) \ 134 pr_debug("0x%02x, ", p[i]); \ 135 pr_debug("\n"); \ 136 } while (0) 137 #else 138 #define D_MEM(...) ((void)0) 139 #endif 140 141 /*-------------------------------------------------------*/ 142 143 /** 144 * 在特定条件下, 判定 error 发生, 145 * 将变量 'ret_var' 设置 'err_code', 146 * log 输出对应的 Error Caution, 147 * 然后跳转 'label' 指定的代码处执行. 148 * @param msg 149 * 纯字串形式的提示信息. 150 * @param ret_var 151 * 标识函数执行状态或者结果的变量, 152 * 将被设置具体的 Error Code. 153 * 通常是 'ret' or 'result'. 154 * @param err_code 155 * 表征特定 error 的常数标识, 156 * 通常是 宏的形态. 157 * @param label 158 * 程序将要跳转到的错误处理代码的标号, 159 * 通常就是 'EXIT'. 160 * @param args... 161 * 对应 'msg_fmt' 实参中, 162 * '%s', '%d', ... 等转换说明符的具体可变长实参. 163 */ 164 #define SET_ERROR_AND_JUMP(msg_fmt, ret_var, err_code, label, args...) \ 165 do { \ 166 E("To set '" #ret_var "' to %d('" #err_code "'), because : " msg_fmt, \ 167 (err_code), \ 168 ## args); \ 169 (ret_var) = (err_code); \ 170 goto label; \ 171 } while (0) 172 173 /* ----------------------------------------------------------------------------- 174 * Types and Structures Definition 175 * ----------------------------------------------------------------------------- 176 */ 177 178 /* ----------------------------------------------------------------------------- 179 * Global Functions' Prototype 180 * ----------------------------------------------------------------------------- 181 */ 182 183 /* ----------------------------------------------------------------------------- 184 * Inline Functions Implementation 185 * ----------------------------------------------------------------------------- 186 */ 187 188 #ifdef __cplusplus 189 } 190 #endif 191 192 #endif /* __CUSTOM_LOG_H__ */ 193