今天写了个log函数,如下(定义在logger.c中, logger.h中有对应的声明)
void Logger(int priority, int errno, char* fmt, ...)
{
char priVc[][8] = {"Emerg", "Alert", "Crit", "Error", "Warning", "Notice", "Info", "Debug"};
char* priPt = priority < 0 || priority >= sizeof(priVc)/sizeof(priVc[0]) ?
"Unknow priority!" : priVc[priority];
char *errMsg = errno <= 0 ? NULL : strerror(errno);
{
va_list argPt;
unsigned Ln;
va_start(argPt, fmt); //now argPt is point to mylog's param:...
Ln = snprintf(LogLastMsg, sizeof(LogLastMsg), "[mylog...][%s]: ", priPt);
Ln += vsnprintf(LogLastMsg + Ln, sizeof(LogLastMsg) - Ln, fmt, argPt);
if (NULL != errMsg)
{
Ln += snprintf(LogLastMsg + Ln, sizeof(LogLastMsg) - Ln, "%d:%s", errno, errMsg);
}
va_end(argPt);
}
//choose the log which should be show on stderr
if (priority < LOG_ERR || priority <= Log2Stderr)
{
fprintf(stderr, "%s", LogLastMsg);
}
//always to syslog
syslog(priority, "%s", LogLastMsg);
if (priority <= LOG_ERR)
{
exit(-1);
}
return ;
}
然后在main函数中调用:
Logger(LOG_INFO, 3, "httpd start...\n");
接着就报错了
expected ‘int * (*)()’ but argument is of type ‘int’
把logger.c logger.h 以及main.c中调用的Logger都替换成my_log,也没有用。int *(*)()
应该是函数指针把,Logger的第二个参数怎么回事函数指针呢,好奇怪。
找到原因了。
errno 在errno.h中被定义成宏了。 不能做形式参数,否则会被变成int ()型