LoginSignup
13
16

More than 5 years have passed since last update.

【C言語】ログはファイルに出力したい

Posted at

大きな開発になればなるほどログって本当に大事。
超簡単にだけどログ出力するソースどーん。

【ソース】
※定義とか出力用関数とかはヘッダや別ファイルに
分けたほうがより良い。今回はめんどいので一つに。
※LOGディレクトリは作成済みの前提。

test.c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

#define TEST_OK  0     /* テスト関数戻り値(正常)*/
#define TEST_NG -1     /* テスト関数戻り値(異常)*/
#define LOG_FILE     "LOG/log.txt"        /* ログディレクトリ(通常)  */
#define ERR_LOG_FILE "LOG/err_log.txt"    /* ログディレクトリ(エラー)*/

FILE *log_file;        /* 通常ログ */
FILE *err_log_file;    /* 異常ログ */

/* 関数プロトタイプ宣言 */
int test_function(int num);                    /* テスト関数        */
void LOG_PRINT(char log_txt[256], ...);        /* 通常ログ出力関数  */
void ERR_LOG_PRINT(char err_txt[256], ...);    /* エラーログ出力関数*/

int main(void)
{

        int ret = TEST_OK;
        int num = 0;

        LOG_PRINT(">>> TEST START !!! \n");

        /* テスト関数起動 */
        ret = test_function(num);
        if(ret != TEST_OK)
        {
                ERR_LOG_PRINT("test_function() NG!\n");
                LOG_PRINT("<<< TEST NG END !!!\n");
                return TEST_NG;
        }

        LOG_PRINT("<<< TEST END !!!\n");

        return TEST_OK;
}

int test_function(int num)
{

        int ret = TEST_OK;
        LOG_PRINT(">>>>>> test_function() START !!! \n");

        if(num <= 0)
        {
                ret = TEST_NG;
        }

        LOG_PRINT("<<<<<< test_func() END !!!\n");
        return ret;

}

void LOG_PRINT(char log_txt[256], ...)
{

        time_t timer;
        struct tm *date;
        char str[256];

        /* 時間取得 */
        timer = time(NULL);
        date = localtime(&timer);
        strftime(str, sizeof(str), "[%Y/%x %H:%M:%S] ", date);

        if ((log_file = fopen(LOG_FILE, "a")) == NULL) {
                ERR_LOG_PRINT("file open error!!\n");
                exit(EXIT_FAILURE);        /* エラーの場合は通常、異常終了する */
        }

        /* 文字列結合 */
        strcat(str,log_txt);

        fputs(str, log_file);
        fclose(log_file); 

        return;

}

void ERR_LOG_PRINT(char err_txt[256], ...)
{
        time_t timer;
        struct tm *date;
        char str[256];

        /* 時間取得 */
        timer = time(NULL);
        date = localtime(&timer);
        strftime(str, sizeof(str), "[%Y/%x %H:%M:%S] ", date);

        if ((err_log_file = fopen("LOG/err_log.txt", "a")) == NULL) {
                printf("ERROR !!\n");
                exit(EXIT_FAILURE);        /* エラーの場合は通常、異常終了する */
        }

        /* 文字列結合 */
        strcat(str,err_txt);

        fputs(str, err_log_file);
        fclose(err_log_file); 

        return;

}

【結果】

LOG/log.txt
[2017/05/05/17 00:05:00] >>> TEST START !!! 
[2017/05/05/17 00:05:00] >>>>>> test_function() START !!! 
[2017/05/05/17 00:05:00] <<<<<< test_func() END !!!
[2017/05/05/17 00:05:00] <<< TEST NG END !!!
LOG/err_log.txt
[2017/05/05/17 00:05:00] test_function() NG!
13
16
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
16