From: erickson Date: Tue, 29 Nov 2005 15:01:28 +0000 (+0000) Subject: moved C code to a unified logging framework which currently supports syslogging and... X-Git-Tag: osrf_rel_2_0_1~1290 X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=9eb990f7de40324c363c2d849de0705fbcac6155;p=OpenSRF.git moved C code to a unified logging framework which currently supports syslogging and file logging added the following to the config framework: syslog local3 git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@583 9efc2488-bf62-4759-914b-345cdb29e865 --- diff --git a/examples/opensrf_core.xml.example b/examples/opensrf_core.xml.example index f15955b..6a82325 100644 --- a/examples/opensrf_core.xml.example +++ b/examples/opensrf_core.xml.example @@ -27,7 +27,8 @@ 5269 10.0.0.3 3 - /openils/var/log/jabber.log + syslog + local2 diff --git a/src/Makefile b/src/Makefile index 1a2a9b2..d29e792 100644 --- a/src/Makefile +++ b/src/Makefile @@ -32,11 +32,10 @@ OPENSRF_TARGETS = libtransport/transport_session.o \ libstack/osrf_transgroup.o \ libstack/osrf_list.o \ libstack/osrf_hash.o \ - libstack/osrf_log.o \ utils/socket_bundle.o \ utils/string_array.o \ utils/utils.o \ - utils/logging.o \ + utils/log.o \ utils/md5.o \ utils/sha.o \ @@ -53,14 +52,13 @@ OPENSRF_HEADERS = libtransport/transport_session.h \ libstack/osrf_application.h \ libstack/osrf_cache.h \ libstack/xml_utils.h \ - libstack/osrf_log.h \ libstack/osrf_transgroup.h \ libstack/osrf_list.h \ libstack/osrf_hash.h \ utils/socket_bundle.h \ utils/string_array.h \ utils/utils.h \ - utils/logging.h \ + utils/log.h \ utils/md5.h \ utils/sha.h \ diff --git a/src/c-apps/osrf_dbmath.c b/src/c-apps/osrf_dbmath.c index b9d8c80..0b930cd 100644 --- a/src/c-apps/osrf_dbmath.c +++ b/src/c-apps/osrf_dbmath.c @@ -1,7 +1,7 @@ #include "opensrf/osrf_app_session.h" #include "opensrf/osrf_application.h" #include "objson/object.h" -#include "opensrf/osrf_log.h" +#include "opensrf/log.h" #define MODULENAME "opensrf.dbmath" diff --git a/src/c-apps/osrf_math.c b/src/c-apps/osrf_math.c index a9b955f..798c631 100644 --- a/src/c-apps/osrf_math.c +++ b/src/c-apps/osrf_math.c @@ -1,7 +1,7 @@ #include "opensrf/osrf_app_session.h" #include "opensrf/osrf_application.h" #include "objson/object.h" -#include "opensrf/osrf_log.h" +#include "opensrf/log.h" #define MODULENAME "opensrf.math" @@ -49,8 +49,6 @@ int osrfMathRun( osrfMethodContext* ctx ) { OSRF_METHOD_VERIFY_CONTEXT(ctx); /* see osrf_application.h */ - osrfLog( OSRF_DEBUG, "Running opensrf.math %s", ctx->method->name ); - /* collect the request params */ jsonObject* x = jsonObjectGetIndex(ctx->params, 0); jsonObject* y = jsonObjectGetIndex(ctx->params, 1); @@ -64,6 +62,9 @@ int osrfMathRun( osrfMethodContext* ctx ) { if( a && b ) { + osrfLogActivity( "Running opensrf.math %s [ %s : %s ]", + ctx->method->name, a, b ); + /* construct a new params object to send to dbmath */ jsonObject* newParams = jsonParseString( "[ %s, %s ]", a, b ); free(a); free(b); diff --git a/src/c-apps/osrf_version.c b/src/c-apps/osrf_version.c index a2187f0..e134b2a 100644 --- a/src/c-apps/osrf_version.c +++ b/src/c-apps/osrf_version.c @@ -2,7 +2,7 @@ #include "opensrf/osrf_application.h" #include "objson/object.h" #include "opensrf/utils.h" -#include "opensrf/osrf_log.h" +#include "opensrf/log.h" #define OSRF_VERSION_CACHE_TIME 300 @@ -40,7 +40,7 @@ int osrfVersion( osrfMethodContext* ctx ) { free(json); if( cachedmd5 ) { - osrfLog( OSRF_DEBUG, "Found %s object in cache, returning....", cachedmd5 ); + osrfLogDebug( "Found %s object in cache, returning....", cachedmd5 ); jsonObject* resp = jsonNewObject(cachedmd5); osrfAppRespondComplete( ctx, resp ); jsonObjectFree(resp); @@ -79,7 +79,7 @@ int osrfVersion( osrfMethodContext* ctx ) { osrfAppRespondComplete( ctx, resp ); jsonObjectFree(resp); osrfAppSessionFree(ses); - osrfLog(OSRF_DEBUG, "Found version string %s, caching and returning...", resultmd5 ); + osrfLogDebug("Found version string %s, caching and returning...", resultmd5 ); osrfCachePutString( paramsmd5, resultmd5, OSRF_VERSION_CACHE_TIME ); free(resultmd5); free(paramsmd5); diff --git a/src/gateway/mod_ils_gateway.c b/src/gateway/mod_ils_gateway.c index 7b01d1e..1ed4004 100644 --- a/src/gateway/mod_ils_gateway.c +++ b/src/gateway/mod_ils_gateway.c @@ -43,8 +43,10 @@ static void mod_ils_gateway_child_init(apr_pool_t *p, server_rec *s) { cfg = ils_gateway_config_file; #endif - if( ! osrf_system_bootstrap_client( cfg, CONFIG_CONTEXT) ) - fatal_handler("Unable to load gateway config file..."); + if( ! osrf_system_bootstrap_client( cfg, CONFIG_CONTEXT) ) { + osrfLogError("Unable to load gateway config file..."); + return; + } fprintf(stderr, "Bootstrapping %d\n", getpid() ); fflush(stderr); } @@ -75,7 +77,7 @@ static int mod_ils_gateway_method_handler (request_rec *r) { /* verify we are connected */ if(!osrf_system_get_transport_client()) { - fatal_handler("Bootstrap Failed, no transport client"); + osrfLogError("Bootstrap Failed, no transport client"); return HTTP_INTERNAL_SERVER_ERROR; } @@ -87,7 +89,7 @@ static int mod_ils_gateway_method_handler (request_rec *r) { ap_setup_client_block(r,REQUEST_CHUNKED_DECHUNK); if(! ap_should_client_block(r)) { - warning_handler("No Post Body"); + osrfLogWarning("No Post Body"); } char body[1025]; @@ -95,7 +97,7 @@ static int mod_ils_gateway_method_handler (request_rec *r) { buffer = buffer_init(1025); while(ap_get_client_block(r, body, 1024)) { - debug_handler("Apache read POST block data: %s\n", body); + osrfLogDebug("Apache read POST block data: %s\n", body); buffer_add( buffer, body ); memset(body,0,1025); } @@ -113,11 +115,11 @@ static int mod_ils_gateway_method_handler (request_rec *r) { } - debug_handler("params args are %s", arg); + osrfLogDebug("params args are %s", arg); if( ! arg || !arg[0] ) { /* we received no request */ - warning_handler("No Args"); + osrfLogWarning("No Args"); return OK; } @@ -145,17 +147,17 @@ static int mod_ils_gateway_method_handler (request_rec *r) { } - info_handler("\nPerforming(%d): service %s | method %s |", + osrfLogInfo("\nPerforming(%d): service %s | method %s |", getpid(), service, method ); int k; for( k = 0; k!= sarray->size; k++ ) { - info_handler( "param %s", string_array_get_string(sarray,k)); + osrfLogInfo( "param %s", string_array_get_string(sarray,k)); } osrf_app_session* session = osrf_app_client_session_init(service); - debug_handler("MOD session service: %s", session->remote_service ); + osrfLogDebug("MOD session service: %s", session->remote_service ); int req_id = osrf_app_session_make_req( session, NULL, method, 1, sarray ); string_array_destroy(sarray); @@ -194,7 +196,7 @@ static int mod_ils_gateway_method_handler (request_rec *r) { jsonObjectSetKey(exception, "is_err", json_parse_string("1")); jsonObjectSetKey(exception, "err_msg", jsonNewObject(exc_buffer->buf) ); - warning_handler("*** Looks like we got a " + osrfLogWarning("*** Looks like we got a " "server exception\n%s", exc_buffer->buf ); buffer_free(exc_buffer); @@ -244,14 +246,13 @@ static int mod_ils_gateway_method_handler (request_rec *r) { buffer_free(result_data); if(content) { - info_handler( "APACHE writing data to web client: %s", content ); + osrfLogInfo( "APACHE writing data to web client: %s", content ); ap_rputs(content,r); free(content); } osrf_app_session_request_finish( session, req_id ); - debug_handler("gateway process message successfully"); - + osrfLogDebug("gateway process message successfully"); osrf_app_session_destroy(session); return OK; diff --git a/src/jserver/osrf_chat.c b/src/jserver/osrf_chat.c index 368ed96..ea72e7a 100644 --- a/src/jserver/osrf_chat.c +++ b/src/jserver/osrf_chat.c @@ -16,6 +16,7 @@ GNU General Public License for more details. #include "osrf_chat.h" #include #include +#include int __osrfChatXMLErrorOcurred = 0; int __osrfChatClientSentDisconnect = 0; @@ -66,7 +67,7 @@ static void chatdbg( osrfChatServer* server ) { node->to, node->resource, node->username, node->domain, node->authkey, node->type ); } - debug_handler("DEBUG:\n%s", buf->buf ); + osrfLogDebug("DEBUG:\n%s", buf->buf ); buffer_free(buf); osrfListIteratorFree(itr); } @@ -181,7 +182,7 @@ int osrfChatServerWait( osrfChatServer* server ) { if(!server) return -1; while(1) { if(socket_wait_all(server->mgr, -1) < 0) - warning_handler( "jserver_wait(): socket_wait_all() returned error"); + osrfLogWarning( "jserver_wait(): socket_wait_all() returned error"); } return -1; } @@ -206,16 +207,16 @@ void osrfChatHandleData( void* cs, osrfChatNode* node = osrfListGetIndex( server->nodeList, sockid ); if(node) - debug_handler("Found node for sockid %d with state %d", sockid, node->state); + osrfLogDebug("Found node for sockid %d with state %d", sockid, node->state); if(!node) { - debug_handler("Adding new connection for sockid %d", sockid ); + osrfLogDebug("Adding new connection for sockid %d", sockid ); node = osrfChatAddNode( server, sockid ); } if(node) { if( (osrfChatPushData( server, node, data ) == -1) ) { - warning_handler("Node at socket %d received bad XML, disconnecting...", sockid ); + osrfLogWarning("Node at socket %d received bad XML, disconnecting...", sockid ); osrfChatSendRaw( node, OSRF_CHAT_PARSE_ERROR ); osrfChatRemoveNode( server, node ); } @@ -274,7 +275,7 @@ int osrfChatSend( osrfChatServer* cs, osrfChatNode* node, char* toAddr, char* fr if( eq( dombuf, cs->domain ) ) { /* this is to a user we host */ - info_handler("Sending message on local connection\nfrom: %s\nto: %s", fromAddr, toAddr ); + osrfLogInfo("Sending message on local connection\nfrom: %s\nto: %s", fromAddr, toAddr ); osrfChatNode* tonode = osrfHashGet(cs->nodeHash, toAddr); if(tonode) { osrfChatSendRaw( tonode, msgXML ); @@ -282,7 +283,7 @@ int osrfChatSend( osrfChatServer* cs, osrfChatNode* node, char* toAddr, char* fr } else { /* send an error message saying we don't have this connection */ - warning_handler("We have no connection for %s", toAddr); + osrfLogWarning("We have no connection for %s", toAddr); char* xml = va_list_to_string( OSRF_CHAT_NO_RECIPIENT, toAddr, fromAddr ); osrfChatSendRaw( node, xml ); free(xml); @@ -293,18 +294,18 @@ int osrfChatSend( osrfChatServer* cs, osrfChatNode* node, char* toAddr, char* fr osrfChatNode* tonode = osrfHashGet(cs->nodeHash, dombuf); if(tonode) { if( tonode->state == OSRF_CHAT_STATE_CONNECTED ) { - debug_handler("Routing message to server %s", dombuf); + osrfLogDebug("Routing message to server %s", dombuf); osrfChatSendRaw( tonode, msgXML ); } else { - info_handler("Received s2s message and we're still trying to connect...caching"); + osrfLogInfo("Received s2s message and we're still trying to connect...caching"); osrfListPush( tonode->msgs, strdup(msgXML) ); } } else { if( osrfChatInitS2S( cs, dombuf, toAddr, msgXML ) != 0 ) { - warning_handler("We are unable to connect to remote server %s for recipient %s", dombuf, toAddr); + osrfLogWarning("We are unable to connect to remote server %s for recipient %s", dombuf, toAddr); char* xml = va_list_to_string( OSRF_CHAT_NO_RECIPIENT, toAddr, fromAddr ); osrfChatSendRaw( node, xml ); free(xml); @@ -322,7 +323,7 @@ void osrfChatCacheS2SMessage( char* toAddr, char* msgXML, osrfChatNode* snode ) osrfChatS2SMessage* msg = safe_malloc(sizeof(osrfChatS2SMessage)); msg->toAddr = strdup(toAddr); msg->msgXML = strdup(msgXML); - info_handler("Pushing client message onto s2s queue waiting for connect... "); + osrfLogInfo("Pushing client message onto s2s queue waiting for connect... "); osrfListPush( snode->msgs, msgXML ); } */ @@ -331,14 +332,14 @@ void osrfChatCacheS2SMessage( char* toAddr, char* msgXML, osrfChatNode* snode ) int osrfChatInitS2S( osrfChatServer* cs, char* remote, char* toAddr, char* msgXML ) { if(!(cs && remote && toAddr && msgXML)) return -1; - info_handler("Initing server2server connection to domain %s", remote ); + osrfLogInfo("Initing server2server connection to domain %s", remote ); osrfChatNode* snode = osrfNewChatS2SNode( cs->domain, remote ); snode->parent = cs; /* try to connect to the remote site */ snode->sockid = socket_open_tcp_client(cs->mgr, cs->s2sport, remote); if(snode->sockid < 1) { - warning_handler("Unable to connect to remote server at %s", remote ); + osrfLogWarning("Unable to connect to remote server at %s", remote ); return -1; } @@ -351,7 +352,7 @@ int osrfChatInitS2S( osrfChatServer* cs, char* remote, char* toAddr, char* msgXM /* send the initial s2s request */ osrfChatSendRaw( snode, OSRF_CHAT_S2S_INIT ); - debug_handler("Added new s2s node..."); + osrfLogDebug("Added new s2s node..."); chatdbg(cs); return 0; @@ -365,7 +366,7 @@ int osrfChatPushData( osrfChatServer* server, osrfChatNode* node, char* data ) { chatdbg(server); - debug_handler("pushing data into xml parser for node %d with state %d:\n%s", + osrfLogDebug("pushing data into xml parser for node %d with state %d:\n%s", node->sockid, node->state, data); node->inparse = 1; xmlParseChunk(node->parserCtx, data, strlen(data), 0); @@ -390,7 +391,7 @@ int osrfChatPushData( osrfChatServer* server, osrfChatNode* node, char* data ) { void osrfChatStartStream( void* blob ) { - debug_handler("Starting new client stream..."); + osrfLogDebug("Starting new client stream..."); } @@ -401,14 +402,14 @@ void osrfChatStartElement( void* blob, const xmlChar *name, const xmlChar **atts int status = -1; char* nm = (char*) name; - debug_handler("Starting element %s with namespace %s and node state %d", + osrfLogDebug("Starting element %s with namespace %s and node state %d", nm, xmlSaxAttr(atts, "xmlns"), node->state ); switch( node->state ) { case OSRF_CHAT_STATE_NONE: status = osrfChatHandleNewConnection( node, nm, atts ); - debug_handler("After NewConnection we have state %d", node->state); + osrfLogDebug("After NewConnection we have state %d", node->state); break; case OSRF_CHAT_STATE_CONNECTING: @@ -457,7 +458,7 @@ void osrfChatStartElement( void* blob, const xmlChar *name, const xmlChar **atts osrfChatParseError( node, "We don't know how to handle the XML data received" ); } -#define CHAT_CHECK_VARS(x,y,z) if(!(x && y)) return -1; if(z) debug_handler(z); +#define CHAT_CHECK_VARS(x,y,z) if(!(x && y)) return -1; if(z) osrfLogDebug(z); @@ -485,7 +486,7 @@ int osrfChatHandleS2SConnected( osrfChatNode* node, const char* name, const xmlC char* from = (char*) xmlGetProp(xmlDocGetRootElement(doc), BAD_CAST "from"); char* to = (char*) xmlGetProp(xmlDocGetRootElement(doc), BAD_CAST "to"); osrfChatSend( node->parent, node, to, from, xml ); - debug_handler("Sending cached message from %s to %s", from, to); + osrfLogDebug("Sending cached message from %s to %s", from, to); xmlFree(to); xmlFree(from); xmlFreeDoc(doc); } @@ -498,7 +499,7 @@ int osrfChatHandleS2SConnected( osrfChatNode* node, const char* name, const xmlC } if(status == 0) { - info_handler("Successfully made S2S connection to %s", node->remote ); + osrfLogInfo("Successfully made S2S connection to %s", node->remote ); node->state = OSRF_CHAT_STATE_CONNECTED; node->xmlstate = 0; } @@ -523,17 +524,17 @@ int osrfChatHandleNewConnection( osrfChatNode* node, const char* name, const xml if(!domain) return -1; if(!eq(domain, node->domain)) { - warning_handler("Client attempting to connect to invalid domain"); + osrfLogWarning("Client attempting to connect to invalid domain"); return -1; } char* buf = va_list_to_string( OSRF_CHAT_START_STREAM, domain, node->authkey ); node->state = OSRF_CHAT_STATE_CONNECTING; - debug_handler("Server node %d setting state to OSRF_CHAT_STATE_CONNECTING[%d]", + osrfLogDebug("Server node %d setting state to OSRF_CHAT_STATE_CONNECTING[%d]", node->sockid, node->state ); - debug_handler("Server responding to connect message with\n%s\n", buf ); + osrfLogDebug("Server responding to connect message with\n%s\n", buf ); osrfChatSendRaw( node, buf ); free(buf); return 0; @@ -541,7 +542,7 @@ int osrfChatHandleNewConnection( osrfChatNode* node, const char* name, const xml /* server to server init */ if(eq(ns, "jabber:server")) { /* client connection */ - info_handler("We received a new server 2 server connection, generating auth key..."); + osrfLogInfo("We received a new server 2 server connection, generating auth key..."); char* xml = va_list_to_string( OSRF_CHAT_S2S_CHALLENGE, node->authkey ); osrfChatSendRaw( node, xml ); free(xml); @@ -564,7 +565,7 @@ char* osrfChatMkAuthKey() { int osrfChatHandleConnecting( osrfChatNode* node, const char* name, const xmlChar** atts ) { CHAT_CHECK_VARS(node, name, "osrfChatHandleConnecting()"); - debug_handler("Handling connect node %s", name ); + osrfLogDebug("Handling connect node %s", name ); if(eq(name, "iq")) node->xmlstate |= OSRF_CHAT_STATE_INIQ; else if(eq(name,"username")) node->xmlstate |= OSRF_CHAT_STATE_INUSERNAME; @@ -604,12 +605,12 @@ int osrfChatHandleConnected( osrfChatNode* node, const char* name, const xmlChar /* takes s2s secret, hashdomain, and the s2s auth token */ static char* osrfChatGenerateS2SKey( char* secret, char* hashdomain, char* authtoken ) { if(!(secret && hashdomain && authtoken)) return NULL; - info_handler("Generating s2s key with auth token: %s", authtoken ); + osrfLogInfo("Generating s2s key with auth token: %s", authtoken ); char* a = shahash(secret); - debug_handler("S2S secret hash: %s", a); + osrfLogDebug("S2S secret hash: %s", a); char* b = va_list_to_string("%s%s", a, hashdomain); char* c = shahash(b); - debug_handler("S2S intermediate hash: %s", c); + osrfLogDebug("S2S intermediate hash: %s", c); char* d = va_list_to_string("%s%s", c, authtoken); char* e = strdup(shahash(d)); free(b); free(d); @@ -626,7 +627,7 @@ int osrfChatHandleS2SChallenge( osrfChatNode* node, const char* name, const xmlC /* we use our domain in the s2s challenge hash */ char* d = osrfChatGenerateS2SKey(node->parent->secret, node->domain, id ); char* e = va_list_to_string(OSRF_CHAT_S2S_RESPONSE, node->remote, node->domain, d ); - info_handler("Answering s2s challenge with key: %s", e ); + osrfLogInfo("Answering s2s challenge with key: %s", e ); osrfChatSendRaw( node, e ); free(d); free(e); node->state = OSRF_CHAT_STATE_S2S_VERIFY; @@ -672,7 +673,7 @@ void osrfChatEndElement( void* blob, const xmlChar* name ) { char* string = xmlDocToString(node->msgDoc, 0 ); char* from = (char*) xmlGetProp(msg, BAD_CAST "from"); - debug_handler( "Routing message to %s\n%s\n", node->to, from, string ); + osrfLogDebug( "Routing message to %s\n%s\n", node->to, from, string ); osrfChatSend( node->parent, node, node->to, from, string ); xmlFree(from); free(string); @@ -687,12 +688,12 @@ void osrfChatEndElement( void* blob, const xmlChar* name ) { node->remote = va_list_to_string( "%s@%s/%s", node->username, node->domain, node->resource ); - info_handler("%s successfully logged in", node->remote ); + osrfLogInfo("%s successfully logged in", node->remote ); - debug_handler("Setting remote address to %s", node->remote ); + osrfLogDebug("Setting remote address to %s", node->remote ); osrfChatSendRaw( node, OSRF_CHAT_LOGIN_OK ); if(osrfHashGet( node->parent->nodeHash, node->remote ) ) { - warning_handler("New node replaces existing node for remote id %s", node->remote); + osrfLogWarning("New node replaces existing node for remote id %s", node->remote); osrfHashRemove(node->parent->nodeHash, node->remote); } osrfHashSet( node->parent->nodeHash, node, node->remote ); @@ -708,7 +709,7 @@ void osrfChatHandleCharacter( void* blob, const xmlChar *ch, int len) { osrfChatNode* node = (osrfChatNode*) blob; /* - debug_handler("Char Handler: state %d, xmlstate %d, chardata %s", + osrfLogDebug("Char Handler: state %d, xmlstate %d, chardata %s", node->state, node->xmlstate, (char*) ch ); */ @@ -742,9 +743,9 @@ void osrfChatHandleCharacter( void* blob, const xmlChar *ch, int len) { (node->xmlstate & OSRF_CHAT_STATE_INS2SRESULT) ) { char* key = strndup((char*) ch, len); - debug_handler("Got s2s key from %s : %s", node->remote, key ); + osrfLogDebug("Got s2s key from %s : %s", node->remote, key ); char* e = osrfChatGenerateS2SKey(node->parent->secret, node->remote, node->authkey ); - info_handler("\nReceived s2s key from server: %s\nKey should be: %s", key, e ); + osrfLogInfo("\nReceived s2s key from server: %s\nKey should be: %s", key, e ); if(eq(key, e)) { char* msg = va_list_to_string(OSRF_CHAT_S2S_VERIFY_REQUEST, @@ -755,7 +756,7 @@ void osrfChatHandleCharacter( void* blob, const xmlChar *ch, int len) { node->xmlstate = 0; } else { - warning_handler("Server2Server keys do not match!"); + osrfLogWarning("Server2Server keys do not match!"); } /* do the hash dance again */ diff --git a/src/jserver/osrf_chat.h b/src/jserver/osrf_chat.h index 292e143..5dfac2f 100644 --- a/src/jserver/osrf_chat.h +++ b/src/jserver/osrf_chat.h @@ -21,7 +21,7 @@ GNU General Public License for more details. #include "opensrf/utils.h" #include "opensrf/osrf_hash.h" #include "opensrf/osrf_list.h" -#include "opensrf/logging.h" +#include "opensrf/log.h" #include "opensrf/xml_utils.h" #include "opensrf/socket_bundle.h" #include "opensrf/sha.h" diff --git a/src/jserver/osrf_chat_main.c b/src/jserver/osrf_chat_main.c index 64ae958..9c13f26 100644 --- a/src/jserver/osrf_chat_main.c +++ b/src/jserver/osrf_chat_main.c @@ -1,7 +1,8 @@ #include "osrf_chat.h" #include "opensrf/osrfConfig.h" #include -#include "opensrf/logging.h" +#include "opensrf/log.h" +#include int main( int argc, char* argv[] ) { @@ -32,7 +33,9 @@ int main( int argc, char* argv[] ) { int port = atoi(sport); int s2port = atoi(s2sport); int level = atoi(llevel); - log_init(level, lfile); + //log_init(level, lfile); + osrfLogInit( OSRF_LOG_TYPE_SYSLOG, "chopchop", level); + osrfLogSetSyslogFacility(LOG_LOCAL2); fprintf(stderr, "Attempting to launch ChopChop with:\n" "domain: %s\nport: %s\nlisten address: %s\nlog level: %s\nlog file: %s\n", @@ -40,14 +43,15 @@ int main( int argc, char* argv[] ) { osrfChatServer* server = osrfNewChatServer(domain, secret, s2port); - if( osrfChatServerConnect( server, port, s2port, listenaddr ) != 0 ) - return fatal_handler("ChopChop unable to bind to port %d on %s", port, listenaddr); + if( osrfChatServerConnect( server, port, s2port, listenaddr ) != 0 ) { + osrfLogError("ChopChop unable to bind to port %d on %s", port, listenaddr); + return -1; + } daemonize(); osrfChatServerWait( server ); osrfChatServerFree( server ); - log_free(); osrfConfigFree(cfg); return 0; diff --git a/src/libstack/Makefile b/src/libstack/Makefile index d773262..ad65bfa 100644 --- a/src/libstack/Makefile +++ b/src/libstack/Makefile @@ -14,7 +14,6 @@ TARGETS = osrf_message.o \ osrfConfig.o \ osrf_application.o \ osrf_cache.o \ - osrf_log.o \ osrf_transgroup.o \ osrf_list.o \ osrf_hash.o \ @@ -29,7 +28,6 @@ HEADERS = osrf_message.h \ osrfConfig.h \ osrf_application.h \ osrf_cache.h \ - osrf_log.h \ osrf_transgroup.h \ osrf_list.h \ osrf_hash.h \ @@ -54,7 +52,6 @@ osrf_prefork.o: osrf_prefork.c osrf_prefork.h osrfConfig.o: osrfConfig.c osrfConfig.h xml_utils.o osrf_application.o: osrf_application.c osrf_application.h osrf_cache.o: osrf_cache.c osrf_cache.h -osrf_log.o: osrf_log.c osrf_log.h osrf_list.o: osrf_list.c osrf_list.h osrf_hash.o: osrf_hash.c osrf_hash.h diff --git a/src/libstack/osrfConfig.c b/src/libstack/osrfConfig.c index 5141984..ce4beec 100644 --- a/src/libstack/osrfConfig.c +++ b/src/libstack/osrfConfig.c @@ -45,7 +45,7 @@ osrfConfig* osrfConfigInit(char* configFile, char* configContext) { xmlDocPtr doc = xmlParseFile(configFile); if(!doc) { - warning_handler( "Unable to parse XML config file %s", configFile); + osrfLogWarning( "Unable to parse XML config file %s", configFile); return NULL; } @@ -53,7 +53,7 @@ osrfConfig* osrfConfigInit(char* configFile, char* configContext) { xmlFreeDoc(doc); if(!cfg->config) { - warning_handler("xmlDocToJSON failed for config %s", configFile); + osrfLogWarning("xmlDocToJSON failed for config %s", configFile); return NULL; } @@ -64,7 +64,7 @@ char* osrfConfigGetValue(osrfConfig* cfg, char* path, ...) { if(!path) return NULL; if(!cfg) cfg = __osrfConfigDefault; - if(!cfg) { warning_handler("No Confif object!"); return NULL; } + if(!cfg) { osrfLogWarning("No Confif object!"); return NULL; } VA_LIST_TO_STRING(path); @@ -89,7 +89,7 @@ int osrfConfigGetValueList(osrfConfig* cfg, osrfStringArray* arr, char* path, .. if(!arr || !path) return 0; if(!cfg) cfg = __osrfConfigDefault; - if(!cfg) { return warning_handler("No Config object!"); } + if(!cfg) { osrfLogWarning("No Config object!"); return -1;} VA_LIST_TO_STRING(path); diff --git a/src/libstack/osrf_app_session.c b/src/libstack/osrf_app_session.c index c41945d..d3b25bb 100644 --- a/src/libstack/osrf_app_session.c +++ b/src/libstack/osrf_app_session.c @@ -50,7 +50,7 @@ void _osrf_app_request_free( void * req ){ /** Pushes the given message onto the list of 'responses' to this request */ void _osrf_app_request_push_queue( osrf_app_request* req, osrf_message* result ){ if(req == NULL || result == NULL) return; - debug_handler( "App Session pushing request [%d] onto request queue", result->thread_trace ); + osrfLogDebug( "App Session pushing request [%d] onto request queue", result->thread_trace ); if(req->result == NULL) req->result = result; else { @@ -72,7 +72,7 @@ void osrf_app_session_request_finish( void osrf_app_session_request_reset_timeout( osrf_app_session* session, int req_id ) { if(session == NULL) return; - debug_handler("Resetting request timeout %d", req_id ); + osrfLogDebug("Resetting request timeout %d", req_id ); osrf_app_request* req = osrfListGetIndex( session->request_queue, req_id ); if(req == NULL) return; req->reset_timeout = 1; @@ -99,13 +99,13 @@ osrf_message* _osrf_app_request_recv( osrf_app_request* req, int timeout ) { while( remaining >= 0 ) { /* tell the session to wait for stuff */ - debug_handler( "In app_request receive with remaining time [%d]", (int) remaining ); + osrfLogDebug( "In app_request receive with remaining time [%d]", (int) remaining ); osrf_app_session_queue_wait( req->session, 0 ); if( req->result != NULL ) { /* if we received anything */ /* pop off the first message in the list */ - debug_handler( "app_request_recv received a message, returning it"); + osrfLogDebug( "app_request_recv received a message, returning it"); osrf_message* ret_msg = req->result; osrf_message* tmp_msg = ret_msg->next; req->result = tmp_msg; @@ -119,7 +119,7 @@ osrf_message* _osrf_app_request_recv( osrf_app_request* req, int timeout ) { if( req->result != NULL ) { /* if we received anything */ /* pop off the first message in the list */ - debug_handler( "app_request_recv received a message, returning it"); + osrfLogDebug( "app_request_recv received a message, returning it"); osrf_message* ret_msg = req->result; osrf_message* tmp_msg = ret_msg->next; req->result = tmp_msg; @@ -131,13 +131,13 @@ osrf_message* _osrf_app_request_recv( osrf_app_request* req, int timeout ) { if(req->reset_timeout) { remaining = (time_t) timeout; req->reset_timeout = 0; - debug_handler("Recevied a timeout reset"); + osrfLogDebug("Recevied a timeout reset"); } else { remaining -= (int) (time(NULL) - start); } } - debug_handler("Returning NULL from app_request_recv after timeout"); + osrfLogDebug("Returning NULL from app_request_recv after timeout"); return NULL; } @@ -145,7 +145,7 @@ osrf_message* _osrf_app_request_recv( osrf_app_request* req, int timeout ) { int _osrf_app_request_resend( osrf_app_request* req ) { if(req == NULL) return 0; if(!req->complete) { - debug_handler( "Resending request [%d]", req->request_id ); + osrfLogDebug( "Resending request [%d]", req->request_id ); return _osrf_app_session_send( req->session, req->payload ); } return 1; @@ -185,7 +185,7 @@ osrf_app_session* osrf_app_client_session_init( char* remote_service ) { session->transport_handle = osrf_system_get_transport_client(); if( session->transport_handle == NULL ) { - warning_handler("No transport client for service 'client'"); + osrfLogWarning("No transport client for service 'client'"); return NULL; } @@ -210,10 +210,10 @@ osrf_app_session* osrf_app_client_session_init( char* remote_service ) { #ifdef ASSUME_STATELESS session->stateless = 1; - debug_handler("%s session is stateless", remote_service ); + osrfLogDebug("%s session is stateless", remote_service ); #else session->stateless = 0; - debug_handler("%s session is NOT stateless", remote_service ); + osrfLogDebug("%s session is NOT stateless", remote_service ); #endif /* build a chunky, random session id */ @@ -222,7 +222,7 @@ osrf_app_session* osrf_app_client_session_init( char* remote_service ) { sprintf(id, "%lf.%d%d", get_timestamp_millis(), (int)time(NULL), getpid()); session->session_id = strdup(id); - debug_handler( "Building a new client session with id [%s] [%s]", + osrfLogDebug( "Building a new client session with id [%s] [%s]", session->remote_service, session->session_id ); session->thread_trace = 0; @@ -236,7 +236,7 @@ osrf_app_session* osrf_app_client_session_init( char* remote_service ) { osrf_app_session* osrf_app_server_session_init( char* session_id, char* our_app, char* remote_id ) { - info_handler("Initing server session with session id %s, service %s," + osrfLogInfo("Initing server session with session id %s, service %s," " and remote_id %s", session_id, our_app, remote_id ); osrf_app_session* session = osrf_app_session_find_session( session_id ); @@ -246,7 +246,7 @@ osrf_app_session* osrf_app_server_session_init( session->transport_handle = osrf_system_get_transport_client(); if( session->transport_handle == NULL ) { - warning_handler("No transport client for service '%s'", our_app ); + osrfLogWarning("No transport client for service '%s'", our_app ); return NULL; } @@ -323,11 +323,11 @@ int osrf_app_session_make_req( osrf_app_request* req = _osrf_app_request_init( session, req_msg ); if(_osrf_app_session_send( session, req_msg ) ) { - warning_handler( "Error sending request message [%d]", session->thread_trace ); + osrfLogWarning( "Error sending request message [%d]", session->thread_trace ); return -1; } - debug_handler( "Pushing [%d] onto requeust queue for session [%s] [%s]", + osrfLogDebug( "Pushing [%d] onto requeust queue for session [%s] [%s]", req->request_id, session->remote_service, session->session_id ); osrfListSet( session->request_queue, req, req->request_id ); return req->request_id; @@ -357,7 +357,7 @@ void osrf_app_session_reset_remote( osrf_app_session* session ){ return; free(session->remote_id); - debug_handler( "App Session [%s] [%s] resetting remote id to %s", + osrfLogDebug( "App Session [%s] [%s] resetting remote id to %s", session->remote_service, session->session_id, session->orig_remote_id ); session->remote_id = strdup(session->orig_remote_id); @@ -396,7 +396,7 @@ int osrf_app_session_connect(osrf_app_session* session){ int timeout = 5; /* XXX CONFIG VALUE */ - debug_handler( "AppSession connecting to %s", session->remote_id ); + osrfLogDebug( "AppSession connecting to %s", session->remote_id ); /* defaulting to protocol 1 for now */ osrf_message* con_msg = osrf_message_init( CONNECT, session->thread_trace, 1 ); @@ -415,7 +415,7 @@ int osrf_app_session_connect(osrf_app_session* session){ } if(session->state == OSRF_SESSION_CONNECTED) - debug_handler(" * Connected Successfully to %s", session->remote_service ); + osrfLogDebug(" * Connected Successfully to %s", session->remote_service ); if(session->state != OSRF_SESSION_CONNECTED) return 0; @@ -434,13 +434,13 @@ int osrf_app_session_disconnect( osrf_app_session* session){ return 1; if(session->stateless && session->state != OSRF_SESSION_CONNECTED) { - debug_handler( + osrfLogDebug( "Exiting disconnect on stateless session %s", session->session_id); return 1; } - debug_handler( "AppSession disconnecting from %s", session->remote_id ); + osrfLogDebug( "AppSession disconnecting from %s", session->remote_id ); osrf_message* dis_msg = osrf_message_init( DISCONNECT, session->thread_trace, 1 ); session->state = OSRF_SESSION_DISCONNECTED; @@ -491,7 +491,7 @@ int osrfAppSessionSendBatch( osrfAppSession* session, osrf_message* msgs[], int transport_message* t_msg = message_init( string, "", session->session_id, session->remote_id, NULL ); - debug_handler("Session [%s] [%s] sending to %s \nData: %s", + osrfLogDebug("Session [%s] [%s] sending to %s \nData: %s", session->remote_service, session->session_id, t_msg->recipient, string ); retval = client_send_message( session->transport_handle, t_msg ); @@ -524,7 +524,7 @@ int _osrf_app_session_send( osrf_app_session* session, osrf_message* msg ){ int osrf_app_session_queue_wait( osrf_app_session* session, int timeout ){ if(session == NULL) return 0; int ret_val = 0; - debug_handler( "AppSession in queue_wait with timeout %d", timeout ); + osrfLogDebug( "AppSession in queue_wait with timeout %d", timeout ); ret_val = osrf_stack_entry_point(session->transport_handle, timeout); return ret_val; } @@ -540,7 +540,7 @@ void osrfAppSessionFree( osrfAppSession* ses ) { void osrf_app_session_destroy( osrf_app_session* session ){ if(session == NULL) return; - debug_handler( "AppSession [%s] [%s] destroying self and deleting requests", + osrfLogDebug( "AppSession [%s] [%s] destroying self and deleting requests", session->remote_service, session->session_id ); if(session->type == OSRF_SESSION_CLIENT && session->state != OSRF_SESSION_DISCONNECTED ) { /* disconnect if we're a client */ diff --git a/src/libstack/osrf_application.c b/src/libstack/osrf_application.c index 9f962e5..5a47d49 100644 --- a/src/libstack/osrf_application.c +++ b/src/libstack/osrf_application.c @@ -1,5 +1,4 @@ #include "osrf_application.h" -#include "osrf_log.h" #include "objson/object.h" //osrfApplication* __osrfAppList = NULL; @@ -13,13 +12,13 @@ int osrfAppRegisterApplication( char* appName, char* soFile ) { if(!__osrfAppHash) __osrfAppHash = osrfNewHash(); - info_handler("Registering application %s with file %s", appName, soFile ); + osrfLogInfo("Registering application %s with file %s", appName, soFile ); osrfApplication* app = safe_malloc(sizeof(osrfApplication)); app->handle = dlopen (soFile, RTLD_NOW); if(!app->handle) { - warning_handler("Failed to dlopen library file %s: %s", soFile, dlerror() ); + osrfLogWarning("Failed to dlopen library file %s: %s", soFile, dlerror() ); dlerror(); /* clear the error */ free(app); return -1; @@ -33,14 +32,14 @@ int osrfAppRegisterApplication( char* appName, char* soFile ) { *(void **) (&init) = dlsym(app->handle, "osrfAppInitialize"); if( (error = dlerror()) != NULL ) { - warning_handler("! Unable to locate method symbol [osrfAppInitialize] for app %s: %s", appName, error ); + osrfLogWarning("! Unable to locate method symbol [osrfAppInitialize] for app %s: %s", appName, error ); } else { /* run the method */ int ret; if( (ret = (*init)()) ) { - warning_handler("Application %s returned non-zero value from " + osrfLogWarning("Application %s returned non-zero value from " "'osrfAppInitialize', not registering...", appName ); //free(app->name); /* need a method to remove an application from the list */ //free(app); @@ -50,9 +49,9 @@ int osrfAppRegisterApplication( char* appName, char* soFile ) { __osrfAppRegisterSysMethods(appName); - info_handler("Application %s registered successfully", appName ); + osrfLogInfo("Application %s registered successfully", appName ); - osrfLogInit(appName); + osrfLogSetAppname(appName); return 0; } @@ -64,9 +63,12 @@ int osrfAppRegisterMethod( char* appName, char* methodName, if( !appName || ! methodName ) return -1; osrfApplication* app = _osrfAppFindApplication(appName); - if(!app) return warning_handler("Unable to locate application %s", appName ); + if(!app) { + osrfLogWarning("Unable to locate application %s", appName ); + return -1; + } - debug_handler("Registering method %s for app %s", methodName, appName ); + osrfLogDebug("Registering method %s for app %s", methodName, appName ); osrfMethod* method = _osrfAppBuildMethod( methodName, symbolName, notes, argc, options ); @@ -166,7 +168,7 @@ int osrfAppRunMethod( char* appName, char* methodName, /* this is the method we're gonna run */ int (*meth) (osrfMethodContext*); - info_handler("Running method [%s] for app [%s] with request id %d and " + osrfLogInfo("Running method [%s] for app [%s] with request id %d and " "thread trace %s", methodName, appName, reqId, ses->session_id ); if( !(app = _osrfAppFindApplication(appName)) ) @@ -226,7 +228,7 @@ int _osrfAppRespond( osrfMethodContext* ctx, jsonObject* data, int complete ) { if(!(ctx && ctx->method)) return -1; if( ctx->method->options & OSRF_METHOD_ATOMIC ) { - osrfLog( OSRF_DEBUG, + osrfLogDebug( "Adding responses to stash for atomic method %s", ctx->method ); if( ctx->responses == NULL ) @@ -254,7 +256,7 @@ int _osrfAppRespond( osrfMethodContext* ctx, jsonObject* data, int complete ) { int __osrfAppPostProcess( osrfMethodContext* ctx, int retcode ) { if(!(ctx && ctx->method)) return -1; - osrfLog( OSRF_DEBUG, "Postprocessing method %s with retcode %d", + osrfLogDebug( "Postprocessing method %s with retcode %d", ctx->method->name, retcode ); if(ctx->responses) { /* we have cached responses to return (no responses have been sent) */ @@ -278,7 +280,7 @@ int osrfAppRequestRespondException( osrfAppSession* ses, int request, char* msg, if(!ses) return -1; if(!msg) msg = ""; VA_LIST_TO_STRING(msg); - osrfLog( OSRF_WARN, "Returning method exception with message: %s", VA_BUF ); + osrfLogWarning( "Returning method exception with message: %s", VA_BUF ); osrfAppSessionStatus( ses, OSRF_STATUS_NOTFOUND, "osrfMethodException", request, VA_BUF ); return 0; } diff --git a/src/libstack/osrf_application.h b/src/libstack/osrf_application.h index 78868d8..51f9867 100644 --- a/src/libstack/osrf_application.h +++ b/src/libstack/osrf_application.h @@ -1,7 +1,7 @@ #include #include #include "opensrf/utils.h" -#include "opensrf/logging.h" +#include "opensrf/log.h" #include "objson/object.h" #include "osrf_app_session.h" #include "osrf_hash.h" @@ -24,20 +24,20 @@ This macro verifies methods receive the correct parameters */ #define _OSRF_METHOD_VERIFY_CONTEXT(d) \ if(!d) return -1; \ - if(!d->session) { osrfLog( OSRF_ERROR, "Session is NULL in app reqeust" ); return -1; }\ - if(!d->method) { osrfLog( OSRF_ERROR, "Method is NULL in app reqeust" ); return -1; }\ - if(!d->params) { osrfLog( OSRF_ERROR, "Params is NULL in app reqeust %s", d->method->name ); return -1; }\ + if(!d->session) { osrfLogError( "Session is NULL in app reqeust" ); return -1; }\ + if(!d->method) { osrfLogError( "Method is NULL in app reqeust" ); return -1; }\ + if(!d->params) { osrfLogError( "Params is NULL in app reqeust %s", d->method->name ); return -1; }\ if( d->params->type != JSON_ARRAY ) { \ - osrfLog( OSRF_ERROR, "'params' is not a JSON array for method %s", d->method->name);\ + osrfLogError( "'params' is not a JSON array for method %s", d->method->name);\ return -1; }\ - if( !d->method->name ) { osrfLog(OSRF_ERROR, "Method name is NULL"); return -1; } + if( !d->method->name ) { osrfLogError( "Method name is NULL"); return -1; } #ifdef OSRF_LOG_PARAMS #define OSRF_METHOD_VERIFY_CONTEXT(d) \ _OSRF_METHOD_VERIFY_CONTEXT(d); \ char* __j = jsonObjectToJSON(d->params);\ if(__j) { \ - osrfLog( OSRF_INFO, "%s %s - %s", d->session->remote_service, d->method->name, __j);\ + osrfLogInfo( "%s %s - %s", d->session->remote_service, d->method->name, __j);\ free(__j); \ } #else @@ -50,8 +50,8 @@ #define OSRF_METHOD_VERIFY_DESCRIPTION(app, d) \ if(!app) return -1; \ if(!d) return -1;\ - if(!d->name) { osrfLog( OSRF_ERROR, "No method name provided in description" ), return -1; } \ - if(!d->symbol) { osrfLog( OSRF_ERROR, "No method symbol provided in description" ), return -1; } \ + if(!d->name) { osrfLogError( "No method name provided in description" ), return -1; } \ + if(!d->symbol) { osrfLogError( "No method symbol provided in description" ), return -1; } \ if(!d->notes) d->notes = ""; \ if(!d->paramNotes) d->paramNotes = "";\ if(!d->returnNotes) d->returnNotes = ""; diff --git a/src/libstack/osrf_log.c b/src/libstack/osrf_log.c index 2bdb2dc..b144814 100644 --- a/src/libstack/osrf_log.c +++ b/src/libstack/osrf_log.c @@ -8,10 +8,10 @@ int __osrfLogLevel = 1; int osrfLogInit(char* appname) { if( !appname ) return -1; - info_handler("Initing application log for app %s", appname ); + osrfLogInfo("Initing application log for app %s", appname ); char* dir = osrf_settings_host_value("/dirs/log"); - if(!dir) return warning_handler("No '/dirs/log' setting in host config"); + if(!dir) return osrfLogWarning("No '/dirs/log' setting in host config"); char* level = osrfConfigGetValue(NULL, "/loglevel"); if(level) { __osrfLogLevel = atoi(level); free(level); } @@ -52,7 +52,7 @@ void osrfLog( enum OSRF_LOG_LEVEL level, char* msg, ... ) { free(filename); if(!file) { - warning_handler("Unable to open application log file %s\n", filename); + osrfLogWarning("Unable to open application log file %s\n", filename); return; } diff --git a/src/libstack/osrf_message.c b/src/libstack/osrf_message.c index 371bd7d..d7b9dde 100644 --- a/src/libstack/osrf_message.c +++ b/src/libstack/osrf_message.c @@ -34,7 +34,7 @@ void osrf_message_set_params( osrf_message* msg, jsonObject* o ) { if(!msg || !o) return; if(o->type != JSON_ARRAY) { - warning_handler("passing non-array to osrf_message_set_params(), fixing..."); + osrfLogWarning("passing non-array to osrf_message_set_params(), fixing..."); jsonObject* clone = jsonObjectClone(o); o = jsonNewObject(NULL); jsonObjectPush(o, clone); @@ -58,9 +58,7 @@ void osrf_message_add_param( osrf_message* msg, char* param_string ) { void osrf_message_set_status_info( osrf_message* msg, char* status_name, char* status_text, int status_code ) { - - if( msg == NULL ) - fatal_handler( "Bad params to osrf_message_set_status_info()" ); + if(!msg) return; if( status_name != NULL ) msg->status_name = strdup( status_name ); @@ -73,9 +71,7 @@ void osrf_message_set_status_info( void osrf_message_set_result_content( osrf_message* msg, char* json_string ) { - if( msg == NULL || json_string == NULL) - warning_handler( "Bad params to osrf_message_set_result_content()" ); - + if( msg == NULL || json_string == NULL) return; msg->result_string = strdup(json_string); if(json_string) msg->_result_content = jsonParseString(json_string); } @@ -218,7 +214,7 @@ int osrf_message_deserialize(char* string, osrf_message* msgs[], int count) { jsonObject* json = jsonParseString(string); if(!json) { - warning_handler( + osrfLogWarning( "osrf_message_deserialize() unable to parse data: \n%s\n", string); return 0; } diff --git a/src/libstack/osrf_message.h b/src/libstack/osrf_message.h index 9467c6b..b87a97e 100644 --- a/src/libstack/osrf_message.h +++ b/src/libstack/osrf_message.h @@ -1,6 +1,6 @@ #include "opensrf/string_array.h" #include "opensrf/utils.h" -#include "opensrf/logging.h" +#include "opensrf/log.h" #include "objson/object.h" #include "objson/json_parser.h" diff --git a/src/libstack/osrf_prefork.c b/src/libstack/osrf_prefork.c index 2c89bb8..528c337 100644 --- a/src/libstack/osrf_prefork.c +++ b/src/libstack/osrf_prefork.c @@ -11,7 +11,10 @@ void sigchld_handler( int sig ); int osrf_prefork_run(char* appname) { - if(!appname) fatal_handler("osrf_prefork_run requires an appname to run!"); + if(!appname) { + osrfLogError("osrf_prefork_run requires an appname to run!"); + return -1; + } set_proc_title( "OpenSRF Listener [%s]", appname ); @@ -19,20 +22,20 @@ int osrf_prefork_run(char* appname) { int maxc = 10; int minc = 3; - info_handler("Loading config in osrf_forker for app %s", appname); + osrfLogInfo("Loading config in osrf_forker for app %s", appname); jsonObject* max_req = osrf_settings_host_value_object("/apps/%s/unix_config/max_requests", appname); jsonObject* min_children = osrf_settings_host_value_object("/apps/%s/unix_config/min_children", appname); jsonObject* max_children = osrf_settings_host_value_object("/apps/%s/unix_config/max_children", appname); - if(!max_req) warning_handler("Max requests not defined, assuming 1000"); + if(!max_req) osrfLogWarning("Max requests not defined, assuming 1000"); else maxr = (int) jsonObjectGetNumber(max_req); - if(!min_children) warning_handler("Min children not defined, assuming 3"); + if(!min_children) osrfLogWarning("Min children not defined, assuming 3"); else minc = (int) jsonObjectGetNumber(min_children); - if(!max_children) warning_handler("Max children not defined, assuming 10"); + if(!max_children) osrfLogWarning("Max children not defined, assuming 10"); else maxc = (int) jsonObjectGetNumber(max_children); jsonObjectFree(max_req); @@ -42,8 +45,12 @@ int osrf_prefork_run(char* appname) { char* resc = va_list_to_string("%s_listener", appname); - if(!osrf_system_bootstrap_client_resc( NULL, NULL, resc )) - fatal_handler("Unable to bootstrap client for osrf_prefork_run()"); + if(!osrf_system_bootstrap_client_resc( NULL, NULL, resc )) { + osrfLogError("Unable to bootstrap client for osrf_prefork_run()"); + free(resc); + return -1; + } + free(resc); prefork_simple* forker = prefork_simple_init( @@ -51,17 +58,19 @@ int osrf_prefork_run(char* appname) { forker->appname = strdup(appname); - if(forker == NULL) - fatal_handler("osrf_prefork_run() failed to create prefork_simple object"); + if(forker == NULL) { + osrfLogError("osrf_prefork_run() failed to create prefork_simple object"); + return -1; + } prefork_launch_children(forker); osrf_prefork_register_routers(appname); - info_handler("Launching osrf_forker for app %s", appname); + osrfLogInfo("Launching osrf_forker for app %s", appname); prefork_run(forker); - warning_handler("prefork_run() retuned - how??"); + osrfLogWarning("prefork_run() retuned - how??"); prefork_free(forker); return 0; @@ -75,14 +84,14 @@ void osrf_prefork_register_routers( char* appname ) { char* routerName = osrfConfigGetValue( NULL, "/router_name" ); transport_client* client = osrfSystemGetTransportClient(); - info_handler("router name is %s and we have %d routers to connect to", routerName, c ); + osrfLogInfo("router name is %s and we have %d routers to connect to", routerName, c ); while( c ) { char* domain = osrfStringArrayGetString(arr, --c); if(domain) { char* jid = va_list_to_string( "%s@%s/router", routerName, domain ); - info_handler("Registering with router %s", jid ); + osrfLogInfo("Registering with router %s", jid ); transport_message* msg = message_init("registering", NULL, NULL, jid, NULL ); message_set_router_info( msg, NULL, NULL, appname, "register", 0 ); @@ -100,10 +109,15 @@ void osrf_prefork_register_routers( char* appname ) { void prefork_child_init_hook(prefork_child* child) { if(!child) return; - info_handler("Child init hook for child %d", child->pid); + osrfLogInfo("Child init hook for child %d", child->pid); char* resc = va_list_to_string("%s_drone",child->appname); - if(!osrf_system_bootstrap_client_resc( NULL, NULL, resc)) - fatal_handler("Unable to bootstrap client for osrf_prefork_run()"); + + if(!osrf_system_bootstrap_client_resc( NULL, NULL, resc)) { + osrfLogError("Unable to bootstrap client for osrf_prefork_run()"); + free(resc); + return; + } + free(resc); set_proc_title( "OpenSRF Drone [%s]", child->appname ); @@ -125,20 +139,24 @@ void prefork_child_process_request(prefork_child* child, char* data) { /* keepalive loop for stateful sessions */ - debug_handler("Entering keepalive loop for session %s", session->session_id ); + osrfLogDebug("Entering keepalive loop for session %s", session->session_id ); } prefork_simple* prefork_simple_init( transport_client* client, int max_requests, int min_children, int max_children ) { - if( min_children > max_children ) - fatal_handler( "min_children (%d) is greater " + if( min_children > max_children ) { + osrfLogError( "min_children (%d) is greater " "than max_children (%d)", min_children, max_children ); + return NULL; + } - if( max_children > ABS_MAX_CHILDREN ) - fatal_handler( "max_children (%d) is greater than ABS_MAX_CHILDREN (%d)", + if( max_children > ABS_MAX_CHILDREN ) { + osrfLogError( "max_children (%d) is greater than ABS_MAX_CHILDREN (%d)", max_children, ABS_MAX_CHILDREN ); + return NULL; + } /* flesh out the struct */ prefork_simple* prefork = (prefork_simple*) safe_malloc(sizeof(prefork_simple)); @@ -158,13 +176,17 @@ prefork_child* launch_child( prefork_simple* forker ) { int status_fd[2]; /* Set up the data pipes and add the child struct to the parent */ - if( pipe(data_fd) < 0 ) /* build the data pipe*/ - fatal_handler( "Pipe making error" ); + if( pipe(data_fd) < 0 ) { /* build the data pipe*/ + osrfLogError( "Pipe making error" ); + return NULL; + } - if( pipe(status_fd) < 0 ) /* build the status pipe */ - fatal_handler( "Pipe making error" ); + if( pipe(status_fd) < 0 ) {/* build the status pipe */ + osrfLogError( "Pipe making error" ); + return NULL; + } - debug_handler( "Pipes: %d %d %d %d", data_fd[0], data_fd[1], status_fd[0], status_fd[1] ); + osrfLogDebug( "Pipes: %d %d %d %d", data_fd[0], data_fd[1], status_fd[0], status_fd[1] ); prefork_child* child = prefork_child_init( forker->max_requests, data_fd[0], data_fd[1], status_fd[0], status_fd[1] ); @@ -173,7 +195,10 @@ prefork_child* launch_child( prefork_simple* forker ) { add_prefork_child( forker, child ); - if( (pid=fork()) < 0 ) fatal_handler( "Forking Error" ); + if( (pid=fork()) < 0 ) { + osrfLogError( "Forking Error" ); + return NULL; + } if( pid > 0 ) { /* parent */ @@ -181,7 +206,7 @@ prefork_child* launch_child( prefork_simple* forker ) { (forker->current_num_children)++; child->pid = pid; - info_handler( "Parent launched %d", pid ); + osrfLogInfo( "Parent launched %d", pid ); /* *no* child pipe FD's can be closed or the parent will re-use fd's that the children are currently using */ return child; @@ -189,7 +214,7 @@ prefork_child* launch_child( prefork_simple* forker ) { else { /* child */ - debug_handler("I am new child with read_data_fd = %d and write_status_fd = %d", + osrfLogDebug("I am new child with read_data_fd = %d and write_status_fd = %d", child->read_data_fd, child->write_status_fd ); child->pid = getpid(); @@ -246,11 +271,11 @@ void prefork_run(prefork_simple* forker) { while(1) { if( forker->first_child == NULL ) {/* no more children */ - warning_handler("No more children..." ); + osrfLogWarning("No more children..." ); return; } - debug_handler("Forker going into wait for data..."); + osrfLogDebug("Forker going into wait for data..."); cur_msg = client_recv( forker->connection, -1 ); //fprintf(stderr, "Got Data %f\n", get_timestamp_millis() ); @@ -263,30 +288,30 @@ void prefork_run(prefork_simple* forker) { check_children( forker ); - debug_handler( "Server received inbound data" ); + osrfLogDebug( "Server received inbound data" ); int k; prefork_child* cur_child = forker->first_child; /* Look for an available child */ for( k = 0; k < forker->current_num_children; k++ ) { - debug_handler("Searching for available child. cur_child->pid = %d", cur_child->pid ); - debug_handler("Current num children %d and loop %d", forker->current_num_children, k); + osrfLogDebug("Searching for available child. cur_child->pid = %d", cur_child->pid ); + osrfLogDebug("Current num children %d and loop %d", forker->current_num_children, k); if( cur_child->available ) { - debug_handler( "sending data to %d", cur_child->pid ); + osrfLogDebug( "sending data to %d", cur_child->pid ); message_prepare_xml( cur_msg ); char* data = cur_msg->msg_xml; if( ! data || strlen(data) < 1 ) break; cur_child->available = 0; - debug_handler( "Writing to child fd %d", cur_child->write_data_fd ); + osrfLogDebug( "Writing to child fd %d", cur_child->write_data_fd ); int written = 0; //fprintf(stderr, "Writing Data %f\n", get_timestamp_millis() ); if( (written = write( cur_child->write_data_fd, data, strlen(data) + 1 )) < 0 ) { - warning_handler("Write returned error %d", errno); + osrfLogWarning("Write returned error %d", errno); cur_child = cur_child->next; continue; } @@ -302,9 +327,9 @@ void prefork_run(prefork_simple* forker) { /* if none available, add a new child if we can */ if( ! honored ) { - debug_handler("Not enough children, attempting to add..."); + osrfLogDebug("Not enough children, attempting to add..."); if( forker->current_num_children < forker->max_children ) { - debug_handler( "Launching new child with current_num = %d", + osrfLogDebug( "Launching new child with current_num = %d", forker->current_num_children ); prefork_child* new_child = launch_child( forker ); @@ -312,8 +337,8 @@ void prefork_run(prefork_simple* forker) { char* data = cur_msg->msg_xml; if( ! data || strlen(data) < 1 ) break; new_child->available = 0; - debug_handler( "sending data to %d", new_child->pid ); - debug_handler( "Writing to new child fd %d", new_child->write_data_fd ); + osrfLogDebug( "sending data to %d", new_child->pid ); + osrfLogDebug( "Writing to new child fd %d", new_child->write_data_fd ); write( new_child->write_data_fd, data, strlen(data) + 1 ); forker->first_child = new_child->next; honored = 1; @@ -321,7 +346,7 @@ void prefork_run(prefork_simple* forker) { } if( !honored ) { - warning_handler( "No children available, sleeping and looping..." ); + osrfLogWarning( "No children available, sleeping and looping..." ); usleep( 50000 ); /* 50 milliseconds */ } @@ -371,7 +396,7 @@ void check_children( prefork_simple* forker ) { FD_CLR(0,&read_set);/* just to be sure */ if( (select_ret=select( max_fd + 1 , &read_set, NULL, NULL, &tv)) == -1 ) { - warning_handler( "Select returned error %d on check_children", errno ); + osrfLogWarning( "Select returned error %d on check_children", errno ); } if( select_ret == 0 ) @@ -385,7 +410,7 @@ void check_children( prefork_simple* forker ) { if( FD_ISSET( cur_child->read_status_fd, &read_set ) ) { //printf( "Server received status from a child %d\n", cur_child->pid ); - debug_handler( "Server received status from a child %d", cur_child->pid ); + osrfLogDebug( "Server received status from a child %d", cur_child->pid ); num_handled++; @@ -393,10 +418,10 @@ void check_children( prefork_simple* forker ) { char buf[64]; memset( buf, 0, 64); if( (n=read(cur_child->read_status_fd, buf, 63)) < 0 ) { - warning_handler("Read error afer select in child status read with errno %d", errno); + osrfLogWarning("Read error afer select in child status read with errno %d", errno); } - debug_handler( "Read %d bytes from status buffer: %s", n, buf ); + osrfLogDebug( "Read %d bytes from status buffer: %s", n, buf ); cur_child->available = 1; } cur_child = cur_child->next; @@ -437,7 +462,7 @@ void prefork_child_wait( prefork_child* child ) { } if( n < 0 ) { - warning_handler( "Child read returned error with errno %d", errno ); + osrfLogWarning( "Child read returned error with errno %d", errno ); break; } @@ -447,7 +472,7 @@ void prefork_child_wait( prefork_child* child ) { buffer_free(gbuf); - debug_handler("Child exiting...[%d]", getpid() ); + osrfLogDebug("Child exiting...[%d]", getpid() ); exit(0); } @@ -499,7 +524,7 @@ void del_prefork_child( prefork_simple* forker, pid_t pid ) { if( forker->first_child == NULL ) { return; } (forker->current_num_children)--; - debug_handler("Deleting Child: %d", pid ); + osrfLogDebug("Deleting Child: %d", pid ); prefork_child* start_child = forker->first_child; /* starting point */ prefork_child* cur_child = start_child; /* current pointer */ @@ -584,7 +609,7 @@ prefork_child* prefork_child_init( int prefork_free( prefork_simple* prefork ) { while( prefork->first_child != NULL ) { - info_handler( "Killing children and sleeping 1 to reap..." ); + osrfLogInfo( "Killing children and sleeping 1 to reap..." ); kill( 0, SIGKILL ); sleep(1); } diff --git a/src/libstack/osrf_settings.c b/src/libstack/osrf_settings.c index 2f2860d..fc32438 100644 --- a/src/libstack/osrf_settings.c +++ b/src/libstack/osrf_settings.c @@ -37,8 +37,10 @@ int osrf_settings_retrieve(char* hostname) { osrf_app_session_request_finish( session, req_id ); osrf_app_session_destroy( session ); - if(!config) - return fatal_handler("Unable to load config for host %s", hostname); + if(!config) { + osrfLogError("Unable to load config for host %s", hostname); + return -1; + } } return 0; diff --git a/src/libstack/osrf_settings.h b/src/libstack/osrf_settings.h index 7241b49..fecfc32 100644 --- a/src/libstack/osrf_settings.h +++ b/src/libstack/osrf_settings.h @@ -8,7 +8,7 @@ #include #include -#include "opensrf/logging.h" +#include "opensrf/log.h" #include "opensrf/utils.h" #include "objson/object.h" #include "objson/json_parser.h" diff --git a/src/libstack/osrf_stack.c b/src/libstack/osrf_stack.c index 0bdca70..cbd8289 100644 --- a/src/libstack/osrf_stack.c +++ b/src/libstack/osrf_stack.c @@ -12,7 +12,7 @@ int osrf_stack_process( transport_client* client, int timeout ) { transport_message* msg = NULL; while( (msg = client_recv( client, timeout )) ) { - debug_handler( "Received message from transport code from %s", msg->sender ); + osrfLogDebug( "Received message from transport code from %s", msg->sender ); osrf_stack_transport_handler( msg, NULL ); timeout = 0; } @@ -29,17 +29,17 @@ osrfAppSession* osrf_stack_transport_handler( transport_message* msg, char* my_s if(!msg) return NULL; - debug_handler( "Transport handler received new message \nfrom %s " + osrfLogDebug( "Transport handler received new message \nfrom %s " "to %s with body \n\n%s\n", msg->sender, msg->recipient, msg->body ); if( msg->is_error && ! msg->thread ) { - warning_handler("!! Received jabber layer error for %s ... exiting\n", msg->sender ); + osrfLogWarning("!! Received jabber layer error for %s ... exiting\n", msg->sender ); message_free( msg ); return NULL; } if(! msg->thread && ! msg->is_error ) { - warning_handler("Received a non-error message with no thread trace... dropping"); + osrfLogWarning("Received a non-error message with no thread trace... dropping"); message_free( msg ); return NULL; } @@ -52,14 +52,14 @@ osrfAppSession* osrf_stack_transport_handler( transport_message* msg, char* my_s if( !session ) return NULL; if(!msg->is_error) - debug_handler("Session [%s] found or built", session->session_id ); + osrfLogDebug("Session [%s] found or built", session->session_id ); osrf_app_session_set_remote( session, msg->sender ); osrf_message* arr[OSRF_MAX_MSGS_PER_PACKET]; memset(arr, 0, OSRF_MAX_MSGS_PER_PACKET ); int num_msgs = osrf_message_deserialize(msg->body, arr, OSRF_MAX_MSGS_PER_PACKET); - debug_handler( "We received %d messages from %s", num_msgs, msg->sender ); + osrfLogDebug( "We received %d messages from %s", num_msgs, msg->sender ); int i; for( i = 0; i != num_msgs; i++ ) { @@ -68,17 +68,17 @@ osrfAppSession* osrf_stack_transport_handler( transport_message* msg, char* my_s someone who no longer exists) and we're not talking to the original remote id for this server, consider it a redirect and pass it up */ if(msg->is_error) { - warning_handler( " !!! Received Jabber layer error message" ); + osrfLogWarning( " !!! Received Jabber layer error message" ); if(strcmp(session->remote_id,session->orig_remote_id)) { - warning_handler( "Treating jabber error as redirect for tt [%d] " + osrfLogWarning( "Treating jabber error as redirect for tt [%d] " "and session [%s]", arr[i]->thread_trace, session->session_id ); arr[i]->m_type = STATUS; arr[i]->status_code = OSRF_STATUS_REDIRECTED; } else { - warning_handler(" * Jabber Error is for top level remote id [%s], no one " + osrfLogWarning(" * Jabber Error is for top level remote id [%s], no one " "to send my message too!!!", session->remote_id ); } } @@ -87,7 +87,7 @@ osrfAppSession* osrf_stack_transport_handler( transport_message* msg, char* my_s } message_free( msg ); - debug_handler("after msg delete"); + osrfLogDebug("after msg delete"); return session; } @@ -104,7 +104,7 @@ int osrf_stack_message_handler( osrf_app_session* session, osrf_message* msg ) { ret_msg= _do_server( session, msg ); if(ret_msg) { - debug_handler("passing message %d / session %s to app handler", + osrfLogDebug("passing message %d / session %s to app handler", msg->thread_trace, session->session_id ); osrf_stack_application_handler( session, ret_msg ); } else @@ -128,9 +128,9 @@ osrf_message* _do_client( osrf_app_session* session, osrf_message* msg ) { switch( msg->status_code ) { case OSRF_STATUS_OK: - debug_handler("We connected successfully"); + osrfLogDebug("We connected successfully"); session->state = OSRF_SESSION_CONNECTED; - debug_handler( "State: %x => %s => %d", session, session->session_id, session->state ); + osrfLogDebug( "State: %x => %s => %d", session, session->session_id, session->state ); return NULL; case OSRF_STATUS_COMPLETE: @@ -165,7 +165,7 @@ osrf_message* _do_client( osrf_app_session* session, osrf_message* msg ) { new_msg = osrf_message_init( RESULT, msg->thread_trace, msg->protocol ); osrf_message_set_status_info( new_msg, msg->status_name, msg->status_text, msg->status_code ); - warning_handler("The stack doesn't know what to do with " + osrfLogWarning("The stack doesn't know what to do with " "the provided message code: %d, name %s. Passing UP.", msg->status_code, msg->status_name ); new_msg->is_exception = 1; @@ -191,7 +191,7 @@ osrf_message* _do_server( osrf_app_session* session, osrf_message* msg ) { if(session == NULL || msg == NULL) return NULL; - debug_handler("Server received message of type %d", msg->m_type ); + osrfLogDebug("Server received message of type %d", msg->m_type ); switch( msg->m_type ) { @@ -209,12 +209,12 @@ osrf_message* _do_server( osrf_app_session* session, osrf_message* msg ) { case REQUEST: - debug_handler("server passing message %d to application handler " + osrfLogDebug("server passing message %d to application handler " "for session %s", msg->thread_trace, session->session_id ); return msg; default: - warning_handler("Server cannot handle message of type %d", msg->m_type ); + osrfLogWarning("Server cannot handle message of type %d", msg->m_type ); return NULL; } diff --git a/src/libstack/osrf_system.c b/src/libstack/osrf_system.c index 6bacb95..acb7c34 100644 --- a/src/libstack/osrf_system.c +++ b/src/libstack/osrf_system.c @@ -36,18 +36,18 @@ int _osrfSystemInitCache() { char* servers[cacheServers->size]; for( i = 0; i != cacheServers->size; i++ ) { servers[i] = jsonObjectGetString( jsonObjectGetIndex(cacheServers, i) ); - info_handler("Adding cache server %s", servers[i]); + osrfLogInfo("Adding cache server %s", servers[i]); } osrfCacheInit( servers, cacheServers->size, atoi(maxCache) ); } else { char* servers[] = { jsonObjectGetString(cacheServers) }; - info_handler("Adding cache server %s", servers[0]); + osrfLogInfo("Adding cache server %s", servers[0]); osrfCacheInit( servers, 1, atoi(maxCache) ); } } else { - fatal_handler( "Missing config value for /cache/global/servers/server _or_ " + osrfLogError( "Missing config value for /cache/global/servers/server _or_ " "/cache/global/max_cache_time"); } @@ -60,7 +60,8 @@ int osrfSystemBootstrap( char* hostname, char* configfile, char* contextNode ) { /* first we grab the settings */ if(!osrfSystemBootstrapClientResc(configfile, contextNode, "settings_grabber" )) { - return fatal_handler("Unable to bootstrap"); + osrfLogError("Unable to bootstrap"); + return -1; } osrf_settings_retrieve(hostname); @@ -94,17 +95,17 @@ int osrfSystemBootstrap( char* hostname, char* configfile, char* contextNode ) { char* libfile = osrf_settings_host_value("/apps/%s/implementation", appname); if(! (appname && libfile) ) { - warning_handler("Missing appname / libfile in settings config"); + osrfLogWarning("Missing appname / libfile in settings config"); continue; } - info_handler("Launching application %s with implementation %s", appname, libfile); + osrfLogInfo("Launching application %s with implementation %s", appname, libfile); int pid; if( (pid = fork()) ) { // storage pid in local table for re-launching dead children... - info_handler("Launched application child %d", pid); + osrfLogInfo("Launched application child %d", pid); } else { @@ -112,7 +113,7 @@ int osrfSystemBootstrap( char* hostname, char* configfile, char* contextNode ) { if( osrfAppRegisterApplication( appname, libfile ) == 0 ) osrf_prefork_run(appname); - debug_handler("Server exiting for app %s and library %s", appname, libfile ); + osrfLogDebug("Server exiting for app %s and library %s", appname, libfile ); exit(0); } } // language == c @@ -133,8 +134,10 @@ int osrfSystemBootstrap( char* hostname, char* configfile, char* contextNode ) { int osrf_system_bootstrap_client_resc( char* config_file, char* contextnode, char* resource ) { - if( !( config_file && contextnode ) && ! osrfConfigHasDefaultConfig() ) - return fatal_handler("No Config File Specified\n" ); + if( !( config_file && contextnode ) && ! osrfConfigHasDefaultConfig() ) { + osrfLogError("No Config File Specified\n" ); + return -1; + } if( config_file ) { osrfConfigCleanup(); @@ -151,6 +154,7 @@ int osrf_system_bootstrap_client_resc( char* config_file, char* contextnode, cha char* password = osrfConfigGetValue( NULL, "/passwd" ); char* port = osrfConfigGetValue( NULL, "/port" ); char* unixpath = osrfConfigGetValue( NULL, "/unixpath" ); + char* facility = osrfConfigGetValue( NULL, "/syslog" ); char* domain = strdup(osrfStringArrayGetString( arr, 0 )); /* just the first for now */ osrfStringArrayFree(arr); @@ -161,9 +165,18 @@ int osrf_system_bootstrap_client_resc( char* config_file, char* contextnode, cha if(port) iport = atoi(port); if(log_level) llevel = atoi(log_level); - log_init( llevel, log_file ); + if(!log_file) { fprintf(stderr, "Log file needed\n"); return -1; } + + if(!strcmp(log_file, "syslog")) { + osrfLogInit( OSRF_LOG_TYPE_SYSLOG, contextnode, llevel ); + osrfLogSetSyslogFacility(osrfLogFacilityToInt(facility)); + + } else { + osrfLogInit( OSRF_LOG_TYPE_FILE, contextnode, llevel ); + osrfLogSetFile( log_file ); + } - info_handler("Bootstrapping system with domain %s, port %d, and unixpath %s", domain, iport, unixpath ); + osrfLogInfo("Bootstrapping system with domain %s, port %d, and unixpath %s", domain, iport, unixpath ); transport_client* client = client_init( domain, iport, unixpath, 0 ); @@ -184,6 +197,7 @@ int osrf_system_bootstrap_client_resc( char* config_file, char* contextnode, cha __osrfGlobalTransportClient = client; } + free(facility); free(log_level); free(log_file); free(username); @@ -208,7 +222,6 @@ int osrf_system_shutdown() { osrfConfigCleanup(); osrf_system_disconnect_client(); osrf_settings_free_host_config(NULL); - log_free(); return 1; } @@ -221,7 +234,7 @@ void __osrfSystemSignalHandler( int sig ) { int status; while( (pid = waitpid(-1, &status, WNOHANG)) > 0) { - warning_handler("We lost child %d", pid); + osrfLogWarning("We lost child %d", pid); } /** relaunch the server **/ diff --git a/src/libstack/osrf_system.h b/src/libstack/osrf_system.h index 14b5d16..34c2632 100644 --- a/src/libstack/osrf_system.h +++ b/src/libstack/osrf_system.h @@ -3,7 +3,7 @@ #include "opensrf/transport_client.h" #include "opensrf/utils.h" -#include "opensrf/logging.h" +#include "opensrf/log.h" #include "osrf_settings.h" #include "osrfConfig.h" #include "osrf_cache.h" diff --git a/src/libstack/osrf_transgroup.c b/src/libstack/osrf_transgroup.c index 6dd554e..83763ab 100644 --- a/src/libstack/osrf_transgroup.c +++ b/src/libstack/osrf_transgroup.c @@ -33,7 +33,7 @@ osrfTransportGroup* osrfNewTransportGroup( osrfTransportGroupNode* nodes[], int for( i = 0; i != count; i++ ) { if(!(nodes[i] && nodes[i]->domain) ) return NULL; osrfHashSet( grp->nodes, nodes[i], nodes[i]->domain ); - debug_handler("Adding domain %s to TransportGroup", nodes[i]->domain); + osrfLogDebug("Adding domain %s to TransportGroup", nodes[i]->domain); } return grp; @@ -49,17 +49,17 @@ int osrfTransportGroupConnectAll( osrfTransportGroup* grp ) { osrfHashIteratorReset(grp->itr); while( (node = osrfHashIteratorNext(grp->itr)) ) { - info_handler("TransportGroup attempting to connect to domain %s", + osrfLogInfo("TransportGroup attempting to connect to domain %s", node->connection->session->server); if(client_connect( node->connection, node->username, node->password, node->resource, 10, AUTH_DIGEST )) { node->active = 1; active++; - info_handler("TransportGroup successfully connected to domain %s", + osrfLogInfo("TransportGroup successfully connected to domain %s", node->connection->session->server); } else { - warning_handler("TransportGroup unable to connect to domain %s", + osrfLogWarning("TransportGroup unable to connect to domain %s", node->connection->session->server); } } @@ -75,7 +75,7 @@ void osrfTransportGroupDisconnectAll( osrfTransportGroup* grp ) { osrfHashIteratorReset(grp->itr); while( (node = osrfHashIteratorNext(grp->itr)) ) { - info_handler("TransportGroup disconnecting from domain %s", + osrfLogInfo("TransportGroup disconnecting from domain %s", node->connection->session->server); client_disconnect(node->connection); node->active = 0; @@ -98,7 +98,8 @@ int osrfTransportGroupSendMatch( osrfTransportGroup* grp, transport_message* msg return 0; } - return warning_handler("Error sending message to domain %s", domain ); + osrfLogWarning("Error sending message to domain %s", domain ); + return -1; } int osrfTransportGroupSend( osrfTransportGroup* grp, transport_message* msg ) { @@ -140,7 +141,8 @@ int osrfTransportGroupSend( osrfTransportGroup* grp, transport_message* msg ) { } else { if(!strcmp(firstdomain, node->domain)) { /* we've made a full loop */ - return warning_handler("We've tried to send to all domains.. giving up"); + osrfLogWarning("We've tried to send to all domains.. giving up"); + return -1; } } diff --git a/src/libtransport/basic_client.c b/src/libtransport/basic_client.c index 817b502..5f14e1f 100644 --- a/src/libtransport/basic_client.c +++ b/src/libtransport/basic_client.c @@ -11,7 +11,7 @@ void sig_int( int sig ) { int main( int argc, char** argv ) { if( argc < 5 ) { - fatal_handler( "Usage: %s \n", argv[0] ); + osrfLogError( "Usage: %s \n", argv[0] ); return 99; } @@ -20,9 +20,11 @@ int main( int argc, char** argv ) { // try to connect, allow 15 second connect timeout if( client_connect( client, argv[1], "jkjkasdf", argv[3], 15, AUTH_DIGEST ) ) - info_handler("Connected...\n"); - else - fatal_handler( "NOT Connected...\n" ); + osrfLogInfo("Connected...\n"); + else { + osrfLogError( "NOT Connected...\n" ); + return -1; + } if( (pid=fork()) ) { /* parent */ diff --git a/src/libtransport/component.c b/src/libtransport/component.c index 3bc81f6..ed58b4b 100644 --- a/src/libtransport/component.c +++ b/src/libtransport/component.c @@ -14,7 +14,8 @@ int main( int argc, char** argv ) { if( argc < 5 ) { - fatal_handler( "Usage: %s ", argv[0] ); + osrfLogError( "Usage: %s ", argv[0] ); + return -1; } int port = atoi(argv[2]); @@ -22,9 +23,11 @@ int main( int argc, char** argv ) { // try to connect, allow 15 second connect timeout if( client_connect( client, argv[3], argv[4], "", 15, 1 ) ) - info_handler("Connected...\n"); - else - fatal_handler( "NOT Connected...\n" ); + osrfLogInfo("Connected...\n"); + else { + osrfLogError( "NOT Connected...\n" ); + return -1; + } transport_message* recv; while( (recv=client_recv( client, -1)) ) { diff --git a/src/libtransport/transport_client.c b/src/libtransport/transport_client.c index ba383ea..b660228 100644 --- a/src/libtransport/transport_client.c +++ b/src/libtransport/transport_client.c @@ -62,7 +62,7 @@ transport_client* client_init( char* server, int port, char* unix_path, int comp if(client->session == NULL) { - fatal_handler( "client_init(): Out of Memory"); + osrfLogError( "client_init(): Out of Memory"); return NULL; } client->session->message_callback = client_message_handler; @@ -119,7 +119,7 @@ transport_message* client_recv( transport_client* client, int timeout ) { // if( ! session_wait( client->session, -1 ) ) { int x; if( (x = session_wait( client->session, -1 )) ) { - warning_handler("session_wait returned failure code %d\n", x); + osrfLogWarning("session_wait returned failure code %d\n", x); return NULL; } } diff --git a/src/libtransport/transport_client.h b/src/libtransport/transport_client.h index 530285d..4d37cc4 100644 --- a/src/libtransport/transport_client.h +++ b/src/libtransport/transport_client.h @@ -1,6 +1,6 @@ #include "transport_session.h" #include "opensrf/utils.h" -#include "opensrf/logging.h" +#include "opensrf/log.h" #include diff --git a/src/libtransport/transport_message.c b/src/libtransport/transport_message.c index 03a8de5..b96fcd0 100644 --- a/src/libtransport/transport_message.c +++ b/src/libtransport/transport_message.c @@ -26,7 +26,7 @@ transport_message* message_init( char* body, msg->subject == NULL || msg->recipient == NULL || msg->sender == NULL ) { - fatal_handler( "message_init(): Out of Memory" ); + osrfLogError( "message_init(): Out of Memory" ); return NULL; } @@ -130,7 +130,7 @@ transport_message* new_message_from_xml( const char* msg_xml ) { encoded_body = strdup( (char*) xmlbuf ); if( encoded_body == NULL ) - fatal_handler("message_to_xml(): Out of Memory"); + osrfLogError("message_to_xml(): Out of Memory"); xmlFree(xmlbuf); xmlFreeDoc(msg_doc); @@ -194,7 +194,7 @@ void message_set_router_info( transport_message* msg, char* router_from, if( msg->router_from == NULL || msg->router_to == NULL || msg->router_class == NULL || msg->router_command == NULL ) - fatal_handler( "message_set_router_info(): Out of Memory" ); + osrfLogError( "message_set_router_info(): Out of Memory" ); return; } @@ -250,7 +250,7 @@ char* message_to_xml( const transport_message* msg ) { xmlKeepBlanksDefault(0); if( ! msg ) { - warning_handler( "Passing NULL message to message_to_xml()"); + osrfLogWarning( "Passing NULL message to message_to_xml()"); return 0; } @@ -312,8 +312,10 @@ char* message_to_xml( const transport_message* msg ) { encoded_body = strdup( (char*) xmlbuf ); - if( encoded_body == NULL ) - fatal_handler("message_to_xml(): Out of Memory"); + if( encoded_body == NULL ) { + osrfLogError("message_to_xml(): Out of Memory"); + return NULL; + } xmlFree(xmlbuf); xmlFreeDoc( doc ); diff --git a/src/libtransport/transport_message.h b/src/libtransport/transport_message.h index 2dc82de..5dbd631 100644 --- a/src/libtransport/transport_message.h +++ b/src/libtransport/transport_message.h @@ -7,7 +7,7 @@ #include #include "opensrf/utils.h" -#include "opensrf/logging.h" +#include "opensrf/log.h" #ifndef TRANSPORT_MESSAGE_H #define TRANSPORT_MESSAGE_H diff --git a/src/libtransport/transport_session.c b/src/libtransport/transport_session.c index e2b6cc4..c399ba1 100644 --- a/src/libtransport/transport_session.c +++ b/src/libtransport/transport_session.c @@ -41,7 +41,7 @@ transport_session* init_transport( char* server, session->router_class_buffer == NULL || session->router_command_buffer == NULL || session->session_id == NULL ) { - fatal_handler( "init_transport(): buffer_init returned NULL" ); + osrfLogError( "init_transport(): buffer_init returned NULL" ); return 0; } @@ -128,7 +128,7 @@ int session_send_msg( if( ! session ) { return -1; } if( ! session->state_machine->connected ) { - warning_handler("State machine is not connected in send_msg()"); + osrfLogWarning("State machine is not connected in send_msg()"); return -1; } @@ -148,7 +148,7 @@ int session_connect( transport_session* session, int size2 = 0; if( ! session ) { - warning_handler( "session is null in connect" ); + osrfLogWarning( "session is null in connect" ); return 0; } @@ -187,7 +187,7 @@ int session_connect( transport_session* session, // if( ! tcp_send( session->sock_obj, stanza1 ) ) { if( socket_send( session->sock_id, stanza1 ) ) { - warning_handler("error sending"); + osrfLogWarning("error sending"); return 0; } @@ -211,7 +211,7 @@ int session_connect( transport_session* session, //if( ! tcp_send( session->sock_obj, stanza2 ) ) { if( socket_send( session->sock_id, stanza2 ) ) { - warning_handler("error sending"); + osrfLogWarning("error sending"); return 0; } } @@ -232,7 +232,7 @@ int session_connect( transport_session* session, session->state_machine->connecting = CONNECTING_1; //if( ! tcp_send( session->sock_obj, stanza1 ) ) { if( socket_send( session->sock_id, stanza1 ) ) { - warning_handler("error sending"); + osrfLogWarning("error sending"); return 0; } @@ -257,7 +257,7 @@ int session_connect( transport_session* session, if( session->state_machine->connecting == CONNECTING_2 ) { //if( ! tcp_send( session->sock_obj, stanza2 ) ) { if( socket_send( session->sock_id, stanza2 ) ) { - warning_handler("error sending"); + osrfLogWarning("error sending"); return 0; } } @@ -285,7 +285,7 @@ int session_connect( transport_session* session, if( session->state_machine->connecting == CONNECTING_2 ) { //if( ! tcp_send( session->sock_obj, stanza2 ) ) { if( socket_send( session->sock_id, stanza2 ) ) { - warning_handler("error sending"); + osrfLogWarning("error sending"); return 0; } } @@ -374,7 +374,7 @@ void startElementHandler( if( strcmp( name, "stream:error" ) == 0 ) { ses->state_machine->in_error = 1; - warning_handler( "Received message from Jabber server" ); + osrfLogWarning( "Received message from Jabber server" ); return; } @@ -398,7 +398,7 @@ void startElementHandler( ses->state_machine->in_message_error = 1; buffer_add( ses->message_error_type, get_xml_attr( atts, "type" ) ); ses->message_error_code = atoi( get_xml_attr( atts, "code" ) ); - warning_handler( "Received message with type %s and code %s", + osrfLogWarning( "Received message with type %s and code %s", get_xml_attr( atts, "type"), get_xml_attr( atts, "code") ); return; } @@ -414,7 +414,7 @@ void startElementHandler( } if( strcmp( get_xml_attr(atts, "type"), "error") == 0 ) { - warning_handler( "Error connecting to jabber" ); + osrfLogWarning( "Error connecting to jabber" ); return; } } @@ -496,8 +496,8 @@ void endElementHandler( void *session, const xmlChar *name) { if( strcmp( name, "iq" ) == 0 ) { ses->state_machine->in_iq = 0; if( ses->message_error_code > 0 ) { - warning_handler( "Error in IQ packet: code %d", ses->message_error_code ); - warning_handler( "Error 401 means not authorized" ); + osrfLogWarning( "Error in IQ packet: code %d", ses->message_error_code ); + osrfLogWarning( "Error 401 means not authorized" ); } reset_session_buffers( session ); return; @@ -585,7 +585,7 @@ void characterHandler( if( ses->state_machine->in_error ) { /* for now... */ - warning_handler( "ERROR Xml fragment: %s\n", ch ); + osrfLogWarning( "ERROR Xml fragment: %s\n", ch ); } } diff --git a/src/libtransport/transport_session.h b/src/libtransport/transport_session.h index 9701cbe..fa13704 100644 --- a/src/libtransport/transport_session.h +++ b/src/libtransport/transport_session.h @@ -6,7 +6,7 @@ #include "transport_message.h" #include "opensrf/utils.h" -#include "opensrf/logging.h" +#include "opensrf/log.h" #include "opensrf/socket_bundle.h" #include "sha.h" diff --git a/src/router/osrf_router.c b/src/router/osrf_router.c index a0d9c42..1cdde27 100644 --- a/src/router/osrf_router.c +++ b/src/router/osrf_router.c @@ -57,14 +57,14 @@ void osrfRouterRun( osrfRouter* router ) { int numhandled = 0; if( (selectret = select(maxfd + 1, &set, NULL, NULL, NULL)) < 0 ) { - warning_handler("Top level select call failed with errno %d", errno); + osrfLogWarning("Top level select call failed with errno %d", errno); continue; } /* see if there is a top level router message */ if( FD_ISSET(routerfd, &set) ) { - debug_handler("Top router socket is active: %d", routerfd ); + osrfLogDebug("Top router socket is active: %d", routerfd ); numhandled++; osrfRouterHandleIncoming( router ); } @@ -82,11 +82,11 @@ void osrfRouterRun( osrfRouter* router ) { if( classname && (class = osrfRouterFindClass( router, classname )) ) { - debug_handler("Checking %s for activity...", classname ); + osrfLogDebug("Checking %s for activity...", classname ); int sockfd = class->ROUTER_SOCKFD; if(FD_ISSET( sockfd, &set )) { - debug_handler("Socket is active: %d", sockfd ); + osrfLogDebug("Socket is active: %d", sockfd ); numhandled++; osrfRouterClassHandleIncoming( router, classname, class ); } @@ -117,7 +117,7 @@ void osrfRouterHandleIncoming( osrfRouter* router ) { if(osrfStringArrayContains( router->trustedServers, domain)) osrfRouterHandleMessage( router, msg ); else - warning_handler("Received message from un-trusted server domain %s", msg->sender); + osrfLogWarning("Received message from un-trusted server domain %s", msg->sender); } message_free(msg); @@ -128,7 +128,7 @@ int osrfRouterClassHandleIncoming( osrfRouter* router, char* classname, osrfRout if(!(router && class)) return -1; transport_message* msg; - debug_handler("osrfRouterClassHandleIncoming()"); + osrfLogDebug("osrfRouterClassHandleIncoming()"); if( (msg = client_recv( class->connection, 0 )) ) { @@ -155,7 +155,7 @@ int osrfRouterClassHandleIncoming( osrfRouter* router, char* classname, osrfRout osrfRouterClassHandleMessage( router, class, msg ); } else { - warning_handler("Received client message from untrusted client domain %s", domain ); + osrfLogWarning("Received client message from untrusted client domain %s", domain ); } } @@ -180,7 +180,7 @@ int osrfRouterHandleMessage( osrfRouter* router, transport_message* msg ) { if(!strcmp(msg->router_command, ROUTER_REGISTER)) { class = osrfRouterFindClass( router, msg->router_class ); - info_handler("Registering class %s", msg->router_class ); + osrfLogInfo("Registering class %s", msg->router_class ); if(!class) class = osrfRouterAddClass( router, msg->router_class ); @@ -196,7 +196,7 @@ int osrfRouterHandleMessage( osrfRouter* router, transport_message* msg ) { } else if( !strcmp( msg->router_command, ROUTER_UNREGISTER ) ) { if( msg->router_class && strcmp( msg->router_class, "") ) { - info_handler("Unregistering router class %s", msg->router_class ); + osrfLogInfo("Unregistering router class %s", msg->router_class ); osrfRouterClassRemoveNode( router, msg->router_class, msg->sender ); } } @@ -231,7 +231,7 @@ osrfRouterClass* osrfRouterAddClass( osrfRouter* router, char* classname ) { int osrfRouterClassAddNode( osrfRouterClass* rclass, char* remoteId ) { if(!(rclass && rclass->nodes && remoteId)) return -1; - info_handler("Adding router node for remote id %s", remoteId ); + osrfLogInfo("Adding router node for remote id %s", remoteId ); osrfRouterNode* node = safe_malloc(sizeof(osrfRouterNode)); node->count = 0; @@ -249,16 +249,16 @@ int osrfRouterClassAddNode( osrfRouterClass* rclass, char* remoteId ) { transport_message* osrfRouterClassHandleBounce( osrfRouter* router, char* classname, osrfRouterClass* rclass, transport_message* msg ) { - debug_handler("osrfRouterClassHandleBounce()"); + osrfLogDebug("osrfRouterClassHandleBounce()"); - warning_handler("Received network layer error message from %s", msg->sender ); + osrfLogWarning("Received network layer error message from %s", msg->sender ); osrfRouterNode* node = osrfRouterClassFindNode( rclass, msg->sender ); transport_message* lastSent = NULL; if( node && osrfHashGetCount(rclass->nodes) == 1 ) { /* the last node is dead */ if( node->lastMessage ) { - warning_handler("We lost the last node in the class, responding with error and removing..."); + osrfLogWarning("We lost the last node in the class, responding with error and removing..."); transport_message* error = message_init( node->lastMessage->body, node->lastMessage->subject, @@ -275,7 +275,7 @@ transport_message* osrfRouterClassHandleBounce( } else { if( node->lastMessage ) { - debug_handler("Cloning lastMessage so next node can send it"); + osrfLogDebug("Cloning lastMessage so next node can send it"); lastSent = message_init( node->lastMessage->body, node->lastMessage->subject, node->lastMessage->thread, "", node->lastMessage->router_from ); message_set_router_info( lastSent, node->lastMessage->router_from, NULL, NULL, NULL, 0 ); @@ -298,7 +298,7 @@ int osrfRouterClassHandleMessage( osrfRouter* router, osrfRouterClass* rclass, transport_message* msg ) { if(!(router && rclass && msg)) return -1; - debug_handler("osrfRouterClassHandleMessage()"); + osrfLogDebug("osrfRouterClassHandleMessage()"); osrfRouterNode* node = osrfHashIteratorNext( rclass->itr ); if(!node) { @@ -312,7 +312,7 @@ int osrfRouterClassHandleMessage( msg->subject, msg->thread, node->remoteId, msg->sender ); message_set_router_info( new_msg, msg->sender, NULL, NULL, NULL, 0 ); - info_handler( "Routing message:\nfrom: [%s]\nto: [%s]", + osrfLogInfo( "Routing message:\nfrom: [%s]\nto: [%s]", new_msg->router_from, new_msg->recipient ); message_free( node->lastMessage ); @@ -323,7 +323,7 @@ int osrfRouterClassHandleMessage( else { message_prepare_xml(new_msg); - warning_handler("Error sending message from %s to %s\n%s", + osrfLogWarning("Error sending message from %s to %s\n%s", new_msg->sender, new_msg->recipient, new_msg->msg_xml ); } @@ -335,7 +335,7 @@ int osrfRouterClassHandleMessage( int osrfRouterRemoveClass( osrfRouter* router, char* classname ) { if(!(router && router->classes && classname)) return -1; - info_handler("Removing router class %s", classname ); + osrfLogInfo("Removing router class %s", classname ); osrfHashRemove( router->classes, classname ); return 0; } @@ -346,7 +346,7 @@ int osrfRouterClassRemoveNode( if(!(router && router->classes && classname && remoteId)) return 0; - info_handler("Removing router node %s", remoteId ); + osrfLogInfo("Removing router node %s", remoteId ); osrfRouterClass* class = osrfRouterFindClass( router, classname ); @@ -491,7 +491,7 @@ int osrfRouterRespondConnect( osrfRouter* router, transport_message* msg, osrfMe osrfMessage* success = osrf_message_init( STATUS, omsg->thread_trace, omsg->protocol ); - debug_handler("router recevied a CONNECT message from %s", msg->sender ); + osrfLogDebug("router recevied a CONNECT message from %s", msg->sender ); osrf_message_set_status_info( success, "osrfConnectStatus", "Connection Successful", OSRF_STATUS_OK ); @@ -516,7 +516,7 @@ int osrfRouterProcessAppRequest( osrfRouter* router, transport_message* msg, osr if(!(router && msg && omsg && omsg->method_name)) return -1; - info_handler("Router received app request: %s", omsg->method_name ); + osrfLogInfo("Router received app request: %s", omsg->method_name ); jsonObject* jresponse = NULL; if(!strcmp( omsg->method_name, ROUTER_REQUEST_CLASS_LIST )) { @@ -579,7 +579,7 @@ int osrfRouterHandleAppResponse( osrfRouter* router, osrf_message_set_result_content( oresponse, json); char* data = osrf_message_serialize(oresponse); - debug_handler( "Responding to client app request with data: \n%s\n", data ); + osrfLogDebug( "Responding to client app request with data: \n%s\n", data ); transport_message* tresponse = message_init( data, "", msg->thread, msg->sender, msg->recipient ); diff --git a/src/router/osrf_router.h b/src/router/osrf_router.h index ebe2897..1122a08 100644 --- a/src/router/osrf_router.h +++ b/src/router/osrf_router.h @@ -3,6 +3,7 @@ #include #include "opensrf/utils.h" +#include "opensrf/log.h" #include "opensrf/osrf_list.h" #include "opensrf/osrf_hash.h" diff --git a/src/router/osrf_router_main.c b/src/router/osrf_router_main.c index e29bf92..5187cc8 100644 --- a/src/router/osrf_router_main.c +++ b/src/router/osrf_router_main.c @@ -1,16 +1,15 @@ #include "osrf_router.h" #include "opensrf/osrfConfig.h" #include "opensrf/utils.h" -#include "opensrf/logging.h" +#include "opensrf/log.h" #include osrfRouter* __osrfRouter = NULL; void routerSignalHandler( int signal ) { - warning_handler("Received signal [%d], cleaning up...", signal ); + osrfLogWarning("Received signal [%d], cleaning up...", signal ); osrfConfigCleanup(); osrfRouterFree(__osrfRouter); - log_free(); } static int __setupRouter( char* config, char* context ); @@ -19,7 +18,7 @@ static int __setupRouter( char* config, char* context ); int main( int argc, char* argv[] ) { if( argc < 3 ) { - fatal_handler( "Usage: %s ", argv[0] ); + osrfLogError( "Usage: %s ", argv[0] ); exit(0); } @@ -54,13 +53,18 @@ int __setupRouter( char* config, char* context ) { int llevel = 1; if(level) llevel = atoi(level); + /* if(!log_init( llevel, log_file )) fprintf(stderr, "Unable to init logging, going to stderr...\n" ); + */ + + osrfLogInit( OSRF_LOG_TYPE_SYSLOG, "router", llevel ); /* XXX config option */ + osrfLogSetSyslogFacility( LOG_LOCAL3 ); /* XXX config option */ free(level); free(log_file); - info_handler( "Router connecting as: server: %s port: %s " + osrfLogInfo( "Router connecting as: server: %s port: %s " "user: %s resource: %s", server, port, username, resource ); int iport = 0; @@ -73,13 +77,15 @@ int __setupRouter( char* config, char* context ) { int i; for( i = 0; i != tservers->size; i++ ) - info_handler( "Router adding trusted server: %s", osrfStringArrayGetString( tservers, i ) ); + osrfLogInfo( "Router adding trusted server: %s", osrfStringArrayGetString( tservers, i ) ); for( i = 0; i != tclients->size; i++ ) - info_handler( "Router adding trusted client: %s", osrfStringArrayGetString( tclients, i ) ); + osrfLogInfo( "Router adding trusted client: %s", osrfStringArrayGetString( tclients, i ) ); - if( tclients->size == 0 || tservers->size == 0 ) - fatal_handler("We need trusted servers and trusted client to run the router..."); + if( tclients->size == 0 || tservers->size == 0 ) { + osrfLogError("We need trusted servers and trusted client to run the router..."); + return -1; + } osrfRouter* router = osrfNewRouter( server, username, resource, password, iport, tclients, tservers ); diff --git a/src/srfsh/srfsh.c b/src/srfsh/srfsh.c index 59dbb14..445139e 100644 --- a/src/srfsh/srfsh.c +++ b/src/srfsh/srfsh.c @@ -13,23 +13,32 @@ int main( int argc, char* argv[] ) { char fbuf[l]; memset(fbuf, 0, l); sprintf(fbuf,"%s/.srfsh.xml",home); + + //osrfLogInit( OSRF_LOG_TYPE_SYSLOG, "srfsh", if(!access(fbuf, R_OK)) { - if( ! osrf_system_bootstrap_client(fbuf, "srfsh") ) - fatal_handler( "Unable to bootstrap client for requests"); + if( ! osrf_system_bootstrap_client(fbuf, "srfsh") ) { + osrfLogError( "Unable to bootstrap client for requests"); + return -1; + } } else { - fatal_handler( "No Config file found at %s", fbuf ); + osrfLogError( "No Config file found at %s", fbuf ); + return -1; } if(argc > 1) { /* for now.. the first arg is used as a script file for processing */ int f; - if( (f = open(argv[1], O_RDONLY)) == -1 ) - fatal_handler("Unable to open file %s for reading, exiting...", argv[1]); + if( (f = open(argv[1], O_RDONLY)) == -1 ) { + osrfLogError("Unable to open file %s for reading, exiting...", argv[1]); + return -1; + } - if(dup2(f, STDIN_FILENO) == -1) - fatal_handler("Unable to duplicate STDIN, exiting..."); + if(dup2(f, STDIN_FILENO) == -1) { + osrfLogError("Unable to duplicate STDIN, exiting..."); + return -1; + } close(f); is_from_script = 1; @@ -158,8 +167,10 @@ int parse_request( char* request ) { if( !strcmp(words[0],"router") ) ret_val = handle_router( words ); + /* else if( !strcmp(words[0],"time") ) ret_val = handle_time( words ); + */ else if (!strcmp(words[0],"request")) ret_val = handle_request( words, 0 ); @@ -498,7 +509,7 @@ int send_request( char* server, osrf_app_session* session = osrf_app_client_session_init(server); if(!osrf_app_session_connect(session)) { - warning_handler( "Unable to connect to remote service %s\n", server ); + osrfLogWarning( "Unable to connect to remote service %s\n", server ); return 1; } @@ -623,6 +634,7 @@ int send_request( char* server, } +/* int handle_time( char* words[] ) { if( ! words[1] ) { @@ -645,6 +657,7 @@ int handle_time( char* words[] ) { return 0; } +*/ diff --git a/src/srfsh/srfsh.h b/src/srfsh/srfsh.h index df61cbb..f6fc7ca 100644 --- a/src/srfsh/srfsh.h +++ b/src/srfsh/srfsh.h @@ -7,7 +7,7 @@ #include #include "utils.h" -#include "logging.h" +#include "log.h" #include diff --git a/src/utils/Makefile b/src/utils/Makefile index d5ad508..77eae02 100644 --- a/src/utils/Makefile +++ b/src/utils/Makefile @@ -1,13 +1,13 @@ -UTIL_HEADERS = md5.h logging.h utils.h socket_bundle.h sha.h string_array.h xml_utils.h -UTIL_OBJECTS = md5.o logging.o utils.o socket_bundle.o sha.o string_array.o +UTIL_HEADERS = md5.h log.h utils.h socket_bundle.h sha.h string_array.h xml_utils.h +UTIL_OBJECTS = md5.o log.o utils.o socket_bundle.o sha.o string_array.o all: $(UTIL_OBJECTS) copy copy: cp $(UTIL_HEADERS) $(TMPDIR) -logging.o: logging.c logging.h +log.o: log.c log.h utils.o: utils.c utils.h socket_bundle.o: socket_bundle.c socket_bundle.h md5.o: md5.c md5.h diff --git a/src/utils/log.c b/src/utils/log.c new file mode 100644 index 0000000..3c65a63 --- /dev/null +++ b/src/utils/log.c @@ -0,0 +1,176 @@ +#include "log.h" + +int __osrfLogType = -1; +int __osrfLogFacility = LOG_LOCAL0; +int __osrfLogActFacility = LOG_LOCAL1; +char* __osrfLogFile = NULL; +char* __osrfLogAppname = NULL; +int __osrfLogLevel = OSRF_LOG_INFO; +int __osrfLogActivityEnabled = 1; + + +void osrfLogInit( int type, const char* appname, int maxlevel ) { + osrfLogSetType(type); + if(appname) osrfLogSetAppname(appname); + osrfLogSetLevel(maxlevel); + if( type == OSRF_LOG_TYPE_SYSLOG ) + openlog(__osrfLogAppname, 0, __osrfLogFacility ); +} + +void osrfLogSetType( int logtype ) { + if( logtype != OSRF_LOG_TYPE_FILE && + logtype != OSRF_LOG_TYPE_SYSLOG ) { + fprintf(stderr, "Unrecognized log type. Logging to stderr\n"); + return; + } + __osrfLogType = logtype; +} + +void osrfLogSetFile( const char* logfile ) { + if(!logfile) return; + if(__osrfLogFile) free(__osrfLogFile); + __osrfLogFile = strdup(logfile); +} + +void osrfLogSetActivityEnabled( int enabled ) { + __osrfLogActivityEnabled = enabled; +} + +void osrfLogSetAppname( const char* appname ) { + if(!appname) return; + if(__osrfLogAppname) free(__osrfLogAppname); + __osrfLogAppname = strdup(appname); + + /* if syslogging, re-open the log with the appname */ + if( __osrfLogType == OSRF_LOG_TYPE_SYSLOG) { + closelog(); + openlog(__osrfLogAppname, 0, __osrfLogFacility); + } +} + +void osrfLogSetSyslogFacility( int facility ) { + __osrfLogFacility = facility; +} +void osrfLogSetSyslogActFacility( int facility ) { + __osrfLogActFacility = facility; +} + +void osrfLogSetLevel( int loglevel ) { + __osrfLogLevel = loglevel; +} + +void osrfLogError( const char* msg, ... ) { OSRF_LOG_GO(msg, OSRF_LOG_ERROR); } +void osrfLogWarning( const char* msg, ... ) { OSRF_LOG_GO(msg, OSRF_LOG_WARNING); } +void osrfLogInfo( const char* msg, ... ) { OSRF_LOG_GO(msg, OSRF_LOG_INFO); } +void osrfLogDebug( const char* msg, ... ) { OSRF_LOG_GO(msg, OSRF_LOG_DEBUG); } +void osrfLogInternal( const char* msg, ... ) { OSRF_LOG_GO(msg, OSRF_LOG_DEBUG); } +void osrfLogActivity( const char* msg, ... ) { + OSRF_LOG_GO(msg, OSRF_LOG_ACTIVITY); + /* activity log entries are also logged as info intries */ + osrfLogDetail( OSRF_LOG_INFO, NULL, -1, NULL, VA_BUF ); +} + +void osrfLogDetail( int level, char* filename, int line, char* func, char* msg, ... ) { + + if( level == OSRF_LOG_ACTIVITY && ! __osrfLogActivityEnabled ) return; + if( level > __osrfLogLevel ) return; + if(!msg) return; + if(!filename) filename = ""; + if(!func) func = ""; + + char lb[8]; + bzero(lb,8); + if(line >= 0) snprintf(lb,8,"%d", line); + + char* l = "INFO"; /* level name */ + int lvl = LOG_INFO; /* syslog level */ + int fac = __osrfLogFacility; + + switch( level ) { + case OSRF_LOG_ERROR: + l = "ERR "; + lvl = LOG_ERR; + break; + + case OSRF_LOG_WARNING: + l = "WARN"; + lvl = LOG_WARNING; + break; + + case OSRF_LOG_INFO: + l = "INFO"; + lvl = LOG_INFO; + break; + + case OSRF_LOG_DEBUG: + l = "DEBG"; + lvl = LOG_DEBUG; + break; + + case OSRF_LOG_INTERNAL: + l = "INT "; + lvl = LOG_DEBUG; + break; + + case OSRF_LOG_ACTIVITY: + l = "ACT"; + lvl = LOG_INFO; + fac = __osrfLogActFacility; + break; + } + + VA_LIST_TO_STRING(msg); + + if(__osrfLogType == OSRF_LOG_TYPE_SYSLOG ) + syslog( fac | lvl, "[%s:%d:%s:%s:%s] %s", l, getpid(), filename, lb, func, VA_BUF ); + + else if( __osrfLogType == OSRF_LOG_TYPE_FILE ) + _osrfLogToFile("[%s:%d:%s:%d:%s] %s", l, getpid(), filename, lb, func, VA_BUF ); +} + + +void _osrfLogToFile( char* msg, ... ) { + + if(!msg) return; + if(!__osrfLogFile) return; + VA_LIST_TO_STRING(msg); + + if(!__osrfLogAppname) __osrfLogAppname = strdup("osrf"); + int l = strlen(VA_BUF) + strlen(__osrfLogAppname) + 24; + char buf[l]; + bzero(buf,l); + + char datebuf[24]; + bzero(datebuf,24); + time_t t = time(NULL); + struct tm* tms = localtime(&t); + strftime(datebuf, 24, "%Y-%m-%d %h:%m:%s", tms); + + FILE* file = fopen(__osrfLogFile, "a"); + if(!file) { + fprintf(stderr, "Unable to fopen file %s for writing", __osrfLogFile); + return; + } + + fprintf(file, "%s %s %s\n", __osrfLogAppname, datebuf, VA_BUF ); + fclose(file); +} + + +int osrfLogFacilityToInt( char* facility ) { + if(!facility) return -1; + if(strlen(facility) < 6) return -1; + switch( facility[5] ) { + case 0: return LOG_LOCAL0; + case 1: return LOG_LOCAL1; + case 2: return LOG_LOCAL2; + case 3: return LOG_LOCAL3; + case 4: return LOG_LOCAL4; + case 5: return LOG_LOCAL5; + case 6: return LOG_LOCAL6; + case 7: return LOG_LOCAL7; + } + return -1; +} + + diff --git a/src/utils/log.h b/src/utils/log.h new file mode 100644 index 0000000..2886dc6 --- /dev/null +++ b/src/utils/log.h @@ -0,0 +1,67 @@ +#include +#include +#include "utils.h" +#include + +#ifndef OSRF_LOG_INCLUDED +#define OSRF_LOG_INCLUDED + +/* log levels */ +#define OSRF_LOG_ERROR 1 +#define OSRF_LOG_WARNING 2 +#define OSRF_LOG_INFO 3 +#define OSRF_LOG_DEBUG 4 +#define OSRF_LOG_INTERNAL 5 +#define OSRF_LOG_ACTIVITY -1 + +#define OSRF_LOG_TYPE_FILE 1 +#define OSRF_LOG_TYPE_SYSLOG 2 + + +#define OSRF_LOG_GO(m,l) \ + if(!m) return; \ + VA_LIST_TO_STRING(m); \ + osrfLogDetail( l, NULL, -1, NULL, VA_BUF ); + + + +/* Initializes the logger. */ +void osrfLogInit( int type, const char* appname, int maxlevel ); +/** Sets the type of logging to perform. See log types */ +void osrfLogSetType( int logtype ); +/** Sets the systlog facility for the regular logs */ +void osrfLogSetSyslogFacility( int facility ); +/** Sets the systlog facility for the activity logs */ +void osrfLogSetSyslogActFacility( int facility ); +/** Sets the log file to use if we're logging to a file */ +void osrfLogSetFile( const char* logfile ); +/* once we know which application we're running, call this method to + * set the appname so log lines can include the app name */ +void osrfLogSetAppname( const char* appname ); +/** Sets the global log level. Any log statements with a higher level + * than "level" will not be logged */ +void osrfLogSetLevel( int loglevel ); +/* Log an error message */ +void osrfLogError( const char* msg, ... ); +/* Log a warning message */ +void osrfLogWarning( const char* msg, ... ); +/* log an info message */ +void osrfLogInfo( const char* msg, ... ); +/* Log a debug message */ +void osrfLogDebug( const char* msg, ... ); +/* Log an internal debug message */ +void osrfLogInternal( const char* msg, ... ); +/* Log an activity message */ +void osrfLogActivity( const char* msg, ... ); + +void osrfLogSetActivityEnabled( int enabled ); + +/* Use this for logging detailed message containing the filename, line number + * and function name in addition to the usual level and message */ +void osrfLogDetail( int level, char* filename, int line, char* func, char* msg, ... ); + +void _osrfLogToFile( char* msg, ... ); + +int osrfLogFacilityToInt( char* facility ); + +#endif diff --git a/src/utils/logging.c b/src/utils/logging.c index 6f60798..1a993d3 100644 --- a/src/utils/logging.c +++ b/src/utils/logging.c @@ -32,7 +32,7 @@ int fatal_handler( char* msg, ... ) { va_list args; if( logging ) { - if( log_level < LOG_ERROR ) + if( log_level < OSRF_LOG_ERROR ) return -1; log_file = fopen( lf, "a" ); @@ -74,7 +74,7 @@ int warning_handler( char* msg, ... ) { pid_t pid = getpid(); va_list args; - if( log_level < LOG_WARNING ) + if( log_level < OSRF_LOG_WARNING ) return -1; if(logging) { @@ -122,7 +122,7 @@ int info_handler( char* msg, ... ) { pid_t pid = getpid(); va_list args; - if( log_level < LOG_INFO ) + if( log_level < OSRF_LOG_INFO ) return -1; if(logging) { @@ -172,7 +172,7 @@ int debug_handler( char* msg, ... ) { pid_t pid = getpid(); va_list args; - if( log_level < LOG_DEBUG ) + if( log_level < OSRF_LOG_DEBUG ) return -1; if(logging) { diff --git a/src/utils/logging.h b/src/utils/logging.h index c2523af..5e8f7bc 100644 --- a/src/utils/logging.h +++ b/src/utils/logging.h @@ -16,10 +16,12 @@ #define LOGGING_H -#define LOG_ERROR 1 -#define LOG_WARNING 2 -#define LOG_INFO 3 -#define LOG_DEBUG 4 +#define OSRF_LOG_ERROR 1 +#define OSRF_LOG_WARNING 2 +#define OSRF_LOG_INFO 3 +#define OSRF_LOG_DEBUG 4 +#define OSRF_LOG_INTERNAL 5 +#define OSRF_LOG_ACTIVITY 6 // --------------------------------------------------------------------------------- // Error handling interface. diff --git a/src/utils/socket_bundle.c b/src/utils/socket_bundle.c index 41012c5..f1ae720 100644 --- a/src/utils/socket_bundle.c +++ b/src/utils/socket_bundle.c @@ -39,7 +39,7 @@ int main(int argc, char* argv[]) { /* -int debug_handler(char* msg, ...) { +int osrfLogDebug(char* msg, ...) { va_list args; va_start(args, msg); vfprintf(stderr, msg, args); @@ -48,7 +48,7 @@ int debug_handler(char* msg, ...) { return -1; } -int warning_handler(char* msg, ...) { +int osrfLogWarning(char* msg, ...) { va_list args; va_start(args, msg); vfprintf(stderr, msg, args); @@ -63,7 +63,7 @@ socket_node* _socket_add_node(socket_manager* mgr, int endpoint, int addr_type, int sock_fd, int parent_id ) { if(mgr == NULL) return NULL; - debug_handler("Adding socket node with fd %d", sock_fd); + osrfLogDebug("Adding socket node with fd %d", sock_fd); socket_node* new_node = safe_malloc(sizeof(socket_node)); new_node->endpoint = endpoint; @@ -84,15 +84,20 @@ socket_node* _socket_add_node(socket_manager* mgr, socket_type is one of INET or UNIX */ int socket_open_tcp_server(socket_manager* mgr, int port, char* listen_ip) { - if( mgr == NULL ) return warning_handler("socket_open_tcp_server(): NULL mgr"); + if( mgr == NULL ) { + osrfLogWarning("socket_open_tcp_server(): NULL mgr"); + return -1; + } int sock_fd; struct sockaddr_in server_addr; sock_fd = socket(AF_INET, SOCK_STREAM, 0); - if(sock_fd < 0) - return warning_handler("tcp_server_connect(): Unable to create socket"); + if(sock_fd < 0) { + osrfLogWarning("tcp_server_connect(): Unable to create socket"); + return -1; + } server_addr.sin_family = AF_INET; @@ -104,11 +109,15 @@ int socket_open_tcp_server(socket_manager* mgr, int port, char* listen_ip) { server_addr.sin_port = htons(port); - if(bind( sock_fd, (struct sockaddr*) &server_addr, sizeof(server_addr)) < 0) - return warning_handler("tcp_server_connect(): cannot bind to port %d", port ); + if(bind( sock_fd, (struct sockaddr*) &server_addr, sizeof(server_addr)) < 0) { + osrfLogWarning("tcp_server_connect(): cannot bind to port %d", port ); + return -1; + } - if(listen(sock_fd, 20) == -1) - return warning_handler("tcp_server_connect(): listen() returned error"); + if(listen(sock_fd, 20) == -1) { + osrfLogWarning("tcp_server_connect(): listen() returned error"); + return -1; + } _socket_add_node(mgr, SERVER_SOCKET, INET, sock_fd, 0); return sock_fd; @@ -117,35 +126,40 @@ int socket_open_tcp_server(socket_manager* mgr, int port, char* listen_ip) { int socket_open_unix_server(socket_manager* mgr, char* path) { if(mgr == NULL || path == NULL) return -1; - debug_handler("opening unix socket at %s", path); + osrfLogDebug("opening unix socket at %s", path); int sock_fd; struct sockaddr_un server_addr; sock_fd = socket(AF_UNIX, SOCK_STREAM, 0); - if(sock_fd < 0) - return warning_handler("socket_open_unix_server(): socket() failed"); + if(sock_fd < 0){ + osrfLogWarning("socket_open_unix_server(): socket() failed"); + return -1; + } server_addr.sun_family = AF_UNIX; strcpy(server_addr.sun_path, path); if( bind(sock_fd, (struct sockaddr*) &server_addr, sizeof(struct sockaddr_un)) < 0) { - return warning_handler( + osrfLogWarning( "socket_open_unix_server(): cannot bind to unix port %s", path ); + return -1; } - if(listen(sock_fd, 20) == -1) - return warning_handler("socket_open_unix_server(): listen() returned error"); + if(listen(sock_fd, 20) == -1) { + osrfLogWarning("socket_open_unix_server(): listen() returned error"); + return -1; + } - debug_handler("unix socket successfully opened"); + osrfLogDebug("unix socket successfully opened"); int i = 1; /* causing problems with router for some reason ... */ - //debug_handler("Setting SO_REUSEADDR"); + //osrfLogDebug("Setting SO_REUSEADDR"); //setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i)); - debug_handler("Setting TCP_NODELAY"); + osrfLogDebug("Setting TCP_NODELAY"); setsockopt(sock_fd, IPPROTO_TCP, TCP_NODELAY, &i, sizeof(i)); _socket_add_node(mgr, SERVER_SOCKET, UNIX, sock_fd, 0); @@ -153,6 +167,33 @@ int socket_open_unix_server(socket_manager* mgr, char* path) { } + +int socket_open_udp_server( + socket_manager* mgr, int port, char* listen_ip ) { + + int sockfd; + struct sockaddr_in server_addr; + + if( (sockfd = socket( AF_INET, SOCK_DGRAM, 0 )) < 0 ) { + osrfLogWarning("Unable to create UDP socket"); + return -1; + } + + server_addr.sin_family = AF_INET; + server_addr.sin_port = htons(port); + if(listen_ip) server_addr.sin_addr.s_addr = inet_addr(listen_ip); + else server_addr.sin_addr.s_addr = htonl(INADDR_ANY); + + if( (bind (sockfd, (struct sockaddr *) &server_addr,sizeof(server_addr))) ) { + osrfLogWarning("Unable to bind to UDP port %d", port); + return -1; + } + + _socket_add_node(mgr, SERVER_SOCKET, INET, sockfd, 0); + return sockfd; +} + + int socket_open_tcp_client(socket_manager* mgr, int port, char* dest_addr) { struct sockaddr_in remoteAddr, localAddr; @@ -163,12 +204,12 @@ int socket_open_tcp_client(socket_manager* mgr, int port, char* dest_addr) { // Create the socket // ------------------------------------------------------------------ if( (sock_fd = socket( AF_INET, SOCK_STREAM, 0 )) < 0 ) { - warning_handler( "tcp_connect(): Cannot create socket" ); + osrfLogWarning( "tcp_connect(): Cannot create socket" ); return -1; } int i = 1; - debug_handler("Setting TCP_NODELAY"); + osrfLogDebug("Setting TCP_NODELAY"); setsockopt(sock_fd, IPPROTO_TCP, TCP_NODELAY, &i, sizeof(i)); @@ -176,7 +217,7 @@ int socket_open_tcp_client(socket_manager* mgr, int port, char* dest_addr) { // Get the hostname // ------------------------------------------------------------------ if( (hptr = gethostbyname( dest_addr ) ) == NULL ) { - warning_handler( "tcp_connect(): Unknown Host => %s", dest_addr ); + osrfLogWarning( "tcp_connect(): Unknown Host => %s", dest_addr ); return -1; } @@ -201,7 +242,7 @@ int socket_open_tcp_client(socket_manager* mgr, int port, char* dest_addr) { // Bind to a local port // ------------------------------------------------------------------ if( bind( sock_fd, (struct sockaddr *) &localAddr, sizeof( localAddr ) ) < 0 ) { - warning_handler( "tcp_connect(): Cannot bind to local port" ); + osrfLogWarning( "tcp_connect(): Cannot bind to local port" ); return -1; } @@ -209,7 +250,7 @@ int socket_open_tcp_client(socket_manager* mgr, int port, char* dest_addr) { // Connect to server // ------------------------------------------------------------------ if( connect( sock_fd, (struct sockaddr*) &remoteAddr, sizeof( struct sockaddr_in ) ) < 0 ) { - warning_handler( "tcp_connect(): Cannot connect to server %s", dest_addr ); + osrfLogWarning( "tcp_connect(): Cannot connect to server %s", dest_addr ); return -1; } @@ -219,21 +260,62 @@ int socket_open_tcp_client(socket_manager* mgr, int port, char* dest_addr) { } +int socket_open_udp_client( + socket_manager* mgr, int port, char* dest_addr) { + + int sockfd; + struct sockaddr_in client_addr, server_addr; + struct hostent* host; + + if( (host = gethostbyname(dest_addr)) == NULL) { + osrfLogWarning("Unable to resolve host: %s", dest_addr); + return -1; + } + + server_addr.sin_family = host->h_addrtype; + memcpy((char *) &server_addr.sin_addr.s_addr, + host->h_addr_list[0], host->h_length); + server_addr.sin_port = htons(port); + + if( (sockfd = socket(AF_INET,SOCK_DGRAM,0)) < 0 ) { + osrfLogWarning("Unable to create UDP socket"); + return -1; + } + + client_addr.sin_family = AF_INET; + client_addr.sin_addr.s_addr = htonl(INADDR_ANY); + client_addr.sin_port = htons(0); + + if( (bind(sockfd, (struct sockaddr *) &client_addr, sizeof(client_addr))) < 0 ) { + osrfLogWarning("Unable to bind UDP socket"); + return -1; + } + + _socket_add_node(mgr, CLIENT_SOCKET, INET, sockfd, -1 ); + + return sockfd; +} + + int socket_open_unix_client(socket_manager* mgr, char* sock_path) { int sock_fd, len; struct sockaddr_un usock; - if( (sock_fd = socket( AF_UNIX, SOCK_STREAM, 0 )) < 0 ) - return warning_handler( "Cannot create socket" ); + if( (sock_fd = socket( AF_UNIX, SOCK_STREAM, 0 )) < 0 ) { + osrfLogWarning( "Cannot create socket" ); + return -1; + } usock.sun_family = AF_UNIX; strcpy( usock.sun_path, sock_path ); len = sizeof( usock.sun_family ) + strlen( usock.sun_path ); - if( connect( sock_fd, (struct sockaddr *) &usock, len ) < 0 ) - return warning_handler( "Error connecting to unix socket" ); + if( connect( sock_fd, (struct sockaddr *) &usock, len ) < 0 ) { + osrfLogWarning( "Error connecting to unix socket" ); + return -1; + } _socket_add_node(mgr, CLIENT_SOCKET, UNIX, sock_fd, -1 ); @@ -259,7 +341,7 @@ void socket_remove_node(socket_manager* mgr, int sock_fd) { if(mgr == NULL) return; - debug_handler("removing socket %d", sock_fd); + osrfLogDebug("removing socket %d", sock_fd); socket_node* head = mgr->socket; socket_node* tail = head; @@ -269,7 +351,7 @@ void socket_remove_node(socket_manager* mgr, int sock_fd) { if(head->sock_fd == sock_fd) { mgr->socket = head->next; free(head); - debug_handler("removing first socket in list"); + osrfLogDebug("removing first socket in list"); return; } @@ -292,24 +374,25 @@ void socket_remove_node(socket_manager* mgr, int sock_fd) { void _socket_print_list(socket_manager* mgr) { if(mgr == NULL) return; socket_node* node = mgr->socket; - debug_handler("socket_node list: ["); + osrfLogDebug("socket_node list: ["); while(node) { - debug_handler("sock_fd: %d | parent_id: %d", + osrfLogDebug("sock_fd: %d | parent_id: %d", node->sock_fd, node->parent_id); node = node->next; } - debug_handler("]"); + osrfLogDebug("]"); } /* sends the given data to the given socket */ int socket_send(int sock_fd, const char* data) { - debug_handler( "socket_bundle sending to %d data %s", + osrfLogDebug( "socket_bundle sending to %d data %s", sock_fd, data); - debug_handler("%d : Sending data at %lf\n", getpid(), get_timestamp_millis()); + osrfLogDebug("%d : Sending data at %lf\n", getpid(), get_timestamp_millis()); signal(SIGPIPE, SIG_IGN); /* in case a unix socket was closed */ if( send( sock_fd, data, strlen(data), 0 ) < 0 ) { - return warning_handler( "tcp_server_send(): Error sending data" ); + osrfLogWarning( "tcp_server_send(): Error sending data" ); + return -1; } return 0; @@ -319,13 +402,13 @@ int socket_send(int sock_fd, const char* data) { it from the socket set */ void socket_disconnect(socket_manager* mgr, int sock_fd) { - debug_handler("Closing socket %d", sock_fd); + osrfLogDebug("Closing socket %d", sock_fd); if( shutdown( sock_fd, SHUT_RDWR ) ) - warning_handler( "socket_disconnect(): Error shuting down socket, removing anyway" ); + osrfLogWarning( "socket_disconnect(): Error shuting down socket, removing anyway" ); if( close( sock_fd ) == -1 ) - warning_handler( "socket_disconnect(): Error closing socket, removing anyway" ); + osrfLogWarning( "socket_disconnect(): Error closing socket, removing anyway" ); if(mgr != NULL) socket_remove_node(mgr, sock_fd); @@ -361,26 +444,31 @@ int socket_wait(socket_manager* mgr, int timeout, int sock_fd) { // If timeout is -1, we block indefinitely if( (retval = select( sock_fd + 1, &read_set, NULL, NULL, NULL)) == -1 ) { - warning_handler("Sys Error: %s", strerror(errno)); - return warning_handler("Call to select interrupted"); + osrfLogWarning("Sys Error: %s", strerror(errno)); + osrfLogWarning("Call to select interrupted"); + return -1; } } else if( timeout > 0 ) { /* timeout of 0 means don't block */ if( (retval = select( sock_fd + 1, &read_set, NULL, NULL, &tv)) == -1 ) { - warning_handler("Sys Error: %s", strerror(errno)); - return warning_handler( "Call to select interrupted" ); + osrfLogWarning("Sys Error: %s", strerror(errno)); + osrfLogWarning( "Call to select interrupted" ); + return -1; } } - debug_handler("%d active sockets after select()", retval); + osrfLogDebug("%d active sockets after select()", retval); return _socket_route_data_id(mgr, sock_fd); } int socket_wait_all(socket_manager* mgr, int timeout) { - if(mgr == NULL) return warning_handler( "tcp_wait(): null mgr" ); + if(mgr == NULL) { + osrfLogWarning( "tcp_wait(): null mgr" ); + return -1; + } int retval = 0; fd_set read_set; @@ -389,7 +477,7 @@ int socket_wait_all(socket_manager* mgr, int timeout) { socket_node* node = mgr->socket; int max_fd = 0; while(node) { - //debug_handler("Adding socket %d to select set",node->sock_fd); + //osrfLogDebug("Adding socket %d to select set",node->sock_fd); FD_SET( node->sock_fd, &read_set ); if(node->sock_fd > max_fd) max_fd = node->sock_fd; node = node->next; @@ -404,19 +492,21 @@ int socket_wait_all(socket_manager* mgr, int timeout) { // If timeout is -1, there is no timeout passed to the call to select if( (retval = select( max_fd, &read_set, NULL, NULL, NULL)) == -1 ) { - warning_handler("Sys Error: %s", strerror(errno)); - return warning_handler("Call to select interrupted"); + osrfLogWarning("Sys Error: %s", strerror(errno)); + osrfLogWarning("Call to select interrupted"); + return -1; } } else if( timeout != 0 ) { /* timeout of 0 means don't block */ if( (retval = select( max_fd, &read_set, NULL, NULL, &tv)) == -1 ) { - warning_handler("Sys Error: %s", strerror(errno)); - return warning_handler( "Call to select interrupted" ); + osrfLogWarning("Sys Error: %s", strerror(errno)); + osrfLogWarning( "Call to select interrupted" ); + return -1; } } - debug_handler("%d active sockets after select()", retval); + osrfLogDebug("%d active sockets after select()", retval); return _socket_route_data(mgr, retval, &read_set); } @@ -443,7 +533,7 @@ int _socket_route_data( if(last_failed_id != -1) { /* in case it was not removed by our overlords */ - debug_handler("Attempting to remove last_failed_id of %d", last_failed_id); + osrfLogDebug("Attempting to remove last_failed_id of %d", last_failed_id); socket_remove_node( mgr, last_failed_id ); last_failed_id = -1; status = -1; @@ -453,7 +543,7 @@ int _socket_route_data( /* does this socket have data? */ if( FD_ISSET( sock_fd, read_set ) ) { - debug_handler("Socket %d active", sock_fd); + osrfLogDebug("Socket %d active", sock_fd); handled++; FD_CLR(sock_fd, read_set); @@ -467,7 +557,7 @@ int _socket_route_data( us...start over with the first socket */ if(status == -1) { last_failed_id = sock_fd; - debug_handler("Backtracking back to start of loop because " + osrfLogDebug("Backtracking back to start of loop because " "of -1 return code from _socket_handle_client_data()"); } } @@ -509,16 +599,18 @@ int _socket_handle_new_client(socket_manager* mgr, socket_node* node) { int new_sock_fd; new_sock_fd = accept(node->sock_fd, NULL, NULL); - if(new_sock_fd < 0) - return warning_handler("_socket_route_data(): accept() failed"); + if(new_sock_fd < 0) { + osrfLogWarning("_socket_route_data(): accept() failed"); + return -1; + } if(node->addr_type == INET) { _socket_add_node(mgr, CLIENT_SOCKET, INET, new_sock_fd, node->sock_fd); - debug_handler("Adding new INET client for %d", node->sock_fd); + osrfLogDebug("Adding new INET client for %d", node->sock_fd); } else if(node->addr_type == UNIX) { _socket_add_node(mgr, CLIENT_SOCKET, UNIX, new_sock_fd, node->sock_fd); - debug_handler("Adding new UNIX client for %d", node->sock_fd); + osrfLogDebug("Adding new UNIX client for %d", node->sock_fd); } return 0; @@ -534,12 +626,12 @@ int _socket_handle_client_data(socket_manager* mgr, socket_node* node) { memset(buf, 0, RBUFSIZE); set_fl(sock_fd, O_NONBLOCK); - debug_handler("Gathering client data for %d", node->sock_fd); + osrfLogDebug("Gathering client data for %d", node->sock_fd); - debug_handler("%d : Received data at %lf\n", getpid(), get_timestamp_millis()); + osrfLogDebug("%d : Received data at %lf\n", getpid(), get_timestamp_millis()); while( (read_bytes = recv(sock_fd, buf, RBUFSIZE-1, 0) ) > 0 ) { - debug_handler("Socket %d Read %d bytes and data: %s", sock_fd, read_bytes, buf); + osrfLogDebug("Socket %d Read %d bytes and data: %s", sock_fd, read_bytes, buf); if(mgr->data_received) mgr->data_received(mgr->blob, mgr, sock_fd, buf, node->parent_id); @@ -550,7 +642,7 @@ int _socket_handle_client_data(socket_manager* mgr, socket_node* node) { clr_fl(sock_fd, O_NONBLOCK); if(read_bytes < 0) { if( errno != EAGAIN ) - warning_handler( " * Error reading socket with errno %d", errno ); + osrfLogWarning( " * Error reading socket with errno %d", errno ); } } else { return -1; } /* inform the caller that this node has been tampered with */ diff --git a/src/utils/socket_bundle.h b/src/utils/socket_bundle.h index 129cf30..6699d32 100644 --- a/src/utils/socket_bundle.h +++ b/src/utils/socket_bundle.h @@ -6,7 +6,7 @@ #include #include "utils.h" -#include "logging.h" +#include "log.h" //--------------------------------------------------------------- // Unix headers @@ -75,6 +75,8 @@ int socket_open_tcp_server(socket_manager*, int port, char* listen_ip ); int socket_open_unix_server(socket_manager* mgr, char* path); +int socket_open_udp_server( socket_manager* mgr, int port, char* listen_ip ); + /* creates a client TCP socket and adds it to the socket set. returns 0 on success. -1 on failure. */ int socket_open_tcp_client(socket_manager*, int port, char* dest_addr); @@ -83,6 +85,8 @@ int socket_open_tcp_client(socket_manager*, int port, char* dest_addr); returns 0 on success. -1 on failure. */ int socket_open_unix_client(socket_manager*, char* sock_path); +int socket_open_udp_client( socket_manager* mgr, int port, char* dest_addr); + /* returns the socket_node with the given sock_fd */ socket_node* socket_find_node(socket_manager*, int sock_fd); diff --git a/src/utils/string_array.c b/src/utils/string_array.c index ffae2fc..7f1ef7d 100644 --- a/src/utils/string_array.c +++ b/src/utils/string_array.c @@ -6,7 +6,7 @@ osrfStringArray* osrfNewStringArray(int size) { string_array* init_string_array(int size) { if(size > STRING_ARRAY_MAX_SIZE) - fatal_handler("init_string_array size is too large"); + osrfLogError("init_string_array size is too large"); string_array* arr = (string_array*) safe_malloc(sizeof(string_array)); @@ -28,7 +28,7 @@ void string_array_add(string_array* arr, char* str) { arr->size++; if( arr->size > STRING_ARRAY_MAX_SIZE ) - fatal_handler("string_array_add size is too large"); + osrfLogError("string_array_add size is too large"); /* if necessary, double capacity */ if(arr->size >= arr->arr_size) { diff --git a/src/utils/string_array.h b/src/utils/string_array.h index 3a7efbd..0bee6e5 100644 --- a/src/utils/string_array.h +++ b/src/utils/string_array.h @@ -1,7 +1,7 @@ #include #include "utils.h" -#include "logging.h" +#include "log.h" #define STRING_ARRAY_MAX_SIZE 1024