Skip to content

Commit

Permalink
Merge pull request #20 from lucmaga/add_metadata
Browse files Browse the repository at this point in the history
Add metadata information to json output

Adds CI using ci-scripts
Fixes #26
  • Loading branch information
anjohnson committed Jun 1, 2024
2 parents 092223c + 18d28e7 commit ebe7120
Show file tree
Hide file tree
Showing 8 changed files with 270 additions and 5 deletions.
1 change: 1 addition & 0 deletions .ci
Submodule .ci added at dead44
74 changes: 74 additions & 0 deletions .github/workflows/ci-build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
name: caPutLog CI

on:
push:
branches:
- '**'
pull_request:
branches:
- '**'

env:
SETUP_PATH: .ci

jobs:
build:
name: ${{ matrix.os }}/${{ matrix.cmp }}/${{ matrix.configuration }}/${{ matrix.cross }}
runs-on: ${{ matrix.os}}
env:
CMP: ${{ matrix.cmp }}
BCFG: ${{ matrix.configuration }}
CI_CROSS_TARGETS: ${{ matrix.cross }}
TEST: ${{ matrix.test }}

strategy:
fail-fast: false
matrix:
include:
- os: ubuntu-latest
cmp: gcc
configuration: default

- os: ubuntu-latest
cmp: gcc
configuration: static

- os: windows-2019
cmp: vs2019
configuration: static

- os: ubuntu-latest
cmp: gcc
configuration: default
cross: "[email protected]"
test: NO

- os: macos-12
cmp: clang
configuration: default

steps:
- uses: actions/checkout@v3
with:
submodules: true

- name: Prepare and compile EPICS dependencies
run: python .ci/cue.py prepare

- name: Build main module
run: python .ci/cue.py build

- name: Run main module tests
run: python .ci/cue.py -T 20M test

- name: Upload tapfiles Artifact
if: ${{ always() }}
uses: actions/upload-artifact@v3
with:
name: tapfiles ${{ matrix.name }}
path: "**/O.*/*.tap"
if-no-files-found: ignore

- name: Collect and show test results
if: ${{ always() }}
run: python .ci/cue.py -T 5M test-results
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule ".ci"]
path = .ci
url = https://github.com/epics-base/ci-scripts
23 changes: 23 additions & 0 deletions caPutLogApp/caPutJsonLogShellCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <errlog.h>
#include <iocsh.h>
#include <epicsExport.h>
#include <string>

#include "caPutJsonLogTask.h"

Expand Down Expand Up @@ -97,6 +98,27 @@ extern "C"
#endif
}

/* Metadata */
int caPutJsonLogAddMetadata(char *property, char *value){
CaPutJsonLogTask *logger = CaPutJsonLogTask::getInstance();
std::string property_str(property);
std::string value_str(value);
if (logger != NULL) return logger->addMetadata(property_str, value_str);
else return -1;
}
static const iocshArg caPutJsonLogAddMetadataArg0 = {"property", iocshArgString};
static const iocshArg caPutJsonLogAddMetadataArg1 = {"value", iocshArgString};
static const iocshArg *const caPutJsonLogAddMetadataArgs[] =
{
&caPutJsonLogAddMetadataArg0,
&caPutJsonLogAddMetadataArg1
};
static const iocshFuncDef caPutJsonLogAddMetadataDef = {"caPutJsonLogAddMetadata", 2, caPutJsonLogAddMetadataArgs};
static void caPutJsonLogAddMetadataCall(const iocshArgBuf *args)
{
caPutJsonLogAddMetadata(args[0].sval, args[1].sval);
}

/* Register JSON IOCsh commands */
static void caPutJsonLogRegister(void)
{
Expand All @@ -108,6 +130,7 @@ extern "C"
iocshRegister(&caPutJsonLogReconfDef,caPutJsonLogReconfCall);
iocshRegister(&caPutJsonLogShowDef,caPutJsonLogShowCall);
iocshRegister(&caPutLogInitDef,caPutLogInitCall);
iocshRegister(&caPutJsonLogAddMetadataDef,caPutJsonLogAddMetadataCall);
caPutLogRegisterDone = 2;
break;

Expand Down
49 changes: 49 additions & 0 deletions caPutLogApp/caPutJsonLogTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@

// Standard library imports
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iterator>
#include <string>

#ifdef _WIN32
Expand Down Expand Up @@ -116,6 +118,42 @@ caPutJsonLogStatus CaPutJsonLogTask::report(int level)
}
}

