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);
/*
* 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
#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")
#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
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);
/*
* 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
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) \
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", ""
/* 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 {
#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;
/*
* 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
};
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)
{
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;
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);
}
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;
+}
#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