Skip to content

Commit d5f1388

Browse files
authored
Changed C library to enable debug prints at runtime (enabled automatically in C++ when VLOG is set for polaris_client).
Merge pull request #39.
2 parents 6439ba9 + 79a316a commit d5f1388

File tree

7 files changed

+138
-55
lines changed

7 files changed

+138
-55
lines changed

Diff for: c/BUILD

+26-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,23 @@ package(default_visibility = ["//visibility:public"])
22

33
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")
44

5+
# Option to disable _all_ print messages:
6+
# --//:polaris_enable_print [enabled; default]
7+
# --//:polaris_enable_print=True [enabled; default]
8+
# --//:polaris_enable_print=False [disabled]
9+
# --no//:polaris_enable_print [disabled]
10+
bool_flag(
11+
name = "polaris_enable_print",
12+
build_setting_default = True,
13+
)
14+
15+
config_setting(
16+
name = "print_enabled",
17+
flag_values = {
18+
":polaris_enable_print": "True",
19+
},
20+
)
21+
522
# Option to enable/disable TLS support (enabled by default):
623
# --//:polaris_enable_tls [enabled; default]
724
# --//:polaris_enable_tls=True [enabled; default]
@@ -38,7 +55,11 @@ cc_library(
3855
name = "polaris_client_tls",
3956
srcs = glob(["src/**/*.c"]),
4057
hdrs = glob(["src/**/*.h"]),
41-
copts = ["-DPOLARIS_USE_TLS=1"],
58+
copts = ["-DPOLARIS_USE_TLS=1"] +
59+
select({
60+
":print_enabled": [],
61+
"//conditions:default": ["-DPOLARIS_NO_PRINT"],
62+
}),
4263
includes = ["src"],
4364
deps = ["@boringssl//:ssl"],
4465
)
@@ -49,5 +70,9 @@ cc_library(
4970
name = "polaris_client_no_tls",
5071
srcs = glob(["src/**/*.c"]),
5172
hdrs = glob(["src/**/*.h"]),
73+
copts = select({
74+
":print_enabled": [],
75+
"//conditions:default": ["-DPOLARIS_NO_PRINT"],
76+
}),
5277
includes = ["src"],
5378
)

Diff for: c/CMakeLists.txt

+4-7
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ option(BUILD_SHARED_LIBS "Build shared libraries instead of static libraries."
99

1010
option(BUILD_EXAMPLES "Build example applications." ON)
1111

12-
set(POLARIS_DEBUG "OFF" CACHE STRING
13-
"Enable Polaris debug/trace print messages.")
14-
set_property(CACHE POLARIS_DEBUG PROPERTY STRINGS OFF ON DEBUG TRACE)
12+
option(POLARIS_ENABLE_PRINT
13+
"Enable Polaris debug/trace print messages." ON)
1514

1615
option(POLARIS_ENABLE_TLS "Enable TLS support when connecting to Polaris." ON)
1716

@@ -22,10 +21,8 @@ else()
2221
add_compile_options(-Wall -Werror)
2322
endif()
2423

25-
if(POLARIS_DEBUG STREQUAL "TRACE")
26-
add_compile_definitions(POLARIS_TRACE)
27-
elseif(POLARIS_DEBUG)
28-
add_compile_definitions(POLARIS_DEBUG)
24+
if(NOT POLARIS_ENABLE_PRINT)
25+
add_compile_definitions(POLARIS_NO_PRINT)
2926
endif()
3027

3128
################################################################################

Diff for: c/examples/connection_retry.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
******************************************************************************/
66

77
#include <signal.h>
8+
#include <stdlib.h> // For atoi()
89

910
#include "point_one/polaris/polaris.h"
1011
#include "point_one/polaris/portability.h"
@@ -26,13 +27,16 @@ void HandleSignal(int sig) {
2627

2728
int main(int argc, const char* argv[]) {
2829
if (argc < 2 || argc > 3) {
29-
P1_fprintf(stderr, "Usage: %s API_KEY [UNIQUE_ID]\n", argv[0]);
30+
P1_fprintf(stderr, "Usage: %s API_KEY [UNIQUE_ID] [LOG_LEVEL]\n", argv[0]);
3031
return 1;
3132
}
3233

3334
const char* api_key = argv[1];
3435
const char* unique_id = argc > 2 ? argv[2] : "device12345";
3536

37+
int log_level = argc > 3 ? atoi(argv[3]) : POLARIS_LOG_LEVEL_INFO;
38+
Polaris_SetLogLevel(log_level);
39+
3640
const int MAX_RECONNECTS = 2;
3741
int auth_valid = 0;
3842
int reconnect_count = 0;

Diff for: c/examples/simple_polaris_client.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
******************************************************************************/
66

77
#include <signal.h>
8+
#include <stdlib.h> // For atoi()
89

910
#include "point_one/polaris/polaris.h"
1011
#include "point_one/polaris/portability.h"
@@ -26,13 +27,16 @@ void HandleSignal(int sig) {
2627

2728
int main(int argc, const char* argv[]) {
2829
if (argc < 2 || argc > 3) {
29-
P1_fprintf(stderr, "Usage: %s API_KEY [UNIQUE_ID]\n", argv[0]);
30+
P1_fprintf(stderr, "Usage: %s API_KEY [UNIQUE_ID] [LOG_LEVEL]\n", argv[0]);
3031
return 1;
3132
}
3233

3334
const char* api_key = argv[1];
3435
const char* unique_id = argc > 2 ? argv[2] : "device12345";
3536

37+
int log_level = argc > 3 ? atoi(argv[3]) : POLARIS_LOG_LEVEL_INFO;
38+
Polaris_SetLogLevel(log_level);
39+
3640
// Retrieve an access token using the specified API key.
3741
if (Polaris_Init(&context) != POLARIS_SUCCESS) {
3842
return 2;

Diff for: c/src/point_one/polaris/polaris.c

+69-45
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,39 @@
2222
#define MAKE_STR(x) #x
2323
#define STR(x) MAKE_STR(x)
2424

25-
#define P1_Print(x, ...) \
25+
#if defined(P1_NO_PRINT)
26+
# define P1_Print(x, ...) do {} while(0)
27+
# define P1_PrintError(x, ...) do {} while(0)
28+
# define P1_DebugPrint(x, ...) do {} while(0)
29+
# define PrintData(buffer, length) do {} while(0)
30+
# if defined(POLARIS_USE_TLS)
31+
# define ShowCerts(ssl) do {} while(0)
32+
# endif
33+
#else
34+
static int __log_level = POLARIS_LOG_LEVEL_INFO;
35+
36+
# define P1_Print(x, ...) \
2637
P1_fprintf(stderr, "polaris.c:" STR(__LINE__) "] " x, ##__VA_ARGS__)
27-
#define P1_PrintError(x, ...) \
38+
# define P1_PrintError(x, ...) \
2839
P1_perror("polaris.c:" STR(__LINE__) "] " x, ##__VA_ARGS__)
40+
# define P1_DebugPrint(x, ...) \
41+
if (__log_level >= POLARIS_LOG_LEVEL_DEBUG) { \
42+
P1_Print(x, ##__VA_ARGS__); \
43+
}
44+
# define P1_TracePrint(x, ...) \
45+
if (__log_level >= POLARIS_LOG_LEVEL_TRACE) { \
46+
P1_Print(x, ##__VA_ARGS__); \
47+
}
48+
49+
static void P1_PrintData(const uint8_t* buffer, size_t length);
50+
# if defined(POLARIS_USE_TLS)
51+
static void ShowCerts(SSL* ssl);
52+
# endif
53+
#endif
2954

30-
#if defined(POLARIS_USE_TLS)
55+
#if defined(POLARIS_NO_PRINT)
56+
# define P1_PrintWriteError(context, x, ret) do {} while(0)
57+
#elif defined(POLARIS_USE_TLS)
3158
static void __P1_PrintWriteError(int line, PolarisContext_t* context,
3259
const char* message, int ret) {
3360
SSL_load_error_strings();
@@ -80,40 +107,10 @@ static void __P1_PrintWriteError(int line, PolarisContext_t* context,
80107
}
81108
}
82109

83-
#define P1_PrintWriteError(context, x, ret) \
110+
# define P1_PrintWriteError(context, x, ret) \
84111
__P1_PrintWriteError(__LINE__, context, x, ret)
85112
#else
86-
#define P1_PrintWriteError(context, x, ret) P1_PrintError(x, ret)
87-
#endif
88-
89-
90-
#if defined(POLARIS_TRACE) && !defined(POLARIS_DEBUG)
91-
# define POLARIS_DEBUG 1
92-
#endif
93-
94-
#if defined(POLARIS_DEBUG) || defined(POLARIS_TRACE)
95-
# define P1_DebugPrint(x, ...) P1_Print(x, ##__VA_ARGS__)
96-
#else
97-
# define P1_DebugPrint(x, ...) do {} while(0)
98-
#endif
99-
100-
#if defined(POLARIS_TRACE)
101-
void PrintData(const uint8_t* buffer, size_t length) {
102-
for (size_t i = 0; i < length; ++i) {
103-
if (i % 16 != 0) {
104-
P1_fprintf(stderr, " ");
105-
}
106-
107-
P1_fprintf(stderr, "%02x", buffer[i]);
108-
109-
if (i % 16 == 15) {
110-
P1_fprintf(stderr, "\n");
111-
}
112-
}
113-
P1_fprintf(stderr, "\n");
114-
}
115-
#else
116-
# define PrintData(buffer, length) do {} while(0)
113+
# define P1_PrintWriteError(context, x, ret) P1_PrintError(x, ret)
117114
#endif
118115

119116
static int OpenSocket(PolarisContext_t* context, const char* endpoint_url,
@@ -127,10 +124,6 @@ static int GetHTTPResponse(PolarisContext_t* context);
127124

128125
static void CloseSocket(PolarisContext_t* context, int destroy_context);
129126

130-
#ifdef POLARIS_USE_TLS
131-
static void ShowCerts(SSL* ssl);
132-
#endif
133-
134127
/******************************************************************************/
135128
int Polaris_Init(PolarisContext_t* context) {
136129
if (POLARIS_RECV_BUFFER_SIZE < POLARIS_MAX_HTTP_MESSAGE_SIZE) {
@@ -167,6 +160,13 @@ void Polaris_Free(PolarisContext_t* context) {
167160
CloseSocket(context, 1);
168161
}
169162

163+
/******************************************************************************/
164+
void Polaris_SetLogLevel(int log_level) {
165+
#if !defined(POLARIS_NO_PRINT)
166+
__log_level = log_level;
167+
#endif
168+
}
169+
170170
/******************************************************************************/
171171
int Polaris_Authenticate(PolarisContext_t* context, const char* api_key,
172172
const char* unique_id) {
@@ -380,7 +380,7 @@ int Polaris_SendECEFPosition(PolarisContext_t* context, double x_m, double y_m,
380380
"Sending ECEF position. [size=%u B, position=[%.2f, %.2f, %.2f]]\n",
381381
(unsigned)message_size, x_m, y_m, z_m);
382382
#endif
383-
PrintData(context->send_buffer, message_size);
383+
P1_PrintData(context->send_buffer, message_size);
384384

385385
#ifdef POLARIS_USE_TLS
386386
int ret = SSL_write(context->ssl, context->send_buffer, message_size);
@@ -423,7 +423,7 @@ int Polaris_SendLLAPosition(PolarisContext_t* context, double latitude_deg,
423423
"Sending LLA position. [size=%u B, position=[%.6f, %.6f, %.2f]]\n",
424424
(unsigned)message_size, latitude_deg, longitude_deg, altitude_m);
425425
#endif
426-
PrintData(context->send_buffer, message_size);
426+
P1_PrintData(context->send_buffer, message_size);
427427

428428
#ifdef POLARIS_USE_TLS
429429
int ret = SSL_write(context->ssl, context->send_buffer, message_size);
@@ -454,7 +454,7 @@ int Polaris_RequestBeacon(PolarisContext_t* context, const char* beacon_id) {
454454

455455
P1_DebugPrint("Sending beacon request. [size=%u B, beacon='%s']\n",
456456
(unsigned)message_size, beacon_id);
457-
PrintData(context->send_buffer, message_size);
457+
P1_PrintData(context->send_buffer, message_size);
458458

459459
#ifdef POLARIS_USE_TLS
460460
int ret = SSL_write(context->ssl, context->send_buffer, message_size);
@@ -860,9 +860,34 @@ static int GetHTTPResponse(PolarisContext_t* context) {
860860
}
861861

862862
/******************************************************************************/
863-
#ifdef POLARIS_USE_TLS
863+
#if !defined(P1_NO_PRINT)
864+
void P1_PrintData(const uint8_t* buffer, size_t length) {
865+
if (__log_level < POLARIS_LOG_LEVEL_TRACE) {
866+
return;
867+
}
868+
869+
for (size_t i = 0; i < length; ++i) {
870+
if (i % 16 != 0) {
871+
P1_fprintf(stderr, " ");
872+
}
873+
874+
P1_fprintf(stderr, "%02x", buffer[i]);
875+
876+
if (i % 16 == 15) {
877+
P1_fprintf(stderr, "\n");
878+
}
879+
}
880+
P1_fprintf(stderr, "\n");
881+
}
882+
#endif
883+
884+
/******************************************************************************/
885+
#if !defined(P1_NO_PRINT) && defined(POLARIS_USE_TLS)
864886
void ShowCerts(SSL* ssl) {
865-
#if defined(POLARIS_DEBUG) || defined(POLARIS_TRACE)
887+
if (__log_level >= POLARIS_LOG_LEVEL_DEBUG) {
888+
return;
889+
}
890+
866891
X509* cert = SSL_get_peer_certificate(ssl);
867892
if (cert != NULL) {
868893
char* line;
@@ -874,6 +899,5 @@ void ShowCerts(SSL* ssl) {
874899
} else {
875900
P1_DebugPrint("No client certificates configured.\n");
876901
}
877-
#endif
878902
}
879903
#endif

Diff for: c/src/point_one/polaris/polaris.h

+21
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@
6969
# define POLARIS_SEND_TIMEOUT_MS 1000
7070
#endif
7171

72+
/**
73+
* @name Polaris Return Codes
74+
* @{
75+
*/
7276
#define POLARIS_SUCCESS 0
7377
#define POLARIS_ERROR -1
7478
#define POLARIS_NOT_ENOUGH_SPACE -2
@@ -78,6 +82,16 @@
7882
#define POLARIS_FORBIDDEN -6
7983
#define POLARIS_CONNECTION_CLOSED -7
8084
#define POLARIS_TIMED_OUT -8
85+
/** @} */
86+
87+
/**
88+
* @name Polaris Logging Verbosity Levels
89+
* @{
90+
*/
91+
#define POLARIS_LOG_LEVEL_INFO 0
92+
#define POLARIS_LOG_LEVEL_DEBUG 1
93+
#define POLARIS_LOG_LEVEL_TRACE 2
94+
/** @} */
8195

8296
struct PolarisContext_s;
8397
typedef struct PolarisContext_s PolarisContext_t;
@@ -131,6 +145,13 @@ int Polaris_Init(PolarisContext_t* context);
131145
*/
132146
void Polaris_Free(PolarisContext_t* context);
133147

148+
/**
149+
* @brief Set the Polaris library print verbosity level.
150+
*
151+
* @param log_level The desired verbosity level.
152+
*/
153+
void Polaris_SetLogLevel(int log_level);
154+
134155
/**
135156
* @brief Authenticate with Polaris.
136157
*

Diff for: src/point_one/polaris/polaris_client.cc

+8
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ PolarisClient::PolarisClient(const std::string& api_key,
6060
: max_reconnect_attempts_(max_reconnect_attempts),
6161
api_key_(api_key),
6262
unique_id_(unique_id) {
63+
// Note that the C library print level will not change if the VLOG level is
64+
// changed dynamically via SetVLOGLevel() at runtime.
65+
if (VLOG_IS_ON(1)) {
66+
Polaris_SetLogLevel(POLARIS_LOG_LEVEL_DEBUG);
67+
} else if (VLOG_IS_ON(2)) {
68+
Polaris_SetLogLevel(POLARIS_LOG_LEVEL_TRACE);
69+
}
70+
6371
SetPolarisEndpoint();
6472

6573
polaris_.SetRTCMCallback([&](const uint8_t* buffer, size_t size_bytes) {

0 commit comments

Comments
 (0)