Fix old-style-declaration warnings
[babeltrace.git] / logging / log.c
index 6d09e56b57beb2c0ca44a3a9adf310be31f1fb84..8d166a3093054680b21317d007dcf1cf67a1a3c4 100644 (file)
@@ -5,6 +5,13 @@
  */
 
 #include <babeltrace/babeltrace-internal.h>
+#include <babeltrace/common-internal.h>
+#include <pthread.h>
+#include <assert.h>
+
+#ifdef __CYGWIN__
+extern unsigned long pthread_getsequence_np(pthread_t *);
+#endif
 
 /* When defined, Android log (android/log.h) will be used by default instead of
  * stderr (ignored on non-Android platforms). Date, time, pid and tid (context)
 #else
        #define BT_LOG_OPTIMIZE_SIZE 0
 #endif
-/* Size of the log line buffer. The buffer is allocated on stack. It limits
- * maximum length of a log line.
+/* Size of the log line buffer. The buffer is a globally allocated per thread.
  */
 #ifndef BT_LOG_BUF_SZ
-       #define BT_LOG_BUF_SZ 512
+       #define BT_LOG_BUF_SZ (4 * 4096)
 #endif
 /* Default number of bytes in one line of memory output. For large values
  * BT_LOG_BUF_SZ also must be increased.
                #undef __STRICT_ANSI__
        #endif
 #endif
-#include <assert.h>
+#include <babeltrace/assert-internal.h>
 #include <ctype.h>
 #include <string.h>
 #include <time.h>
 #include <stddef.h>
 #include <stdlib.h>
 #include <stdio.h>
+
+#define BT_LOG_OUTPUT_LEVEL dummy
+
 #include <babeltrace/logging-internal.h>
 #include <babeltrace/logging.h>
 
        #include <sys/time.h>
        #if defined(__linux__)
                #include <linux/limits.h>
+       #elif (defined(__sun__) || defined(__CYGWIN__))
+               /* Solaris and Cygwin have no sys/syslimits.h */
        #else
                #include <sys/syslimits.h>
        #endif
        #define memccpy _memccpy
 #endif
 
-#if (defined(_MSC_VER) && !defined(__INTEL_COMPILER)) || defined(__MINGW64__)
+#if (defined(_MSC_VER) && !defined(__INTEL_COMPILER)) || \
+               (defined(__MINGW64__) && !defined(__USE_MINGW_ANSI_STDIO))
        #define vsnprintf(s, sz, fmt, va) fake_vsnprintf(s, sz, fmt, va)
        static int fake_vsnprintf(char *s, size_t sz, const char *fmt, va_list ap)
        {
@@ -525,11 +537,10 @@ static void buffer_callback(bt_log_message *msg, char *buf);
 STATIC_ASSERT(eol_fits_eol_sz, sizeof(BT_LOG_EOL) <= BT_LOG_EOL_SZ);
 STATIC_ASSERT(eol_sz_greater_than_zero, 0 < BT_LOG_EOL_SZ);
 STATIC_ASSERT(eol_sz_less_than_buf_sz, BT_LOG_EOL_SZ < BT_LOG_BUF_SZ);
-#if !defined(_WIN32) && !defined(_WIN64)
-       STATIC_ASSERT(buf_sz_less_than_pipe_buf, BT_LOG_BUF_SZ <= PIPE_BUF);
-#endif
 static const char c_hex[] = "0123456789abcdef";
 
+static __thread char logging_buf[4 * 4096];
+
 static INSTRUMENTED_CONST unsigned g_buf_sz = BT_LOG_BUF_SZ - BT_LOG_EOL_SZ;
 static INSTRUMENTED_CONST time_cb g_time_cb = time_callback;
 static INSTRUMENTED_CONST pid_cb g_pid_cb = pid_callback;
@@ -673,6 +684,7 @@ static const bt_log_output out_stderr = {BT_LOG_OUT_STDERR};
        BT_LOG_DEFINE_GLOBAL_OUTPUT_LEVEL = 0;
 #endif
 
+BT_HIDDEN
 const bt_log_spec _bt_log_stderr_spec =
 {
        BT_LOG_GLOBAL_FORMAT,
@@ -731,7 +743,7 @@ static char lvl_char(const int lvl)
 #define TCACHE_FLUID (0x40000000 | 0x80000000)
 static unsigned g_tcache_mode = TCACHE_STALE;
 static struct timeval g_tcache_tv = {0, 0};
-static struct tm g_tcache_tm = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
+static struct tm g_tcache_tm = {0};
 
 static INLINE int tcache_get(const struct timeval *const tv, struct tm *const tm)
 {
@@ -819,6 +831,11 @@ static void pid_callback(int *const pid, int *const tid)
 #else
        #if defined(_WIN32) || defined(_WIN64)
        *tid = GetCurrentThreadId();
+       #elif defined(__CYGWIN__)
+       pthread_t thr = pthread_self();
+       *tid = (int)pthread_getsequence_np(&thr);
+       #elif defined(__sun__)
+       *tid = (int)pthread_self();
        #elif defined(__ANDROID__)
        *tid = gettid();
        #elif defined(__linux__)
@@ -889,7 +906,7 @@ static char *put_integer_r(unsigned v, const int sign,
                                                   const unsigned w, const char wc, char *const e)
 {
        static const char _signs[] = {'-', '0', '+'};
-       static const char *const signs = _signs + 1;
+       const char *const signs = _signs + 1;
        char *p = e;
        do { *--p = '0' + v % 10; } while (0 != (v /= 10));
        if (0 == sign) return put_padding_r(w, wc, p, e);
@@ -1245,11 +1262,36 @@ static void _bt_log_write_imp(
                const int lvl, const char *const tag, const char *const fmt, va_list va)
 {
        bt_log_message msg;
-       char buf[BT_LOG_BUF_SZ];
+       char *buf = logging_buf;
        const unsigned mask = log->output->mask;
        msg.lvl = lvl;
        msg.tag = tag;
        g_buffer_cb(&msg, buf);
+       const char *rst_color_p = bt_common_color_reset();
+       const char *rst_color_e = rst_color_p + strlen(rst_color_p);
+       const char *color_p = "";
+       const char *color_e = color_p;
+
+       switch (lvl) {
+       case BT_LOG_INFO:
+               color_p = bt_common_color_fg_blue();
+               color_e = color_p + strlen(color_p);
+               break;
+       case BT_LOG_WARN:
+               color_p = bt_common_color_fg_yellow();
+               color_e = color_p + strlen(color_p);
+               break;
+       case BT_LOG_ERROR:
+       case BT_LOG_FATAL:
+               color_p = bt_common_color_fg_red();
+               color_e = color_p + strlen(color_p);
+               break;
+       default:
+               break;
+       }
+
+       msg.p = put_stringn(color_p, color_e, msg.p, msg.e);
+
        if (BT_LOG_PUT_CTX & mask)
        {
                put_ctx(&msg);
@@ -1266,6 +1308,7 @@ static void _bt_log_write_imp(
        {
                put_msg(&msg, fmt, va);
        }
+       msg.p = put_stringn(rst_color_p, rst_color_e, msg.p, msg.e);
        log->output->callback(&msg, log->output->arg);
        if (0 != mem && BT_LOG_PUT_MSG & mask)
        {
This page took 0.025919 seconds and 4 git commands to generate.