Skip to content

Commit

Permalink
libopsdp: Update to new logger API
Browse files Browse the repository at this point in the history
utils/logger.h was providing some really messy API for the logger. This
patch integrates the new reworked API into libosdp. This is an
experimental change with a lot of potential change/enhancements in
future.

Signed-off-by: Siddharth Chandrasekaran <[email protected]>
  • Loading branch information
sidcha committed Sep 3, 2023
1 parent 48d6b3a commit c9f3f98
Show file tree
Hide file tree
Showing 21 changed files with 57 additions and 49 deletions.
2 changes: 1 addition & 1 deletion doc/libosdp/debugging.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ as,

.. code:: c
osdp_logger_init(LOG_DEBUG, uart_puts);
osdp_logger_init("osdp::cp", LOG_DEBUG, uart_puts);
Packet Trace Builds
-------------------
Expand Down
4 changes: 3 additions & 1 deletion include/osdp.h
Original file line number Diff line number Diff line change
Expand Up @@ -967,6 +967,7 @@ typedef int (*osdp_log_puts_fn_t)(const char *msg);
/**
* @brief Configure OSDP Logging.
*
* @param name A soft name for this module; will appear in all the log lines.
* @param log_level OSDP log levels of type `enum osdp_log_level_e`. Default is
* LOG_INFO.
* @param puts_fn A puts() like function that will be invoked to write the log
Expand All @@ -975,7 +976,8 @@ typedef int (*osdp_log_puts_fn_t)(const char *msg);
* definition to see the behavioral expectations. When this is
* set to NULL, LibOSDP will log to stderr.
*/
void osdp_logger_init(int log_level, osdp_log_puts_fn_t puts_fn);
void osdp_logger_init(const char *name, int log_level,
osdp_log_puts_fn_t puts_fn);

/**
* @brief Get LibOSDP version as a `const char *`. Used in diagnostics.
Expand Down
4 changes: 2 additions & 2 deletions include/osdp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ class Common {
public:
Common() {}

void logger_init(int log_level, osdp_log_puts_fn_t puts_fn)
void logger_init(const char *name, int log_level, osdp_log_puts_fn_t puts_fn)
{
osdp_logger_init(log_level, puts_fn);
osdp_logger_init(name, log_level, puts_fn);
}

const char *get_version()
Expand Down
2 changes: 1 addition & 1 deletion osdpctl/cmd_start.c
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ int cmd_handler_start(int argc, char *argv[], void *data)
info->scbk = NULL;
}

osdp_logger_init(c->log_level, NULL);
osdp_logger_init("osdp::cp", c->log_level, NULL);

if (c->mode == CONFIG_MODE_CP) {
c->cp_ctx = osdp_cp_setup2(c->num_pd, info_arr);
Expand Down
4 changes: 2 additions & 2 deletions python/pyosdp_base.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ static PyObject *pyosdp_set_loglevel(pyosdp_base_t *self, PyObject *args)
return NULL;
}

osdp_logger_init(log_level, NULL);
osdp_logger_init("pyosdp", log_level, NULL);

Py_RETURN_NONE;
}
Expand Down Expand Up @@ -263,7 +263,7 @@ static int pyosdp_base_tp_init(pyosdp_base_t *self, PyObject *args, PyObject *kw

channel_manager_init(&self->channel_manager);

osdp_logger_init(OSDP_LOG_INFO, NULL);
osdp_logger_init("pyosdp", OSDP_LOG_INFO, NULL);

return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion samples/c/cp_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ int main()
{
osdp_t *ctx;

osdp_logger_init(OSDP_LOG_DEBUG, NULL);
osdp_logger_init("osdp::cp", OSDP_LOG_DEBUG, NULL);

ctx = osdp_cp_setup2(1, pd_info);
if (ctx == NULL) {
Expand Down
2 changes: 1 addition & 1 deletion samples/c/pd_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ int main()
{
osdp_t *ctx;

osdp_logger_init(OSDP_LOG_DEBUG, NULL);
osdp_logger_init("osdp::pd", OSDP_LOG_DEBUG, NULL);

ctx = osdp_pd_setup(&info_pd);
if (ctx == NULL) {
Expand Down
2 changes: 1 addition & 1 deletion samples/cpp/cp_app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ int main()
{
OSDP::ControlPanel cp;

cp.logger_init(OSDP_LOG_DEBUG, NULL);
cp.logger_init("osdp::cp", OSDP_LOG_DEBUG, NULL);

cp.setup(1, pd_info);

Expand Down
2 changes: 1 addition & 1 deletion samples/cpp/pd_app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ int main()
{
OSDP::PeripheralDevice pd;

pd.logger_init(OSDP_LOG_DEBUG, NULL);
pd.logger_init("osdp::pd", OSDP_LOG_DEBUG, NULL);

pd.setup(&info_pd);

Expand Down
29 changes: 13 additions & 16 deletions src/osdp_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@

#include "osdp_common.h"

#ifdef CONFIG_DISABLE_PRETTY_LOGGING
LOGGER_DEFINE(osdp, LOG_INFO, LOGGER_FLAG_NO_COLORS);
#else
LOGGER_DEFINE(osdp, LOG_INFO, LOGGER_FLAG_NONE);
#endif

uint16_t crc16_itu_t(uint16_t seed, const uint8_t *src, size_t len)
{
for (; len > 0; len--) {
Expand Down Expand Up @@ -198,18 +192,21 @@ int osdp_rb_pop_buf(struct osdp_rb *p, uint8_t *buf, int max_len)
/* --- Exported Methods --- */

OSDP_EXPORT
void osdp_logger_init(int log_level, osdp_log_puts_fn_t log_fn)
void osdp_logger_init(const char *name, int log_level,
osdp_log_puts_fn_t log_fn)
{
logger_t *ctx = get_log_ctx(osdp);
logger_t ctx;
FILE *file = NULL;
int flags = LOGGER_FLAG_NONE;

logger_set_log_level(ctx, log_level);
if (log_fn) {
logger_set_put_fn(ctx, log_fn);
logger_set_file(ctx, NULL);
} else {
logger_set_put_fn(ctx, NULL);
logger_set_file(ctx, stderr);
}
#ifdef CONFIG_DISABLE_PRETTY_LOGGING
flags |= LOGGER_FLAG_NO_COLORS;
#endif
if (!log_fn)
file = stderr;

logger_init(&ctx, log_level, name, REPO_ROOT, log_fn, file, flags);
logger_set_default(&ctx); /* Mark this config as logging default */
}

OSDP_EXPORT
Expand Down
20 changes: 15 additions & 5 deletions src/osdp_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
#include <utils/utils.h>
#include <utils/queue.h>
#include <utils/slab.h>
#include <utils/logger.h>
#include <utils/assert.h>

#define USE_CUSTOM_LOGGER
#include <utils/logger.h>

#include <osdp.h>
#include "osdp_config.h" /* generated */
#include "osdp_export.h" /* generated */
Expand All @@ -31,6 +33,15 @@

#define ARG_UNUSED(x) (void)(x)

#define LOG_EM(...) __logger_log(&pd->logger, LOG_EMERG, __FILE__, __LINE__, __VA_ARGS__)
#define LOG_ALERT(...) __logger_log(&pd->logger, LOG_ALERT, __FILE__, __LINE__, __VA_ARGS__)
#define LOG_CRIT(...) __logger_log(&pd->logger, LOG_CRIT, __FILE__, __LINE__, __VA_ARGS__)
#define LOG_ERR(...) __logger_log(&pd->logger, LOG_ERR, __FILE__, __LINE__, __VA_ARGS__)
#define LOG_INF(...) __logger_log(&pd->logger, LOG_INFO, __FILE__, __LINE__, __VA_ARGS__)
#define LOG_WRN(...) __logger_log(&pd->logger, LOG_WARNING,__FILE__, __LINE__, __VA_ARGS__)
#define LOG_NOT(...) __logger_log(&pd->logger, LOG_NOTICE, __FILE__, __LINE__, __VA_ARGS__)
#define LOG_DBG(...) __logger_log(&pd->logger, LOG_DEBUG, __FILE__, __LINE__, __VA_ARGS__)

#define ISSET_FLAG(p, f) (((p)->flags & (f)) == (f))
#define SET_FLAG(p, f) ((p)->flags |= (f))
#define CLEAR_FLAG(p, f) ((p)->flags &= ~(f))
Expand Down Expand Up @@ -84,7 +95,7 @@ union osdp_ephemeral_data {
assert(TO_OSDP(ctx)->_magic == OSDP_CTX_MAGIC);
#define input_check_pd_offset(ctx, pd) \
if (pd < 0 || pd >= NUM_PD(ctx)) { \
LOG_ERR("Invalid PD number %d", pd); \
LOG_PRINT("Invalid PD number %d", pd); \
return -1; \
}
#define input_check2(_1, _2) \
Expand Down Expand Up @@ -326,6 +337,8 @@ struct osdp_pd {
/* PD command callback to app with opaque arg pointer as passed by app */
void *command_callback_arg;
pd_command_callback_t command_callback;

logger_t logger; /* logger context (from utils/logger.h) */
};

struct osdp {
Expand Down Expand Up @@ -371,9 +384,6 @@ int osdp_phy_send_packet(struct osdp_pd *pd, uint8_t *buf,
__weak int64_t osdp_millis_now(void);
int64_t osdp_millis_since(int64_t last);
uint16_t osdp_compute_crc16(const uint8_t *buf, size_t len);
void osdp_log(int log_level, const char *fmt, ...);
void osdp_log_ctx_reset();
void osdp_log_ctx_restore();

const char *osdp_cmd_name(int cmd_id);
const char *osdp_reply_name(int reply_id);
Expand Down
1 change: 1 addition & 0 deletions src/osdp_config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#define GIT_REV "@GIT_REV@"
#define GIT_TAG "@GIT_TAG@"
#define GIT_DIFF "@GIT_DIFF@"
#define REPO_ROOT "@REPO_ROOT@"

/**
* @brief Other OSDP constants
Expand Down
8 changes: 5 additions & 3 deletions src/osdp_cp.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
#include "osdp_common.h"
#include "osdp_file.h"

LOGGER_DECLARE(osdp, "CP");

#define CMD_POLL_LEN 1
#define CMD_LSTAT_LEN 1
#define CMD_ISTAT_LEN 1
Expand Down Expand Up @@ -1227,6 +1225,7 @@ static struct osdp *__cp_setup(int num_pd, osdp_pd_info_t *info_list,
struct osdp_pd *pd = NULL;
struct osdp *ctx;
osdp_pd_info_t *info;
char name[24] = {0};

ctx = calloc(1, sizeof(struct osdp));
if (ctx == NULL) {
Expand Down Expand Up @@ -1271,6 +1270,10 @@ static struct osdp *__cp_setup(int num_pd, osdp_pd_info_t *info_list,
if (IS_ENABLED(CONFIG_OSDP_SKIP_MARK_BYTE)) {
SET_FLAG(pd, PD_FLAG_PKT_SKIP_MARK);
}

logger_get_default(&pd->logger);
snprintf(name, sizeof(name), "OSDP: CP: PD-%d", pd->address);
logger_set_name(&pd->logger, name);
}

if (scbk_count != num_pd) {
Expand Down Expand Up @@ -1342,7 +1345,6 @@ void osdp_cp_refresh(osdp_t *ctx)

do {
pd = GET_CURRENT_PD(ctx);
LOG_SET_PREFIX(pd->name);

if (cp_refresh(pd) < 0)
break;
Expand Down
2 changes: 0 additions & 2 deletions src/osdp_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

#include "osdp_file.h"

LOGGER_DECLARE(osdp, "FOP");

static inline void file_state_reset(struct osdp_file *f)
{
f->flags = 0;
Expand Down
10 changes: 5 additions & 5 deletions src/osdp_pd.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
#include <stdlib.h>
#endif

LOGGER_DECLARE(osdp, "PD");

#define CMD_POLL_DATA_LEN 0
#define CMD_LSTAT_DATA_LEN 0
#define CMD_ISTAT_DATA_LEN 0
Expand Down Expand Up @@ -1059,11 +1057,10 @@ osdp_t *osdp_pd_setup(osdp_pd_info_t *info)
{
struct osdp_pd *pd;
struct osdp *ctx;
char name[16] = {0};

assert(info);

LOG_SET_PREFIX(info->name);

#ifndef CONFIG_OSDP_STATIC_PD
ctx = calloc(1, sizeof(struct osdp));
if (ctx == NULL) {
Expand Down Expand Up @@ -1099,6 +1096,10 @@ osdp_t *osdp_pd_setup(osdp_pd_info_t *info)
pd->seq_number = -1;
memcpy(&pd->channel, &info->channel, sizeof(struct osdp_channel));

logger_get_default(&pd->logger);
snprintf(name, sizeof(name), "OSDP: PD-%d", pd->address);
logger_set_name(&pd->logger, name);

if (pd_event_queue_init(pd)) {
goto error;
}
Expand Down Expand Up @@ -1148,7 +1149,6 @@ void osdp_pd_refresh(osdp_t *ctx)
input_check(ctx);
struct osdp_pd *pd = GET_CURRENT_PD(ctx);

LOG_SET_PREFIX(pd->name);
osdp_pd_update(pd);
}

Expand Down
2 changes: 0 additions & 2 deletions src/osdp_phy.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

#include "osdp_common.h"

LOGGER_DECLARE(osdp, "PHY");

#define OSDP_PKT_MARK 0xFF
#define OSDP_PKT_SOM 0x53
#define PKT_CONTROL_SQN 0x03
Expand Down
2 changes: 1 addition & 1 deletion tests/unit-tests/test-cp-fsm.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ int test_cp_fsm_setup(struct test *t)
.channel.flush = NULL,
.scbk = NULL,
};
osdp_logger_init(t->loglevel, NULL);
osdp_logger_init("osdp::cp", t->loglevel, NULL);
struct osdp *ctx = (struct osdp *)osdp_cp_setup2(1, &info);
if (ctx == NULL) {
printf(" init failed!\n");
Expand Down
2 changes: 1 addition & 1 deletion tests/unit-tests/test-cp-phy-fsm.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ int test_cp_phy_fsm_setup(struct test *t)
.channel.flush = NULL,
.scbk = NULL,
};
osdp_logger_init(t->loglevel, NULL);
osdp_logger_init("osdp::cp", t->loglevel, NULL);
struct osdp *ctx = (struct osdp *)osdp_cp_setup2(1, &info);
if (ctx == NULL) {
printf(" init failed!\n");
Expand Down
2 changes: 1 addition & 1 deletion tests/unit-tests/test-cp-phy.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ int test_cp_phy_setup(struct test *t)
.channel.flush = NULL,
.scbk = NULL,
};
osdp_logger_init(t->loglevel, NULL);
osdp_logger_init("osdp::cp", t->loglevel, NULL);
struct osdp *ctx = (struct osdp *)osdp_cp_setup2(1, &info);
if (ctx == NULL) {
printf(SUB_1 "init failed!\n");
Expand Down
2 changes: 1 addition & 1 deletion tests/unit-tests/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ int test_mock_pd_receive(void *data, uint8_t *buf, int len)

int test_setup_devices(struct test *t, osdp_t **cp, osdp_t **pd)
{
osdp_logger_init(t->loglevel, NULL);
osdp_logger_init("osdp", t->loglevel, NULL);

uint8_t scbk[16] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
Expand Down

2 comments on commit c9f3f98

@rm5248
Copy link

@rm5248 rm5248 commented on c9f3f98 Oct 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This breaks the API and ABI of libosdp because the signature of osdp_logger_init changes. I would expect that to be a bump in the major version of the library.

personal preference: don't try to format the entire log message before calling the log function. In C++ people may be using a logging framework like log4cxx or spdlog which already have log levels, line numbers, date/time formatting. So from libosdp we get a message like:

osdp: [2023-10-09T13:51:23] [INFO ] src/osdp_pd.c:1126: Setup complete - libosdp-2.3.0 osdp_pd_status (dee622a)

which could then be formatted by a logging framework to something like:

[2023-10-09T13:51:23] [INFO] libosdp - osdp: [2023-10-09T13:51:23] [INFO ] src/osdp_pd.c:1126: Setup complete - libosdp-2.3.0 osdp_pd_status (dee622a)

I have a simple logger that can be used as a starting point/inspiration: https://github.com/rm5248/simplelogger

@sidcha
Copy link
Member Author

@sidcha sidcha commented on c9f3f98 Oct 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created an issue (#139) out of this. Thanks for reporting it.

Please sign in to comment.