Skip to content

Commit f891c75

Browse files
committed
D4T-1827: Add logging callback functions
1 parent 540fefc commit f891c75

File tree

5 files changed

+160
-15
lines changed

5 files changed

+160
-15
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ endif()
3838

3939
set(PROXYRES_HDRS
4040
include/proxyres/config.h
41+
include/proxyres/log.h
4142
include/proxyres/proxyres.h
4243
include/proxyres/resolver.h)
4344
if(PROXYRES_EXECUTE)
@@ -56,6 +57,7 @@ list(APPEND PROXYRES_HDRS
5657
util.h)
5758
list(APPEND PROXYRES_SRCS
5859
config.c
60+
log.c
5961
net_util.c
6062
proxyres.c
6163
resolver.c

doc/proxy_log.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# proxy_log <!-- omit in toc -->
2+
3+
Logging functions.
4+
5+
## API <!-- omit in toc -->
6+
7+
- [proxy\_log\_set\_error\_cb](#proxy_log_set_error_cb)
8+
- [proxy\_log\_set\_warn\_cb](#proxy_log_set_warn_cb)
9+
- [proxy\_log\_set\_info\_cb](#proxy_log_set_info_cb)
10+
- [proxy\_log\_set\_debug\_cb](#proxy_log_set_debug_cb)
11+
12+
## proxy_log_set_error_cb
13+
14+
Set the print callback for error messages.
15+
16+
**Arguments**
17+
|Type|Name|Description|
18+
|-|-|:-|
19+
|void *|func|Callback to receive messages|
20+
21+
## proxy_log_set_warn_cb
22+
23+
Set the print callback for warning messages.
24+
25+
**Arguments**
26+
|Type|Name|Description|
27+
|-|-|:-|
28+
|void *|func|Callback to receive messages|
29+
30+
## proxy_log_set_info_cb
31+
32+
Set the print callback for info messages.
33+
34+
**Arguments**
35+
|Type|Name|Description|
36+
|-|-|:-|
37+
|void *|func|Callback to receive messages|
38+
39+
## proxy_log_set_debug_cb
40+
41+
Set the print callback for debug messages.
42+
43+
**Arguments**
44+
|Type|Name|Description|
45+
|-|-|:-|
46+
|void *|func|Callback to receive messages|;

include/proxyres/log.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#pragma once
2+
3+
#include <stdarg.h>
4+
5+
#ifdef __cplusplus
6+
extern "C" {
7+
#endif
8+
9+
void proxy_log_set_error_cb(void (*func)(const char *fmt, va_list args));
10+
11+
void proxy_log_set_warn_cb(void (*func)(const char *fmt, va_list args));
12+
13+
void proxy_log_set_info_cb(void (*func)(const char *fmt, va_list args));
14+
15+
void proxy_log_set_debug_cb(void (*func)(const char *fmt, va_list args));
16+
17+
#ifdef __cplusplus
18+
}
19+
#endif

log.c

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include "log.h"
2+
3+
static void (*log_error_func)(const char *fmt, va_list args);
4+
static void (*log_warn_func)(const char *fmt, va_list args);
5+
static void (*log_info_func)(const char *fmt, va_list args);
6+
static void (*log_debug_func)(const char *fmt, va_list args);
7+
8+
void proxy_log_set_error_cb(void (*func)(const char *fmt, va_list args)) {
9+
log_error_func = func;
10+
}
11+
12+
void proxy_log_set_warn_cb(void (*func)(const char *fmt, va_list args)) {
13+
log_warn_func = func;
14+
}
15+
16+
void proxy_log_set_info_cb(void (*func)(const char *fmt, va_list args)) {
17+
log_info_func = func;
18+
}
19+
20+
void proxy_log_set_debug_cb(void (*func)(const char *fmt, va_list args)) {
21+
log_debug_func = func;
22+
}
23+
24+
void log_error(const char *fmt, ...) {
25+
va_list args;
26+
va_start(args, fmt);
27+
if (log_error_func)
28+
log_error_func(fmt, args);
29+
else
30+
vprintf(fmt, args);
31+
va_end(args);
32+
}
33+
34+
void log_warn(const char *fmt, ...) {
35+
va_list args;
36+
va_start(args, fmt);
37+
if (log_warn_func)
38+
log_warn_func(fmt, args);
39+
else
40+
vprintf(fmt, args);
41+
va_end(args);
42+
}
43+
44+
void log_info(const char *fmt, ...) {
45+
va_list args;
46+
va_start(args, fmt);
47+
if (log_info_func)
48+
log_info_func(fmt, args);
49+
#ifdef _DEBUG
50+
else
51+
vprintf(fmt, args);
52+
#endif
53+
va_end(args);
54+
}
55+
56+
void log_debug(const char *fmt, ...) {
57+
va_list args;
58+
va_start(args, fmt);
59+
if (log_debug_func)
60+
log_debug_func(fmt, args);
61+
#ifdef _DEBUG
62+
else
63+
vprintf(fmt, args);
64+
#endif
65+
va_end(args);
66+
}

log.h

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
1-
#pragma once
2-
3-
#define LOG_ERROR(fmt, ...) \
4-
{ printf("ERROR - "), printf(fmt, ##__VA_ARGS__); }
5-
#define LOG_WARN(fmt, ...) \
6-
{ printf("WARNING - "), printf(fmt, ##__VA_ARGS__); }
7-
#ifdef _DEBUG
8-
# define LOG_INFO(fmt, ...) \
9-
{ printf("INFO - "), printf(fmt, ##__VA_ARGS__); }
10-
# define LOG_DEBUG(fmt, ...) \
11-
{ printf("DEBUG - "), printf(fmt, ##__VA_ARGS__); }
12-
#else
13-
# define LOG_INFO(fmt, ...)
14-
# define LOG_DEBUG(fmt, ...)
15-
#endif
1+
#pragma once
2+
3+
#include <stdarg.h>
4+
#include <stdio.h>
5+
6+
void proxy_log_set_error_cb(void (*func)(const char *fmt, va_list args));
7+
void proxy_log_set_warn_cb(void (*func)(const char *fmt, va_list args));
8+
void proxy_log_set_info_cb(void (*func)(const char *fmt, va_list args));
9+
void proxy_log_set_debug_cb(void (*func)(const char *fmt, va_list args));
10+
11+
#ifdef __cplusplus
12+
extern "C" {
13+
#endif
14+
15+
void log_error(const char *fmt, ...);
16+
void log_warn(const char *fmt, ...);
17+
void log_info(const char *fmt, ...);
18+
void log_debug(const char *fmt, ...);
19+
20+
#ifdef __cplusplus
21+
}
22+
#endif
23+
24+
#define LOG_ERROR log_error
25+
#define LOG_WARN log_warn
26+
#define LOG_INFO log_info
27+
#define LOG_DEBUG log_debug

0 commit comments

Comments
 (0)