Mercurial > hg > freeDiameter
annotate libfdproto/log.c @ 882:ae96ae28f3dd 1.1.5
Limit python version to avoid using python3 (not compatible)
author | Sebastien Decugis <sdecugis@freediameter.net> |
---|---|
date | Sat, 03 Nov 2012 18:45:41 +0100 |
parents | cb84f5be889d |
children | b839a1b1c1da |
rev | line source |
---|---|
0 | 1 /********************************************************************************************************* |
2 * Software License Agreement (BSD License) * | |
740
4a9f08d6b6ba
Updated my mail address
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
735
diff
changeset
|
3 * Author: Sebastien Decugis <sdecugis@freediameter.net> * |
0 | 4 * * |
788
cb84f5be889d
Updated copyright information
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
784
diff
changeset
|
5 * Copyright (c) 2012, WIDE Project and NICT * |
0 | 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 | |
658
f198d16fa7f4
Initial commit for 1.1.0:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
653
diff
changeset
|
36 #include "fdproto-internal.h" |
0 | 37 |
38 #include <stdarg.h> | |
39 | |
40 pthread_mutex_t fd_log_lock = PTHREAD_MUTEX_INITIALIZER; | |
41 pthread_key_t fd_log_thname; | |
735
da8bfb070900
Fix the logic of local + global debug level
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
706
diff
changeset
|
42 int fd_g_debug_lvl = INFO; |
0 | 43 |
174
2b2f78036749
Added simple command-line switch to turn on full debug for specific function or file
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
22
diff
changeset
|
44 /* These may be used to pass specific debug requests via the command-line parameters */ |
2b2f78036749
Added simple command-line switch to turn on full debug for specific function or file
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
22
diff
changeset
|
45 char * fd_debug_one_function = NULL; |
2b2f78036749
Added simple command-line switch to turn on full debug for specific function or file
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
22
diff
changeset
|
46 char * fd_debug_one_file = NULL; |
2b2f78036749
Added simple command-line switch to turn on full debug for specific function or file
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
22
diff
changeset
|
47 |
183
960764fbc68f
Fix compilation error
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
174
diff
changeset
|
48 /* Useless function, only to ease setting up a breakpoint in gdb (break fd_breakhere) -- use TRACE_HERE */ |
960764fbc68f
Fix compilation error
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
174
diff
changeset
|
49 int fd_breaks = 0; |
960764fbc68f
Fix compilation error
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
174
diff
changeset
|
50 int fd_breakhere(void) { return ++fd_breaks; } |
174
2b2f78036749
Added simple command-line switch to turn on full debug for specific function or file
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
22
diff
changeset
|
51 |
784
e87d083d0342
Added ability to register another log function; thanks to Zack for the code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
52 /* Allow passing of the log and debug information from base stack to extensions */ |
e87d083d0342
Added ability to register another log function; thanks to Zack for the code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
53 void (*fd_external_logger)( const char * format, va_list *args ) = NULL; |
e87d083d0342
Added ability to register another log function; thanks to Zack for the code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
54 |
e87d083d0342
Added ability to register another log function; thanks to Zack for the code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
55 /* Register an dexternal call back for tracing and debug */ |
e87d083d0342
Added ability to register another log function; thanks to Zack for the code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
56 int fd_log_handler_register( void (*logger)(const char * format, va_list *args)) |
e87d083d0342
Added ability to register another log function; thanks to Zack for the code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
57 { |
e87d083d0342
Added ability to register another log function; thanks to Zack for the code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
58 CHECK_PARAMS( logger ); |
e87d083d0342
Added ability to register another log function; thanks to Zack for the code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
59 |
e87d083d0342
Added ability to register another log function; thanks to Zack for the code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
60 if ( fd_external_logger != NULL ) |
e87d083d0342
Added ability to register another log function; thanks to Zack for the code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
61 { |
e87d083d0342
Added ability to register another log function; thanks to Zack for the code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
62 return EALREADY; /* only one registeration allowed */ |
e87d083d0342
Added ability to register another log function; thanks to Zack for the code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
63 } |
e87d083d0342
Added ability to register another log function; thanks to Zack for the code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
64 else |
e87d083d0342
Added ability to register another log function; thanks to Zack for the code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
65 { |
e87d083d0342
Added ability to register another log function; thanks to Zack for the code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
66 fd_external_logger = logger; |
e87d083d0342
Added ability to register another log function; thanks to Zack for the code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
67 } |
e87d083d0342
Added ability to register another log function; thanks to Zack for the code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
68 return 0; |
e87d083d0342
Added ability to register another log function; thanks to Zack for the code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
69 } |
e87d083d0342
Added ability to register another log function; thanks to Zack for the code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
70 |
e87d083d0342
Added ability to register another log function; thanks to Zack for the code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
71 /* Implement a simple reset function here */ |
e87d083d0342
Added ability to register another log function; thanks to Zack for the code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
72 int fd_log_handler_unregister ( void ) |
e87d083d0342
Added ability to register another log function; thanks to Zack for the code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
73 { |
e87d083d0342
Added ability to register another log function; thanks to Zack for the code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
74 fd_external_logger = NULL; |
e87d083d0342
Added ability to register another log function; thanks to Zack for the code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
75 return 0; /* Successfull in all cases. */ |
e87d083d0342
Added ability to register another log function; thanks to Zack for the code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
76 } |
e87d083d0342
Added ability to register another log function; thanks to Zack for the code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
77 |
200
ff557d8b48d7
Fix deadlock in some circumstances
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
183
diff
changeset
|
78 static void fd_cleanup_mutex_silent( void * mutex ) |
ff557d8b48d7
Fix deadlock in some circumstances
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
183
diff
changeset
|
79 { |
ff557d8b48d7
Fix deadlock in some circumstances
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
183
diff
changeset
|
80 (void)pthread_mutex_unlock((pthread_mutex_t *)mutex); |
ff557d8b48d7
Fix deadlock in some circumstances
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
183
diff
changeset
|
81 } |
ff557d8b48d7
Fix deadlock in some circumstances
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
183
diff
changeset
|
82 |
0 | 83 /* Log a debug message */ |
687
026802543f57
Prepare for new message log facility
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
662
diff
changeset
|
84 void fd_log_debug_fstr ( FILE * fstr, const char * format, ... ) |
0 | 85 { |
86 va_list ap; | |
87 | |
88 (void)pthread_mutex_lock(&fd_log_lock); | |
89 | |
200
ff557d8b48d7
Fix deadlock in some circumstances
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
183
diff
changeset
|
90 pthread_cleanup_push(fd_cleanup_mutex_silent, &fd_log_lock); |
0 | 91 |
92 va_start(ap, format); | |
784
e87d083d0342
Added ability to register another log function; thanks to Zack for the code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
93 if ( fd_external_logger != NULL ) |
e87d083d0342
Added ability to register another log function; thanks to Zack for the code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
94 { |
e87d083d0342
Added ability to register another log function; thanks to Zack for the code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
95 fd_external_logger( format, &ap ); |
e87d083d0342
Added ability to register another log function; thanks to Zack for the code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
96 } |
e87d083d0342
Added ability to register another log function; thanks to Zack for the code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
97 else |
e87d083d0342
Added ability to register another log function; thanks to Zack for the code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
98 { |
e87d083d0342
Added ability to register another log function; thanks to Zack for the code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
99 vfprintf( fstr ?: stdout, format, ap); |
e87d083d0342
Added ability to register another log function; thanks to Zack for the code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
100 fflush(fstr ?: stdout); |
e87d083d0342
Added ability to register another log function; thanks to Zack for the code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
101 } |
0 | 102 va_end(ap); |
103 | |
104 pthread_cleanup_pop(0); | |
105 | |
106 (void)pthread_mutex_unlock(&fd_log_lock); | |
107 } | |
108 | |
109 /* Function to set the thread's friendly name */ | |
110 void fd_log_threadname ( char * name ) | |
111 { | |
112 void * val = NULL; | |
113 | |
114 TRACE_ENTRY("%p(%s)", name, name?:"/"); | |
115 | |
116 /* First, check if a value is already assigned to the current thread */ | |
117 val = pthread_getspecific(fd_log_thname); | |
706
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
687
diff
changeset
|
118 if (TRACE_BOOL(ANNOYING)) { |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
687
diff
changeset
|
119 if (val) { |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
687
diff
changeset
|
120 fd_log_debug("(Thread '%s' renamed to '%s')\n", (char *)val, name); |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
687
diff
changeset
|
121 } else { |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
687
diff
changeset
|
122 fd_log_debug("(Thread %p named '%s')\n", pthread_self(), name?:"(nil)"); |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
687
diff
changeset
|
123 } |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
687
diff
changeset
|
124 } |
0 | 125 if (val != NULL) { |
126 free(val); | |
127 } | |
128 | |
129 /* Now create the new string */ | |
130 if (name == NULL) { | |
131 CHECK_POSIX_DO( pthread_setspecific(fd_log_thname, NULL), /* continue */); | |
132 return; | |
133 } | |
134 | |
135 CHECK_MALLOC_DO( val = strdup(name), return ); | |
136 | |
137 CHECK_POSIX_DO( pthread_setspecific(fd_log_thname, val), /* continue */); | |
138 return; | |
139 } | |
140 | |
22
0b3b46da2c12
Progress on server code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
2
diff
changeset
|
141 /* Write time into a buffer */ |
0b3b46da2c12
Progress on server code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
2
diff
changeset
|
142 char * fd_log_time ( struct timespec * ts, char * buf, size_t len ) |
0 | 143 { |
144 int ret; | |
145 size_t offset = 0; | |
146 struct timespec tp; | |
147 struct tm tm; | |
148 | |
149 /* Get current time */ | |
22
0b3b46da2c12
Progress on server code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
2
diff
changeset
|
150 if (!ts) { |
0b3b46da2c12
Progress on server code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
2
diff
changeset
|
151 ret = clock_gettime(CLOCK_REALTIME, &tp); |
0b3b46da2c12
Progress on server code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
2
diff
changeset
|
152 if (ret != 0) { |
0b3b46da2c12
Progress on server code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
2
diff
changeset
|
153 snprintf(buf, len, "%s", strerror(ret)); |
0b3b46da2c12
Progress on server code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
2
diff
changeset
|
154 return buf; |
0b3b46da2c12
Progress on server code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
2
diff
changeset
|
155 } |
0b3b46da2c12
Progress on server code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
2
diff
changeset
|
156 ts = &tp; |
0 | 157 } |
158 | |
22
0b3b46da2c12
Progress on server code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
2
diff
changeset
|
159 offset += strftime(buf + offset, len - offset, "%D,%T", localtime_r( &ts->tv_sec , &tm )); |
0b3b46da2c12
Progress on server code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
2
diff
changeset
|
160 offset += snprintf(buf + offset, len - offset, ".%6.6ld", ts->tv_nsec / 1000); |
0 | 161 |
162 return buf; | |
163 } |