/** * This function will delete the data from CacheData list. * * @param cache the cache pointer * @param name the cache data name * * @return error code */ static CacheErrCode delData(pCache const cache, const char* name) { CacheErrCode errorCode = CACHE_NO_ERR; pCacheData data = cache->dataHead, dataTemp; /* lock the thread pool synchronized lock */ cache->pool->lock(cache->pool); assert((name != NULL) && (strlen(name) <= CACHE_NAME_MAX)); /* check cache initialize */ if (cache->dataHead == NULL) { log_d("the %s's data list is NULL,delete data fail", cache->name); errorCode = CACHE_NO_VALUE; } /* search the data from list*/ if (errorCode == CACHE_NO_ERR) { if (strcmp(data->name, name)) { /* list head */ for (;;) { if (data->next == NULL) {/* list tail */ log_d("could not find %s", name); errorCode = CACHE_NAME_ERROR; break; } else { if (!strcmp(data->next->name, name)) { break; } else { data = data->next; } } } } } if (errorCode == CACHE_NO_ERR) { /* delete data is head node */ if (data == cache->dataHead) { /* the list has one node */ if ((cache->dataHead == cache->dataTail) && (cache->dataHead != NULL)) { cache->dataHead = cache->dataTail = NULL; } else { /* the list has more than one node*/ cache->dataHead = data->next; } } else if (data->next == cache->dataTail) {/* delete data is tail node */ cache->dataTail = data; cache->dataTail->next = NULL; data = data->next; /* data will be freed in the end */ } else { dataTemp = data->next; data->next = data->next->next; data = dataTemp; /* data will be freed in the end */ } free(data->value); data->value = NULL; free(data); data = NULL; dataTemp = NULL; log_d("delete %s data node is success", name); } /* unlock the thread pool synchronized lock */ cache->pool->unlock(cache->pool); return errorCode; }
/** * This function will initialize the thread pool. * * @param pool the ThreadPool pointer * @param name the ThreadPool name * @param maxThreadNum the max thread number in this ThreadPool * @param threadStackSize the thread stack size in this ThreadPool * * @return error code */ ThreadPoolErrCode initThreadPool(pThreadPool const pool, const char* name, uint8_t maxThreadNum, uint32_t threadStack) { ThreadPoolErrCode errorCode = THREAD_POOL_NO_ERR; uint8_t i = 0; if (maxThreadNum > THREAD_POOL_MAX_THREAD_NUM) { errorCode = THREAD_POOL_MAX_NUM_ERR; } if (errorCode == THREAD_POOL_NO_ERR) { pthread_mutex_init(&(pool->queueLock), NULL); pthread_mutex_init(&(pool->userLock), NULL); pthread_cond_init(&(pool->queueReady), NULL); pool->queueHead = NULL; pool->maxThreadNum = maxThreadNum; pool->curWaitThreadNum = 0; pool->isShutdown = false; pool->addTask = addTask; pool->delAll = delAll; pool->destroy = destroy; pool->lock = syncLock; pool->unlock = syncUnlock; pool->threadID = (pthread_t *) malloc(maxThreadNum * sizeof(pthread_t)); assert(pool->threadID != NULL); for (i = 0; i < maxThreadNum; i++) { pthread_create(&(pool->threadID[i]), NULL, threadJob, pool); log_d("create thread success.Current total thread number is %d", i + 1); } log_d("initialize thread pool success!"); } return errorCode; }
int read_task(const char *filename) { log_d("read_task filename:%s", filename); FILE *fp = fopen(filename, "r"); if (fp == NULL) { fprintf(stderr, "cannot open %s\n", filename); return 0; } char str_line[1024] = { }; //每行最大读取的字符数 while (!feof(fp)) { if (fgets(str_line, 1024, fp) == NULL) //读取一行 { break; } if (strlen(str_line) > 0) { trim(str_line); add_task(parse_cmd(str_line)); log_d("%s", str_line); //输出 } memset(str_line, 0, 1024); } fclose(fp); return 1; }
static void dump_pollfds(int n, int r) { char *buf, *b; int i; buf = malloc(5 * n + 10); if (buf == 0) return; if (r == 0) { b = buf + sprintf(buf, "fds: "); for (i = 0; i < n; i++) b += sprintf(b, " %4d", pollfds[i].fd); log_d("%s", buf); b = buf + sprintf(buf, "events: "); for (i = 0; i < n; i++) b += sprintf(b, " %4hd", pollfds[i].events); log_d("%s", buf); } else { b = buf + sprintf(buf, "revents: "); for (i = 0; i < n; i++) b += sprintf(b, " %4hd", pollfds[i].revents); log_d("%s", buf); } free(buf); }
int main(int argc, char *argv[]) { log_init(LOG_LEVEL_E, LOG_MODE_PRINT); log_v("log level is LOG_LEVEL_E version log\n"); log_d("log level is LOG_LEVEL_E debug log\n"); log_e("log level is LOG_LEVEL_E error log\n"); log_init(LOG_LEVEL_D, LOG_MODE_PRINT); log_v("log level is LOG_LEVEL_D version log\n"); log_d("log level is LOG_LEVEL_D debug log\n"); log_e("log level is LOG_LEVEL_D error log\n"); log_init(LOG_LEVEL_V, LOG_MODE_PRINT); log_v("log level is LOG_LEVEL_V version log\n"); log_d("log level is LOG_LEVEL_V debug log\n"); log_e("log level is LOG_LEVEL_V error log\n"); log_init(LOG_LEVEL_V, LOG_MODE_FILE); log_v("log level is LOG_LEVEL_V version log\n"); log_d("log level is LOG_LEVEL_V debug log\n"); log_e("log level is LOG_LEVEL_V error log\n"); return 0; }
static void reap_children(void) { pid_t pid; int errno_save, status, exitstatus, termsig; errno_save = errno; while (1) { pid = waitpid(-1, &status, WNOHANG); if (pid <= 0) break; if (WIFEXITED(status)) { ++stats.exited_children; exitstatus = WEXITSTATUS(status); if (exitstatus || debug) log_d("child process %d exited with status %d", (int) pid, exitstatus); } else if (WIFSIGNALED(status)) { ++stats.exited_children; termsig = WTERMSIG(status); log_d("child process %d killed by signal %d", (int) pid, termsig); } else log_d("child process %d stopped!?", (int) pid); } if (pid < 0 && errno != ECHILD) lerror("waitpid"); errno = errno_save; }
static int fill_connection(struct connection *cn) { struct pool *p; int poolleft, n, m; intmax_t fileleft; if (cn->rfd == -1) return 0; p = &cn->output; poolleft = p->ceiling - p->end; fileleft = cn->left; n = fileleft > poolleft ? poolleft : (int) fileleft; if (n <= 0) return 0; cn->left -= n; m = read(cn->rfd, p->end, n); if (debug) log_d("fill_connection: %d %d %d %d", cn->rfd, (int) (p->end - p->floor), n, m); if (m != n) { if (m == -1) lerror("read"); else log_d("premature end of file %s", cn->r->path_translated); return -1; } p->end += n; cn->file_offset += n; return n; }
int process_imap(struct request *r) { FILE *fp; int fd; int retval; if (r->method == M_HEAD) { r->status = 204; return 0; } else if (r->method != M_GET) { r->status = 405; return 0; } fd = open(r->path_translated, O_RDONLY | O_NONBLOCK); if (fd == -1) { log_d("cannot open map file %s", r->path_translated); lerror("open"); r->status = 500; return 0; } fcntl(fd, F_SETFD, FD_CLOEXEC); fp = fdopen(fd, "r"); if (fp == 0) { log_d("process_imap: fdopen failed"); close(fd); r->status = 500; return 0; } retval = f_process_imap(r, fp); fclose(fp); return retval; }
void exec_task() { if (taskcount > 0) { int i = 0; time_t current; Interval_Task *task; while (1) { current = time(NULL); for (; i < taskcount; i++) { task = tasks[i]; time_t j_time = (current - task->last_time); if (j_time >= task->interval) { log_d("exec cmdline:%s", task->cmdline); int result = system(task->cmdline); log_d("system result:%d", result); task->last_time = current; } } i = 0; sleep(1); } } }
/** * This function will add CacheData to list. * * @param cache the cache pointer * @param name the name of CacheData * @param length the value length * @param value the value point * @param valueChangedListener the changed value's callback function * * @return error code */ static CacheErrCode addData(pCache const cache, const char* name, uint8_t length, uint16_t* value, void* (*valueChangedListener)(void *arg)) { CacheErrCode errorCode = CACHE_NO_ERR; pCacheData data; /* lock the thread pool synchronized lock */ cache->pool->lock(cache->pool); data = (pCacheData) malloc(sizeof(CacheData)); assert(data != NULL); if (hasData(cache, name) != NULL) {/* the data must not exist in list */ log_d("the name of %s data is already exist in cache data list", name); errorCode = CACHE_NAME_ERROR; } else { strcpy(data->name, name); } if (errorCode == CACHE_NO_ERR) { if (length > CACHE_LENGTH_MAX) { log_d("the name %s is too long,can't add to list", name); errorCode = CACHE_LENGTH_ERROR; } else { data->length = length; } } if (errorCode == CACHE_NO_ERR) { /* malloc data value space */ data->value = (uint16_t*) malloc(length * sizeof(uint16_t)); assert(data->value != NULL); memcpy(data->value, value, length * sizeof(uint16_t)); data->valueChangedListener = valueChangedListener; data->next = NULL; /* if list is NULL ,then head node is equal of tail node*/ if (cache->dataHead == NULL) { cache->dataHead = cache->dataTail = data; } else if ((cache->dataHead == cache->dataTail) /* the list has one node */ && (cache->dataHead != NULL)) { cache->dataHead->next = data; cache->dataTail = data; } else { /* the list has more than one node*/ cache->dataTail->next = data; cache->dataTail = data; } log_d("add %s to data list is success", name); } else if (errorCode != CACHE_NO_ERR) { free(data); data = NULL; } /* unlock the thread pool synchronized lock */ cache->pool->unlock(cache->pool); return errorCode; }
void lerror(const char *s) { int saved_errno; char *errmsg; saved_errno = errno; errmsg = strerror(errno); if (s && *s) log_d("%s: %s", s, errmsg ? errmsg : "???"); else log_d("%s", errmsg ? errmsg : "???"); errno = saved_errno; }
int open_log(const char *name) { char converted_name[PATHLEN]; const char *n; struct tm *tp; int rv; n = name; if (strchr(name, '%')) { current_time = time(0); if (log_gmt) tp = gmtime(¤t_time); else tp = localtime(¤t_time); if (tp) { if (strftime(converted_name, PATHLEN - 1, name, tp)) n = converted_name; } } rv = open(n, O_WRONLY | O_CREAT | O_APPEND, 0666); if (rv == -1) { log_d("cannot open %s", n); lerror("open"); } return rv; }
/** * This function will add a task to thread pool. * * @param pool the ThreadPool pointer * @param process task function pointer * @param arg task function arguments * * @return error code */ static ThreadPoolErrCode addTask(pThreadPool const pool, void *(*process)(void *arg), void *arg) { ThreadPoolErrCode errorCode = THREAD_POOL_NO_ERR; pTask member = NULL; pTask newtask = (pTask) malloc(sizeof(Task)); assert(newtask != NULL); newtask->process = process; newtask->arg = arg; newtask->next = NULL; /* lock thread pool */ pthread_mutex_lock(&(pool->queueLock)); member = pool->queueHead; /* task queue is NULL */ if (member == NULL) { pool->queueHead = newtask; } else { /* look up for queue tail */ while (member->next != NULL) { member = member->next; } member->next = newtask; } /* add current waiting thread number */ pool->curWaitThreadNum++; pthread_mutex_unlock(&(pool->queueLock)); /* wake up a waiting thread to process task */ pthread_cond_signal(&(pool->queueReady)); log_d("add a task to task queue success."); return errorCode; }
CMultiFileWatch() { m_fd = inotify_init1(IN_NONBLOCK); log_d(MODULE, "CMultiFileWatch(): m_fd=%i", m_fd); if(m_fd == -1){ throw Exception(ss_()+"inotify_init() failed: "+strerror(errno)); } }
/** * Elog demo */ static void test_elog(void) { log_a("Hello EasyLogger!"); log_e("Hello EasyLogger!"); log_w("Hello EasyLogger!"); log_i("Hello EasyLogger!"); log_d("Hello EasyLogger!"); log_v("Hello EasyLogger!"); //elog_raw("Hello EasyLogger!"); }
/** * EasyLogger demo */ void test_elog(void) { /* test log output for all level */ log_a("Hello EasyLogger!"); log_e("Hello EasyLogger!"); log_w("Hello EasyLogger!"); log_i("Hello EasyLogger!"); log_d("Hello EasyLogger!"); log_v("Hello EasyLogger!"); // elog_raw("Hello EasyLogger!"); }
void element::Render() { lock(); if (m_parent) { m_parent->lock(); if (GetHide() == 0) { log_d("%s doRender",GetName()); doRender(); } else { log_d("Render %s hide\r\n", GetName()); } //printf("draw %s parent %s %d %d %d %d\n",m_parent->GetName(),GetName(),GetWidth(), GetHeight(), GetX(), GetY()); m_parent->RenderFrom(this, 0, 0, GetWidth(), GetHeight(), GetX(), GetY()); //控件输出到父亲 m_parent->unlock(); //m_parent->Flush(); } else { RenderEB(); if (GetHide() == 0) { log_d("%s doRender\n",GetName()); doRender(); } else { log_d("Render %s hide\r\n", GetName()); } //printf("draw %s %d %d %d %d\n",GetName(),GetWidth(), GetHeight(), GetX(), GetY()); m_proc->Draw(this, 0, 0, GetWidth(), GetHeight(), GetX(), GetY());//控件输出到容器 RenderET(); } unlock(); }
void WiFiSTAClass::_smartConfigCallback(uint32_t st, void* result) { smartconfig_status_t status = (smartconfig_status_t) st; log_d("Status: %s", sc_status_strings[st % 5]); if (status == SC_STATUS_GETTING_SSID_PSWD) { smartconfig_type_t * type = (smartconfig_type_t *)result; log_d("Type: %s", sc_type_strings[*type % 3]); } else if (status == SC_STATUS_LINK) { wifi_sta_config_t *sta_conf = reinterpret_cast<wifi_sta_config_t *>(result); log_d("SSID: %s", (char *)(sta_conf->ssid)); esp_wifi_set_config(WIFI_IF_STA, (wifi_config_t *)sta_conf); esp_wifi_connect(); _smartConfigDone = true; } else if (status == SC_STATUS_LINK_OVER) { if(result){ ip4_addr_t * ip = (ip4_addr_t *)result; log_d("Sender IP: " IPSTR, IP2STR(ip)); } WiFi.stopSmartConfig(); } }
int init_pollfds(size_t n) { if (n == 0) return 0; pollfds = malloc(n * sizeof *pollfds); if (pollfds == 0) { log_d("init_pollfds: out of memory"); return -1; } return 0; }
static void pool_adjust(struct pool *p, size_t s) { size_t n; n = p->size; if (s > 0 && s < n) n -= n % s; if (debug) log_d("pool_adjust: n=%u", (unsigned) n); p->ceiling = p->floor + n; }
int shell_exec(const ss_ &command, const ExecOptions &opts) { log_d(MODULE, "shell_exec(\"%s\")", cs(command)); int f = fork(); if(f == 0){ execl("/bin/sh", "sh", "-c", command.c_str(), (const char*)nullptr); } int exit_status; while(wait(&exit_status) > 0); return exit_status; }
void bina_exit() { renderer_destroy(); if (m.window) { sdl_destroy_window(m.window); } log_d("exiting library"); sdl_quit(); }
int init_log_buffer(size_t size) { log_buffer_size = size; if (size == 0) return 0; log_buffer = malloc(size); if (log_buffer == 0) { log_d("init_log_buffer: out of memory"); return -1; } return 0; }
void hidc_wait_for_empty_service_class(int timeout) { int dd; uint32_t cur_cls = 0; /* current Device Class */ time_t timeout_end = time(NULL) + timeout; log_d("start waiting for empty service class"); if ((dd = open_hci_dev()) < 0) return; get_device_class(dd, &cur_cls); while (((cur_cls & 0x00fff000) != 0) && (time(NULL) <= timeout_end)) { usleep(10 * 1000); get_device_class(dd, &cur_cls); } close_hci_dev(dd); log_d("stop waiting for empty service class"); }
void add(const ss_ &path, std::function<void(const ss_ &path)> cb) { for(auto &pair : m_watch){ sp_<WatchThing> &watch = pair.second; if(watch->path == path){ log_d(MODULE, "Adding callback to path \"%s\" (inotify fd=%i)", cs(path), pair.first); watch->cbs.push_back(cb); return; } } int r = inotify_add_watch(m_fd, path.c_str(), IN_CLOSE_WRITE | IN_MOVED_TO | IN_CREATE | IN_MOVED_FROM | IN_DELETE | IN_MODIFY | IN_ATTRIB); if(r == -1){ throw Exception(ss_()+"inotify_add_watch() failed: "+ strerror(errno)+" (path="+path+")"); } log_d(MODULE, "Watching path \"%s\" (inotify fd=%i)", cs(path), m_fd); m_watch[r] = sp_<WatchThing>(new WatchThing(path, {cb})); }
static int read_connection(struct connection *cn) { size_t bytestoread, bytestomove, offset; ssize_t nr; bytestoread = cn->header_input.ceiling - cn->header_input.end; if (bytestoread == 0) { offset = cn->header_input.start - cn->header_input.floor; if (offset == 0) { log_d("input buffer full"); close_connection(cn); return -1; } bytestomove = cn->header_input.end - cn->header_input.start; memmove(cn->header_input.floor, cn->header_input.start, bytestomove); cn->header_input.start -= offset; cn->header_input.middle -= offset; cn->header_input.end -= offset; bytestoread = cn->header_input.ceiling - cn->header_input.end; } nr = read(cn->fd, cn->header_input.end, bytestoread); if (debug) log_d("read_connection: %d %d %u %d", cn->fd, (int) (cn->header_input.end - cn->header_input.floor), (unsigned) bytestoread, (int) nr); if (nr == -1) { if (errno == EAGAIN) return 0; if (debug) lerror("read"); close_connection(cn); return -1; } if (nr == 0) { close_connection(cn); return -1; } cn->nread += nr; cn->header_input.end += nr; cn->t = current_time; return 0; }
void BrucoSession::on_recv_proxy(const char *buf, int buf_size) { std::string target_str(buf, buf_size); if (dump_stream_) { log_d("dump_stream : session=%ld : [out_bound] %s", session_id_, escape(target_str).c_str()); } if (outbound_key_check_xor256_) { std::string key = read_key_(); if (check_contain_key_xor256_(key, target_str)) { log_w("break_session : type=outbound_contain_key_xor256, outbound_key_file=%s", outbound_key_file_.c_str()); break_session(); return; } } else if (outbound_key_check_) { std::string key = read_key_(); if (check_contain_key_(key, target_str)) { log_w("break_session : %s, type=outbound_contain_key, outbound_key_file=%s", outbound_key_file_.c_str()); break_session(); return; } } if (outbound_pass_re_ != NULL) { bool rv = RE2::PartialMatch(target_str, *outbound_pass_re_); if (rv) { BrucoPipe::on_recv_proxy(buf, buf_size); return; } } if (outbound_deny_re_ != NULL) { bool rv = RE2::PartialMatch(target_str, *outbound_deny_re_); if (rv) { log_w("break_session : type=outbound_deny_re, outbound_re=%s", outbound_deny_re_->pattern().c_str()); break_session(); return; } } // default policy if (outbound_default_pass_) { BrucoPipe::on_recv_proxy(buf, buf_size); } else { break_session(); } }
static int new_pool(struct pool *p, size_t s) { char *t; t = malloc(s); if (t == 0) { log_d("new_pool: out of memory"); return -1; } p->floor = p->start = p->middle = p->end = t; p->ceiling = t + s; p->size = s; return 0; }
/** * This function will find the data in CacheData list. * * @param cache the cache pointer * @param name the name of CacheData * * @return the CacheData point which has found,If not found will return NULL. */ static pCacheData hasData(pCache const cache, const char* name) { pCacheData data = cache->dataHead; assert((name != NULL) && (strlen(name) <= CACHE_NAME_MAX)); //this cache is null if (cache->dataHead == NULL) { log_d("the %s's data list is NULL,find data fail", cache->name); return NULL; } /* search the data from list*/ for (;;) { if (!strcmp(data->name, name)) { return data; } else { if (data->next == NULL) {/* list tail */ log_d("could not find %s", name); break; } data = data->next; } } return NULL; }
/** * Elog demo */ static void test_elog(void) { /* output all saved log from flash */ elog_flash_output_all(); /* test log output for all level */ log_a("Hello EasyLogger!"); log_e("Hello EasyLogger!"); log_w("Hello EasyLogger!"); log_i("Hello EasyLogger!"); log_d("Hello EasyLogger!"); log_v("Hello EasyLogger!"); elog_raw("Hello EasyLogger!"); /* trigger assert. Now will run elog_user_assert_hook. All log information will save to flash. */ ELOG_ASSERT(0); }