|
Document of c-modernization-kit (util) 1.0.0
|
#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>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) |
| ファイルトレースプロバイダを終了する。 | |
| #define TRACE_FILE_LINE_BUF 1100 |
1 行分のスタックバッファサイズ。
Definition at line 21 of file file-provider.c.
Referenced by trace_file_provider_write().
| #define FILE_LOCK_TIMEOUT_MS 100 |
ファイル書き込みロック取得のタイムアウト (ミリ秒)。
Definition at line 24 of file file-provider.c.
Referenced by trace_file_provider_write().
| #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().
| #define TRACE_FILE_SUFFIX_MAX 5 |
ローテーションパスのサフィックス最大長 (".999\0" = 5 文字)。
Definition at line 30 of file file-provider.c.
Referenced by trace_file_provider_init().
| #define PATH_MAX 4096 |
Definition at line 37 of file file-provider.c.
| #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().
|
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().
|
static |
現在時刻を "YYYY-MM-DD HH:MM:SS.mmm" (UTC) 形式でバッファへ書き込む。
| buf | 書き込み先バッファ。 |
| buf_size | バッファサイズ (TRACE_FILE_TS_LEN + 1 以上を推奨)。 |
Definition at line 99 of file file-provider.c.
Referenced by trace_file_provider_write().
|
static |
ファイルを追記モードで開き current_bytes を初期サイズで初期化する。
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().
|
static |
ローテーション後の新規ファイルを空で作成して開く。 current_bytes は必ず 0 に設定される。
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().
|
static |
開いているファイルを閉じる。未開の場合は何もしない (冪等)。
Definition at line 230 of file file-provider.c.
References trace_file_provider::fd.
Referenced by rotate_file(), and trace_file_provider_dispose().
|
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().
| 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 を使用します。
| [in] | path | 出力ファイルパス。NULL の場合は NULL を返します。 |
| [in] | max_bytes | 1 ファイルあたりの最大バイト数。0 でデフォルト値を使用。 |
| [in] | generations | 保持する旧世代数。0 以下でデフォルト値を使用。 |
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().
| 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 を返します。
| [in] | handle | trace_file_provider_init の戻り値。NULL は無視。 |
| [in] | level | トレースレベル (TRACE_FILE_LV_* 定数)。 |
| [in] | message | null 終端 UTF-8 文字列。NULL は無視。 |
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().
| void TRACE_FILE_UTIL_API trace_file_provider_dispose | ( | trace_file_provider_t * | handle | ) |
ファイルトレースプロバイダを終了する。
ファイルハンドルを閉じ、ロック / mutex を解放してメモリを解放します。
ハンドルが NULL の場合は何もしません。
| [in] | handle | trace_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().