树莓派 log 日志 打印到 TXT

本文介绍了一个简单的日志记录程序实现,该程序利用 C 语言标准库与时间处理函数,实现了向指定文件中记录带有时间戳的日志信息的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<stdio.h>

#include <stdarg.h>

#include <unistd.h>

#include <stdint.h>

#include <stdarg.h>

#include <fcntl.h>

#include <time.h>

int vAppLogPrintf( uint8_t *buff, uint32_t len )
{
        
    int f_log;
    
    f_log = open( "log.txt", O_CREAT|O_RDWR, 0755 );

    if( f_log < 0 )
    {
        return -1;
    } 

    //将文件写入指针 移动到文件结尾
    lseek( f_log, 0, SEEK_END );
    
    //将消息缓冲区 写入到文件
    write( f_log, buff, len );
    
    close( f_log );
    
    return 0;

}

int appLogPrintf(const char *fmt, ...)
{
    va_list args;
    int r,printed_len;
    static char printk_buf[1024];

    va_start(args, fmt);
    /* Emit the output into the temporary buffer */
    printed_len = vsnprintf(printk_buf, sizeof(printk_buf), fmt, args);
    va_end(args);

    return vAppLogPrintf( printk_buf, printed_len );

}

int main( int argc, char **argv )
{
    time_t systemTimeNow;
    
    for( ;; )
    {
            
        systemTimeNow = time( NULL );/* 获取当前系统时间 */
    
        appLogPrintf( "hello,world,%s\n", ctime( &systemTimeNow ) );
        
        printf( "hello,world,%s\n", ctime( &systemTimeNow ) );
        
        sleep( 1 );
    }
}

 

 

转载于:https://www.cnblogs.com/suozhang/p/9407060.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值