22
22
#define MAKE_STR (x ) #x
23
23
#define STR (x ) MAKE_STR(x)
24
24
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 , ...) \
26
37
P1_fprintf(stderr, "polaris.c:" STR(__LINE__) "] " x, ##__VA_ARGS__)
27
- #define P1_PrintError (x , ...) \
38
+ # define P1_PrintError (x , ...) \
28
39
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
29
54
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 )
31
58
static void __P1_PrintWriteError (int line , PolarisContext_t * context ,
32
59
const char * message , int ret ) {
33
60
SSL_load_error_strings ();
@@ -80,40 +107,10 @@ static void __P1_PrintWriteError(int line, PolarisContext_t* context,
80
107
}
81
108
}
82
109
83
- #define P1_PrintWriteError (context , x , ret ) \
110
+ # define P1_PrintWriteError (context , x , ret ) \
84
111
__P1_PrintWriteError(__LINE__, context, x, ret)
85
112
#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)
117
114
#endif
118
115
119
116
static int OpenSocket (PolarisContext_t * context , const char * endpoint_url ,
@@ -127,10 +124,6 @@ static int GetHTTPResponse(PolarisContext_t* context);
127
124
128
125
static void CloseSocket (PolarisContext_t * context , int destroy_context );
129
126
130
- #ifdef POLARIS_USE_TLS
131
- static void ShowCerts (SSL * ssl );
132
- #endif
133
-
134
127
/******************************************************************************/
135
128
int Polaris_Init (PolarisContext_t * context ) {
136
129
if (POLARIS_RECV_BUFFER_SIZE < POLARIS_MAX_HTTP_MESSAGE_SIZE ) {
@@ -167,6 +160,13 @@ void Polaris_Free(PolarisContext_t* context) {
167
160
CloseSocket (context , 1 );
168
161
}
169
162
163
+ /******************************************************************************/
164
+ void Polaris_SetLogLevel (int log_level ) {
165
+ #if !defined(POLARIS_NO_PRINT )
166
+ __log_level = log_level ;
167
+ #endif
168
+ }
169
+
170
170
/******************************************************************************/
171
171
int Polaris_Authenticate (PolarisContext_t * context , const char * api_key ,
172
172
const char * unique_id ) {
@@ -380,7 +380,7 @@ int Polaris_SendECEFPosition(PolarisContext_t* context, double x_m, double y_m,
380
380
"Sending ECEF position. [size=%u B, position=[%.2f, %.2f, %.2f]]\n" ,
381
381
(unsigned )message_size , x_m , y_m , z_m );
382
382
#endif
383
- PrintData (context -> send_buffer , message_size );
383
+ P1_PrintData (context -> send_buffer , message_size );
384
384
385
385
#ifdef POLARIS_USE_TLS
386
386
int ret = SSL_write (context -> ssl , context -> send_buffer , message_size );
@@ -423,7 +423,7 @@ int Polaris_SendLLAPosition(PolarisContext_t* context, double latitude_deg,
423
423
"Sending LLA position. [size=%u B, position=[%.6f, %.6f, %.2f]]\n" ,
424
424
(unsigned )message_size , latitude_deg , longitude_deg , altitude_m );
425
425
#endif
426
- PrintData (context -> send_buffer , message_size );
426
+ P1_PrintData (context -> send_buffer , message_size );
427
427
428
428
#ifdef POLARIS_USE_TLS
429
429
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) {
454
454
455
455
P1_DebugPrint ("Sending beacon request. [size=%u B, beacon='%s']\n" ,
456
456
(unsigned )message_size , beacon_id );
457
- PrintData (context -> send_buffer , message_size );
457
+ P1_PrintData (context -> send_buffer , message_size );
458
458
459
459
#ifdef POLARIS_USE_TLS
460
460
int ret = SSL_write (context -> ssl , context -> send_buffer , message_size );
@@ -860,9 +860,34 @@ static int GetHTTPResponse(PolarisContext_t* context) {
860
860
}
861
861
862
862
/******************************************************************************/
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 )
864
886
void ShowCerts (SSL * ssl ) {
865
- #if defined(POLARIS_DEBUG ) || defined(POLARIS_TRACE )
887
+ if (__log_level >= POLARIS_LOG_LEVEL_DEBUG ) {
888
+ return ;
889
+ }
890
+
866
891
X509 * cert = SSL_get_peer_certificate (ssl );
867
892
if (cert != NULL ) {
868
893
char * line ;
@@ -874,6 +899,5 @@ void ShowCerts(SSL* ssl) {
874
899
} else {
875
900
P1_DebugPrint ("No client certificates configured.\n" );
876
901
}
877
- #endif
878
902
}
879
903
#endif
0 commit comments