FS-9952: Added support to default ks_log system for including optional prefixes as desired, also added thread and time prefix options
This commit is contained in:
parent
8f569f715b
commit
75ee45395d
|
@ -90,6 +90,8 @@ KS_DECLARE_DATA extern ks_logger_t ks_log;
|
|||
KS_DECLARE(void) ks_global_set_logger(ks_logger_t logger);
|
||||
/*! Sets the default log level for libks */
|
||||
KS_DECLARE(void) ks_global_set_default_logger(int level);
|
||||
/*! Sets the default log prefix for libks */
|
||||
KS_DECLARE(void) ks_global_set_default_logger_prefix(ks_log_prefix_t prefix);
|
||||
|
||||
KS_DECLARE(size_t) ks_url_encode(const char *url, char *buf, size_t len);
|
||||
KS_DECLARE(char *) ks_url_decode(char *s);
|
||||
|
|
|
@ -1,23 +1,23 @@
|
|||
/*
|
||||
* Copyright (c) 2007-2015, Anthony Minessale II
|
||||
* All rights reserved.
|
||||
*
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
*
|
||||
* * Neither the name of the original author; nor the names of any contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
|
@ -103,9 +103,10 @@ KS_BEGIN_EXTERN_C
|
|||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netdb.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <sys/syscall.h>
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma comment(lib, "Ws2_32.lib")
|
||||
|
||||
|
|
|
@ -45,6 +45,14 @@ KS_BEGIN_EXTERN_C
|
|||
#endif
|
||||
ks_thread_os_handle_t;
|
||||
|
||||
typedef
|
||||
#ifdef WIN32
|
||||
DWORD
|
||||
#else
|
||||
pid_t
|
||||
#endif
|
||||
ks_pid_t;
|
||||
|
||||
struct ks_thread {
|
||||
ks_pool_t *pool;
|
||||
#ifdef WIN32
|
||||
|
@ -76,6 +84,7 @@ struct ks_thread {
|
|||
|
||||
KS_DECLARE(int) ks_thread_set_priority(int nice_val);
|
||||
KS_DECLARE(ks_thread_os_handle_t) ks_thread_self(void);
|
||||
KS_DECLARE(ks_pid_t) ks_thread_self_id(void);
|
||||
KS_DECLARE(ks_thread_os_handle_t) ks_thread_os_handle(ks_thread_t *thread);
|
||||
KS_DECLARE(ks_status_t) ks_thread_create_ex(ks_thread_t **thread, ks_thread_function_t func, void *data,
|
||||
uint32_t flags, size_t stack_size, ks_thread_priority_t priority, ks_pool_t *pool);
|
||||
|
|
|
@ -1,23 +1,23 @@
|
|||
/*
|
||||
* Copyright (c) 2007-2015, Anthony Minessale II
|
||||
* All rights reserved.
|
||||
*
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
*
|
||||
* * Neither the name of the original author; nor the names of any contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
|
@ -38,7 +38,7 @@
|
|||
|
||||
KS_BEGIN_EXTERN_C
|
||||
|
||||
#define KS_STR2ENUM_P(_FUNC1, _FUNC2, _TYPE) KS_DECLARE(_TYPE) _FUNC1 (const char *name); KS_DECLARE(const char *) _FUNC2 (_TYPE type);
|
||||
#define KS_STR2ENUM_P(_FUNC1, _FUNC2, _TYPE) KS_DECLARE(_TYPE) _FUNC1 (const char *name); KS_DECLARE(const char *) _FUNC2 (_TYPE type);
|
||||
|
||||
#define KS_STR2ENUM(_FUNC1, _FUNC2, _TYPE, _STRINGS, _MAX) \
|
||||
KS_DECLARE(_TYPE) _FUNC1 (const char *name) \
|
||||
|
@ -63,7 +63,7 @@ KS_BEGIN_EXTERN_C
|
|||
return _STRINGS[(int)type]; \
|
||||
} \
|
||||
|
||||
#define KS_ENUM_NAMES(_NAME, _STRINGS) static const char * _NAME [] = { _STRINGS , NULL };
|
||||
#define KS_ENUM_NAMES(_NAME, _STRINGS) static const char * _NAME [] = { _STRINGS , NULL };
|
||||
|
||||
#define KS_VA_NONE "%s", ""
|
||||
|
||||
|
@ -142,7 +142,7 @@ KS_BEGIN_EXTERN_C
|
|||
/* insert new entries before this */\
|
||||
"COUNT"
|
||||
|
||||
KS_STR2ENUM_P(ks_str2ks_status, ks_status2str, ks_status_t)
|
||||
KS_STR2ENUM_P(ks_str2ks_status, ks_status2str, ks_status_t)
|
||||
|
||||
/*! \brief Used internally for truth test */
|
||||
typedef enum {
|
||||
|
@ -173,6 +173,19 @@ KS_BEGIN_EXTERN_C
|
|||
#define KS_LOG_ALERT KS_PRE, KS_LOG_LEVEL_ALERT
|
||||
#define KS_LOG_EMERG KS_PRE, KS_LOG_LEVEL_EMERG
|
||||
|
||||
typedef enum {
|
||||
KS_LOG_PREFIX_NONE = 0,
|
||||
|
||||
KS_LOG_PREFIX_LEVEL = 1 << 0,
|
||||
KS_LOG_PREFIX_FILE = 1 << 1,
|
||||
KS_LOG_PREFIX_LINE = 1 << 2,
|
||||
KS_LOG_PREFIX_FUNC = 1 << 3,
|
||||
KS_LOG_PREFIX_THREAD = 1 << 4,
|
||||
KS_LOG_PREFIX_TIME = 1 << 5,
|
||||
|
||||
KS_LOG_PREFIX_ALL = KS_LOG_PREFIX_LEVEL | KS_LOG_PREFIX_FILE | KS_LOG_PREFIX_LINE | KS_LOG_PREFIX_FUNC | KS_LOG_PREFIX_THREAD | KS_LOG_PREFIX_TIME,
|
||||
} ks_log_prefix_t;
|
||||
|
||||
struct ks_pool_s;
|
||||
|
||||
typedef struct ks_pool_s ks_pool_t;
|
||||
|
|
|
@ -1,23 +1,23 @@
|
|||
/*
|
||||
* Copyright (c) 2007-2014, Anthony Minessale II
|
||||
* All rights reserved.
|
||||
*
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
*
|
||||
* * Neither the name of the original author; nor the names of any contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
|
@ -55,6 +55,7 @@ static const char *LEVEL_NAMES[] = {
|
|||
};
|
||||
|
||||
static int ks_log_level = 7;
|
||||
static ks_log_prefix_t ks_log_prefix = KS_LOG_PREFIX_ALL;
|
||||
|
||||
static const char *cut_path(const char *in)
|
||||
{
|
||||
|
@ -78,6 +79,9 @@ static void default_logger(const char *file, const char *func, int line, int lev
|
|||
char *data;
|
||||
va_list ap;
|
||||
int ret;
|
||||
char buf[1024];
|
||||
//int remaining = sizeof(buf) - 1;
|
||||
int used = 0;
|
||||
|
||||
if (level < 0 || level > 7) {
|
||||
level = 7;
|
||||
|
@ -93,7 +97,33 @@ static void default_logger(const char *file, const char *func, int line, int lev
|
|||
ret = ks_vasprintf(&data, fmt, ap);
|
||||
|
||||
if (ret != -1) {
|
||||
fprintf(stderr, "[%s] %s:%d %s() %s", LEVEL_NAMES[level], fp, line, func, data);
|
||||
buf[0] = '\0';
|
||||
used += 1;
|
||||
|
||||
if (ks_log_prefix & KS_LOG_PREFIX_LEVEL) {
|
||||
used += snprintf(buf + used - 1, sizeof(buf) - used, "[%s] ", LEVEL_NAMES[level]);
|
||||
}
|
||||
if (ks_log_prefix & KS_LOG_PREFIX_TIME) {
|
||||
used += snprintf(buf + used - 1, sizeof(buf) - used, "@%lld ", (long long int)ks_time_now());
|
||||
}
|
||||
if (ks_log_prefix & KS_LOG_PREFIX_THREAD) {
|
||||
used += snprintf(buf + used - 1, sizeof(buf) - used, "#%d ", (int32_t)ks_thread_self_id());
|
||||
}
|
||||
if (ks_log_prefix & KS_LOG_PREFIX_FILE) {
|
||||
used += snprintf(buf + used - 1, sizeof(buf) - used, fp);
|
||||
if (ks_log_prefix & KS_LOG_PREFIX_LINE) {
|
||||
used += snprintf(buf + used - 1, sizeof(buf) - used, ":%d", line);
|
||||
}
|
||||
used += snprintf(buf + used - 1, sizeof(buf) - used, " ");
|
||||
}
|
||||
if (ks_log_prefix & KS_LOG_PREFIX_FUNC) {
|
||||
used += snprintf(buf + used - 1, sizeof(buf) - used, "%s() ", func);
|
||||
}
|
||||
|
||||
used += snprintf(buf + used - 1, sizeof(buf) - used, data);
|
||||
|
||||
//fprintf(stderr, "[%s] %s:%d %s() %s", LEVEL_NAMES[level], fp, line, func, data);
|
||||
fprintf(stderr, buf);
|
||||
free(data);
|
||||
}
|
||||
|
||||
|
@ -121,3 +151,8 @@ KS_DECLARE(void) ks_global_set_default_logger(int level)
|
|||
ks_log = default_logger;
|
||||
ks_log_level = level;
|
||||
}
|
||||
|
||||
KS_DECLARE(void) ks_global_set_default_logger_prefix(ks_log_prefix_t prefix)
|
||||
{
|
||||
ks_log_prefix = prefix;
|
||||
}
|
||||
|
|
|
@ -39,6 +39,17 @@ KS_DECLARE(ks_thread_os_handle_t) ks_thread_self(void)
|
|||
#endif
|
||||
}
|
||||
|
||||
KS_DECLARE(ks_pid_t) ks_thread_self_id(void)
|
||||
{
|
||||
#ifdef WIN32
|
||||
return GetCurrentThreadId();
|
||||
#elseif gettid
|
||||
return gettid();
|
||||
#else
|
||||
return syscall(SYS_gettid);
|
||||
#endif
|
||||
}
|
||||
|
||||
static void ks_thread_init_priority(void)
|
||||
{
|
||||
#ifdef WIN32
|
||||
|
|
Loading…
Reference in New Issue