写一个打印日志的函数

今天为了研究PHP内核,为了查看PHP工作流程,自己写了一个打印日志的函数,这里主要介绍C打印日志函数

直接上码 [gcc file.c编译,然后会产生一个a.out的文件,./a.out允许一下,会生成test.txt日志文件]

#include <stdio.h>
#include <string.h>
#include <time.h>

void save_log();

int main()
{
    char str[80] = "PUSH LOG";
    save_log(str);
}

void save_log(char *str)
{
    time_t rawtime;
    struct tm * timeinfo;
    time ( &rawtime );
    timeinfo = localtime ( &rawtime );

    char buf[512];
    sprintf(buf, "%s", asctime (timeinfo));

    
    FILE *fp = fopen("test.txt", "a+");

    if (fp==0) {
        printf("can't open file
");
        return;
    }

     strcat(str,"
");

    fseek(fp, 0,SEEK_END);
    fwrite(buf, strlen(buf) - 1, 1, fp);
    fwrite("    ", 4, 1, fp);
    fwrite(str, strlen(str) * sizeof(char), 1, fp);
    fclose(fp);
    return;
}
原文地址:https://www.cnblogs.com/xiaozong/p/5820767.html