Created
May 20, 2013 14:03
-
-
Save poohtaro/5612430 to your computer and use it in GitHub Desktop.
Mac OS Xでsyslogにデバッグログを出力する。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <syslog.h> | |
#include <asl.h> | |
int main(int argc, const char * argv[]) | |
{ | |
// BSD API によるデバッグログ出力 | |
openlog(NULL, LOG_PID|LOG_CONS|LOG_NDELAY|LOG_PERROR, LOG_USER); | |
setlogmask(LOG_UPTO(LOG_DEBUG)); | |
syslog(LOG_DEBUG, "Hello, BSD API!"); | |
closelog(); | |
// 同じことを ASL API で | |
aslclient asl = asl_open(NULL, NULL, ASL_OPT_STDERR|ASL_OPT_NO_DELAY | |
|ASL_OPT_NO_REMOTE); | |
asl_set_filter(asl, ASL_FILTER_MASK_UPTO(ASL_LEVEL_DEBUG)); | |
asl_log(asl, NULL, ASL_LEVEL_DEBUG, "Hello, ASL API!"); | |
asl_close(asl); | |
// オープン/クローズは必須ではない。 | |
// stderrへの同時出力など、オプション設定が必要なければ、 | |
// syslog(), asl_log() の呼び出しだけでもよい。 | |
syslog(LOG_NOTICE, "LOG_NOTICE(BSD API)"); | |
asl_log(NULL, NULL, ASL_LEVEL_NOTICE, "ASL_LEVEL_NOTICE(ASL API)"); | |
return 0; | |
} | |
/* | |
プログラム名(openの引数ident)が仮に「hoge」であるとして、プログラムhogeのデバッグログ出力を、 | |
/var/log/hoge.log ファイルに書き込みたい場合、 | |
/etc/asl.conf に、 | |
? [= Sender hoge] file /var/log/hoge.log | |
のように記述しておく。 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment