Document of c-modernization-kit (util) 1.0.0
Loading...
Searching...
No Matches
file-provider.c File Reference
#include <trace-file-util.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <pthread.h>
#include <time.h>
#include <limits.h>
Include dependency graph for file-provider.c:

Go to the source code of this file.

Data Structures

struct  trace_file_provider
 ファイルトレースプロバイダハンドル構造体 (内部定義)。 More...

Macros

#define TRACE_FILE_LINE_BUF   1100
 1 行分のスタックバッファサイズ。
#define FILE_LOCK_TIMEOUT_MS   100
 ファイル書き込みロック取得のタイムアウト (ミリ秒)。
#define TRACE_FILE_TS_LEN   23
 タイムスタンプ部分の文字数 ("YYYY-MM-DD HH:MM:SS.mmm" = 23 文字)。
#define TRACE_FILE_SUFFIX_MAX   5
 ローテーションパスのサフィックス最大長 (".999\0" = 5 文字)。
#define PATH_MAX   4096
#define TRACE_FILE_PATH_MAX   PATH_MAX

Functions

static char level_char (int level)
 トレースレベル整数をレベル文字に変換する。
static void format_timestamp (char *buf, int buf_size)
 現在時刻を "YYYY-MM-DD HH:MM:SS.mmm" (UTC) 形式でバッファへ書き込む。
static int open_file (trace_file_provider_t *p)
 ファイルを追記モードで開き current_bytes を初期サイズで初期化する。
static int open_file_truncate (trace_file_provider_t *p)
 ローテーション後の新規ファイルを空で作成して開く。 current_bytes は必ず 0 に設定される。
static void close_file (trace_file_provider_t *p)
 開いているファイルを閉じる。未開の場合は何もしない (冪等)。
static void rotate_file (trace_file_provider_t *p)
 トレースファイルをローテーションする。
trace_file_provider_t *TRACE_FILE_UTIL_API trace_file_provider_init (const char *path, size_t max_bytes, int generations)
 ファイルトレースプロバイダを初期化する。
int TRACE_FILE_UTIL_API trace_file_provider_write (trace_file_provider_t *handle, int level, const char *message)
 ファイルへトレースメッセージを書き込む。
void TRACE_FILE_UTIL_API trace_file_provider_dispose (trace_file_provider_t *handle)
 ファイルトレースプロバイダを終了する。

Macro Definition Documentation

◆ TRACE_FILE_LINE_BUF

#define TRACE_FILE_LINE_BUF   1100

1 行分のスタックバッファサイズ。

Definition at line 21 of file file-provider.c.

Referenced by trace_file_provider_write().

◆ FILE_LOCK_TIMEOUT_MS

#define FILE_LOCK_TIMEOUT_MS   100

ファイル書き込みロック取得のタイムアウト (ミリ秒)。

Definition at line 24 of file file-provider.c.

Referenced by trace_file_provider_write().

◆ TRACE_FILE_TS_LEN

#define TRACE_FILE_TS_LEN   23

タイムスタンプ部分の文字数 ("YYYY-MM-DD HH:MM:SS.mmm" = 23 文字)。

Definition at line 27 of file file-provider.c.

Referenced by trace_file_provider_write().

◆ TRACE_FILE_SUFFIX_MAX

#define TRACE_FILE_SUFFIX_MAX   5

ローテーションパスのサフィックス最大長 (".999\0" = 5 文字)。

Definition at line 30 of file file-provider.c.

Referenced by trace_file_provider_init().

◆ PATH_MAX

#define PATH_MAX   4096

Definition at line 37 of file file-provider.c.

◆ TRACE_FILE_PATH_MAX

#define TRACE_FILE_PATH_MAX   PATH_MAX

Definition at line 39 of file file-provider.c.

Referenced by rotate_file(), and trace_file_provider_init().

Function Documentation

◆ level_char()

char level_char ( int level)
static

トレースレベル整数をレベル文字に変換する。

Definition at line 82 of file file-provider.c.

References TRACE_FILE_LV_CRITICAL, TRACE_FILE_LV_ERROR, TRACE_FILE_LV_INFO, and TRACE_FILE_LV_WARNING.

Referenced by trace_file_provider_write().

Here is the caller graph for this function:

◆ format_timestamp()

void format_timestamp ( char * buf,
int buf_size )
static

現在時刻を "YYYY-MM-DD HH:MM:SS.mmm" (UTC) 形式でバッファへ書き込む。

Parameters
buf書き込み先バッファ。
buf_sizeバッファサイズ (TRACE_FILE_TS_LEN + 1 以上を推奨)。

Definition at line 99 of file file-provider.c.

Referenced by trace_file_provider_write().

Here is the caller graph for this function:

◆ open_file()

int open_file ( trace_file_provider_t * p)
static

ファイルを追記モードで開き current_bytes を初期サイズで初期化する。

Returns
成功 0 / 失敗 -1。

Definition at line 133 of file file-provider.c.

References trace_file_provider::current_bytes, trace_file_provider::fd, and trace_file_provider::path.

Referenced by trace_file_provider_init().

Here is the caller graph for this function:

◆ open_file_truncate()

int open_file_truncate ( trace_file_provider_t * p)
static

ローテーション後の新規ファイルを空で作成して開く。 current_bytes は必ず 0 に設定される。

Returns
成功 0 / 失敗 -1。

Definition at line 202 of file file-provider.c.

References trace_file_provider::current_bytes, trace_file_provider::fd, and trace_file_provider::path.

Referenced by rotate_file().

Here is the caller graph for this function:

◆ close_file()

void close_file ( trace_file_provider_t * p)
static

開いているファイルを閉じる。未開の場合は何もしない (冪等)。

Definition at line 230 of file file-provider.c.

References trace_file_provider::fd.

Referenced by rotate_file(), and trace_file_provider_dispose().

Here is the caller graph for this function:

◆ rotate_file()

void rotate_file ( trace_file_provider_t * p)
static

トレースファイルをローテーションする。

ロック保持中から呼ばれる。
リネームに失敗した場合はその世代でカスケードを打ち切り、 呼び出し元をブロックせずに続行する (ベストエフォート)。

Definition at line 253 of file file-provider.c.

References close_file(), trace_file_provider::generations, open_file_truncate(), trace_file_provider::path, and TRACE_FILE_PATH_MAX.

Referenced by trace_file_provider_write().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ trace_file_provider_init()

trace_file_provider_t *TRACE_FILE_UTIL_API trace_file_provider_init ( const char * path,
size_t max_bytes,
int generations )

ファイルトレースプロバイダを初期化する。

指定されたファイルパスへの書き込みを開始します。
ファイルが存在する場合は追記します。存在しない場合は新規作成します。
max_bytes に 0 を指定した場合は TRACE_FILE_DEFAULT_MAX_BYTES を使用します。
generations に 0 以下を指定した場合は TRACE_FILE_DEFAULT_GENERATIONS を使用します。

ローテーション動作
書き込み後にファイルサイズが max_bytes に達すると、以下の名前でファイルを保持します。
path ... 現在のトレースファイル (新規作成)
path.1 ... 直前の世代
path.2 ... 2 世代前
path.N ... N 世代前 (N = generations)
generations 世代を超えた古いファイルは削除されます。
Parameters
[in]path出力ファイルパス。NULL の場合は NULL を返します。
[in]max_bytes1 ファイルあたりの最大バイト数。0 でデフォルト値を使用。
[in]generations保持する旧世代数。0 以下でデフォルト値を使用。
Returns
成功時: ハンドル。失敗時: NULL。
使用例
"C:\\logs\\myapp.log", 0, 0);
TRACE_FILE_UTIL_EXPORT trace_file_provider_t *TRACE_FILE_UTIL_API trace_file_provider_init(const char *path, size_t max_bytes, int generations)
ファイルトレースプロバイダを初期化する。
struct trace_file_provider trace_file_provider_t
ファイルトレースプロバイダハンドル (不透明型)。

Definition at line 309 of file file-provider.c.

References trace_file_provider::current_bytes, trace_file_provider::fd, trace_file_provider::generations, trace_file_provider::max_bytes, trace_file_provider::mutex, trace_file_provider::mutex_initialized, open_file(), trace_file_provider::path, TRACE_FILE_DEFAULT_GENERATIONS, TRACE_FILE_DEFAULT_MAX_BYTES, TRACE_FILE_PATH_MAX, and TRACE_FILE_SUFFIX_MAX.

Referenced by trace_modify_filetrc().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ trace_file_provider_write()

int TRACE_FILE_UTIL_API trace_file_provider_write ( trace_file_provider_t * handle,
int level,
const char * message )

ファイルへトレースメッセージを書き込む。

タイムスタンプとトレースレベル文字を付加し 1 行で書き込みます。
書き込み後にファイルサイズが max_bytes に達した場合はローテーションします。
handle または message が NULL の場合は何もせず 0 を返します。
ファイルがロックされている等の I/O 失敗は呼び出し元をブロックせず -1 を返します。

Parameters
[in]handletrace_file_provider_init の戻り値。NULL は無視。
[in]levelトレースレベル (TRACE_FILE_LV_* 定数)。
[in]messagenull 終端 UTF-8 文字列。NULL は無視。
Returns
成功 0 / 失敗 -1。

Definition at line 387 of file file-provider.c.

References trace_file_provider::current_bytes, trace_file_provider::fd, FILE_LOCK_TIMEOUT_MS, format_timestamp(), level_char(), trace_file_provider::max_bytes, trace_file_provider::mutex, rotate_file(), TRACE_FILE_LINE_BUF, and TRACE_FILE_TS_LEN.

Referenced by write_dual().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ trace_file_provider_dispose()

void TRACE_FILE_UTIL_API trace_file_provider_dispose ( trace_file_provider_t * handle)

ファイルトレースプロバイダを終了する。

ファイルハンドルを閉じ、ロック / mutex を解放してメモリを解放します。
ハンドルが NULL の場合は何もしません。

Parameters
[in]handletrace_file_provider_init の戻り値。

Definition at line 508 of file file-provider.c.

References close_file(), trace_file_provider::mutex, trace_file_provider::mutex_initialized, and trace_file_provider::path.

Referenced by trace_dispose(), and trace_modify_filetrc().

Here is the call graph for this function:
Here is the caller graph for this function: