1 | /********************************************************************************************************* |
---|
2 | * Software License Agreement (BSD License) * |
---|
3 | * Author: Sebastien Decugis <sdecugis@nict.go.jp> * |
---|
4 | * * |
---|
5 | * Copyright (c) 2009, WIDE Project and NICT * |
---|
6 | * All rights reserved. * |
---|
7 | * * |
---|
8 | * Redistribution and use of this software in source and binary forms, with or without modification, are * |
---|
9 | * permitted provided that the following conditions are met: * |
---|
10 | * * |
---|
11 | * * Redistributions of source code must retain the above * |
---|
12 | * copyright notice, this list of conditions and the * |
---|
13 | * following disclaimer. * |
---|
14 | * * |
---|
15 | * * Redistributions in binary form must reproduce the above * |
---|
16 | * copyright notice, this list of conditions and the * |
---|
17 | * following disclaimer in the documentation and/or other * |
---|
18 | * materials provided with the distribution. * |
---|
19 | * * |
---|
20 | * * Neither the name of the WIDE Project or NICT nor the * |
---|
21 | * names of its contributors may be used to endorse or * |
---|
22 | * promote products derived from this software without * |
---|
23 | * specific prior written permission of WIDE Project and * |
---|
24 | * NICT. * |
---|
25 | * * |
---|
26 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED * |
---|
27 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A * |
---|
28 | * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR * |
---|
29 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * |
---|
30 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * |
---|
31 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR * |
---|
32 | * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * |
---|
33 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * |
---|
34 | *********************************************************************************************************/ |
---|
35 | |
---|
36 | #include "libfD.h" |
---|
37 | |
---|
38 | #include <stdarg.h> |
---|
39 | |
---|
40 | pthread_mutex_t fd_log_lock = PTHREAD_MUTEX_INITIALIZER; |
---|
41 | pthread_key_t fd_log_thname; |
---|
42 | int fd_g_debug_lvl = 0; |
---|
43 | |
---|
44 | /* Log a debug message */ |
---|
45 | void fd_log_debug ( char * format, ... ) |
---|
46 | { |
---|
47 | va_list ap; |
---|
48 | |
---|
49 | (void)pthread_mutex_lock(&fd_log_lock); |
---|
50 | |
---|
51 | pthread_cleanup_push(fd_cleanup_mutex, &fd_log_lock); |
---|
52 | |
---|
53 | va_start(ap, format); |
---|
54 | vfprintf( stdout, format, ap); |
---|
55 | va_end(ap); |
---|
56 | fflush(stdout); |
---|
57 | |
---|
58 | pthread_cleanup_pop(0); |
---|
59 | |
---|
60 | (void)pthread_mutex_unlock(&fd_log_lock); |
---|
61 | } |
---|
62 | |
---|
63 | /* Function to set the thread's friendly name */ |
---|
64 | void fd_log_threadname ( char * name ) |
---|
65 | { |
---|
66 | void * val = NULL; |
---|
67 | |
---|
68 | TRACE_ENTRY("%p(%s)", name, name?:"/"); |
---|
69 | |
---|
70 | /* First, check if a value is already assigned to the current thread */ |
---|
71 | val = pthread_getspecific(fd_log_thname); |
---|
72 | if (val != NULL) { |
---|
73 | TRACE_DEBUG(FULL, "Freeing old thread name: %s", val); |
---|
74 | free(val); |
---|
75 | } |
---|
76 | |
---|
77 | /* Now create the new string */ |
---|
78 | if (name == NULL) { |
---|
79 | CHECK_POSIX_DO( pthread_setspecific(fd_log_thname, NULL), /* continue */); |
---|
80 | return; |
---|
81 | } |
---|
82 | |
---|
83 | CHECK_MALLOC_DO( val = strdup(name), return ); |
---|
84 | |
---|
85 | CHECK_POSIX_DO( pthread_setspecific(fd_log_thname, val), /* continue */); |
---|
86 | return; |
---|
87 | } |
---|
88 | |
---|
89 | /* Write current time into a buffer */ |
---|
90 | char * fd_log_time ( char * buf, size_t len ) |
---|
91 | { |
---|
92 | int ret; |
---|
93 | size_t offset = 0; |
---|
94 | struct timespec tp; |
---|
95 | struct tm tm; |
---|
96 | |
---|
97 | /* Get current time */ |
---|
98 | ret = clock_gettime(CLOCK_REALTIME, &tp); |
---|
99 | if (ret != 0) { |
---|
100 | snprintf(buf, len, "%s", strerror(ret)); |
---|
101 | return buf; |
---|
102 | } |
---|
103 | |
---|
104 | offset += strftime(buf + offset, len - offset, "%D,%T", localtime_r( &tp.tv_sec , &tm )); |
---|
105 | offset += snprintf(buf + offset, len - offset, ".%6.6ld", tp.tv_nsec / 1000); |
---|
106 | |
---|
107 | return buf; |
---|
108 | } |
---|