diff options
Diffstat (limited to 'agni.c')
-rw-r--r-- | agni.c | 133 |
1 files changed, 67 insertions, 66 deletions
@@ -31,6 +31,7 @@ #include "irc_parse.h" #include "mmm.h" #include "stat.h" +#include "log.h" #include "arg.h" @@ -249,7 +250,7 @@ int th_start_client(void *data) qtbl = tbl_qcmd_c(100); if (qtbl == NULL) { - PERM(); + LOG_NONE_W(); } ////////////////////////////////////////////////////////////////////////////// @@ -266,8 +267,8 @@ int th_start_client(void *data) memset(&stats, 0, sizeof(stat_server)); atomic_fetch_add(&cfg->running,1); - printf("Start client\n"); - printf("Server %d\n",cfg->tid); + LOG_NONE_I("Start client\n"); + LOG_NONE_I("Server %d\n",cfg->tid); sleep(1); //PRINT("SERVER:%s PORT:%s USER:%s\n", cfg->server, cfg->port, cfg->user); @@ -294,7 +295,7 @@ int th_start_client(void *data) fret = irc_open(cfg->server, cfg->port, &conn); if (fret != 0) { - ERROR("Cant connect\n"); + LOG_NONE_E("Cant connect\n"); return -1; } //lets timeout after 150 sec, ping from server is 120 sec @@ -311,8 +312,7 @@ int th_start_client(void *data) mq = cfg->mq; if (mq_ntf_getattr(mq, MQ_OUT, &ptr_out_attr) == -1) { - printf("Cant get attribute\n"); - ENL(); + LOG_NONE_E("Cant get attribute\n"); } //PRINT("SERVER:%s PORT:%s USER:%s\n", cfg->server, cfg->port, cfg->user); PRINT("out_attr.mq_msgsize %ld\n", out_attr.mq_msgsize); @@ -320,22 +320,21 @@ int th_start_client(void *data) if (out_buf == NULL) { - ERROR("Can allocate out_buf, size of %ld\n", out_attr.mq_msgsize); + LOG_NONE_E("Can allocate out_buf, size of %ld\n", out_attr.mq_msgsize); } memset(out_buf, 0, out_attr.mq_msgsize); //PRINT("SERVER:%s PORT:%s USER:%s\n", cfg->server, cfg->port, cfg->user); if (mq_ntf_getattr(mq, MQ_IN, &ptr_in_attr) == -1) { - printf("Cant get attribute\n"); - ENL(); + LOG_NONE_E("Cant get attribute\n"); } in_buf = malloc(in_attr.mq_msgsize); memset(in_buf, 0, in_attr.mq_msgsize); if (in_buf == NULL) { - ENL(); + LOG_NONE_E(); } @@ -346,12 +345,12 @@ int th_start_client(void *data) ib = irc_buf_create(); if (ib == NULL) { - PERM(); + LOG_NONE_E(); //return 0; } //send command wait for response - printf("Start loop\n"); + LOG_NONE_I("Start loop\n"); run = 1; while (run) { @@ -368,15 +367,15 @@ int th_start_client(void *data) //memset(in_buf, 0, in_attr.mq_msgsize); if ((byte_num = mq_ntf_read(mq, MQ_IN, in_buf, in_attr.mq_msgsize)) == -1) { - printf("Cant read input message \n"); + LOG_NONE_W("Cant read input message \n"); } else { in_buf[byte_num] = 0x0; - PRINT("Recieve %s\n", in_buf); + LOG_NONE_E("Recieve %s\n", in_buf); } break; default: - printf("Unknown event type\n"); + LOG_NONE_W("Unknown event type\n"); } //fast code to exit if QUIT command is recieved @@ -387,7 +386,7 @@ int th_start_client(void *data) nb_init(nb_resp); nb_load(nb_resp, (unsigned char *)in_buf); rpc_resp_unmarsh(nb_resp, &resp); - PRINT("RESP: %d-%s(%s)\n", resp->id, resp->result, resp->error); + LOG_NONE_D("RESP: %d-%s(%s)\n", resp->id, resp->result, resp->error); tble_cmd_resp *match_resp = malloc(sizeof(tble_cmd_resp)); memset(match_resp, 0, sizeof(tble_cmd_resp)); @@ -400,7 +399,7 @@ int th_start_client(void *data) if (cmd_id > 0) { //BUG HERE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!.\.,\<\/\/\// - printf("cmd_id %d\n", cmd_id); + LOG_NONE_D("cmd_id %d\n", cmd_id); tble_qcmd *cmd = tbl_qcmd_match_id(qtbl, cmd_id); if (cmd != NULL) { @@ -439,7 +438,7 @@ int th_start_client(void *data) sds sending = sdsnew(cmd_buf); sending = sdscat(sending, tokens[out_i]); sending = sdscat(sending," \r\n"); - printf("SEND>>>>%s",sending); + LOG_NONE_D("SEND>>>>%s",sending); write(conn.conn_fd, sending, sdslen(sending)); sdsfree(sending); } @@ -475,7 +474,7 @@ int th_start_client(void *data) //if((cret = read(conn,conn_buf, 256))>0) if ((cret = irc_read(&conn, conn_buf, 256))>0) { - PRINT(">>READ %d<<\n", cret); + LOG_NONE_D(">>READ %d<<\n", cret); irc_buf_puts(ib, conn_buf, cret); while (irc_buf_ready(ib) == 1) { @@ -491,10 +490,10 @@ int th_start_client(void *data) int fret; const int pong_buf_sz = 128; char pong_buf[pong_buf_sz]; - snprintf(pong_buf,pong_buf_sz,"PONG %s\n",cfg->user); - PRINT("%s",pong_buf) - fret = write(conn.conn_fd,pong_buf,strlen(pong_buf)); - printf("OUT>>PONG %d\n",fret); + snprintf(pong_buf, pong_buf_sz, "PONG %s\n", cfg->user); + LOG_NONE_D("%s", pong_buf) + fret = write(conn.conn_fd, pong_buf, strlen(pong_buf)); + LOG_NONE_D("OUT>>PONG %d\n",fret); } @@ -504,7 +503,7 @@ int th_start_client(void *data) int j; for (j=0;j<token_len(itok);j++) { - printf("TK:[%s]\n",itok->tk_list[j]->token); + LOG_NONE_D("TK:[%s]\n", itok->tk_list[j]->token); } if (token_cmp(itok,1,"NOTICE")==1) @@ -516,13 +515,13 @@ int th_start_client(void *data) { memset(cmd_buf,0,128); - char *uname = token_new_str(itok,0); - char *ircident = token_new_str(itok,0); - char *irccmd = token_new_str(itok,1); + char *uname = token_new_str(itok,0); + char *ircident = token_new_str(itok,0); + char *irccmd = token_new_str(itok,1); char *dest_name = token_new_str(itok,2); - char *msg = token_new_str(itok,3); //could send without first symbol pal + char *msg = token_new_str(itok,3); //could send without first symbol pal - PRINT("dest_name %s\n",dest_name); + LOG_NONE_D("dest_name %s\n",dest_name); char *sep = strchr(uname,'!'); uname[sep-uname] = 0x0; @@ -531,8 +530,8 @@ int th_start_client(void *data) { //lets check if one more argument is there { - char *chan_name=NULL; - int iter=0; + char *chan_name = NULL; + int iter = 0; char *sep_chan = strchr(msg,' ')+1; //small hack //check if channel in allowed list, if yes then continue @@ -540,7 +539,7 @@ int th_start_client(void *data) while (cfg->channels[iter]!=NULL) { chan_name = cfg->channels[iter]; - printf("%s\n",chan_name); + LOG_NONE_D("%s\n",chan_name); if ((strncmp(chan_name,sep_chan,strlen(chan_name))==0) && (strlen(sep_chan)==strlen(chan_name))) { @@ -553,7 +552,7 @@ int th_start_client(void *data) iter++; if (cfg->channels[iter]==NULL) { - PRINT("Cant find this chan in da list\n"); + LOG_NONE_W("Cant find this chan in da list\n"); } } } @@ -592,7 +591,7 @@ int th_start_client(void *data) memcpy(out_buf, buf_nb, nb_req->size > out_attr.mq_msgsize ? out_attr.mq_msgsize : nb_req->size); mq_ntf_write(mq, MQ_OUT, out_buf, out_attr.mq_msgsize); FREE(buf_nb); - PRINT("Write %d bytes\n", nb_req->size); + LOG_NONE_D("Write %d bytes\n", nb_req->size); rpc_req_free(req); nb_free(nb_req); @@ -626,7 +625,7 @@ int th_start_client(void *data) { while (-1 == irc_reconnect(&conn)) { - printf("Reconnect in 10 sec\n"); + LOG_NONE_I("Reconnect in 10 sec\n"); sleep(10); } //sleep(1); @@ -640,13 +639,13 @@ int th_start_client(void *data) { if (conn.err == ERR_SC_TIMEOUT) { - printf("TIMEOUT\n"); + LOG_NONE_I("TIMEOUT\n"); if (irc_reconnect(&conn)==-1) { while (-1 == irc_reconnect(&conn)) { - printf("Reconnect in 10 sec\n"); + LOG_NONE_I("Reconnect in 10 sec\n"); sleep(10); } //sleep(1); @@ -662,7 +661,7 @@ int th_start_client(void *data) //send command over queue } - printf("End client\n"); + LOG_NONE_I("End client\n"); atomic_fetch_sub( &cfg->running,1); return 0; @@ -753,7 +752,7 @@ int th_event_manager(void *data) ecmd->type = TBL_T_LUA; break; default: - ERROR("No such type, something wrong here\n"); + LOG_NONE_E("No such type, something wrong here\n"); } ecmd->callback = single_cmd->callback; @@ -769,17 +768,17 @@ int th_event_manager(void *data) tbl_exec_print_tbl(etbl, TBL_PF_EXEC_ALL); //config mq - printf("Start event thread\n"); + LOG_NONE_I("Start event thread\n"); mq = cfg->mq_listen; //get mq attributes, FIX out/in everytime could be different pal if (mq_ntf_getattr(mq, MQ_OUT, &ptr_out_attr) == -1) { - printf("Cant get attribute\n"); + LOG_NONE_E("Cant get attribute\n"); } if (mq_ntf_getattr(mq, MQ_IN, &ptr_in_attr) == -1) { - printf("Cant get attribute\n"); + LOG_NONE_E("Cant get attribute\n"); } ////////////////////////////////////////////////////////////////////////////// @@ -796,7 +795,7 @@ int th_event_manager(void *data) //maybe its not null in_buf = malloc(in_attr.mq_msgsize); - printf("Start event loop\n"); + LOG_NONE_I("Start event loop\n"); run = 1; while(run) { @@ -815,12 +814,12 @@ int th_event_manager(void *data) //will fail if some of queue have different buffer size param if ((bytes = mq_ntf_read(&mq[i_mq], MQ_OUT, out_buf, out_attr.mq_msgsize)) == -1) { - printf("Cant read output message\n"); + LOG_NONE_D("Cant read output message\n"); } else { out_buf[out_attr.mq_msgsize-1] = 0x0; stats.cnt_ipc_rx += bytes; - printf("Recieve %s\n", out_buf); + LOG_NONE_D("Recieve %s\n", out_buf); } break; } @@ -834,7 +833,7 @@ int th_event_manager(void *data) if (0 == nb_load(nb_req, (unsigned char *)out_buf)) { rpc_req_unmarsh(nb_req, &req); - PRINT("EVENT-REQ: %d-%s(%s) %s-%s-%s\n", req->id, req->method, req->params, req->user, req->mask, req->server); + LOG_NONE_D("EVENT-REQ: %d-%s(%s) %s-%s-%s\n", req->id, req->method, req->params, req->user, req->mask, req->server); //nb_print(nb_req); if (strncmp(req->method,":PRIVMSG",8)) @@ -860,7 +859,7 @@ int th_event_manager(void *data) { if (ret_msg != NULL) { - printf("Ret message %s\n",(char *)ret_msg); + LOG_NONE_D("Ret message %s\n",(char *)ret_msg); //send response resp = rpc_resp_new(ret_msg,"None",req->id); @@ -873,7 +872,7 @@ int th_event_manager(void *data) mq_ntf_write(mq_cur, MQ_IN, buf_nb, nb_resp->size > in_attr.mq_msgsize ? in_attr.mq_msgsize : nb_resp->size); stats.cnt_ipc_tx += nb_resp->size > in_attr.mq_msgsize ? in_attr.mq_msgsize : nb_resp->size; FREE(buf_nb); - printf("EVENT-Write %ld bytes\n", nb_resp->size > in_attr.mq_msgsize ? in_attr.mq_msgsize : nb_resp->size); + LOG_NONE_D("EVENT-Write %ld bytes\n", nb_resp->size > in_attr.mq_msgsize ? in_attr.mq_msgsize : nb_resp->size); FREE(ret_msg); nb_free(nb_resp); @@ -883,24 +882,24 @@ int th_event_manager(void *data) } } else { - ERROR("Command execution error\n"); + LOG_NONE_E("Command execution error\n"); stats.cnt_cmd_err += 1; } sdsfree(exec_params); } else { - PRINT("Command not in a table\n"); + LOG_NONE_E("Command not in a table\n"); } } else { - ERROR("Unsupporetd irc command\n"); + LOG_NONE_E("Unsupporetd irc command\n"); } //PNL();nb_free(nb_req);PNL(); } else { - ERROR("wrong recieved rpc command\n"); + LOG_NONE_E("wrong recieved rpc command\n"); } } @@ -915,8 +914,7 @@ int th_event_manager(void *data) } FREE(out_buf); - PNL(); - printf("End event thread\n"); + LOG_NONE_D("End event thread\n"); atomic_fetch_sub( &cfg->running,1); return 0; @@ -938,6 +936,9 @@ int main(int argc, char **argv) /*set atomic variables to init value*/ atomic_store(&_glbl_id, 0); + /*logging facilities*/ + log_init(); + log_register_sub(LOG_SUB_NONE, LOG_LEVEL_DEBUG, LOG_LOC_STDIO, NULL); /*Load configs*/ cfg = arg_load(argc, argv, agni_cmd_args); @@ -983,15 +984,15 @@ int main(int argc, char **argv) srvc->stack = malloc(STACK_SIZE); //NULL will brake everything ;) if (srvc->stack == NULL) { - ERROR("BLow"); + LOG_NONE_F("BLow"); } srvc->user = alloc_new_str(isrvc.user); - IF_NULL(srvc->user,{},{ERROR("Cant allocate srvc->user\n")}); + IF_NULL(srvc->user,{},{LOG_NONE_F("Cant allocate srvc->user\n")}); //srvc->password = alloc_new_str(isrvc.password); srvc->server = alloc_new_str(isrvc.server); - IF_NULL(srvc->user,{},{ERROR("Cant allocate srvc->server\n")}); + IF_NULL(srvc->user,{},{LOG_NONE_F("Cant allocate srvc->server\n")}); srvc->port = alloc_new_str(isrvc.port); - IF_NULL(srvc->user,{},{ERROR("Cant allocate srvc->port\n")}); + IF_NULL(srvc->user,{},{LOG_NONE_F("Cant allocate srvc->port\n")}); srvc->ssl = isrvc.ssl; //srvc->channels = isrvc.channels; for (j=0;j<SERV_CHAN_MAX_NUM;j++) @@ -999,7 +1000,7 @@ int main(int argc, char **argv) if (isrvc.channels[j]) { srvc->channels[j] = alloc_new_str(isrvc.channels[j]); - IF_NULL(srvc->channels[j],{},{ERROR("Cannt alloc srvc->channels[j]\n");}) + IF_NULL(srvc->channels[j],{},{LOG_NONE_F("Cannt alloc srvc->channels[j]\n");}) } else { srvc->channels[j] = NULL; @@ -1013,18 +1014,18 @@ int main(int argc, char **argv) /* initalise posix mq */ if (0 != mq_ntf_open(&mq_array[i], i)) { - printf("Couldnt open mq_ntf_open\n"); + LOG_NONE_F("Couldnt open mq_ntf_open\n"); } srvc->mq = &mq_array[i]; //try to drain mq mq_ntf_drain(&mq_array[i]); - PRINT("SERVER:%s PORT:%s USER:%s\n", srvc->server, srvc->port, srvc->user); + LOG_NONE_I("SERVER:%s PORT:%s USER:%s\n", srvc->server, srvc->port, srvc->user); /* clone new proc */ if (-1 == clone(th_start_client, srvc->stack+STACK_SIZE, CLONE_VM|CLONE_FILES, (void *)srvc)) { - ERROR("Cant allocate new server process\n"); + LOG_NONE_E("Cant allocate new server process\n"); } } @@ -1032,20 +1033,20 @@ int main(int argc, char **argv) evhnd_cfg = malloc(sizeof(event_handler_cfg)); if (evhnd_cfg == NULL) { - ERROR("cant allocate event handler\n"); + LOG_NONE_E("cant allocate event handler\n"); } memset(evhnd_cfg, 0, sizeof(event_handler_cfg)); evhnd_cfg->stack = malloc(EVENT_HND_STACK_SIZE); if (evhnd_cfg->stack == NULL) { - ERROR("Cant allocate event handler stack\n"); + LOG_NONE_E("Cant allocate event handler stack\n"); } atomic_store(&evhnd_cfg->running, 0); evhnd_cfg->mq_num = cnt_servers; evhnd_cfg->mq_listen = mq_array; if (-1 == clone(th_event_manager, evhnd_cfg->stack+EVENT_HND_STACK_SIZE, CLONE_VM|CLONE_FILES, (void *)evhnd_cfg)) { - ERROR("Couldnt create process\n"); + LOG_NONE_E("Couldnt create process\n"); } //PNL(); @@ -1071,7 +1072,7 @@ int main(int argc, char **argv) FREE(cfg_list); - ERROR("Exit\n"); + LOG_NONE_E("Exit\n"); return 0; }
\ No newline at end of file |