Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions caPutLogApp/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ LIBRARY_IOC = caPutLog
USR_CPPFLAGS += -DUSE_TYPED_RSET
USR_CPPFLAGS_WIN32 += -DNOMINMAX

# set time stamp format:
#USR_CPPFLAGS+=-DDEFAULT_TIME_FMT="%Y-%m-%d %H:%M:%S.%6f"

caPutLog_SRCS += caPutLogTask.c
caPutLog_SRCS += caPutLogAs.c
caPutLog_SRCS += caPutLogClient.c
Expand Down
113 changes: 80 additions & 33 deletions caPutLogApp/caPutJsonLogTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
#include <cstring>
#include <string>

#ifdef _WIN32
#define strtok_r strtok_s
#endif

// Epics Base imports
#include <epicsStdio.h>
#include <envDefs.h>
Expand All @@ -28,6 +32,7 @@
#include <dbAccessDefs.h>
#include <epicsMath.h>
#include <epicsExit.h>
#include <cantProceed.h>
#include <yajl_gen.h>

// This module imports
Expand Down Expand Up @@ -63,12 +68,23 @@ CaPutJsonLogTask::CaPutJsonLogTask()
: caPutJsonLogQ(caPutLogJsonMsgQueueSize, sizeof(LOGDATA *)),
threadId(NULL),
taskStopper(false),
caPutJsonLogClient(NULL),
clients(NULL),
clientsMutex(),
pCaPutJsonLogPV(NULL)
{ }

CaPutJsonLogTask::~CaPutJsonLogTask()
{ }
{
clientItem *client, *nextclient;
epicsMutex::guard_t G(clientsMutex);
nextclient = clients;
clients = NULL;
while (nextclient) {
client = nextclient;
nextclient = client->next;
free(client);
}
}

