自定义打印函数/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
原创粉丝点击