From 097b93aa8b3a1f9cbdcaa5dc5eab71f89b8881af Mon Sep 17 00:00:00 2001 From: Koennecke Mark Date: Tue, 17 May 2016 11:30:38 +0200 Subject: [PATCH] Added a loglsisten command for listening into the log --- loglisten.c | 95 +++++++++++++++++++++++++++++++++++++++++++++++++++++ logv2.c | 2 +- make_gen | 2 +- ofac.c | 1 + 4 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 loglisten.c diff --git a/loglisten.c b/loglisten.c new file mode 100644 index 00000000..111b251a --- /dev/null +++ b/loglisten.c @@ -0,0 +1,95 @@ +/** + * This is a means to listen into the stream of log messages from the logv2 + * logging system. + * + * Mark Koennecke, May 2016 + */ +#include +#include +#include + +/* + From logv2.c +*/ +extern int subsystemFromText(const char *text); +/*=============================================================================*/ +static int listenerList; +static int callbackRegistered = 0; + +typedef struct { + SConnection *pCon; + char subsystem[64]; +} ListenEntry; +/*-----------------------------------------------------------------------------*/ +static void LogListenCallback(unsigned int severity, const char *timeStamp, + const char *subsystem, + const char *message, void *userData) +{ + ListenEntry current; + int status, cleanupNeeded = 0; + + status = LLDnodePtr2First(listenerList); + while(status != 0){ + LLDnodeDataTo(listenerList,¤t); + if(strcmp(current.subsystem,subsystem) == 0) { + if(SCisConnected(current.pCon)){ + SCPureSockWrite(current.pCon,(char *)message,eValue); + } else { + cleanupNeeded = 1; + } + } + status = LLDnodePtr2Next(listenerList); + } + + /* + lld lists sometimes get confused when deleting nodes on the fly. + This is why this has been put into a separate loop + */ + if(cleanupNeeded){ + status = LLDnodePtr2First(listenerList); + while(status != 0){ + LLDnodeDataTo(listenerList,¤t); + if(!SCisConnected(current.pCon)){ + SCDeleteConnection(current.pCon); + LLDnodeDelete(listenerList); + } + status = LLDnodePtr2Next(listenerList); + } + } + +} +/*-----------------------------------------------------------------------------*/ +static int LogListenAction(SConnection * pCon, SicsInterp * pSics, + void *pData, int argc, char *argv[]) +{ + ListenEntry listLog; + + if(argc < 2){ + SCWrite(pCon,"ERROR: need subsystem argument for loglisten", eError); + return 0; + } + + if(subsystemFromText(argv[1]) < 0){ + SCPrintf(pCon,eError, "ERROR: invalid subsystem %s specified", argv[1]); + return 0; + } + + + listLog.pCon = SCCopyConnection(pCon); + strncpy(listLog.subsystem,argv[1],sizeof(listLog.subsystem)); + LLDnodeAppendFrom(listenerList,&listLog); + + if(!callbackRegistered){ + RegisterLogCallback(LogListenCallback,NULL); + callbackRegistered = 1; + } + + SCSendOK(pCon); + return 1; +} +/*-----------------------------------------------------------------------------*/ +void LogListenInit(void) +{ + listenerList = LLDcreate(sizeof(ListenEntry)); + AddCmd("loglisten", LogListenAction); +} diff --git a/logv2.c b/logv2.c index 8ac06b87..4cbdf83f 100644 --- a/logv2.c +++ b/logv2.c @@ -202,7 +202,7 @@ static unsigned int sevFromText(const char *txt) return sev; } /*=============================================================================*/ -static unsigned int subsystemFromText(const char *text) +int subsystemFromText(const char *text) { int i; static const char *subText[] = {"sys", diff --git a/make_gen b/make_gen index f4f8cad0..07887aa0 100644 --- a/make_gen +++ b/make_gen @@ -14,7 +14,7 @@ SOBJ = network.o ifile.o conman.o SCinter.o splitter.o passwd.o \ sicsexit.o costa.o task.o $(FORTIFYOBJ) testprot.o\ macro.o ofac.o obpar.o obdes.o drive.o status.o intserv.o \ devexec.o mumo.o mumoconf.o selector.o selvar.o fupa.o lld.o \ - lld_blob.o strrepl.o lin2ang.o fomerge.o \ + lld_blob.o strrepl.o lin2ang.o fomerge.o loglisten.o \ script.o o2t.o alias.o stringdict.o sdynar.o \ histmem.o histdriv.o histsim.o interface.o callback.o \ event.o emon.o evcontroller.o evdriver.o simev.o perfmon.o \ diff --git a/ofac.c b/ofac.c index df88921e..9220920e 100644 --- a/ofac.c +++ b/ofac.c @@ -55,6 +55,7 @@ static void InitGeneral(void) INIT(InitTaskOBJ); INIT(RemoteObjectInit); INIT(Logv2Init); + INIT(LogListenInit); INIT(SiteInit); /* site specific initializations */ }