linux下c/c++日志记录及文件读取

13 篇文章 0 订阅

1、日志记录

(1)代码writelog.cpp

[html]  view plain  copy
  1. /*日志记录*/  
  2. #include <stdio.h>  
  3. #include <stdlib.h>  
  4. #include <time.h>  
  5. #include <unistd.h>  
  6. #include <assert.h>  
  7. #include <string.h>  
  8. #include <string>  
  9. #include <fcntl.h>  
  10. #include <stdarg.h>  
  11.   
  12. enum switch_mode  
  13. {  
  14.     mode_minute,  
  15.     mode_hour,  
  16.     mode_day,  
  17.     mode_month  
  18. };  
  19. int file_fd = -1;  
  20. int log_file(switch_mode mode = mode_day)  
  21. {  
  22.     char file_path[512] = {0};  
  23.     char filetime[32] = {0};  
  24.     struct tm tm_time;  
  25.     time_t t_log;  
  26.     std::string log_time = "";  
  27.   
  28.     assert(getcwd(file_path, 512) != NULL);    //当前目录  
  29.     if (file_path[strlen(file_path) - 1] != '/') {  
  30.         file_path[strlen(file_path)] = '/';  
  31.     }  
  32.     if(access(file_path, F_OK) != 0) {     //目录不存在  
  33.         std::string build_path ="mkdir -p ";  
  34.         build_path += file_path;  
  35.         assert(system(build_path.c_str()) !=0 );  
  36.     }  
  37.   
  38.     t_log = time(NULL);  
  39.     localtime_r(&t_log, &tm_time);  
  40.     strftime(filetime, sizeof(filetime), "%Y%m%d%H%M%S", &tm_time); //日志的时间  
  41.     switch(mode) {  //日志存储模式  
  42.     case mode_minute:  
  43.         log_time.assign(filetime, 0, 12);  
  44.         break;  
  45.     case mode_hour:  
  46.         log_time.assign(filetime, 0, 10);  
  47.         break;  
  48.     case mode_day:  
  49.         log_time.assign(filetime, 0, 8);  
  50.         break;  
  51.     case mode_month:  
  52.         log_time.assign(filetime, 0, 6);  
  53.         break;  
  54.     default:  
  55.         log_time.assign(filetime, 0, 8);  
  56.     }  
  57.     strcat(file_path, "log_");  
  58.     strcat(file_path, log_time.c_str());  
  59.     strcat(file_path, ".log");  
  60.   
  61.     file_fd = open(file_path, O_RDWR|O_CREAT|O_APPEND, 0666);  
  62.     assert(file_fd != -1);  
  63.     return 0;  
  64. }  
  65. void write_cmd(const char *fmt,...)  
  66. {  
  67.     va_list ap;  
  68.     va_start(ap,fmt);  
  69.     vprintf(fmt,ap);  
  70.     va_end(ap);  
  71. }  
  72. int write_log(const char *msg, ...)  
  73. {  
  74.     char final[2048] = {0};   //当前时间记录  
  75.     va_list vl_list;  
  76.     va_start(vl_list, msg);  
  77.     char content[1024] = {0};  
  78.     vsprintf(content, msg, vl_list);   //格式化处理msg到字符串  
  79.     va_end(vl_list);  
  80.   
  81.     time_t  time_write;  
  82.     struct tm tm_Log;  
  83.     time_write = time(NULL);        //日志存储时间  
  84.     localtime_r(&time_write, &tm_Log);  
  85.     strftime(final, sizeof(final), "[%Y-%m-%d %H:%M:%S] ", &tm_Log);  
  86.   
  87.     strncat(final, content, strlen(content));  
  88.     assert(msg != NULL && file_fd != -1);  
  89.     assert( write(file_fd, final, strlen(final)) == strlen(final));  
  90.     return 0;  
  91. }  
  92. void close_file()  
  93. {  
  94.     close(file_fd);  
  95. }  
  96. /******************日志记录测试******************/  
  97. int main()  
  98. {  
  99.     log_file();  
  100.     write_cmd("the address for cmd:%d\n", 100);  
  101.     write_log("the address for log:%d\n", 200);  
  102.     close_file();  
  103.     return 0;  
  104. }  

(2)编译运行

[html]  view plain  copy
  1. g++ -o writelog writelog.cpp  
运行控制台显示:

文件log_20151105.log内容:

2、文件读取

(1)代码readdata.c
[html]  view plain  copy
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <assert.h>  
  4. int read_file(char *file_name)  
  5. {  
  6.     char *buff;  
  7.     FILE *fp = fopen(file_name, "r+");  
  8.     assert(fp);  
  9.     int flag = fseek(fp, 0, SEEK_END);  
  10.     assert(flag == 0);  
  11.     int len = ftell(fp);  
  12.     buff = (char *)malloc(sizeof(char) * (len + 1));  
  13.     flag = fseek(fp, 0, SEEK_SET);  
  14.     assert(flag == 0);  
  15.   
  16.     int num = fread(buff, 1, len + 1, fp);  
  17.     assert(num == len);  
  18.   
  19.     printf("len:%d, num:%d, buff:%s", len, num, buff);  
  20.     free(buff);  
  21.     buff = NULL;  
  22.     fclose(fp);  
  23.     return 0;  
  24. }  
  25. int read_file_p(char *file_name)  
  26. {  
  27.     char buff[1024] = {0};  
  28.     FILE *fp = fopen(file_name, "r+");  
  29.     assert(fp);  
  30.     int ch;  
  31.     int i = 0;  
  32.     do {  
  33.         ch = fgetc(fp);  
  34.         buff[i++] = ch;  
  35.     } while(ch != EOF);  
  36.     buff[i - 2] = '\0';  
  37.     fclose(fp);  
  38.   
  39.     printf("buff:%s\n", buff);  
  40.     return 0;  
  41. }  
  42. int main(int argc, char *argv[])  
  43. {  
  44.     assert(argc == 2);  
  45.     read_file(argv[1]);  
  46.     read_file_p(argv[1]);  
  47.     return 0;  
  48. }  
(2)编译运行
[html]  view plain  copy
  1. gcc -o readdata readdata.c  
  2. ./readdata log_20151105.log  
  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值