andorid 自定義log存入檔案
阿新 • • 發佈:2018-12-24
// // Created by darren on 17-10-9. // #include <stdio.h> #include <string.h> #include <stdlib.h> #include <unistd.h> #include <stdarg.h> #include <time.h> #include <sys/time.h> #include "5.log.h" #define PROCESSNAME "log.tmp" #define MAXBUFSIZE 1024 int get_current_dir(char*buf); int get_time(char *data); int file_copy(const char *in, const char *out); int main() { int a = 678; Log("tian %d", a); return 0; } int get_current_dir(char *buf) { int count; count = readlink("/proc/self/exe", buf, MAXBUFSIZE); if (count < 0 || count >= MAXBUFSIZE) { printf("Failed\n"); return -1; } buf[count] = '\0'; char *tmp = strrchr(buf, '/'); if (!tmp) { return -2; } buf[strlen(buf) - strlen(tmp)] = '\0'; return 0; } int get_time(char *data) { if (!data) { return -1; } time_t t; time(&t); struct tm local = {0}; localtime_r(&t, &local); sprintf(data,"%04d-%02d-%02d %02d:%02d:%02d", local.tm_year + 1900, local.tm_mon + 1, local.tm_mday, local.tm_hour, local.tm_min, local.tm_sec); return 0; } long int get_timeStemap() { struct timeval tv; gettimeofday(&tv, NULL); printf("second:%ld\n", tv.tv_sec); //秒 // printf("millisecond:%ld\n", tv.tv_sec * 1000 + tv.tv_usec / 1000); //毫秒 // printf("microsecond:%ld\n", tv.tv_sec * 1000000 + tv.tv_usec); //微秒 return tv.tv_sec * 1000 + tv.tv_usec / 1000; } int file_copy(const char *from, const char *to) { if (!from || !to) { return -1; } if (access(from, F_OK) != 0) { return -2; } FILE *fp = fopen(from, "r"); if (!fp) { return -3; } FILE *ft = fopen(to, "w+"); if (!ft) { return -4; } char tmp[1024]; size_t len; if (len = fread(tmp, 1, sizeof(tmp), fp)) { fwrite(tmp, 1, len, ft); } if (fp) { fclose(fp); } if (ft) { fclose(ft); } return 0; } int Log(const char *fm, ...) { int buflen = 5120; char buf[buflen]; int i = 0; memset(buf, 0, buflen); va_list args; va_start(args, fm); vsnprintf(buf, buflen, fm, args); va_end(args); char *logpath = (char *) malloc(MAXBUFSIZE); get_current_dir(logpath); if (access(logpath, 0) != 0) { char cmdstr[256] = {0}; sprintf(cmdstr, "mkdir -p %s", logpath); system(cmdstr); } char fname[256]; memset(fname, 0, sizeof(fname)); sprintf(fname, "%s/%s", logpath, PROCESSNAME); FILE *logfile = NULL; do { logfile = fopen(fname, "ab+"); if (logfile) break; } while (i < 3); if (!logfile) { return -2; } char *pTemp = (char *) malloc(MAXBUFSIZE); if (!pTemp) { return -2; } get_time(pTemp); char *dg = (char *) malloc(MAXBUFSIZE); if (!dg) { return -2; } sprintf(dg, "%s:%s\n", pTemp, buf); printf("%s\n", dg); fwrite(dg, 1, strlen(dg), logfile); //const char *dest = "/home/darren/ClionProjects/My_test/c++_test/effective_c++_test/log.11"; if (ftell(logfile) > 15 * 1024 * 1024) { // 15M ,判斷檔案大小 fclose(logfile); char path[1024]; sprintf(path, "%s/%ld", logpath, get_timeStemap()); printf("path:%s\n", path); if (!rename(fname, path)) { return -5; } } else { fclose(logfile); } //file_copy(fname, dest); if (pTemp) { free(pTemp); pTemp = NULL; } if (dg) { free(dg); dg = NULL; } if (logpath) { free(logpath); logpath = NULL; } return 0; }