自定义打印函数/log打印工具
来源:互联网 发布:淘宝api 源码下载 编辑:程序博客网 时间:2024/05/21 19:22
最近因为在写JNI库,需要打印log来调试,自家的平板倒是有MTKLogger,但是手上的手机就没有这个东东,所以要自己写文件打印log,于是研究了下自定义log打印。
示范如下:
平台ubuntu-x64
编译工具gcc
#include <stdio.h>#include <string.h>#include <stdlib.h>#include <unistd.h>#include <stdarg.h>#define LOGI(fmt, args...) myprintf(fmt, ##args)#define ATTR __attribute__static char sprint_buf[1024];static int myprintf(char *fmt, ...) ATTR((format(printf,1,2)));int myprintf(char *fmt, ...){ va_list args; int n; va_start(args, fmt); n = vsprintf(sprint_buf, fmt, args); va_end(args); write(1, sprint_buf, n); return n;}int main(void){ LOGI("%s-%d%d-%d%d.txt", "sdcard0", 1, 2, 3, 43); return 0;}
移植到JNI,其实差别不大:
平台Android 4.4.2
编译工具 ndk
utils.c
#include "utils.h"int log_fd = -1;int init_log(void){time_t Time1 = time(NULL);struct tm *tm1 = localtime(&Time1);uint8 log_file[128] = {0};sprintf(log_file, "%s%d%d-%d%d.txt", "/storage/sdcard0/Download/", tm1->tm_mon + 1, tm1->tm_mday, tm1->tm_hour, tm1->tm_min);LOGI("start %s", log_file);log_fd = open("/storage/sdcard0/Download/fuck3.txt", O_CREAT | O_RDWR, 0644);if(log_fd < 0){LOGI("ERR");return _ERR;}LOGI("end");return _OK;}void uninit_log(void){close(log_fd);}#define BUF_LEN(1024)#define ATTR __attribute__static char sprint_buf[BUF_LEN];static int myprintf(char *fmt, ...) ATTR((format(printf,1,2)));int log_printf(char *fmt, ...){ va_list args; int n; memset(sprint_buf, 0, BUF_LEN); va_start(args, fmt); n = vsprintf(sprint_buf, fmt, args); va_end(args); if(log_fd > 0 ) { write(log_fd, sprint_buf, n); } return n;}
utils.h
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/types.h>#include <asm/types.h>#include <sys/stat.h>#include <sys/time.h>#include <sys/resource.h>#include <sys/mman.h> //for map_shared#include <sys/socket.h>#include <sys/ioctl.h>#include <sys/select.h>#include <poll.h>#include <fcntl.h>#include <unistd.h>#include <pthread.h>#include <signal.h>#include <arpa/inet.h>#include <net/if.h> //for struct ifreq#include <netinet/in.h>#include <time.h>#include <errno.h>#include <termios.h>#include <assert.h>#include <stdint.h>#include <linux/videodev2.h>#include <jni.h>#include <android/log.h>#ifndef__COMMON_H_#define__COMMON_H_#define _OK(0)//function return: successful#define _ERR(-1)//function return: failedtypedef unsigned int uint32;typedef intint32;typedefunsigned char uint8;typedef charint8;#define TRUE1#define FALSE0static const char *UTAG = "ALCAR_CAMERA";int init_log(void);int log_printf(char *fmt, ...);void uninit_log(void);#define DEBUG#ifdefDEBUG#define LOGI(fmt, args...) __android_log_print(ANDROID_LOG_INFO, UTAG, fmt, ##args)#define LOGW(fmt, args...) __android_log_print(ANDROID_LOG_WARN, UTAG, fmt, ##args)#define LOGE(fmt, args...) __android_log_print(ANDROID_LOG_ERROR, UTAG, fmt, ##args)#else#define LOGI(fmt, args...)log_printf(fmt, ##args)#define LOGW(fmt, args...)log_printf(fmt, ##args)#define LOGE(fmt, args...)log_printf(fmt, ##args)#endif#defineERROR(cond, err, str)\if(cond)\{\LOGE(str);\goto err;\}#define FREE(ptr) \{ \ if(NULL != ptr) \ { \ free(ptr); \ ptr = NULL; \} \}#define CLOSE(fd)\{\ if(-1 != fd) \ { \ close(fd); \ fd = -1;\ } \}#endif /* end of file */
PS:
__android_log_print(ANDROID_LOG_INFO, UTAG, fmt, ##args)是MTK的官方log打印工具
0 0
- 自定义打印函数/log打印工具
- 自定义Android Log打印
- Android 自定义打印log
- android 自定义log打印
- Log打印工具类
- Log 打印工具类
- Swift 自定义打印, 自定义输出函数 ,自定义log, Debug
- 自定义Log、打印执行哪个函数、判断当前是什么输入法
- android 自定义打印log类
- 打印Log的工具类
- Log日志打印工具类
- eprint自定义打印工具
- 打印LOG
- 打印log
- Log打印
- log打印
- 打印 Log
- 打印Log
- Web 前端攻防
- 数据结构上机测试1:顺序表的应用
- 【拾遗】检测cin输入是否满足类型
- 李在福羽毛球教学经典视频汇总31集
- linux 硬盘使用情况查看
- 自定义打印函数/log打印工具
- Android网络层与数据层设计
- IOS-IAP 苹果支付
- jQuery页面交互
- Android Studio Gradle 导出JavaDocJar提示编码GBK的字符无法映射解决办法
- 捕获edittext 确认键
- Spring IOC三种注入方式(接口注入、setter注入、构造器注入)
- JSON toJSONString
- 云计算如何改变IT运维管理的未来