consolidate common win32 macros (#192)

more logging changed
fix stripping file prefix for logging
This commit is contained in:
ekoby
2020-12-02 09:09:45 -05:00
committed by GitHub
parent bc0229dde7
commit 491cd81d96
10 changed files with 84 additions and 53 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
#if ($HEADER_COMMENTS)
/*
Copyright (c) $YEAR Netfoundry, Inc.
Copyright (c) $YEAR NetFoundry, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
+3 -3
View File
@@ -100,11 +100,11 @@ macro(PUBCOMP comp)
add_dependencies(publish ${comp}-pub)
endmacro(PUBCOMP)
# use prefix length to trim path for logging, see inc_internal/utils.h __FILENAME__
# use prefix length to trim path for logging, see utils.c
if (CMAKE_GENERATOR STREQUAL "Ninja" AND CMAKE_BINARY_DIR MATCHES "${CMAKE_SOURCE_DIR}")
# if CMAKE_BINARY_DIR is inside CMAKE_SOURCE_DIR Ninja uses relative paths which screws logging of the filename
# so we just trim the first "../" here, this is not always correct but will show proper file
set(SOURCE_PATH_SIZE 3)
# so we just leave it be -- it shows enough information to find the source
set(SOURCE_PATH_SIZE 0)
else()
string(LENGTH "${CMAKE_SOURCE_DIR}/" SOURCE_PATH_SIZE)
endif()
+21
View File
@@ -29,6 +29,27 @@ limitations under the License.
extern "C" {
#endif
#if _WIN32
# if !defined(strncasecmp)
# define strncasecmp _strnicmp
# endif
# if !defined(strcasecmp)
# define strcasecmp _stricmp
# endif
# if !defined(MIN)
# define MIN(a,b) ((a)<(b) ? (a) : (b))
# endif
# if !defined(MAX)
# define MAX(a,b) ((a) > (b) ? (a) : (b))
# endif
typedef unsigned int uint;
#endif
extern const char *ziti_get_build_version(int verbose);
extern const char *ziti_git_branch();
+1 -8
View File
@@ -39,11 +39,6 @@ limitations under the License.
#endif
#if _WIN32
#define uint unsigned int
#endif
typedef struct ziti_channel ziti_channel_t;
typedef void (*reply_cb)(void *ctx, message *m);
@@ -79,7 +74,7 @@ typedef struct ziti_channel {
uv_timer_t latency_timer;
enum conn_state state;
uint reconnect_count;
uint32_t reconnect_count;
struct ch_conn_req **conn_reqs;
int conn_reqs_n;
@@ -93,8 +88,6 @@ typedef struct ziti_channel {
LIST_HEAD(con_list, msg_receiver) receivers;
LIST_HEAD(waiter, waiter_s) waiters;
LIST_ENTRY(ziti_channel) next;
} ziti_channel_t;
struct ziti_write_req_s {
+13 -4
View File
@@ -40,23 +40,32 @@ enum DebugLevel {
};
#define ZITI_LOG(level, fmt, ...) do { \
if (level <= ziti_debug_level) { ziti_logger(level, __FILE__, __LINE__, __func__, fmt, ##__VA_ARGS__); }\
if (level <= ziti_log_level) { ziti_logger(level, __FILE__, __LINE__, __func__, fmt, ##__VA_ARGS__); }\
} while(0)
#ifdef __cplusplus
extern "C" {
#endif
#define ZITI_LOG_DEFAULT_LEVEL (-1)
typedef void (*log_writer)(int level, const char *loc, const char *msg, size_t msglen);
ZITI_FUNC extern void
ziti_logger(int level, const char *file, unsigned int line, const char *func, const char *fmt, ...);
ZITI_FUNC extern void init_debug(uv_loop_t *loop);
// call once
// use ZITI_LOG_DEFAULT_LEVEL to use default(INFO)/ZITI_LOG env var
// pass logger = NULL to use default output
ZITI_FUNC extern void ziti_log_init(uv_loop_t *loop, int level, log_writer logger);
ZITI_FUNC extern void ziti_set_log(log_writer log, uv_loop_t *loop);
ZITI_FUNC extern void ziti_log_set_logger(log_writer logger);
ZITI_FUNC extern int ziti_debug_level;
// use ZITI_LOG_DEFAULT_LEVEL to reset to default(INFO) or ZITI_LOG env var
ZITI_FUNC extern void ziti_log_set_level(int level);
// don't use directly
ZITI_FUNC extern int ziti_log_level;
#ifdef __cplusplus
}
+2 -1
View File
@@ -16,9 +16,10 @@ limitations under the License.
#include <stdint.h>
#include <stdlib.h>
#include "utils.h"
#if _WIN32
#include <crtdefs.h>
#define MIN(a,b) ((a)<(b) ? (a) : (b))
#else
#include <sys/param.h>
#endif
+40 -18
View File
@@ -99,14 +99,10 @@ const char* ziti_git_commit() {
return to_str(ZITI_COMMIT);
}
int ziti_debug_level = INFO;
int ziti_log_level = ZITI_LOG_DEFAULT_LEVEL;
static FILE *ziti_debug_out;
static bool log_initialized = false;
#if _WIN32
#define strcasecmp _stricmp
#endif
const char *(*get_elapsed)();
static const char *get_elapsed_time();
@@ -126,15 +122,45 @@ static char elapsed_buffer[32];
static uint64_t clock_offset;
static uv_prepare_t log_flusher;
static log_writer logger = default_log_writer;
static log_writer logger = NULL;
static void init_debug(uv_loop_t *loop);
void ziti_set_log(log_writer log_func, uv_loop_t *loop) {
void ziti_log_init(uv_loop_t *loop, int level, log_writer log_func) {
init_debug(loop);
uv_mbed_set_debug(ziti_debug_level, uv_mbed_logger);
logger = log_func;
if (level == ZITI_LOG_DEFAULT_LEVEL)
level = ziti_log_level; // in case it was set before
ziti_log_set_level(level);
if (log_func == NULL) {
// keep the logger if it was already set
ziti_log_set_logger(logger ? logger : default_log_writer);
} else {
ziti_log_set_logger(log_func);
}
}
void init_debug(uv_loop_t *loop) {
void ziti_log_set_level(int level) {
if (level == ZITI_LOG_DEFAULT_LEVEL) {
char *lvl = getenv("ZITI_LOG");
if (lvl != NULL) {
ziti_log_level = (int) strtol(lvl, NULL, 10);
} else {
ziti_log_level = INFO;
}
} else {
ziti_log_level = level;
}
uv_mbed_set_debug(ziti_log_level, uv_mbed_logger);
}
void ziti_log_set_logger(log_writer log) {
logger = log;
}
static void init_debug(uv_loop_t *loop) {
if (log_initialized) {
return;
}
@@ -145,12 +171,8 @@ void init_debug(uv_loop_t *loop) {
}
ts_loop = loop;
log_initialized = true;
char *level = getenv("ZITI_LOG");
if (level != NULL) {
ziti_debug_level = (int) strtol(level, NULL, 10);
}
ziti_log_set_level(ziti_log_level);
ziti_debug_out = stderr;
uv_mbed_set_debug(ziti_debug_level, uv_mbed_logger);
starttime = uv_now(loop);
uv_timeval64_t clock;
@@ -172,10 +194,10 @@ void ziti_logger(int level, const char *file, unsigned int line, const char *fun
va_start(argp, fmt);
char location[128];
if (func && func[0]) {
snprintf(location, sizeof(location), "%s:%d %s()", file, line, func);
snprintf(location, sizeof(location), "%s:%d %s()", file + SOURCE_PATH_SIZE, line, func);
}
else {
snprintf(location, sizeof(location), "%s:%d", file, line);
snprintf(location, sizeof(location), "%s:%d", file + SOURCE_PATH_SIZE, line);
}
int len = vsnprintf(logbuf, loglinelen, fmt, argp);
@@ -258,7 +280,7 @@ int lt_zero(int v) { return v < 0; }
int non_zero(int v) { return v != 0; }
void hexDump (char *desc, void *addr, int len) {
if (DEBUG > ziti_debug_level) return;
if (DEBUG > ziti_log_level) return;
ZITI_LOG(DEBUG, " ");
int i;
unsigned char buffLine[17];
+1 -5
View File
@@ -41,10 +41,6 @@ limitations under the License.
#endif
#endif
#if _WIN32
#define strncasecmp _strnicmp
#endif
static const char *ALL_CONFIG_TYPES[] = {
"all",
NULL
@@ -180,7 +176,7 @@ int load_tls(ziti_config *cfg, tls_context **ctx) {
}
int ziti_init_opts(ziti_options *options, uv_loop_t *loop, void *init_ctx) {
init_debug(loop);
ziti_log_init(loop, ZITI_LOG_DEFAULT_LEVEL, NULL);
metrics_init(loop, 5);
uv_timeval64_t start_time;
-7
View File
@@ -21,13 +21,6 @@ limitations under the License.
#include <ziti_ctrl.h>
#include <uv_mbed/um_http.h>
#if _WIN32
#define strcasecmp _stricmp
#define strncasecmp _strnicmp
#endif
#define DEFAULT_PAGE_SIZE 25
const char *const PC_DOMAIN_TYPE = "DOMAIN";
+2 -6
View File
@@ -33,10 +33,6 @@ limitations under the License.
#endif
#endif
#if _WIN32
#define strncasecmp _strnicmp
#endif
static void well_known_certs_cb(char *base64_encoded_pkcs7, ziti_error *err, void *req);
static void enroll_cb(ziti_enrollment_resp *er, ziti_error *err, void *ctx);
@@ -90,7 +86,7 @@ static int check_cert_required(enroll_cfg *ecfg) {
}
int ziti_enroll(ziti_enroll_opts *opts, uv_loop_t *loop, ziti_enroll_cb enroll_cb, void *enroll_ctx) {
init_debug(loop);
ziti_log_init(loop, ZITI_LOG_DEFAULT_LEVEL, NULL);
uv_timeval64_t start_time;
uv_gettimeofday(&start_time);
@@ -115,7 +111,7 @@ int ziti_enroll(ziti_enroll_opts *opts, uv_loop_t *loop, ziti_enroll_cb enroll_c
ecfg->private_key = opts->enroll_key;
TRY(ziti, load_jwt(opts->jwt, ecfg, &ecfg->zejh, &ecfg->zej));
if (DEBUG <= ziti_debug_level) {
if (DEBUG <= ziti_log_level) {
dump_ziti_enrollment_jwt_header(ecfg->zejh, 0);
dump_ziti_enrollment_jwt(ecfg->zej, 0);
}