log是系统不可缺少的部分,对查找系统的问题,记录系统运行状况非常有用。Eucalyptus的log部分还比较简单,只是简单记录了特殊事件
log的级别:
enum {EUCADEBUG2, EUCADEBUG, EUCAINFO, EUCAWARN, EUCAERROR, EUCAFATAL};
记当log文件的全局变量
FILE *LOGFH=NULL; //文件指针
char logFile[1024]; //文件名
下面就是记录log的函数,总的来说就是写入文件或命令行输出,可直接看懂
int logfile(char *file, int in_loglevel) {
logging = 0;
if (in_loglevel >= EUCADEBUG2 && in_loglevel <= EUCAFATAL) {
loglevel = in_loglevel;
} else {
loglevel = EUCADEBUG;
}
if (file == NULL) {
LOGFH = NULL;
} else {
if (LOGFH != NULL) {
fclose(LOGFH);
}
snprintf(logFile, 1024, "%s", file);
LOGFH = fopen(file, "a");
if (LOGFH) {
logging=1;
}
}
return(1-logging);
}
void eventlog(char *hostTag, char *userTag, char *cid, char *eventTag, char *other) {
double ts;
struct timeval tv;
char hostTagFull[256];
char hostName [256];
FILE *PH;
if (!timelog) return;
hostTagFull[0] = '/0';
PH = popen("hostname", "r");
fscanf(PH, "%256s", hostName);
pclose(PH);
snprintf (hostTagFull, 256, "%s/%s", hostName, hostTag);
gettimeofday(&tv, NULL);
ts = (double)tv.tv_sec + ((double)tv.tv_usec / 1000000.0);
logprintf("TIMELOG %s:%s:%s:%s:%f:%s/n", hostTagFull, userTag, cid, eventTag, ts, other);
}
int logprintf(const char *format, ...) {
va_list ap;
int rc;
char buf[27], *eol;
time_t t;
FILE *file;
rc = 1;
va_start(ap, format);
if (logging) {
file = LOGFH;
} else {
file = stdout;
}
t = time(NULL);
if (ctime_r(&t, buf)) {
eol = strchr(buf, '/n');
if (eol) {
*eol = '/0';
}
fprintf(file, "[%s] ", buf);
}
rc = vfprintf(file, format, ap);
fflush(file);
va_end(ap);
return(rc);
}
int logprintfl(int level, const char *format, ...) {
va_list ap;
int rc, fd;
char buf[27], *eol;
time_t t;
struct stat statbuf;
FILE *file;
if (level < loglevel) {
return(0);
}
rc = 1;
va_start(ap, format);
if (logging) {
file = LOGFH;
fd = fileno(file);
if (fd > 0) {
rc = fstat(fd, &statbuf);
if (!rc && ((int)statbuf.st_size > MAXLOGFILESIZE)) {
int i;
char oldFile[1024], newFile[1024];
rc = stat(logFile, &statbuf);
if (!rc && ((int)statbuf.st_size > MAXLOGFILESIZE)) {
for (i=4; i>=0; i--) {
snprintf(oldFile, 1024, "%s.%d", logFile, i);
snprintf(newFile, 1024, "%s.%d", logFile, i+1);
rename(oldFile, newFile);
}
snprintf(oldFile, 1024, "%s", logFile);
snprintf(newFile, 1024, "%s.%d", logFile, 0);
rename(oldFile, newFile);
}
fclose(LOGFH);
LOGFH = fopen(logFile, "a");
if (LOGFH) {
file = LOGFH;
} else {
file = stdout;
}
}
}
} else {
file = stdout;
}
t = time(NULL);
if (ctime_r(&t, buf)) {
eol = strchr(buf, '/n');
if (eol) {
*eol = '/0';
}
fprintf(file, "[%s]", buf);
}
fprintf(file, "[%06d]", getpid());
if (level == EUCADEBUG2) {fprintf(file, "[%-10s] ", "EUCADEBUG2");}
else if (level == EUCADEBUG) {fprintf(file, "[%-10s] ", "EUCADEBUG");}
else if (level == EUCAINFO) {fprintf(file, "[%-10s] ", "EUCAINFO");}
else if (level == EUCAWARN) {fprintf(file, "[%-10s] ", "EUCAWARN");}
else if (level == EUCAERROR) {fprintf(file, "[%-10s] ", "EUCAERROR");}
else if (level == EUCAFATAL) {fprintf(file, "[%-10s] ", "EUCAFATAL");}
else {fprintf(file, "[%-10s] ", "EUCADEBUG");}
rc = vfprintf(file, format, ap);
fflush(file);
va_end(ap);
return(rc);
}