Created
December 9, 2013 16:41
-
-
Save doi-t/7875514 to your computer and use it in GitHub Desktop.
エラーメッセージ関数のサンプル
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 <stdarg.h> | |
#include <string.h> | |
#include <errno.h> | |
#include <time.h> | |
#include <sys/types.h> | |
#include <unistd.h> | |
#define ERROR(fmt, ...) err_msg(__FILE__, __FUNCTION__, __LINE__, "error", fmt, ##__VA_ARGS__) | |
#define WARNNING(fmt, ...) err_msg(__FILE__, __FUNCTION__, __LINE__, "warnning", fmt, ##__VA_ARGS__) | |
void | |
err_msg(const char *file, const char *function, int line, const char *type, const char *fmt, ...) | |
{ | |
time_t timer; | |
time(&timer); | |
char *t = ctime(&timer); | |
t[strlen(t)-1] = '\0'; | |
fprintf(stderr, "%s ", t); /* 現在時刻を表示 */ | |
fprintf(stderr, "%d ", getpid()); /* プロセスIDを出力 */ | |
fprintf(stderr, "%s %s %d %s ", file, function, line, type); | |
va_list ap; | |
va_start(ap, fmt); | |
vfprintf(stderr, fmt, ap); /* fmtの書式に従って可変個数引数を順次呼び出す */ | |
va_end(ap); | |
fprintf(stderr, " %s(%d)", strerror(errno), errno); /* エラーメッセージを出力 */ | |
fputc('\n', stderr); | |
} | |
int | |
main(int argc, char **argv) | |
{ | |
const char msg[20] = "foo"; | |
ERROR("hoge"); | |
WARNNING("fuga:%s", msg); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment