树莓派 log 日志 打印到 TXT

#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
", ctime( &systemTimeNow ) );
        
        printf( "hello,world,%s
", ctime( &systemTimeNow ) );
        
        sleep( 1 );
    }
}

原文地址:https://www.cnblogs.com/suozhang/p/9407060.html