Mercurial > hg > freeDiameter
annotate libfdproto/log.c @ 931:155d45d06530 1.1.6
Updated copyright information
author | Sebastien Decugis <sdecugis@freediameter.net> |
---|---|
date | Sun, 03 Mar 2013 09:15:36 +0100 |
parents | 5053f1abcf5d |
children | d95cd3ca9e8d |
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 * * |
928
5053f1abcf5d
Updated copyright information
Sebastien Decugis <sdecugis@freediameter.net>
parents:
909
diff
changeset
|
5 * Copyright (c) 2013, 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 | |
909
047f99b09632
Clean some externs for picky linkers (by Thomas)
Sebastien Decugis <sdecugis@freediameter.net>
parents:
905
diff
changeset
|
40 FILE * fd_g_debug_fstr; |
047f99b09632
Clean some externs for picky linkers (by Thomas)
Sebastien Decugis <sdecugis@freediameter.net>
parents:
905
diff
changeset
|
41 |
0 | 42 pthread_mutex_t fd_log_lock = PTHREAD_MUTEX_INITIALIZER; |
43 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
|
44 int fd_g_debug_lvl = INFO; |
0 | 45 |
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
|
46 /* 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
|
47 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
|
48 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
|
49 |
183
960764fbc68f
Fix compilation error
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
174
diff
changeset
|
50 /* 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
|
51 int fd_breaks = 0; |
960764fbc68f
Fix compilation error
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
174
diff
changeset
|
52 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
|
53 |
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
|
54 /* 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
|
55 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
|
56 |
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 /* 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
|
58 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
|
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 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
|
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 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
|
63 { |
905 | 64 return EALREADY; /* only one registration allowed */ |
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
|
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 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
|
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 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
|
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 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
|
71 } |
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 |
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 /* 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
|
74 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
|
75 { |
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 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
|
77 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
|
78 } |
e87d083d0342
Added ability to register another log function; thanks to Zack for the code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
79 |
200
ff557d8b48d7
Fix deadlock in some circumstances
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
183
diff
changeset
|
80 static void fd_cleanup_mutex_silent( void * 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 (void)pthread_mutex_unlock((pthread_mutex_t *)mutex); |
ff557d8b48d7
Fix deadlock in some circumstances
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
183
diff
changeset
|
83 } |
ff557d8b48d7
Fix deadlock in some circumstances
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
183
diff
changeset
|
84 |
0 | 85 /* Log a debug message */ |
687
026802543f57
Prepare for new message log facility
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
662
diff
changeset
|
86 void fd_log_debug_fstr ( FILE * fstr, const char * format, ... ) |
0 | 87 { |
88 va_list ap; | |
89 | |
90 (void)pthread_mutex_lock(&fd_log_lock); | |
91 | |
200
ff557d8b48d7
Fix deadlock in some circumstances
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
183
diff
changeset
|
92 pthread_cleanup_push(fd_cleanup_mutex_silent, &fd_log_lock); |
0 | 93 |
94 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
|
95 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
|
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 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
|
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 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
|
100 { |
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 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
|
102 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
|
103 } |
0 | 104 va_end(ap); |
105 | |
106 pthread_cleanup_pop(0); | |
107 | |
108 (void)pthread_mutex_unlock(&fd_log_lock); | |
109 } | |
110 | |
111 /* Function to set the thread's friendly name */ | |
904
b839a1b1c1da
Make freeDiameter headers more C++ friendly -- thank you Thomas
Sebastien Decugis <sdecugis@freediameter.net>
parents:
788
diff
changeset
|
112 void fd_log_threadname ( const char * name ) |
0 | 113 { |
114 void * val = NULL; | |
115 | |
116 TRACE_ENTRY("%p(%s)", name, name?:"/"); | |
117 | |
118 /* First, check if a value is already assigned to the current thread */ | |
119 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
|
120 if (TRACE_BOOL(ANNOYING)) { |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
687
diff
changeset
|
121 if (val) { |
904
b839a1b1c1da
Make freeDiameter headers more C++ friendly -- thank you Thomas
Sebastien Decugis <sdecugis@freediameter.net>
parents:
788
diff
changeset
|
122 fd_log_debug("(Thread '%s' renamed to '%s')\n", (char *)val, name?:"(nil)"); |
706
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
687
diff
changeset
|
123 } else { |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
687
diff
changeset
|
124 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
|
125 } |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
687
diff
changeset
|
126 } |
0 | 127 if (val != NULL) { |
128 free(val); | |
129 } | |
130 | |
131 /* Now create the new string */ | |
132 if (name == NULL) { | |
133 CHECK_POSIX_DO( pthread_setspecific(fd_log_thname, NULL), /* continue */); | |
134 return; | |
135 } | |
136 | |
137 CHECK_MALLOC_DO( val = strdup(name), return ); | |
138 | |
139 CHECK_POSIX_DO( pthread_setspecific(fd_log_thname, val), /* continue */); | |
140 return; | |
141 } | |
142 | |
22
0b3b46da2c12
Progress on server code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
2
diff
changeset
|
143 /* Write time into a buffer */ |
0b3b46da2c12
Progress on server code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
2
diff
changeset
|
144 char * fd_log_time ( struct timespec * ts, char * buf, size_t len ) |
0 | 145 { |
146 int ret; | |
147 size_t offset = 0; | |
148 struct timespec tp; | |
149 struct tm tm; | |
150 | |
151 /* Get current time */ | |
22
0b3b46da2c12
Progress on server code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
2
diff
changeset
|
152 if (!ts) { |
0b3b46da2c12
Progress on server code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
2
diff
changeset
|
153 ret = clock_gettime(CLOCK_REALTIME, &tp); |
0b3b46da2c12
Progress on server code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
2
diff
changeset
|
154 if (ret != 0) { |
0b3b46da2c12
Progress on server code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
2
diff
changeset
|
155 snprintf(buf, len, "%s", strerror(ret)); |
0b3b46da2c12
Progress on server code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
2
diff
changeset
|
156 return buf; |
0b3b46da2c12
Progress on server code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
2
diff
changeset
|
157 } |
0b3b46da2c12
Progress on server code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
2
diff
changeset
|
158 ts = &tp; |
0 | 159 } |
160 | |
22
0b3b46da2c12
Progress on server code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
2
diff
changeset
|
161 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
|
162 offset += snprintf(buf + offset, len - offset, ".%6.6ld", ts->tv_nsec / 1000); |
0 | 163 |
164 return buf; | |
165 } |