/* record job information of completed job to job log */ int log_job_record(const char *buf) { struct tm *ptm; struct tm tmpPtm; time_t now; now = time((time_t *)0); ptm = localtime_r(&now,&tmpPtm); pthread_mutex_lock(&job_log_mutex); /* do we need to switch the log to the new day? */ if (job_log_auto_switch && (ptm->tm_yday != joblog_open_day)) { job_log_close(1); job_log_open(NULL, job_log_directory); if (job_log_opened < 1) { log_err(-1, __func__, "job_log_opened < 1"); pthread_mutex_unlock(&job_log_mutex); return(-1); } } fprintf(joblogfile, "%s\n", buf); fflush(joblogfile); pthread_mutex_unlock(&job_log_mutex); return(0); }
/* record job information of completed job to job log */ int log_job_record(char *buf) { char id[] = "log_job_record"; struct tm *ptm; struct tm tmpPtm; time_t now; now = time((time_t *)0); ptm = localtime_r(&now,&tmpPtm); /* do we need to switch the log to the new day? */ if (job_log_auto_switch && (ptm->tm_yday != joblog_open_day)) { job_log_close(1); job_log_open(NULL, job_log_directory); if (job_log_opened < 1) { log_err(-1, id, "job_log_opened < 1"); return(-1); } } fprintf(joblogfile, "%s\n", buf); return(0); }
void job_log_roll( int max_depth) { int i, suffix_size, file_buf_len, as; int err = 0; char *source = NULL; char *dest = NULL; pthread_mutex_lock(&job_log_mutex); if (!job_log_opened) { pthread_mutex_unlock(&job_log_mutex); return; } /* save value of job_log_auto_switch */ as = job_log_auto_switch; job_log_close(1); /* find out how many characters the suffix could be. (save in suffix_size) start at 1 to account for the "." */ for (i = max_depth, suffix_size = 1;i > 0;suffix_size++, i /= 10) /* NO-OP */; /* allocate memory for rolling */ file_buf_len = sizeof(char) * (strlen(joblogpath) + suffix_size + 1); source = (char*)calloc(1, file_buf_len); dest = (char*)calloc(1, file_buf_len); if ((source == NULL) || (dest == NULL)) { err = errno; goto done_job_roll; } /* call unlink to delete logname.max_depth - it doesn't matter if it doesn't exist, so we'll ignore ENOENT */ sprintf(dest, "%s.%d", joblogpath, max_depth); if ((unlink(dest) != 0) && (errno != ENOENT)) { err = errno; goto done_job_roll; } /* logname.max_depth is gone, so roll the rest of the log files */ for (i = max_depth - 1;i >= 0;i--) { if (i == 0) { strcpy(source, joblogpath); } else { sprintf(source, "%s.%d", joblogpath, i); } sprintf(dest, "%s.%d", joblogpath, i + 1); /* rename file if it exists */ if ((rename(source, dest) != 0) && (errno != ENOENT)) { err = errno; goto done_job_roll; } } /* END for (i) */ done_job_roll: if (as) { job_log_open(NULL, job_log_directory); } else { job_log_open(joblogpath, job_log_directory); } if (source != NULL) free(source); if (dest != NULL) free(dest); if (err != 0) { log_err(err, "log_roll", "error while rollng logs"); } else { log_record( PBSEVENT_SYSTEM, PBS_EVENTCLASS_SERVER, "Job Log", "Job Log Rolled"); } pthread_mutex_unlock(&job_log_mutex); return; } /* END job_log_roll() */