Improve log pathname trimming

This commit is contained in:
Andrew Bettison 2012-05-23 18:04:10 +09:30
parent 59e58da2f4
commit 2f6f642a02
2 changed files with 11 additions and 15 deletions

24
log.c
View File

@ -62,22 +62,18 @@ 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)
const char *trimbuildpath(const 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;
/* Remove common path prefix */
int lastsep = 0;
int i;
for (i = 0; __FILE__[i] && s[i]; ++i) {
if (i && s[i - 1] == '/')
lastsep = i;
if (__FILE__[i] != s[i])
break;
}
return &s[build_path_len];
return &s[lastsep];
}
int setReason(char *fmt, ...)

View File

@ -765,8 +765,8 @@ 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);
const char *trimbuildpath(const char *s);
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)