mirror of
https://gitlab.freedesktop.org/NetworkManager/NetworkManager.git
synced 2026-01-04 11:00:18 +01:00
n-dhcp4: add logging API
In some cases it is useful to have the library log what it is doing for debugging purposes; add a simple API that allows setting a syslog-style logging level and specifying a logging function. https://github.com/nettools/n-dhcp4/pull/8
This commit is contained in:
parent
d99c91a05a
commit
87a26ea594
3 changed files with 37 additions and 0 deletions
|
|
@ -94,6 +94,9 @@ int n_dhcp4_client_config_dup(NDhcp4ClientConfig *config, NDhcp4ClientConfig **d
|
|||
dup->n_mac = config->n_mac;
|
||||
memcpy(dup->broadcast_mac, config->broadcast_mac, sizeof(dup->broadcast_mac));
|
||||
dup->n_broadcast_mac = config->n_broadcast_mac;
|
||||
dup->log.level = config->log.level;
|
||||
dup->log.func = config->log.func;
|
||||
dup->log.data = config->log.data;
|
||||
|
||||
r = n_dhcp4_client_config_set_client_id(dup,
|
||||
config->client_id,
|
||||
|
|
@ -248,6 +251,15 @@ _c_public_ int n_dhcp4_client_config_set_client_id(NDhcp4ClientConfig *config, c
|
|||
return 0;
|
||||
}
|
||||
|
||||
_c_public_ void n_dhcp4_client_config_set_log_level(NDhcp4ClientConfig *config, int level) {
|
||||
config->log.level = level;
|
||||
}
|
||||
|
||||
_c_public_ void n_dhcp4_client_config_set_log_func(NDhcp4ClientConfig *config, NDhcp4LogFunc func, void *data) {
|
||||
config->log.func = func;
|
||||
config->log.data = data;
|
||||
}
|
||||
|
||||
/**
|
||||
* n_dhcp4_c_event_node_new() - allocate new event
|
||||
* @nodep: output argument for new event
|
||||
|
|
|
|||
|
|
@ -239,6 +239,11 @@ struct NDhcp4ClientConfig {
|
|||
size_t n_broadcast_mac;
|
||||
uint8_t *client_id;
|
||||
size_t n_client_id;
|
||||
struct {
|
||||
int level;
|
||||
NDhcp4LogFunc func;
|
||||
void *data;
|
||||
} log;
|
||||
};
|
||||
|
||||
#define N_DHCP4_CLIENT_CONFIG_NULL(_x) { \
|
||||
|
|
@ -686,3 +691,19 @@ static inline uint64_t n_dhcp4_gettime(clockid_t clock) {
|
|||
|
||||
return ts.tv_sec * 1000ULL * 1000ULL * 1000ULL + ts.tv_nsec;
|
||||
}
|
||||
|
||||
#define n_dhcp4_c_log(_config, _level, ...) \
|
||||
do { \
|
||||
const NDhcp4ClientConfig *__config = _config; \
|
||||
\
|
||||
if (_level <= __config->log.level && __config->log.func) { \
|
||||
if (1) { \
|
||||
_config->log.func(_level, \
|
||||
__config->log.data, \
|
||||
__VA_ARGS__); \
|
||||
} else { \
|
||||
/* To have the compiler check arguments */ \
|
||||
printf(__VA_ARGS__); \
|
||||
} \
|
||||
} \
|
||||
} while (0)
|
||||
|
|
|
|||
|
|
@ -29,6 +29,8 @@ typedef struct NDhcp4ServerEvent NDhcp4ServerEvent;
|
|||
typedef struct NDhcp4ServerIp NDhcp4ServerIp;
|
||||
typedef struct NDhcp4ServerLease NDhcp4ServerLease;
|
||||
|
||||
typedef void (*NDhcp4LogFunc)(int level, void *data, const char *fmt, ...);
|
||||
|
||||
#define N_DHCP4_CLIENT_START_DELAY_RFC2131 (UINT64_C(9000))
|
||||
|
||||
enum {
|
||||
|
|
@ -111,6 +113,8 @@ void n_dhcp4_client_config_set_request_broadcast(NDhcp4ClientConfig *config, boo
|
|||
void n_dhcp4_client_config_set_mac(NDhcp4ClientConfig *config, const uint8_t *mac, size_t n_mac);
|
||||
void n_dhcp4_client_config_set_broadcast_mac(NDhcp4ClientConfig *config, const uint8_t *mac, size_t n_mac);
|
||||
int n_dhcp4_client_config_set_client_id(NDhcp4ClientConfig *config, const uint8_t *id, size_t n_id);
|
||||
void n_dhcp4_client_config_set_log_level(NDhcp4ClientConfig *config, int level);
|
||||
void n_dhcp4_client_config_set_log_func(NDhcp4ClientConfig *config, NDhcp4LogFunc func, void *data);
|
||||
|
||||
/* client-probe configs */
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue