]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
FS-9952: Added support to default ks_log system for including optional prefixes as...
authorShane Bryldt <astaelan@gmail.com>
Wed, 8 Mar 2017 19:51:54 +0000 (19:51 +0000)
committerMike Jerris <mike@jerris.com>
Wed, 22 Mar 2017 21:42:50 +0000 (17:42 -0400)
libs/libks/src/include/ks.h
libs/libks/src/include/ks_platform.h
libs/libks/src/include/ks_threadmutex.h
libs/libks/src/include/ks_types.h
libs/libks/src/ks_log.c
libs/libks/src/ks_thread.c

index f7b88d002d74722cf17595c4ec2f596b81e463bc..d5094f29a6a80fb49a2b1686d1e2633ee4c6aefa 100644 (file)
@@ -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);
index 741a904bd0b6fd01339c9f330216ad6746ecfbd5..0b6a04c1af65fbc62ab0eab256195dbcd43058d8 100644 (file)
@@ -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")
 
index ceac9e45e88c11bf53c70b20914bb1103ce6e8af..0739efe5dbda0e4a9517cccee5490690e6043d09 100644 (file)
@@ -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);
index 74d59c03a0a1ee159bdb1eefeab0e00bdb0e3256..f7e1aea07da4fd8443ff86505ee6e3561f5dcaa0 100644 (file)
@@ -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;
index a7c49f75ce6ccf99f5b5e648a51ea6d90e350f44..1d9f2a12eb96bc6ac65454ca8fb7359faa261ce8 100644 (file)
@@ -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;
+}
index 890d22a02e8b9781ec17bf63a2ee93e84d7ac129..adb8b2163a47bb6b5c269bacb93076f61fddffff 100644 (file)
@@ -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