1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

C言語 printf文 マクロを用いたログ出力方法

Last updated at Posted at 2021-03-08

printf()などで使用できる便利なマクロメモ

| 項目| マクロ名 |概要|出力指定子|
|:-:|:-:|:-:|:-:|:-:|
| ファイル名 |__FILE__| どのファイルによって出力されたログなのかが特定できる | %s|
|行番号|__LINE__| ファイル内のどこで出力されたログなのかが特定できる | %d|
| 関数名 |__FUNCTION__
__func__| どの関数で出力されたログなのかが特定できる |%s|

↑printf()に使用できるプリプロセッサで標準定義されたマクロ

C
) printf("%s %d %s", __FILE__, __LINE__, __func__);

出力をマクロ関数化

C
//ログ出力用の関数マクロ定義
//...の内容は__VA_ARGS__(可変個引数)にコピーされる
# define DEBUG_PRINT(...)     printf("%s(%d) %s:", __FILE__, __LINE__, __func__), printf(__VA_ARGS__)

C
)DEBUG_PRITNT("ログ出力結果 %s \n", str);
1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?