Linux下写日志

13 篇文章 0 订阅
#include <stdio.h>  
#include <stdlib.h>  
#include <time.h>  
#include <unistd.h>  
#include <assert.h>  
#include <string.h>  
#include <string>  
#include <fcntl.h>  
#include <stdarg.h>  
  
enum switch_mode  
{  
    mode_minute,  
    mode_hour,  
    mode_day,  
    mode_month  
};  
int file_fd = -1;  
int log_file(switch_mode mode = mode_day)  
{  
    char file_path[512] = {0};  
    char filetime[32] = {0};  
    struct tm tm_time;  
    time_t t_log;  
    std::string log_time = "";  
  
    assert(getcwd(file_path, 512) != NULL);    //当前目录  
    if (file_path[strlen(file_path) - 1] != '/') {  
        file_path[strlen(file_path)] = '/';  
    }  
    if(access(file_path, F_OK) != 0) {     //目录不存在  
        std::string build_path ="mkdir -p ";  
        build_path += file_path;  
        assert(system(build_path.c_str()) !=0 );  
    }  
  
    t_log = time(NULL);  
    localtime_r(&t_log, &tm_time);  
    strftime(filetime, sizeof(filetime), "%Y%m%d%H%M%S", &tm_time); //日志的时间  
    switch(mode) {  //日志存储模式  
    case mode_minute:  
        log_time.assign(filetime, 0, 12);  
        break;  
    case mode_hour:  
        log_time.assign(filetime, 0, 10);  
        break;  
    case mode_day:  
        log_time.assign(filetime, 0, 8);  
        break;  
    case mode_month:  
        log_time.assign(filetime, 0, 6);  
        break;  
    default:  
        log_time.assign(filetime, 0, 8);  
    }  
    strcat(file_path, "log_");  
    strcat(file_path, log_time.c_str());  
    strcat(file_path, ".log");  
  
    file_fd = open(file_path, O_RDWR|O_CREAT|O_APPEND, 0666);  
    assert(file_fd != -1);  
    return 0;  
}  
void write_cmd(const char *fmt,...)  
{  
    va_list ap;  
    va_start(ap,fmt);  
    vprintf(fmt,ap);  
    va_end(ap);  
}  
int write_log(const char *msg, ...)  
{  
    char final[2048] = {0};   //当前时间记录  
    va_list vl_list;  
    va_start(vl_list, msg);  
    char content[1024] = {0};  
    vsprintf(content, msg, vl_list);   //格式化处理msg到字符串  
    va_end(vl_list);  
  
    time_t  time_write;  
    struct tm tm_Log;  
    time_write = time(NULL);        //日志存储时间  
    localtime_r(&time_write, &tm_Log);  
    strftime(final, sizeof(final), "[%Y-%m-%d %H:%M:%S] ", &tm_Log);  
  
    strncat(final, content, strlen(content));  
    assert(msg != NULL && file_fd != -1);  
    assert( write(file_fd, final, strlen(final)) == strlen(final));  
    return 0;  
}  
void close_file()  
{  
    close(file_fd);  
}  

int main()  
{  
    log_file();  
    write_cmd("the address for cmd:%d\n", 100);  
    write_log("the address for log:%d\n", 200);  
    close_file();  
    return 0;  
}






  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值