diff --git a/src/libCom/logClient/iocLog.c b/src/libCom/logClient/iocLog.c index c6c707c30..e62da2050 100644 --- a/src/libCom/logClient/iocLog.c +++ b/src/libCom/logClient/iocLog.c @@ -18,7 +18,6 @@ #define epicsExportSharedSymbols #include "envDefs.h" -#include "errlog.h" #include "logClient.h" #include "iocLog.h" @@ -90,9 +89,6 @@ static logClientId iocLogClientInit (void) return NULL; } id = logClientCreate (addr, port); - if (id != NULL) { - errlogAddListener ( logClientSendMessage, id ); - } return id; } @@ -139,13 +135,3 @@ logClientId epicsShareAPI logClientInit (void) { return iocLogClientInit (); } - -/* - * logClientSendMessage (); deprecated - */ -void logClientSendMessage ( logClientId id, const char * message ) -{ - if ( !iocLogDisable ) { - logClientSend (id, message); - } -} diff --git a/src/libCom/logClient/logClient.c b/src/libCom/logClient/logClient.c index 4f5daaab1..99ee671d9 100644 --- a/src/libCom/logClient/logClient.c +++ b/src/libCom/logClient/logClient.c @@ -24,6 +24,8 @@ #define epicsExportSharedSymbols #include "dbDefs.h" #include "epicsEvent.h" +#include "iocLog.h" +#include "errlog.h" #include "epicsMutex.h" #include "epicsThread.h" #include "epicsTime.h" @@ -54,6 +56,11 @@ static const double LOG_RESTART_DELAY = 5.0; /* sec */ static const double LOG_SERVER_CREATE_CONNECT_SYNC_TIMEOUT = 5.0; /* sec */ static const double LOG_SERVER_SHUTDOWN_TIMEOUT = 30.0; /* sec */ +/* + * If set using iocLogPrefix() this string is prepended to all log messages: + */ +static char* logClientPrefix = NULL; + /* * logClientClose () */ @@ -147,6 +154,8 @@ static void logClientDestroy (logClientId id) return; } + errlogRemoveListeners ( logClientSendMessage, (void *) pClient ); + logClientClose ( pClient ); epicsMutexDestroy ( pClient->mutex ); @@ -156,22 +165,13 @@ static void logClientDestroy (logClientId id) free ( pClient ); } -/* - * logClientSend () +/* + * This method requires the pClient->mutex be owned already. */ -void epicsShareAPI logClientSend ( logClientId id, const char * message ) -{ - logClient * pClient = ( logClient * ) id; +static void sendMessageChunk(logClient * pClient, const char * message) { unsigned strSize; - if ( ! pClient || ! message ) { - return; - } - strSize = strlen ( message ); - - epicsMutexMustLock ( pClient->mutex ); - while ( strSize ) { unsigned msgBufBytesLeft = sizeof ( pClient->msgBuf ) - pClient->nextMsgIndex; @@ -228,10 +228,31 @@ void epicsShareAPI logClientSend ( logClientId id, const char * message ) break; } } - +} + + +/* + * logClientSend () + */ +void epicsShareAPI logClientSend ( logClientId id, const char * message ) +{ + logClient * pClient = ( logClient * ) id; + + if ( ! pClient || ! message ) { + return; + } + + epicsMutexMustLock ( pClient->mutex ); + + if (logClientPrefix) { + sendMessageChunk(pClient, logClientPrefix); + } + sendMessageChunk(pClient, message); + epicsMutexUnlock (pClient->mutex); } + void epicsShareAPI logClientFlush ( logClientId id ) { logClient * pClient = ( logClient * ) id; @@ -528,6 +549,8 @@ logClientId epicsShareAPI logClientCreate ( pClient->name, LOG_SERVER_CREATE_CONNECT_SYNC_TIMEOUT ); } + errlogAddListener ( logClientSendMessage, (void *) pClient ); + return (void *) pClient; } @@ -550,5 +573,46 @@ void epicsShareAPI logClientShow (logClientId id, unsigned level) pClient->sock==INVALID_SOCKET?"INVALID":"OK", pClient->connectCount); } + + if (logClientPrefix) { + printf ("log client: prefix is \"%s\"\n", logClientPrefix); + } } +/* + * logClientSendMessage (); deprecated + */ +void logClientSendMessage ( logClientId id, const char * message ) +{ + if ( !iocLogDisable ) { + logClientSend (id, message); + } +} + +/* + * iocLogPrefix() + */ +void epicsShareAPI iocLogPrefix(const char * prefix) +{ + + /* If we have already established a log prefix, don't let the user change + * it. The iocLogPrefix command is expected to be run from the IOC startup + * script during initialization; the prefix can't be changed once it has + * been set. + */ + + if (logClientPrefix) { + printf ("iocLogPrefix: The prefix was already set to \"%s\" " + "and can't be changed.\n", logClientPrefix); + return; + } + + if (prefix) { + unsigned prefixLen = strlen(prefix); + if (prefixLen > 0) { + char * localCopy = malloc(prefixLen+1); + strcpy(localCopy, prefix); + logClientPrefix = localCopy; + } + } +} diff --git a/src/libCom/logClient/logClient.h b/src/libCom/logClient/logClient.h index 8f1ec09d5..1797bbb20 100644 --- a/src/libCom/logClient/logClient.h +++ b/src/libCom/logClient/logClient.h @@ -33,6 +33,7 @@ epicsShareFunc logClientId epicsShareAPI logClientCreate ( epicsShareFunc void epicsShareAPI logClientSend (logClientId id, const char *message); epicsShareFunc void epicsShareAPI logClientShow (logClientId id, unsigned level); epicsShareFunc void epicsShareAPI logClientFlush (logClientId id); +epicsShareFunc void epicsShareAPI iocLogPrefix(const char* prefix); /* deprecated interface; retained for backward compatibility */ /* note: implementations are in iocLog.c, not logClient.c */