int write_command(struct ELM_handle * h, const char * cmd, ssize_t rsize) { log_next(">>"); if (write_all(h, cmd, strlen(cmd))) return -1; if (!h->rsize_not_supported && rsize != ELM_UNDEF_RSIZE) { char buffer[8]; sprintf(buffer, " %d", (int) rsize); if (write_all(h, buffer, strlen(buffer))) return -1; } return write_all(h, "\r", 1); }
int index_add(struct index *idx, struct slice *sk, struct slice *sv) { uint64_t value_offset; struct skiplist *list; struct skiplist *new_list; value_offset = log_append(idx->log, sk, sv); list = idx->list; if(!list) { __DEBUG(LEVEL_ERROR, "ERROR: List<%d> is NULL", idx->lsn); return 0; } if(!skiplist_notfull(list)) { idx->bg_merge_count++; /* If the detached-merge thread isnot finished, hold on it * Notice: it will block the current process, * but it happens only once in a thousand years on production environment. */ pthread_mutex_lock(&idx->merge_mutex); /* start to merge with detached thread */ pthread_t tid; idx->park->list = list; idx->park->lsn = idx->lsn; pthread_mutex_unlock(&idx->merge_mutex); pthread_create(&tid, &idx->attr, _merge_job, idx); idx->mtbl_rem_count = 0; new_list = skiplist_new(idx->max_mtbl_size); idx->list = new_list; idx->lsn++; log_next(idx->log, idx->lsn); } skiplist_insert(idx->list, sk->data, value_offset, sv == NULL ? DEL : ADD); idx->mtbl_rem_count++; return 1; }
int read_all(struct ELM_handle * h) { log_next("<<"); size_t offset = 0; while (offset < BUFFER_SIZE) { if (wait(h, POLLIN) == 0) { ssize_t read_result = read(h->fd, h->reply + offset, BUFFER_SIZE - offset); if (read_result > 0) { offset += read_result; if (offset > 0 && h->reply[offset - 1] == '>') { log_write(h->reply, offset); log_ok(); h->reply[offset - 2] = '\0'; /* remove last \r> */ return 0; } } else { log_write(h->reply, offset); log_fail(); sprintf(h->error_msg, "read(...) error: %s", strerror(errno)); return -1; } } else { log_write(h->reply, offset); log_fail(); return -1; } } /* while (offset < buffer_size) */ log_write(h->reply, offset); log_fail(); sprintf(h->error_msg, "Internal error: buffer small"); return -1; }
struct log_head* log_list(int count, struct log_head *h) { unsigned int min = count; if (count) min = (count < current_id) ? (current_id - count) : (0); if (!h && oldest->id >= min) return oldest; if (!h) h = oldest; while (h != newest) { h = log_next(h, h->size); if (!h->size && (h > newest)) h = log; if (h->id >= min && (h != newest)) return h; } return NULL; }
static int replay_logblocks(struct replay *rp, replay_log_t replay_log_func) { if(DEBUG_MODE_K==1) { printk(KERN_INFO"%25s %25s %4d #in\n",__FILE__,__func__,__LINE__); } struct sb *sb = rp->sb; unsigned logcount = be32_to_cpu(sb->super.logcount); int err; sb->lognext = 0; while (sb->lognext < logcount) { trace("log block %i, blocknr %Lx, unify %Lx", sb->lognext, rp->blocknrs[sb->lognext], rp->unify_index); log_next(sb); err = replay_log_func(rp, sb->logbuf); log_drop(sb); if (err) return err; } return 0; }
struct index *index_new(const char *basedir, int max_mtbl_size, int tolog) { char dbfile[FILE_PATH_SIZE]; struct index *idx = malloc(sizeof(struct index)); struct idx_park *park = malloc(sizeof(struct idx_park)); ensure_dir_exists(basedir); idx->lsn = 0; idx->bloom_hits = 0; idx->bg_merge_count = 0; idx->max_mtbl = 1; idx->max_mtbl_size = max_mtbl_size; memset(idx->basedir, 0, FILE_PATH_SIZE); memcpy(idx->basedir, basedir, FILE_PATH_SIZE); /* sst */ idx->sst = sst_new(idx->basedir); idx->list = skiplist_new(max_mtbl_size); pthread_mutex_init(&idx->merge_mutex, NULL); /* container */ park->list = NULL; park->lsn = idx->lsn; idx->park = park; /* log */ idx->log = log_new(idx->basedir, idx->lsn, tolog); /* * Log Recovery Processes : * 1) read old log file and add entries to memtable * 2) read new log file abd add entries to memtable * 3) merge the current active log's memtable * 4) remove old log file, new log file * 5) create new memtable and log file */ if (log_recovery(idx->log, idx->list)) { __DEBUG(LEVEL_DEBUG, "prepare to merge logs, merge count #%d....", idx->list->count); sst_merge(idx->sst, idx->list, 1); remove(idx->log->log_new); remove(idx->log->log_old); idx->list = skiplist_new(idx->max_mtbl_size); } /* Create new log : 0.log */ log_next(idx->log, 0); memset(dbfile, 0, FILE_PATH_SIZE); snprintf(dbfile, FILE_PATH_SIZE, "%s/ness.db", idx->basedir); idx->db_rfd = open(dbfile, LSM_OPEN_FLAGS, 0644); /* Detached thread attr */ pthread_attr_init(&idx->attr); pthread_attr_setdetachstate(&idx->attr, PTHREAD_CREATE_DETACHED); return idx; }
/* Read headers of log records and dump accordingly */ static void dump_dmesg_structured(int fd) { #define OUT_BUF_SIZE 4096 uint64_t log_buf, log_buf_offset, ts_nsec; uint32_t log_first_idx, log_next_idx, current_idx, len = 0, i; int log_buf_len; char *buf, out_buf[OUT_BUF_SIZE]; ssize_t ret; char *msg; uint16_t text_len; imaxdiv_t imaxdiv_sec, imaxdiv_usec; if (!log_buf_vaddr) { fprintf(stderr, "Missing the log_buf symbol\n"); exit(60); } if (!log_buf_len_vaddr) { fprintf(stderr, "Missing the log_bug_len symbol\n"); exit(61); } if (!log_first_idx_vaddr) { fprintf(stderr, "Missing the log_first_idx symbol\n"); exit(62); } if (!log_next_idx_vaddr) { fprintf(stderr, "Missing the log_next_idx symbol\n"); exit(63); } if (!log_sz) { fprintf(stderr, "Missing the struct log size export\n"); exit(64); } if (log_offset_ts_nsec == UINT64_MAX) { fprintf(stderr, "Missing the log.ts_nsec offset export\n"); exit(65); } if (log_offset_len == UINT16_MAX) { fprintf(stderr, "Missing the log.len offset export\n"); exit(66); } if (log_offset_text_len == UINT16_MAX) { fprintf(stderr, "Missing the log.text_len offset export\n"); exit(67); } log_buf = read_file_pointer(fd, vaddr_to_offset(log_buf_vaddr)); log_buf_len = read_file_s32(fd, vaddr_to_offset(log_buf_len_vaddr)); log_first_idx = read_file_u32(fd, vaddr_to_offset(log_first_idx_vaddr)); log_next_idx = read_file_u32(fd, vaddr_to_offset(log_next_idx_vaddr)); log_buf_offset = vaddr_to_offset(log_buf); buf = calloc(1, log_buf_len); if (!buf) { fprintf(stderr, "Failed to malloc %d bytes for the logbuf:" " %s\n", log_buf_len, strerror(errno)); exit(64); } ret = pread(fd, buf, log_buf_len, log_buf_offset); if (ret != log_buf_len) { fprintf(stderr, "Failed to read log buffer of size %d bytes:" " %s\n", log_buf_len, strerror(errno)); exit(65); } /* Parse records and write out data at standard output */ current_idx = log_first_idx; len = 0; while (current_idx != log_next_idx) { msg = log_from_idx(buf, current_idx); ts_nsec = struct_val_u64(msg, log_offset_ts_nsec); imaxdiv_sec = imaxdiv(ts_nsec, 1000000000); imaxdiv_usec = imaxdiv(imaxdiv_sec.rem, 1000); len += sprintf(out_buf + len, "[%5llu.%06llu] ", (long long unsigned int)imaxdiv_sec.quot, (long long unsigned int)imaxdiv_usec.quot); /* escape non-printable characters */ text_len = struct_val_u16(msg, log_offset_text_len); for (i = 0; i < text_len; i++) { unsigned char c = log_text(msg)[i]; if (c < ' ' || c >= 128) len += sprintf(out_buf + len, "\\x%02x", c); else out_buf[len++] = c; if (len >= OUT_BUF_SIZE - 16) { write_to_stdout(out_buf, len); len = 0; } } out_buf[len++] = '\n'; /* Move to next record */ current_idx = log_next(buf, current_idx); } if (len) write_to_stdout(out_buf, len); }
void main(void) { long lxsize; LogEntry loginfo; int status; char buf[200]; /* File system setup and partitioning */ fs_ext = fs_get_flash_lx(); if (fs_ext == 0) { printf("No flash available!\n"); exit(1); } /* * Get the size of the entire flash with the given sector size */ lxsize = fs_get_lx_size(fs_ext, 1, MY_LS_SHIFT); /* * Partition the filesystem - always give 1/8 to the backup partition, as we * have room to spare. */ backup_ext = fs_setup(fs_ext, MY_LS_SHIFT, 0, NULL, FS_PARTITION_FRACTION, 0x2000, MY_LS_SHIFT, 0, NULL); if (backup_ext == 0) { printf("Could not create backup extent!\n"); exit(2); } lxsize = fs_get_lx_size(fs_ext, 1, MY_LS_SHIFT); lxsize = fs_get_lx_size(backup_ext, 1, MY_LS_SHIFT); if (fs_init(0, 0) != 0) { printf("Filesystem failed to initialize!\n"); exit(3); } #ifdef FORMAT if (lx_format(fs_ext, 0) != 0) { printf("Filesystem failed to format!\n"); exit(4); } if (lx_format(backup_ext, 0) != 0) { printf("Backup area failed to format!\n"); exit(5); } #endif fs_set_lx(fs_ext, fs_ext); /* * Reset all logs if requested. */ #ifdef RESET_ALL_LOGS log_open(LOG_DEST_ALL, 1); #endif /* * This call is necessary to initialize target communications * beween the DeviceMate and the target processor. */ targetproc_init(); /* * Initialize the TCP/IP stack and the web server. */ sock_init(); http_init(); /* * The following improves interactive performance of the web server. */ tcp_reserveport(80); /* * Print out previous log entries */ if (!log_seek(LOG_DEST_FS2, 0)) { printf("Scanning previous log entries, oldest first...\n"); for (;;) { if (log_next(LOG_DEST_FS2, &loginfo) < 0) break; printf("%s\n", log_format(&loginfo, buf, sizeof(buf), 1)); } printf("End of messages.\n"); } /* * Log an initial entry. */ #define LOG_TEST_STRING "~~~{ Started test run. }~~~" status = log_put(LOG_MAKEPRI(2,LOG_INFO), 0, LOG_TEST_STRING, strlen(LOG_TEST_STRING)); if (status != 0) { printf("Failed to add 1st message: %d\n", status); } /* * Drive the target communications and the web server continuously. * This is all that is necessary as the main part of the program. */ for (;;) { targetproc_tick(); http_handler(); } }
int testLog () { log_t *log = NULL; int* d1 = (int*) malloc( sizeof (int) ); int* d2 = (int*) malloc( sizeof (int) ); int* d3 = (int*) malloc( sizeof (int) ); assert( d1 && d2 && d3 ); *d1 = 1; *d2 = 10; *d3 = 100; /*Test des fonctions log_create et log_insertAfter*/ log = log_create( sizeof (int) ); if (log != NULL) printf("log created\n"); else return -1; log_freeForward(log); /*25 est à la fois start, selected et end car seul maillon de la chaine*/ log_insertAfter( log, ((void*)d1) ); log_next(log); log_insertAfter( log, ((void*)d2) ); log_next(log); log_insertAfter( log, ((void*)d3) ); log_next(log); printf("a "); _log_view(log); log_start(log); printf("b "); _log_view(log); log_freeForward(log); log_freeForward(log); log_freeForward(log); printf("c "); _log_view(log); /*Test de log_insertBefore : cas elt selected est premier */ log_insertAfter( log, ((void*)d2) ); printf("b "); _log_view(log); log_next(log); log_freeSelected(log); printf("B "); _log_view(log); /*Test de log_insertBefore : cas elt selected milieu */ log_insertBefore( log , ((void*)d3) ); log_previous( log ); printf("c "); _log_view(log); /*Test de log_insertBefore : cas elt selected milieu */ log_end( log ); printf("d "); _log_view(log); /*Test de la fct de sauvegarde*/ log_save( log, "save1" ); printf("log save to save1\n"); /* destroy */ log_destroy(log); printf("log destroyed\n"); /*Test de la fct de chargement*/ log = log_load( "save1", sizeof(int) ); _log_view(log); return (0); }
int main() { LogEntry loginfo; int status; char buf[200]; #ifdef LOG_USE_FS2 #define DEST_IN_USE LOG_DEST_FS2 printf("Initializing file system. Please wait...\n"); status = fs_init(0,0); if (status) { printf("Could not initialize filesystem, error number %d\n", errno); exit(2); } printf("...done. Using LX#%d for logging\n", (int)LOG_FS2_DATALX(0)); #else #define DEST_IN_USE LOG_DEST_XMEM #endif #ifdef RESET_ALL_LOGS log_open(LOG_DEST_ALL, 1); #endif targetproc_init(); reprint: #ifdef READ_BACKWARDS if (!log_seek(DEST_IN_USE, 1)) { printf("Scanning previous log entries, most recent first...\n"); for (;;) { if (log_prev(DEST_IN_USE, &loginfo) < 0) break; printf("%s\n", log_format(&loginfo, buf, sizeof(buf), 1)); } printf("End of messages.\n"); } #else if (!log_seek(DEST_IN_USE, 0)) { printf("Scanning previous log entries, oldest first...\n"); for (;;) { if (log_next(DEST_IN_USE, &loginfo) < 0) break; printf("%s\n", log_format(&loginfo, buf, sizeof(buf), 1)); } printf("End of messages.\n"); } #endif #define LOG_TEST_STRING "~~~{ Started test run. }~~~" status = log_put(LOG_MAKEPRI(2,LOG_INFO), 0, LOG_TEST_STRING, strlen(LOG_TEST_STRING)); if (status != 0) printf("Failed to add 1st message: %d\n", status); for (;;) { targetproc_tick(); if (kbhit()) { gets(buf); // ignore it goto reprint; } } }
void log_add(char *buf, int size, int source) { regmatch_t matches[4]; struct log_head *next; int priority = 0; int ret; /* bounce out if we don't have init'ed yet (regmatch etc will blow) */ if (!log) { fprintf(stderr, buf); return; } /* strip trailing newline */ if (buf[size - 2] == '\n') { buf[size - 2] = '\0'; size -= 1; } /* strip the priority */ ret = regexec(&pat_prio, buf, 3, matches, 0); if (!ret) { priority = atoi(&buf[matches[1].rm_so]); size -= matches[2].rm_so; buf += matches[2].rm_so; } #if 0 /* strip kernel timestamp */ ret = regexec(&pat_tstamp,buf, 4, matches, 0); if ((source == SOURCE_KLOG) && !ret) { size -= matches[3].rm_so; buf += matches[3].rm_so; } #endif /* strip syslog timestamp */ if ((source == SOURCE_SYSLOG) && (size > SYSLOG_PADDING) && (buf[SYSLOG_PADDING - 1] == ' ')) { size -= SYSLOG_PADDING; buf += SYSLOG_PADDING; } DEBUG(2, "-> %d - %s\n", priority, buf); /* find new oldest entry */ next = log_next(newest, size); if (next > newest) { while ((oldest > newest) && (oldest <= next) && (oldest != log)) oldest = log_next(oldest, oldest->size); } else { DEBUG(2, "Log wrap\n"); newest->size = 0; next = log_next(log, size); for (oldest = log; oldest <= next; oldest = log_next(oldest, oldest->size)) ; newest = log; } /* add the log message */ newest->size = size; newest->id = current_id++; newest->priority = priority; newest->source = source; clock_gettime(CLOCK_REALTIME, &newest->ts); strcpy(newest->data, buf); ubus_notify_log(newest); newest = next; }