Skip to content

Commit 25b50aa

Browse files
committed
JsonLogTask: fix value comparisons
- fix string comparison (was comparing pointers) (fixes #43) - improve value comparison code - REF improve name of compare function
1 parent e066277 commit 25b50aa

File tree

2 files changed

+13
-37
lines changed

2 files changed

+13
-37
lines changed

caPutLogApp/caPutJsonLogTask.cpp

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ caPutJsonLogStatus CaPutJsonLogTask::buildJsonMsg(const VALUE *pold_value, const
458458

459459
// Dont log duplicate values if configured so
460460
if (this->config == caPutJsonLogOnChange && !burst) {
461-
if (this->compareValues(pLogData))
461+
if (this->valuesAreEqual(pLogData))
462462
return caPutJsonLogSuccess;
463463
}
464464

@@ -794,46 +794,22 @@ void CaPutJsonLogTask::calculateMax(VALUE *pres, const VALUE *pa, const VALUE *p
794794
}
795795
}
796796

797-
#define SINGLE_TYPE_COMPARE(_t, _s) \
798-
if (pLogData->is_array) \
799-
return memcmp(pa->a_##_t, pb->a_##_t, size * _s) == 0; \
800-
return pa->v_##_t == pb->v_##_t;
801-
802-
bool CaPutJsonLogTask::compareValues(const LOGDATA *pLogData) {
797+
bool CaPutJsonLogTask::valuesAreEqual(const LOGDATA *pLogData) {
803798
const VALUE *pa = &pLogData->old_value;
804799
const VALUE *pb = &pLogData->new_value.value;
805-
const int size = pLogData->old_log_size;
800+
size_t size = pLogData->is_array ? pLogData->old_log_size : 1;
806801

807-
if (pLogData->is_array && pLogData->old_log_size != pLogData->new_log_size)
802+
if (pLogData->old_log_size != pLogData->new_log_size)
808803
return false;
809804

810-
switch (pLogData->type)
811-
{
812-
case DBR_CHAR:
813-
SINGLE_TYPE_COMPARE(int8, sizeof(epicsInt8));
814-
case DBR_UCHAR:
815-
SINGLE_TYPE_COMPARE(uint8, sizeof(epicsUInt8));
816-
case DBR_SHORT:
817-
SINGLE_TYPE_COMPARE(int16, sizeof(epicsInt16));
818-
case DBR_USHORT:
819-
case DBR_ENUM:
820-
SINGLE_TYPE_COMPARE(uint16, sizeof(epicsUInt16));
821-
case DBR_LONG:
822-
SINGLE_TYPE_COMPARE(int32, sizeof(epicsInt32));
823-
case DBR_ULONG:
824-
SINGLE_TYPE_COMPARE(uint32, sizeof(epicsUInt32));
825-
case DBR_INT64:
826-
SINGLE_TYPE_COMPARE(int64, sizeof(epicsInt64));
827-
case DBR_UINT64:
828-
SINGLE_TYPE_COMPARE(uint64, sizeof(epicsUInt64));
829-
case DBR_FLOAT:
830-
SINGLE_TYPE_COMPARE(float, sizeof(epicsFloat32));
831-
case DBR_DOUBLE:
832-
SINGLE_TYPE_COMPARE(double, sizeof(epicsFloat64));
833-
case DBR_STRING:
834-
SINGLE_TYPE_COMPARE(string, MAX_STRING_SIZE);
835-
default:
836-
return 0;
805+
if (pLogData->type == DBR_STRING) {
806+
for (size_t i = 0; i < size; i++) {
807+
if (strncmp(pa->a_string[i], pb->a_string[i], MAX_STRING_SIZE) != 0)
808+
return false;
809+
}
810+
return true;
811+
} else {
812+
return memcmp(pa, pb, size*dbValueSize(pLogData->type)) == 0;
837813
}
838814
}
839815

caPutLogApp/caPutJsonLogTask.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ class epicsShareClass CaPutJsonLogTask {
300300
* @return true If values are the same.
301301
* @return false If values are not the same.
302302
*/
303-
bool compareValues(const LOGDATA *pLogData);
303+
bool valuesAreEqual(const LOGDATA *pLogData);
304304

305305
/**
306306
* @brief Get a string representation of the value stored in the ::VALUE.

0 commit comments

Comments
 (0)