improved WHY() macros to not show build path in messages.

This commit is contained in:
gardners 2012-05-22 09:54:10 +09:30
parent 62b400fe23
commit e3a61f7614
2 changed files with 27 additions and 7 deletions

18
log.c
View File

@ -65,6 +65,24 @@ void vlogMessage(int level, char *fmt, va_list ap)
fprintf(stderr, "%s: %s\n", levelstr, buf);
}
int build_path_len=-1;
char *trimbuildpath(char *s)
{
if (build_path_len==-1) {
/* Find common path prefix so that we can get rid of the build path
when reporting file names in logs */
int lastSlash=0;
int i=0;
for(i=0;i<strlen(__FILE__)&&i<strlen(s);i++)
if (__FILE__[i]!=s[i]) break;
else if (s[i]=='/') lastSlash=i;
build_path_len=lastSlash;
}
return &s[build_path_len];
}
int setReason(char *fmt, ...)
{
va_list ap;

View File

@ -764,23 +764,25 @@ long long debugFlagMask(const char *flagname);
char *catv(const char *data, char *buf, size_t len);
int dump(char *name,unsigned char *addr,int len);
char *trimbuildpath(char *s);
#define FATALF(F,...) do { logMessage(LOG_LEVEL_FATAL, "%s:%d:%s() " F, __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__); exit(-1); } while(1)
#define FATAL(X) FATALF("%s", (X))
#define FATAL_perror(X) FATALF("%s: %s [errno=%d]", (X), strerror(errno), errno)
#define WHY(X) setReason("%s:%d:%s() %s",__FILE__,__LINE__,__FUNCTION__,X)
#define WHYNULL(X) (setReason("%s:%d:%s() %s",__FILE__,__LINE__,__FUNCTION__,X), NULL)
#define WHYF(F,...) setReason("%s:%d:%s() " F, __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__)
#define WHY_perror(X) setReason("%s:%d:%s() %s: %s [errno=%d]", __FILE__, __LINE__, __FUNCTION__, X, strerror(errno), errno)
#define WHY(X) setReason("%s:%d:%s() %s",trimbuildpath(__FILE__),__LINE__,__FUNCTION__,X)
#define WHYNULL(X) (setReason("%s:%d:%s() %s",trimbuildpath(__FILE__),__LINE__,__FUNCTION__,X), NULL)
#define WHYF(F,...) setReason("%s:%d:%s() " F,trimbuildpath(__FILE__), __LINE__, __FUNCTION__, ##__VA_ARGS__)
#define WHY_perror(X) setReason("%s:%d:%s() %s: %s [errno=%d]", trimbuildpath(__FILE__), __LINE__, __FUNCTION__, X, strerror(errno), errno)
#define WARNF(F,...) logMessage(LOG_LEVEL_WARN, "%s:%d:%s() " F, __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__)
#define WARNF(F,...) logMessage(LOG_LEVEL_WARN, "%s:%d:%s() " F, trimbuildpath(__FILE__), __LINE__, __FUNCTION__, ##__VA_ARGS__)
#define WARN(X) WARNF("%s", (X))
#define WARN_perror(X) WARNF("%s: %s [errno=%d]", (X), strerror(errno), errno)
#define INFOF(F,...) logMessage(LOG_LEVEL_INFO, "%s:%d:%s() " F, __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__)
#define INFOF(F,...) logMessage(LOG_LEVEL_INFO, "%s:%d:%s() " F,trimbuildpath( __FILE__), __LINE__, __FUNCTION__, ##__VA_ARGS__)
#define INFO(X) INFOF("%s", (X))
#define DEBUGF(F,...) logMessage(LOG_LEVEL_DEBUG, "%s:%d:%s() " F, __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__)
#define DEBUGF(F,...) logMessage(LOG_LEVEL_DEBUG, "%s:%d:%s() " F, trimbuildpath(__FILE__), __LINE__, __FUNCTION__, ##__VA_ARGS__)
#define DEBUG(X) DEBUGF("%s", (X))
#define DEBUG_perror(X) DEBUGF("%s: %s [errno=%d]", (X), strerror(errno), errno)
#define D DEBUG("D")