/* erts_run_erl_log_write() * Writes a message to lfd. If the current log file is full, * a new log file is opened. */ int erts_run_erl_log_write(char* buf, size_t len) { int size; ssize_t res; /* Decide if new logfile needed, and open if so */ size = lseek(LFD,0,SEEK_END); if(size+len > LOG_MAXSIZE) { int res; do { res = close(LFD); } while (res < 0 && errno == EINTR); LOG_NUM = next_log(LOG_NUM); LFD = open_log(LOG_NUM, O_RDWR|O_CREAT|O_TRUNC|O_SYNC); } /* Write to log file */ if ((res = erts_run_erl_write_all(LFD, buf, len)) < 0) { erts_run_erl_log_status("Error in writing to log.\n"); } #if USE_FSYNC fsync(LFD); #endif return res; }
event_manager::event_manager(string path, size_t reqmaxsize, uint16_t reqmaxlogs) { uint16_t x; eventpath = path; latestid = 0; dirp = NULL; logcount = 0; maxsize = -1; maxlogs = -1; currentsize = get_managed_size(); if (reqmaxsize) maxsize = reqmaxsize; if (reqmaxlogs) maxlogs = reqmaxlogs; // examine the files being managed and advance latestid to that value while ( (x = next_log()) ) { logcount++; if ( x > latestid ) latestid = x; } return; }
void test_db(int n) { char *dir = Option.dir; struct db db; u64 key; struct lump val; u64 i; int rc; if (1) set_cleanup(cleanup); create_db(dir); db = open_db(dir); for (i = 0; i < n; i++) { key = randkey(); if (0) val = randlump(); if (1) val = key2lump(key); //printf("%lld %*s %zd\n", key, val.size, (char *)val.d, strlen(val.d)); insert_db(&db, key, val); } if (1) for (i = 0; i < n + 1; i++) { rc = next_log(db.r); if (rc == DONE) break; //printf("%lld %s\n", key_log(db.r), (char *)val_log(db.r).d); } if (1) merge_db(&db); close_db(&db); destroy_db(dir); }
FILE* open_log_file(char* fmt, int* p_num, size_t* p_size) { char name[64]; struct stat file_status; FILE* ret; if(max_num == 0 || max_size == 0) { return NULL; } //Get last log file for(; *p_num < max_num; (*p_num)++) { sprintf(name, fmt, *p_num); if(access(name, F_OK) != 0) { if(*p_num > 0) { (*p_num)--; } break; } } //Get file status sprintf(name, fmt, *p_num); if(stat(name, &file_status) == 0) { if(file_status.st_size >= max_size) { next_log(fmt, p_num); sprintf(name, fmt, *p_num); } } else { if(errno != ENOENT) { return NULL; } } //Open file ret = fopen(name, "a"); chmod(name, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); if(ret != NULL) { *p_size = ftell(ret); } return ret; }
static int open_log(int log_num, int flags) { char buf[FILENAME_MAX]; time_t now; struct tm *tmptr; char log_buffer[ALIVE_BUFFSIZ+1]; /* Remove the next log (to keep a "hole" in the log sequence) */ sn_printf(buf, sizeof(buf), "%s/%s%d", LOG_DIR, LOG_STUBNAME, next_log(log_num)); unlink(buf); /* Create or continue on the current log file */ sn_printf(buf, sizeof(buf), "%s/%s%d", LOG_DIR, LOG_STUBNAME, log_num); LFD = sf_open(buf, flags, LOG_PERM); if(LFD <0){ ERRNO_ERR1(LOG_ERR,"Can't open log file '%s'.", buf); exit(1); } /* Write a LOGGING STARTED and time stamp into the log file */ time(&now); if (LOG_ALIVE_IN_GMT) { tmptr = gmtime(&now); } else { tmptr = localtime(&now); } if (!strftime(log_buffer, ALIVE_BUFFSIZ, LOG_ALIVE_FORMAT, tmptr)) { strn_cpy(log_buffer, sizeof(log_buffer), "(could not format time in 256 positions " "with current format string.)"); } log_buffer[ALIVE_BUFFSIZ] = '\0'; sn_printf(buf, sizeof(buf), "\n=====\n===== LOGGING STARTED %s\n=====\n", log_buffer); if (erts_run_erl_write_all(LFD, buf, strlen(buf)) < 0) erts_run_erl_log_status("Error in writing to log.\n"); #if USE_FSYNC fsync(LFD); #endif return LFD; }
event_manager::event_manager(string path) { uint16_t x; eventpath = path; latestid = 0; dirp = NULL; logcount = 0; // examine the files being managed and advance latestid to that value while ( (x = next_log()) ) { logcount++; if ( x > latestid ) latestid = x; } return; }
/* write_to_log() * Writes a message to a log file. If the current log file is full, * a new log file is opened. */ static void write_to_log(int* lfd, int* log_num, char* buf, int len) { int size; /* Decide if new logfile needed, and open if so */ size = lseek(*lfd,0,SEEK_END); if(size+len > log_maxsize) { sf_close(*lfd); *log_num = next_log(*log_num); *lfd = open_log(*log_num, O_RDWR|O_CREAT|O_TRUNC|O_SYNC); } /* Write to log file */ if (write_all(*lfd, buf, len) < 0) { status("Error in writing to log.\n"); } #if USE_FSYNC fsync(*lfd); #endif }