This is Unofficial EPICS BASE Doxygen Site
logClient.h File Reference
#include "libComAPI.h"
#include "osiSock.h"
#include "iocLog.h"
+ Include dependency graph for logClient.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Typedefs

typedef void * logClientId
 

Functions

LIBCOM_API logClientId epicsStdCall logClientCreate (struct in_addr server_addr, unsigned short server_port)
 
LIBCOM_API void epicsStdCall logClientSend (logClientId id, const char *message)
 
LIBCOM_API void epicsStdCall logClientShow (logClientId id, unsigned level)
 
LIBCOM_API void epicsStdCall logClientFlush (logClientId id)
 
LIBCOM_API void epicsStdCall iocLogPrefix (const char *prefix)
 
LIBCOM_API logClientId epicsStdCall logClientInit (void)
 

Typedef Documentation

typedef void* logClientId

Definition at line 30 of file logClient.h.

Function Documentation

LIBCOM_API void epicsStdCall iocLogPrefix ( const char *  prefix)

Definition at line 549 of file logClient.c.

550 {
551 
552  /* If we have already established a log prefix, don't let the user change
553  * it. The iocLogPrefix command is expected to be run from the IOC startup
554  * script during initialization; the prefix can't be changed once it has
555  * been set.
556  */
557 
558  if (logClientPrefix) {
559  printf ("iocLogPrefix: The prefix was already set to \"%s\" "
560  "and can't be changed.\n", logClientPrefix);
561  return;
562  }
563 
564  if (prefix) {
565  unsigned prefixLen = strlen(prefix);
566  if (prefixLen > 0) {
567  char * localCopy = malloc(prefixLen+1);
568  strcpy(localCopy, prefix);
569  logClientPrefix = localCopy;
570  }
571  }
572 }
#define printf
Definition: epicsStdio.h:41
LIBCOM_API logClientId epicsStdCall logClientCreate ( struct in_addr  server_addr,
unsigned short  server_port 
)

Definition at line 453 of file logClient.c.

