如何用C语言生成日志文件?

时间:2017-04-04 11:58:42

标签: c

我创建了一个C程序,用一个函数登录到一个文件,但是没有创建日志文件。

我的日志文件程序就像......

int foo_handler( request_rec* inpRequest ) {
    int nReturnVal = DECLINED;

    if ( inpRequest->handler != NULL && strcmp( inpRequest->handler, "foo" ) == 0 )
    {
        ap_rputs( "Hello World from FOO", inpRequest );
        nReturnVal = OK;
    }

    return nReturnVal; }

void foo_hooks( apr_pool_t* inpPool ) {
    ap_hook_handler( foo_handler, NULL, NULL, APR_HOOK_MIDDLE ); }


module AP_MODULE_DECLARE_DATA foo_module = {
    STANDARD20_MODULE_STUFF,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    foo_hooks };

1 个答案:

答案 0 :(得分:2)

老实说,我不确定你的程序应该做什么。如果你包含整个内容可能会有所帮助,这样我们就可以看到你是如何通过main调用你的函数的。无论如何,如果你想要一个日志文件,最简单的方法就是输出到一个文本文件。

#include <stdio.h>
#include <stdlib.h>



int main()
{
    // Open a file pointer named "log.txt" for writing (w+)
    // If you google c file i/o you'll find all the specifiers for
    // writing, reading, writing and reading, etc. I just chose
    // only writing here as an example
    FILE* fp;
    fp = fopen("log.txt", "w");

    // Generate whatever you want logged here, "data" is just an example
    char* data = "The data to be logged...";

    // This lines writes the info in "data" to the file pointer specified
    fputs(data, fp);

    // Always remember to close your files
    fclose(fp);

    return EXIT_SUCCESS;
}