IoTivity-Lite
oc_log.h File Reference

OCF public log functions. More...

#include "oc_config.h"
#include "oc_export.h"
#include "util/oc_compiler.h"
#include "util/oc_features.h"
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <stdio.h>

Macros

#define OC_IPADDR_BUFF_SIZE    (64)
 
#define OC_LOG_LEVEL_DEBUG_MACRO   (7)
 
#define OC_LOG_LEVEL_DISABLED_MACRO   (-1)
 
#define OC_LOG_LEVEL_ERROR_MACRO   (3)
 
#define OC_LOG_LEVEL_INFO_MACRO   (6)
 
#define OC_LOG_LEVEL_IS_ENABLED(level)   (0)
 
#define OC_LOG_LEVEL_NOTICE_MACRO   (5)
 
#define OC_LOG_LEVEL_TRACE_MACRO   (8)
 
#define OC_LOG_LEVEL_WARNING_MACRO   (4)
 
#define OC_PRINT_ENDPOINT_ADDR(endpoint, addr_memb)
 
#define OC_PRINTF(...)   printf(__VA_ARGS__)
 
#define OC_PRINTipaddr(endpoint)   OC_PRINT_ENDPOINT_ADDR(endpoint, addr)
 
#define OC_PRINTipaddr_local(endpoint)    OC_PRINT_ENDPOINT_ADDR(endpoint, addr_local)
 
#define OC_SNPRINT_ENDPOINT_ADDR(str, size, endpoint, addr_memb)
 
#define OC_SNPRINTF(...)   snprintf(__VA_ARGS__)
 
#define OC_SNPRINTFipaddr(str, size, endpoint)    OC_SNPRINT_ENDPOINT_ADDR(str, size, endpoint, addr)
 

Typedefs

typedef void(* oc_print_log_fn_t) (oc_log_level_t level, oc_log_component_t component, const char *file, int line, const char *func_name, const char *format,...)
 Custom logging function. More...
 

Enumerations

enum  oc_log_component_t { OC_LOG_COMPONENT_DEFAULT = 1 << 0 , OC_LOG_COMPONENT_CLOUD = 1 << 1 , OC_LOG_COMPONENT_COAP = 1 << 2 , OC_LOG_COMPONENT_DEVICE_PROVISIONING = 1 << 3 }
 Log component determines the source of the message. More...
 
enum  oc_log_level_t {
  OC_LOG_LEVEL_DISABLED = OC_LOG_LEVEL_DISABLED_MACRO , OC_LOG_LEVEL_ERROR = OC_LOG_LEVEL_ERROR_MACRO , OC_LOG_LEVEL_WARNING = OC_LOG_LEVEL_WARNING_MACRO , OC_LOG_LEVEL_NOTICE ,
  OC_LOG_LEVEL_INFO = OC_LOG_LEVEL_INFO_MACRO , OC_LOG_LEVEL_DEBUG = OC_LOG_LEVEL_DEBUG_MACRO , OC_LOG_LEVEL_TRACE = OC_LOG_LEVEL_TRACE_MACRO
}
 Log level determines the importance of the message. More...
 

Functions

const char * oc_log_component_name (oc_log_component_t component) 1
 Get component name. More...
 
oc_log_level_t oc_log_get_level (void)
 Get log level of the global logger. More...
 
const char * oc_log_level_to_label (oc_log_level_t level) 1
 Convert log level to string. More...
 
void oc_log_set_function (oc_print_log_fn_t log_func)
 Set log callback function. More...
 
void oc_log_set_level (oc_log_level_t level)
 Set log level of the global logger, logs with lower importance will be ignored. More...
 

Detailed Description

OCF public log functions.

Allow to set log callback function and level.

Author
Jozef Kralik, Daniel Adam

Macro Definition Documentation

◆ OC_PRINT_ENDPOINT_ADDR

#define OC_PRINT_ENDPOINT_ADDR (   endpoint,
  addr_memb 
)
Value:
do { \
char _oc_log_ipaddr_buff[OC_IPADDR_BUFF_SIZE]; \
memset(_oc_log_ipaddr_buff, 0, OC_IPADDR_BUFF_SIZE); \
OC_SNPRINT_ENDPOINT_ADDR(_oc_log_ipaddr_buff, OC_IPADDR_BUFF_SIZE, \
endpoint, addr_memb); \
OC_PRINTF("%s", _oc_log_ipaddr_buff); \
} while (0)

Typedef Documentation

◆ oc_print_log_fn_t

typedef void(* oc_print_log_fn_t) (oc_log_level_t level, oc_log_component_t component, const char *file, int line, const char *func_name, const char *format,...)

Custom logging function.

Parameters
levellog level of the message
componentlog component of the message
filefile of the log message call
lineline of the log message call in file
func_namefunction name in which the log message call is invoked
formatformat of the log message

Enumeration Type Documentation

◆ oc_log_component_t

Log component determines the source of the message.

The components are defined as bit flags.

Enumerator
OC_LOG_COMPONENT_DEFAULT 

default, non-specific component

OC_LOG_COMPONENT_CLOUD 

cloud

OC_LOG_COMPONENT_COAP 

coap

OC_LOG_COMPONENT_DEVICE_PROVISIONING 

device provisioning

◆ oc_log_level_t

Log level determines the importance of the message.

The levels are in order of decreasing importance.

Enumerator
OC_LOG_LEVEL_DISABLED 

disable logging

OC_LOG_LEVEL_ERROR 

error conditions

OC_LOG_LEVEL_WARNING 

warning conditions

OC_LOG_LEVEL_NOTICE 

normal, but significant condition

OC_LOG_LEVEL_INFO 

informational message

OC_LOG_LEVEL_DEBUG 

debug level message

OC_LOG_LEVEL_TRACE 

trace level message

Function Documentation

◆ oc_log_component_name()

const char* oc_log_component_name ( oc_log_component_t  component)

Get component name.

It is thread safe.

Parameters
componentComponent type
Returns
const char* Name of a component.
Empty string for an invalid IoTivity-lite component type.

◆ oc_log_get_level()

oc_log_level_t oc_log_get_level ( void  )

Get log level of the global logger.

It is thread safe.

Returns
Log level

◆ oc_log_level_to_label()

const char* oc_log_level_to_label ( oc_log_level_t  level)

Convert log level to string.

It is thread safe.

Returns
Log level in const char *.
Empty string for an invalid log level value

◆ oc_log_set_function()

void oc_log_set_function ( oc_print_log_fn_t  log_func)

Set log callback function.

It is recommended to set it before oc_main_init because it is not thread safe.

Parameters
log_funcLog callback function
Note
If log_func is NULL, the default log function will be used which prints to a message in format "[OC <rfc3339 time>] <label>: <filename>:<line> <message>" to stdout.

◆ oc_log_set_level()

void oc_log_set_level ( oc_log_level_t  level)

Set log level of the global logger, logs with lower importance will be ignored.

It is thread safe.

Parameters
levelLog level
Note
If log level is not set, the default log level is OC_LOG_LEVEL_INFO.