455 {
456  logClient *pClient;
457 
458  pClient = calloc (1, sizeof (*pClient));
459  if (pClient==NULL) {
460  return NULL;
461  }
462 
463  pClient->addr.sin_family = AF_INET;
464  pClient->addr.sin_addr = server_addr;
465  pClient->addr.sin_port = htons(server_port);
466  ipAddrToDottedIP (&pClient->addr, pClient->name, sizeof(pClient->name));
467 
468  pClient->mutex = epicsMutexCreate ();
469  if ( ! pClient->mutex ) {
470  free ( pClient );
471  return NULL;
472  }
473 
474  pClient->sock = INVALID_SOCKET;
475  pClient->connected = 0u;
476  pClient->connFailStatus = 0;
477  pClient->shutdown = 0;
478  pClient->shutdownConfirm = 0;
479 
480  epicsAtExit (logClientDestroy, (void*) pClient);
481 
483  if ( ! pClient->stateChangeNotify ) {
484  epicsMutexDestroy ( pClient->mutex );
485  free ( pClient );
486  return NULL;
487  }
488 
490  if ( ! pClient->shutdownNotify ) {
491  epicsMutexDestroy ( pClient->mutex );
493  free ( pClient );
494  return NULL;
495  }
496 
498  "logRestart", epicsThreadPriorityLow,
500  logClientRestart, pClient );
501  if ( pClient->restartThreadId == NULL ) {
502  epicsMutexDestroy ( pClient->mutex );
504  epicsEventDestroy ( pClient->shutdownNotify );
505  free (pClient);
506  fprintf(stderr, "log client: unable to start reconnection thread\n");
507  return NULL;
508  }
509 
510  return (void *) pClient;
511 }
#define INVALID_SOCKET
Definition: osdSock.h:32
void epicsStdCall epicsMutexDestroy(epicsMutexId pmutexNode)
Destroy an epicsMutex semaphore.
Definition: epicsMutex.cpp:127
struct sockaddr_in addr
Definition: logClient.c:42
#define NULL
Definition: catime.c:38
epicsEventId stateChangeNotify
Definition: logClient.c:47
LIBCOM_API unsigned int epicsStdCall epicsThreadGetStackSize(epicsThreadStackSizeClass size)
Definition: osdThread.c:466
epicsThreadId epicsStdCall epicsThreadCreate(const char *name, unsigned int priority, unsigned int stackSize, EPICSTHREADFUNC funptr, void *parm)
Definition: epicsThread.cpp:33
epicsMutexId mutex
Definition: logClient.c:44
epicsEventId shutdownNotify
Definition: logClient.c:48
SOCKET sock
Definition: logClient.c:45
#define epicsThreadPriorityLow
Definition: epicsThread.h:75
LIBCOM_API void epicsEventDestroy(epicsEventId id)
Destroy an epicsEvent and any resources it holds.
Definition: osdEvent.c:70
int connFailStatus
Definition: logClient.c:55
char name[64]
Definition: logClient.c:43
#define epicsMutexCreate()
Create an epicsMutex semaphore for use from C code.
Definition: epicsMutex.h:168
#define stderr
Definition: epicsStdio.h:32
#define epicsAtExit(F, A)
Convenience macro to register a function and context value to be run when the process exits...
Definition: epicsExit.h:70
unsigned shutdown
Definition: logClient.c:53
unsigned shutdownConfirm
Definition: logClient.c:54
LIBCOM_API epicsEventId epicsEventCreate(epicsEventInitialState initialState)
Create an epicsEvent for use from C code, or return NULL.
Definition: osdEvent.c:47
epicsThreadId restartThreadId
Definition: logClient.c:46
unsigned epicsStdCall ipAddrToDottedIP(const struct sockaddr_in *paddr, char *pBuf, unsigned bufSize)
Definition: osiSock.c:144
unsigned connected
Definition: logClient.c:52
LIBCOM_API void epicsStdCall logClientFlush ( logClientId  id)

Definition at line 219 of file logClient.c.

220 {
221  unsigned nSent;
222  int status = 0;
223 
224  logClient * pClient = ( logClient * ) id;
225 
226  if ( ! pClient || ! pClient->connected ) {
227  return;
228  }
229 
230  epicsMutexMustLock ( pClient->mutex );
231 
232  nSent = pClient->backlog;
233  while ( nSent < pClient->nextMsgIndex && pClient->connected ) {
234  status = send ( pClient->sock, pClient->msgBuf + nSent,
235  pClient->nextMsgIndex - nSent, 0 );
236  if ( status < 0 ) break;
237  nSent += status;
238  }
239 
240  if ( pClient->backlog > 0 && status >= 0 ) {
241  /* On Linux send 0 bytes can detect EPIPE */
242  /* NOOP on Windows, fails on vxWorks */
243  errno = 0;
244  status = send ( pClient->sock, NULL, 0, 0 );
245  if (!(errno == SOCK_ECONNRESET || errno == SOCK_EPIPE)) status = 0;
246  }
247 
248  if ( status < 0 ) {
249  if ( ! pClient->shutdown ) {
250  char sockErrBuf[128];
251  epicsSocketConvertErrnoToString(sockErrBuf, sizeof(sockErrBuf));
252  fprintf(stderr, "log client: lost contact with log server at '%s'\n"
253  " because \"%s\"\n", pClient->name, sockErrBuf);
254  }
255  pClient->backlog = 0;
256  logClientClose ( pClient );
257  }
258  else if ( nSent > 0 && pClient->nextMsgIndex > 0 ) {
259  int backlog = epicsSocketUnsentCount ( pClient->sock );
260  if (backlog >= 0) {
261  pClient->backlog = backlog;
262  nSent -= backlog;
263  }
264  pClient->nextMsgIndex -= nSent;
265  if ( nSent > 0 && pClient->nextMsgIndex > 0 ) {
266  memmove ( pClient->msgBuf, & pClient->msgBuf[nSent],
267  pClient->nextMsgIndex );
268  }
269  }
270  epicsMutexUnlock ( pClient->mutex );
271 }
pvd::Status status
int epicsSocketUnsentCount(SOCKET sock)
char msgBuf[0x4000]
Definition: logClient.c:41
#define NULL
Definition: catime.c:38
void epicsStdCall epicsMutexUnlock(epicsMutexId pmutexNode)
Release the semaphore.
Definition: epicsMutex.cpp:140
void epicsSocketConvertErrnoToString(char *pBuf, unsigned bufSize)
epicsMutexId mutex
Definition: logClient.c:44
#define SOCK_ECONNRESET
Definition: osdSock.h:53
unsigned backlog
Definition: logClient.c:51
SOCKET sock
Definition: logClient.c:45
unsigned nextMsgIndex
Definition: logClient.c:50
char name[64]
Definition: logClient.c:43
#define SOCK_EPIPE
Definition: osdSock.h:65
#define stderr
Definition: epicsStdio.h:32
unsigned shutdown
Definition: logClient.c:53
#define epicsMutexMustLock(ID)
Claim a semaphore (see epicsMutexLock()).
Definition: epicsMutex.h:214
unsigned connected
Definition: logClient.c:52
LIBCOM_API logClientId epicsStdCall logClientInit ( void  )