caPutJsonLogStatus CaPutJsonLogTask::addMetadata(std::string property, std::string value)
{
std::pair<std::map<std::string, std::string>::iterator, bool> ret;
ret = metadata.insert(std::pair<std::string, std::string>(property,value));
if ( ret.second == false ) {
metadata.erase(property);
ret = metadata.insert(std::pair<std::string, std::string>(property,value));
if (ret.second == false) {
errlogSevPrintf(errlogMinor, "caPutJsonLog: fail to add property %s to json log\n", property.c_str());
return caPutJsonLogError;
}
}
errlogSevPrintf(errlogInfo, "caPutJsonLog: add property %s with value %s to json log\n", property.c_str(), value.c_str());
return caPutJsonLogSuccess;
}

bool CaPutJsonLogTask::isMetadataKey(std::string property)
{
return metadata.count(property) > 0;
}

void CaPutJsonLogTask::removeAllMetadata()
{
metadata.clear();
}

size_t CaPutJsonLogTask::metadataCount()
{
return metadata.size();
}

std::map<std::string, std::string> CaPutJsonLogTask::getMetadata()
{
return metadata;
}

caPutJsonLogStatus CaPutJsonLogTask::initialize(const char* addresslist, caPutJsonLogConfig config)
{
caPutJsonLogStatus status;
Expand Down Expand Up @@ -447,6 +485,17 @@ caPutJsonLogStatus CaPutJsonLogTask::buildJsonMsg(const VALUE *pold_value, const
reinterpret_cast<const unsigned char *>(pLogData->userid),
strlen(pLogData->userid)));

// Add metadata
std::map<std::string, std::string>::iterator meta_it;
for(meta_it = metadata.begin(); meta_it != metadata.end(); meta_it++){
CALL_YAJL_FUNCTION_AND_CHECK_STATUS(status, yajl_gen_string(handle,
reinterpret_cast<const unsigned char *>(meta_it->first.c_str()),
meta_it->first.length()));
CALL_YAJL_FUNCTION_AND_CHECK_STATUS(status, yajl_gen_string(handle,
reinterpret_cast<const unsigned char *>(meta_it->second.c_str()),
meta_it->second.length()));
}

// Add PV name
const unsigned char str_pvName[] = "pv";
CALL_YAJL_FUNCTION_AND_CHECK_STATUS(status, yajl_gen_string(handle, str_pvName,
Expand Down
39 changes: 39 additions & 0 deletions caPutLogApp/caPutJsonLogTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <logClient.h>
#include <epicsMessageQueue.h>
#include <dbAddr.h>
#include <map>
#include <epicsThread.h>

// Includes from this module
Expand Down Expand Up @@ -142,6 +143,41 @@ class epicsShareClass CaPutJsonLogTask {
*/
caPutJsonLogStatus report(int level);

/**
* @brief Add IOC metadata to json output
*
* @param property JSON property
* @param value Value associated with property
*/
caPutJsonLogStatus addMetadata(std::string property, std::string value);

/**
* @brief Check if a property is a key in the metadata map
*
* @param property JSON property
* @return bool Status.
*/
bool isMetadataKey(std::string property);

/**
* @brief Clears the metadata map
*/
void removeAllMetadata();

/**
* @brief Gets the number of elements in the metadata map
*
* @return size_t Number of elements in metadata
*/
size_t metadataCount();

/**
* @brief Gets the metadata list object
*
* @return map<string, string> the Metadata map
*/
std::map<std::string, std::string> getMetadata();

private:

// Singleton instance of this class.
Expand Down Expand Up @@ -172,6 +208,9 @@ class epicsShareClass CaPutJsonLogTask {
// Total count of logged puts
int caPutTotalCount; // To modify or read this value only epicsAtomic methods should be used

// IOC metadata
std::map<std::string, std::string> metadata;

// Class methods (Do not allow public constructors - class is designed as singleton)
CaPutJsonLogTask();
virtual ~CaPutJsonLogTask();
Expand Down
1 change: 1 addition & 0 deletions test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ include $(TOP)/configure/CONFIG
DBDDEPENDS_FILES += dbTestIoc.dbd$(DEP)
TARGETS += $(COMMON_DIR)/dbTestIoc.dbd
dbTestIoc_DBD += base.dbd
CXXFLAGS=-std=c++11

# Libraries to which the test executable is linked
PROD_LIBS = caPutLog
Expand Down
Loading

0 comments on commit ebe7120

Please sign in to comment.