Skip to content

Instantly share code, notes, and snippets.

@ykst
Created January 28, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ykst/64969c1b06678ee12eef to your computer and use it in GitHub Desktop.
Save ykst/64969c1b06678ee12eef to your computer and use it in GitHub Desktop.
ログに出すモジュール名の定義をMakefileで自動化する

ログに出すモジュール名の定義をMakefileで自動化する

ログツール等で__FILE____BASE_FILE__を渡してソース位置を表示する事をよくやると思いますが、 モジュールのようにもう少し大きい括りを渡してフィルタを楽にしたい時が有ります。

#ifndef MODULE_NAME
#   define MODULE_NAME "FOO"
#endif

#define ERR(fmt, ...) printf("ERR %s %s %d %s (" fmt ")\n", MODULE_NAME, __FILE__, __LINE__, __FUNC__, ##__VA_ARGS__)

void foobar() {
    ERR("status = %d", -1); // -> ERR FOO src/foo/bar.c 100 foobar (status = -1)
}

このMODULE_NAMEを個別に定義しはじめると面倒なので、 ビルドシステムがMakefileであれば、$(CC) -cに渡されるフラグを一つ追加します。

-DMODULE_NAME="\"$(shell basename $(dir $<) | tr a-z A-Z)\""

大抵はモジュール名 = ディレクトリ名で十分なので、問題ないでしょう。

参考

http://stackoverflow.com/questions/8487986/file-macro-shows-full-path

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment