Skip to content

Commit

Permalink
Add dps log component
Browse files Browse the repository at this point in the history
If custom DPS logging function is not configured then use oc log
with the dps log component
  • Loading branch information
Danielius1922 committed Sep 11, 2024
1 parent 5b48fbd commit 9df86e3
Show file tree
Hide file tree
Showing 14 changed files with 76 additions and 230 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ concurrency:

# Controls when the action will run.
on:
# push:
# branches:
# - master
# pull_request:
# types: [opened, synchronize, reopened]
push:
branches:
- master
pull_request:
types: [opened, synchronize, reopened]

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/check-format.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ concurrency:
# Controls when the action will run.
on:
# Triggers the workflow on push or pull request events but only for the master branch
# push:
# branches: [master]
# pull_request:
# branches: [master]
push:
branches: [master]
pull_request:
branches: [master]

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
Expand Down
10 changes: 5 additions & 5 deletions .github/workflows/cmake-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ concurrency:

# Controls when the action will run.
on:
# push:
# branches:
# - master
# pull_request:
# types: [opened, synchronize, reopened]
push:
branches:
- master
pull_request:
types: [opened, synchronize, reopened]

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/ghcr-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ on:
- cloud-server-debug
- cloud-server-discovery-resource-observable
- cloud-server-discovery-resource-observable-debug
- dps-cloud-server
- dps-cloud-server-debug
jobs:
check_package:
name: Check released packages
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/plgd-device-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ concurrency:

on:
# Triggers the workflow on push or pull request events but only for the master branch
# push:
# branches: [master]
# pull_request:
# branches: [master]
push:
branches: [master]
pull_request:
branches: [master]

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/plgd-hub-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ concurrency:

on:
# Triggers the workflow on push or pull request events but only for the master branch
# push:
# branches: [master]
# pull_request:
# branches: [master]
push:
branches: [master]
pull_request:
branches: [master]

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
Expand Down
10 changes: 5 additions & 5 deletions .github/workflows/static-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ concurrency:
cancel-in-progress: ${{ github.ref_name != 'master' }}

on:
# push:
# branches:
# - master
# pull_request:
# types: [opened, synchronize, reopened]
push:
branches:
- master
pull_request:
types: [opened, synchronize, reopened]

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
Expand Down
4 changes: 4 additions & 0 deletions api/oc_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ oc_log_component_name(oc_log_component_t component)
#endif /* OC_CLOUD */
case OC_LOG_COMPONENT_COAP:
return "coap";
#ifdef OC_HAS_FEATURE_PLGD_DEVICE_PROVISIONING
case OC_LOG_COMPONENT_DEVICE_PROVISIONING:
return "dps";
#endif
}
return "";
}
Expand Down
48 changes: 0 additions & 48 deletions api/plgd/device-provisioning-client/plgd_dps_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,41 +36,8 @@ static struct
} g_dps_logger = {
.fn = NULL,
.level = OC_LOG_LEVEL_INFO,
.components = PLGD_DPS_LOG_COMPONENT_DEFAULT,
};

const char *
dps_log_get_component_label(plgd_dps_log_component_t component)
{
if (component == PLGD_DPS_LOG_COMPONENT_DEFAULT) {
return "default";
}
return "";
}

static void dps_log_default_fn(oc_log_level_t level,
plgd_dps_log_component_t component,
const char *file, int line,
const char *func_name, const char *format, ...)
OC_PRINTF_FORMAT(6, 7);
static void
dps_log_default_fn(oc_log_level_t level, plgd_dps_log_component_t component,
const char *file, int line, const char *func_name,
const char *format, ...)
{
char dps_log_fn_buf[64] = { 0 }; // NOLINT(readability-magic-numbers)
oc_clock_time_rfc3339(dps_log_fn_buf, sizeof(dps_log_fn_buf));

printf("[DPS:%s %s] %s: %s <%s:%d>: ", dps_log_get_component_label(component),
dps_log_fn_buf, oc_log_level_to_label(level), file, func_name, line);
va_list args;
va_start(args, format);
vprintf(format, args);
va_end(args);
printf("\n");
fflush(stdout);
}

void
plgd_dps_set_log_fn(plgd_dps_print_log_fn_t log_fn)
{
Expand All @@ -80,9 +47,6 @@ plgd_dps_set_log_fn(plgd_dps_print_log_fn_t log_fn)
plgd_dps_print_log_fn_t
plgd_dps_get_log_fn(void)
{
if (g_dps_logger.fn == NULL) {
return &dps_log_default_fn;
}
return g_dps_logger.fn;
}

Expand All @@ -99,15 +63,3 @@ plgd_dps_log_get_level(void)
{
return OC_ATOMIC_LOAD8(g_dps_logger.level);
}

void
plgd_dps_log_set_components(uint32_t components)
{
OC_ATOMIC_STORE32(g_dps_logger.components, components);
}

uint32_t
plgd_dps_log_get_components(void)
{
return OC_ATOMIC_LOAD32(g_dps_logger.components);
}
78 changes: 18 additions & 60 deletions api/plgd/device-provisioning-client/plgd_dps_log_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,130 +16,88 @@
*
****************************************************************************/

// TODO: merge with oc_log?

#ifndef PLGD_DPS_LOG_INTERNAL_H
#define PLGD_DPS_LOG_INTERNAL_H

#include "plgd/plgd_dps.h"
#include "port/oc_log_internal.h"
#include "util/oc_compiler.h"

#include "string.h"
#include "util/oc_compiler.h"

#ifdef __cplusplus
extern "C" {
#endif

#ifndef __FILENAME__
#ifdef WIN32
// NOLINTNEXTLINE(bugprone-reserved-identifier)
#define __FILENAME__ \
(strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__)
#else
// NOLINTNEXTLINE(bugprone-reserved-identifier)
#define __FILENAME__ \
(strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
#endif
#endif /* !__FILENAME__ */

#ifndef PLGD_DPS_LOG_MAXIMUM_LEVEL
#define PLGD_DPS_LOG_MAXIMUM_LEVEL (OC_LOG_LEVEL_DISABLED_MACRO)
#endif

#define PLGD_DPS_LOG_LEVEL_IS_ENABLED(level) \
((level) <= (PLGD_DPS_LOG_MAXIMUM_LEVEL))

#ifdef __cplusplus
#define DPS_LOG_CAST
#else
#define DPS_LOG_CAST (plgd_dps_log_component_t)
#endif

#define DPS_LOG(log_level, log_component, ...) \
#define DPS_LOG(log_level, ...) \
do { \
if (plgd_dps_log_get_level() >= (log_level) && \
(plgd_dps_log_get_components() & log_component) != 0) { \
plgd_dps_get_log_fn()((log_level), DPS_LOG_CAST(log_component), \
__FILENAME__, __LINE__, __func__, __VA_ARGS__); \
if (plgd_dps_log_get_level() >= (log_level)) { \
plgd_dps_print_log_fn_t _dps_logger_fn = plgd_dps_get_log_fn(); \
if (_dps_logger_fn != NULL) { \
_dps_logger_fn((log_level), __FILENAME__, __LINE__, __func__, \
__VA_ARGS__); \
break; \
} \
OC_LOG_WITH_COMPONENT(log_level, OC_LOG_COMPONENT_DEVICE_PROVISIONING, \
__VA_ARGS__); \
} \
} while (0)

#define DPS_TRACE_IS_ENABLED \
PLGD_DPS_LOG_LEVEL_IS_ENABLED(OC_LOG_LEVEL_TRACE_MACRO)
#if DPS_TRACE_IS_ENABLED
#define DPS_TRACE_COMPONENT(component, ...) \
DPS_LOG(OC_LOG_LEVEL_TRACE, (component), __VA_ARGS__)
#define DPS_TRACE(...) \
DPS_TRACE_COMPONENT(PLGD_DPS_LOG_COMPONENT_DEFAULT, __VA_ARGS__)
#define DPS_TRACE(...) DPS_LOG(OC_LOG_LEVEL_TRACE, __VA_ARGS__)
#else /* !DPS_TRACE_IS_ENABLED */
#define DPS_TRACE(...)
#define DPS_TRACE_COMPONENT(component, ...)
#endif /* DPS_TRACE_IS_ENABLED */

#define DPS_DBG_IS_ENABLED \
PLGD_DPS_LOG_LEVEL_IS_ENABLED(OC_LOG_LEVEL_DEBUG_MACRO)
#if DPS_DBG_IS_ENABLED
#define DPS_DBG_COMPONENT(component, ...) \
DPS_LOG(OC_LOG_LEVEL_DEBUG, (component), __VA_ARGS__)
#define DPS_DBG(...) \
DPS_DBG_COMPONENT(PLGD_DPS_LOG_COMPONENT_DEFAULT, __VA_ARGS__)
#define DPS_DBG(...) DPS_LOG(OC_LOG_LEVEL_DEBUG, __VA_ARGS__)
#else /* !DPS_DBG_IS_ENABLED */
#define DPS_DBG_COMPONENT(component, ...)
#define DPS_DBG(...)
#endif /* DPS_DBG_IS_ENABLED */

#define DPS_INFO_IS_ENABLED \
PLGD_DPS_LOG_LEVEL_IS_ENABLED(OC_LOG_LEVEL_INFO_MACRO)
#if DPS_INFO_IS_ENABLED
#define DPS_INFO_COMPONENT(component, ...) \
DPS_LOG(OC_LOG_LEVEL_INFO, (component), __VA_ARGS__)
#define DPS_INFO(...) \
DPS_INFO_COMPONENT(PLGD_DPS_LOG_COMPONENT_DEFAULT, __VA_ARGS__)
#define DPS_INFO(...) DPS_LOG(OC_LOG_LEVEL_INFO, __VA_ARGS__)
#else /* !DPS_INFO_IS_ENABLED */
#define DPS_INFO_COMPONENT(component, ...)
#define DPS_INFO(...)
#endif /* DPS_INFO_IS_ENABLED */

#define DPS_NOTE_IS_ENABLED \
PLGD_DPS_LOG_LEVEL_IS_ENABLED(OC_LOG_LEVEL_NOTICE_MACRO)
#if DPS_NOTE_IS_ENABLED
#define DPS_NOTE_COMPONENT(component, ...) \
DPS_LOG(OC_LOG_LEVEL_NOTICE, (component), __VA_ARGS__)
#define DPS_NOTE(...) \
DPS_NOTE_COMPONENT(PLGD_DPS_LOG_COMPONENT_DEFAULT, __VA_ARGS__)
#define DPS_NOTE(...) DPS_LOG(OC_LOG_LEVEL_NOTICE, __VA_ARGS__)
#else /* !DPS_NOTE_IS_ENABLED */
#define DPS_NOTE_COMPONENT(component, ...)
#define DPS_NOTE(...)
#endif /* DPS_NOTE_IS_ENABLED */

#define DPS_WRN_IS_ENABLED \
PLGD_DPS_LOG_LEVEL_IS_ENABLED(OC_LOG_LEVEL_WARNING_MACRO)
#if DPS_WRN_IS_ENABLED
#define DPS_WRN_COMPONENT(component, ...) \
DPS_LOG(OC_LOG_LEVEL_WARNING, (component), __VA_ARGS__)
#define DPS_WRN(...) \
DPS_WRN_COMPONENT(PLGD_DPS_LOG_COMPONENT_DEFAULT, __VA_ARGS__)
#define DPS_WRN(...) DPS_LOG(OC_LOG_LEVEL_WARNING, __VA_ARGS__)
#else /* !DPS_WRN_IS_ENABLED */
#define DPS_WRN_COMPONENT(component, ...)
#define DPS_WRN(...)
#endif /* DPS_WRN_IS_ENABLED */

#define DPS_ERR_IS_ENABLED \
PLGD_DPS_LOG_LEVEL_IS_ENABLED(OC_LOG_LEVEL_ERROR_MACRO)
#if DPS_ERR_IS_ENABLED
#define DPS_ERR_COMPONENT(component, ...) \
DPS_LOG(OC_LOG_LEVEL_ERROR, (component), __VA_ARGS__)
#define DPS_ERR(...) \
DPS_ERR_COMPONENT(PLGD_DPS_LOG_COMPONENT_DEFAULT, __VA_ARGS__)
#define DPS_ERR(...) DPS_LOG(OC_LOG_LEVEL_ERROR, __VA_ARGS__)
#else /* !DPS_ERR_IS_ENABLED */
#define DPS_ERR_COMPONENT(component, ...)
#define DPS_ERR(...)
#endif /* DPS_ERR_IS_ENABLED */

/** * @brief Get component label as a C-string. */
const char *dps_log_get_component_label(plgd_dps_log_component_t component)
OC_RETURNS_NONNULL;

#ifdef __cplusplus
}
#endif
Expand Down
Loading

0 comments on commit 9df86e3

Please sign in to comment.