Definition at line 157 of file iocLog.c.

158 {
159  return iocLogClientInit ();
160 }
LIBCOM_API void epicsStdCall logClientSend ( logClientId  id,
const char *  message 
)

Definition at line 200 of file logClient.c.

201 {
202  logClient * pClient = ( logClient * ) id;
203 
204  if ( ! pClient || ! message ) {
205  return;
206  }
207 
208  epicsMutexMustLock ( pClient->mutex );
209 
210  if (logClientPrefix) {
211  sendMessageChunk(pClient, logClientPrefix);
212  }
213  sendMessageChunk(pClient, message);
214 
215  epicsMutexUnlock (pClient->mutex);
216 }
void epicsStdCall epicsMutexUnlock(epicsMutexId pmutexNode)
Release the semaphore.
Definition: epicsMutex.cpp:140
epicsMutexId mutex
Definition: logClient.c:44
#define epicsMutexMustLock(ID)
Claim a semaphore (see epicsMutexLock()).
Definition: epicsMutex.h:214
LIBCOM_API void epicsStdCall logClientShow ( logClientId  id,
unsigned  level 
)

Definition at line 516 of file logClient.c.

517 {
518  logClient *pClient = (logClient *) id;
519 
520  if ( pClient->connected ) {
521  printf ("log client: connected to log server at '%s'\n", pClient->name);
522  }
523  else {
524  printf ("log client: disconnected from log server at '%s'\n",
525  pClient->name);
526  }
527 
528  if (logClientPrefix) {
529  printf ("log client: prefix is \"%s\"\n", logClientPrefix);
530  }
531 
532  if (level>0) {
533  printf ("log client: sock %s, connect cycles = %u\n",
534  pClient->sock==INVALID_SOCKET?"INVALID":"OK",
535  pClient->connectCount);
536  }
537  if (level>1) {
538  printf ("log client: %u bytes in buffer\n", pClient->nextMsgIndex);
539  if (pClient->nextMsgIndex)
540  printf("-------------------------\n"
541  "%.*s-------------------------\n",
542  (int)(pClient->nextMsgIndex), pClient->msgBuf);
543  }
544 }
#define INVALID_SOCKET
Definition: osdSock.h:32
unsigned connectCount
Definition: logClient.c:49
#define printf
Definition: epicsStdio.h:41
char msgBuf[0x4000]
Definition: logClient.c:41
SOCKET sock
Definition: logClient.c:45
unsigned nextMsgIndex
Definition: logClient.c:50
char name[64]
Definition: logClient.c:43
unsigned connected
Definition: logClient.c:52