caPutJsonLogStatus CaPutJsonLogTask::reconfigure(caPutJsonLogConfig config)
{
Expand All @@ -84,22 +100,26 @@ caPutJsonLogStatus CaPutJsonLogTask::reconfigure(caPutJsonLogConfig config)

caPutJsonLogStatus CaPutJsonLogTask::report(int level)
{
if (this->caPutJsonLogClient != NULL) {
logClientShow(this->caPutJsonLogClient, level);

if (clients != NULL) {
clientItem *client;
epicsMutex::guard_t G(clientsMutex);
for (client = clients; client; client = client->next) {
logClientShow(client->caPutJsonLogClient, level);
}
return caPutJsonLogSuccess;
}
else {
errlogSevPrintf(errlogMinor, "caPutJsonLog: log client not initialised\n");
errlogSevPrintf(errlogMinor, "caPutJsonLog: no clients initialised\n");
return caPutJsonLogError;
}
}

caPutJsonLogStatus CaPutJsonLogTask::initialize(const char* address, caPutJsonLogConfig config)
caPutJsonLogStatus CaPutJsonLogTask::initialize(const char* addresslist, caPutJsonLogConfig config)
{
caPutJsonLogStatus status;

// Store passed configuration parameters
this->address = epicsStrDup(address);
this->reconfigure(config);

// Check if user enabled the logger
Expand All @@ -112,26 +132,44 @@ caPutJsonLogStatus CaPutJsonLogTask::initialize(const char* address, caPutJsonLo
this->configurePvLogging();

// Initialize server logging
if (this->caPutJsonLogClient == NULL) {
status = configureServerLogging(address);
if (status != caPutJsonLogSuccess) return status;
if (!addresslist || !addresslist[0]) {
addresslist = envGetConfigParamPtr(&EPICS_CA_JSON_PUT_LOG_ADDR);
}

// Start logger
status = this->start();
if (status != caPutJsonLogSuccess) {
return status;
if (addresslist == NULL) {
errlogSevPrintf(errlogMajor, "caPutJsonLog: server address not specified\n");
return caPutJsonLogError;
}

// Initialize caPutLogAs
status = static_cast<caPutJsonLogStatus>(caPutLogAsInit(caddPutToQueue, NULL));
if (status != caPutJsonLogSuccess) {
errlogSevPrintf(errlogMinor, "caPutJsonLog: failed to configure Access security\n");
return caPutJsonLogError;
char *addresslistcopy1, *addresslistcopy2;
addresslistcopy2 = addresslistcopy1 = epicsStrDup(addresslist);
char *saveptr;
while (true) {
char *address = strtok_r(addresslistcopy1, " \t\n\r", &saveptr);
if (!address) break;
addresslistcopy1 = NULL;
configureServerLogging(address);
}
free(addresslistcopy2);
if (!clients)
return caPutJsonLogError;

// Start logger if not done already
if (!threadId) {
status = this->start();
if (status != caPutJsonLogSuccess) {
return status;
}

// Register exit handler
epicsAtExit(caPutJsonLogExit, NULL);
// Initialize caPutLogAs
status = static_cast<caPutJsonLogStatus>(caPutLogAsInit(caddPutToQueue, NULL));
if (status != caPutJsonLogSuccess) {
errlogSevPrintf(errlogMinor, "caPutJsonLog: failed to configure Access security\n");
return caPutJsonLogError;
}

// Register exit handler
epicsAtExit(caPutJsonLogExit, NULL);
}

return caPutJsonLogSuccess;
}
Expand Down Expand Up @@ -174,14 +212,15 @@ caPutJsonLogStatus CaPutJsonLogTask::configureServerLogging(const char* address)
{
int status;
struct sockaddr_in saddr;
clientItem** pclient;

// Parse the address
if (!address || !address[0]) {
address = envGetConfigParamPtr(&EPICS_CA_JSON_PUT_LOG_ADDR);
}
if (address == NULL) {
errlogSevPrintf(errlogMajor, "caPutJsonLog: server address not specified\n");
return caPutJsonLogError;
epicsMutex::guard_t G(clientsMutex);
for (pclient = &clients; *pclient; pclient = &(*pclient)->next) {
if (strcmp(address, (*pclient)->address) == 0) {
fprintf (stderr, "caPutJsonLog: address %s already configured\n", address);
return caPutJsonLogSuccess;
}
}

status = aToIPAddr(address, this->default_port, &saddr);
Expand All @@ -191,9 +230,15 @@ caPutJsonLogStatus CaPutJsonLogTask::configureServerLogging(const char* address)
}

// Create log client
this->caPutJsonLogClient = logClientCreate(saddr.sin_addr, ntohs(saddr.sin_port));
if (this->caPutJsonLogClient == NULL) return caPutJsonLogError;

*pclient = (clientItem*)callocMustSucceed(1,sizeof(clientItem)+strlen(address),"caPutJsonLog");
strcpy((*pclient)->address, address);
(*pclient)->caPutJsonLogClient = logClientCreate(saddr.sin_addr, ntohs(saddr.sin_port));
if (!(*pclient)->caPutJsonLogClient) {
fprintf (stderr, "caPutJsonLog: cannot create logClient %s\n", address);
free(*pclient);
*pclient = NULL;
return caPutJsonLogError;
}
return caPutJsonLogSuccess;
}

Expand Down Expand Up @@ -558,8 +603,10 @@ caPutJsonLogStatus CaPutJsonLogTask::buildJsonMsg(const VALUE *pold_value, const

void CaPutJsonLogTask::logToServer(std::string &msg)
{
if (caPutJsonLogClient) {
logClientSend(caPutJsonLogClient, msg.c_str());
clientItem* client;
epicsMutex::guard_t G(clientsMutex);
for (client = clients; client; client = client->next) {
logClientSend (client->caPutJsonLogClient, msg.c_str());
}
}

Expand Down
10 changes: 7 additions & 3 deletions caPutLogApp/caPutJsonLogTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ class epicsShareClass CaPutJsonLogTask {
static CaPutJsonLogTask *instance;

// Logger configuration
const char * address;
int config; // To modify or read this value only epicsAtomic methods should be used

// Interthread communication
Expand All @@ -158,8 +157,13 @@ class epicsShareClass CaPutJsonLogTask {
epicsThreadId threadId;
int taskStopper; // To modify or read this value only epicsAtomic methods should be used

//Logging to a server
logClientId caPutJsonLogClient;
//Logging to a list of servers
struct clientItem {
logClientId caPutJsonLogClient;
struct clientItem *next;
char address[1];
} *clients;
epicsMutex clientsMutex;

// Logging to a PV
DBADDR caPutJsonLogPV;
Expand Down
11 changes: 5 additions & 6 deletions caPutLogApp/caPutLog.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ void caPutLogShow (int level)
{
if (level < 0) level = 0;
if (level > 2) level = 2;
#if 0
caPutLogAsShow(level);
#endif
caPutLogTaskShow();
caPutLogClientShow(level);
}

Expand All @@ -55,9 +53,10 @@ void caPutLogShow (int level)
*/
int caPutLogReconf (int config)
{
#if 0
caPutLogTaskReconf(config);
#endif
if (config < 0)
caPutLogTaskStop();
else
caPutLogTaskStart(config);
caPutLogClientFlush();
return caPutLogSuccess;
}
Expand Down
1 change: 1 addition & 0 deletions caPutLogApp/caPutLog.dbd
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
variable(caPutLogDebug,int)
registrar(caPutLogRegister)
1 change: 1 addition & 0 deletions caPutLogApp/caPutLog.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ extern "C" {
epicsShareFunc int caPutLogInit (const char *addr_str, int config);
epicsShareFunc int caPutLogReconf (int config);
epicsShareFunc void caPutLogShow (int level);
epicsShareFunc void caPutLogSetTimeFmt (const char *format);

#ifdef __cplusplus
}
Expand Down
Loading