From 6f2f82e34ee173a0626432b57ebcbb8375e83a68 Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Fri, 16 Apr 2010 15:04:18 +0200 Subject: [PATCH] yaz_log: Log level tid logs thread ID Add new utility yaz_thread_id_cstr to return human-readable ID of current thread. At this stage pthread_self is used. This can be misleading because pthread_t (pointers presumably) are reused between thread creations. --- include/yaz/Makefile.am | 2 +- include/yaz/log.h | 2 ++ include/yaz/thread_id.h | 59 +++++++++++++++++++++++++++++++++++++++++++++++ src/Makefile.am | 2 +- src/log.c | 17 ++++++++++++-- src/thread_id.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 137 insertions(+), 4 deletions(-) create mode 100644 include/yaz/thread_id.h create mode 100644 src/thread_id.c diff --git a/include/yaz/Makefile.am b/include/yaz/Makefile.am index c2ec308..843dd22 100644 --- a/include/yaz/Makefile.am +++ b/include/yaz/Makefile.am @@ -20,7 +20,7 @@ pkginclude_HEADERS= backend.h ccl.h ccl_xml.h cql.h rpn2cql.h comstack.h \ z-univ.h z-oclcui.h zes-expi.h zes-exps.h zes-order.h zes-pquery.h \ zes-psched.h zes-admin.h zes-pset.h zes-update.h zes-update0.h \ zoom.h z-charneg.h charneg.h soap.h srw.h zgdu.h matchstr.h json.h \ - file_glob.h dirent.h + file_glob.h dirent.h thread_id.h EXTRA_DIST = yaz-version.h.in diff --git a/include/yaz/log.h b/include/yaz/log.h index b7cfad6..ddcbb85 100644 --- a/include/yaz/log.h +++ b/include/yaz/log.h @@ -48,6 +48,8 @@ YAZ_BEGIN_CDECL #define YLOG_LOG 0x00000008 /** \brief log level: append system error message */ #define YLOG_ERRNO 0x00000010 +/** \brief log level: append thread Id */ +#define YLOG_TID 0x00000020 /** \brief log level: application */ #define YLOG_APP 0x00000040 /** \brief log level: malloc debug */ diff --git a/include/yaz/thread_id.h b/include/yaz/thread_id.h new file mode 100644 index 0000000..608ed14 --- /dev/null +++ b/include/yaz/thread_id.h @@ -0,0 +1,59 @@ +/* This file is part of the YAZ toolkit. + * Copyright (C) 1995-2010 Index Data. + * 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 Index Data nor the names of its contributors + * may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** + * \file mutex.h + * \brief Header for Mutex functions + */ +#ifndef YAZ_THREAD_ID_H +#define YAZ_THREAD_ID_H + +#include +#include + +YAZ_BEGIN_CDECL + +/** \brief format current thread as printable C-string + \param buf buffer for string + \param buf_max maximum number of bytes (including trailing \0) + */ + +YAZ_EXPORT +void yaz_thread_id_cstr(char *buf, size_t buf_max); + +YAZ_END_CDECL + +#endif +/* + * Local variables: + * c-basic-offset: 4 + * c-file-style: "Stroustrup" + * indent-tabs-mode: nil + * End: + * vim: shiftwidth=4 tabstop=8 expandtab + */ + diff --git a/src/Makefile.am b/src/Makefile.am index e43e84a..e35f300 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -102,7 +102,7 @@ libyaz_la_SOURCES=version.c options.c log.c \ copy_types.c match_glob.c poll.c daemon.c \ iconv_encode_marc8.c iconv_encode_iso_8859_1.c iconv_encode_wchar.c \ iconv_decode_marc8.c iconv_decode_iso5426.c iconv_decode_danmarc.c sc.c \ - json.c xml_include.c file_glob.c dirent.c mutex.c + json.c xml_include.c file_glob.c dirent.c mutex.c thread_id.c libyaz_la_LDFLAGS=-version-info $(YAZ_VERSION_INFO) diff --git a/src/log.c b/src/log.c index 6b82bcd..b001c31 100644 --- a/src/log.c +++ b/src/log.c @@ -28,6 +28,7 @@ #include #include #include +#include #include #include #include @@ -55,6 +56,7 @@ static char l_fname[512] = ""; static char l_old_default_format[] = "%H:%M:%S-%d/%m"; static char l_new_default_format[] = "%Y%m%d-%H%M%S"; #define TIMEFORMAT_LEN 50 +#define TID_LEN 30 static char l_custom_format[TIMEFORMAT_LEN] = ""; static char *l_actual_format = l_old_default_format; @@ -79,7 +81,8 @@ static struct { { YLOG_LOG, "log" }, { YLOG_ERRNO, ""}, { YLOG_MALLOC, "malloc"}, - { YLOG_APP, "app" }, + { YLOG_TID, "tid" }, + { YLOG_APP, "app" }, { YLOG_NOTIME, "notime" }, { YLOG_APP2, "app2" }, { YLOG_APP3, "app3" }, @@ -382,6 +385,7 @@ static void yaz_log_to_file(int level, const char *log_message) if (file) { char tbuf[TIMEFORMAT_LEN]; + char tid[TID_LEN]; char flags[1024]; int i; @@ -411,7 +415,16 @@ static void yaz_log_to_file(int level, const char *log_message) } if (tbuf[0]) strcat(tbuf, " "); - fprintf(file, "%s%s%s %s%s\n", tbuf, l_prefix, flags, l_prefix2, + tid[0] = '\0'; + + if (l_level & YLOG_TID) + { + yaz_thread_id_cstr(tid, sizeof(tid)-1); + if (tid[0]) + strcat(tid, " "); + } + + fprintf(file, "%s%s%s%s %s%s\n", tbuf, l_prefix, tid, flags, l_prefix2, log_message); if (l_level & YLOG_FLUSH) fflush(file); diff --git a/src/thread_id.c b/src/thread_id.c new file mode 100644 index 0000000..e3f9b3e --- /dev/null +++ b/src/thread_id.c @@ -0,0 +1,59 @@ +/* This file is part of the YAZ toolkit. + * Copyright (C) 1995-2010 Index Data + * See the file LICENSE for details. + */ + +/** + * \file thraedid.c + * \brief Returns printable thread ID + * + */ +#if HAVE_CONFIG_H +#include +#endif + +#include +#include + +#ifdef WIN32 +#include +#endif + +#if HAVE_SYS_TIME_H +#include +#endif + +#if YAZ_POSIX_THREADS +#include +#endif + +#include + +void yaz_thread_id_cstr(char *buf, size_t buf_max) +{ +#ifdef WIN32 + *buf = '\0'; +#elif YAZ_POSIX_THREADS + pthread_t t = pthread_self(); + size_t i; + *buf = '\0'; + for (i = 0; i < sizeof(t); i++) + { + if (strlen(buf) >= buf_max-2) + break; + sprintf(buf + strlen(buf), "%02x", ((const unsigned char *) &t)[i]); + } +#else + *buf = '\0'; +#endif +} + +/* + * Local variables: + * c-basic-offset: 4 + * c-file-style: "Stroustrup" + * indent-tabs-mode: nil + * End: + * vim: shiftwidth=4 tabstop=8 expandtab + */ + -- 1.7.10.4