Mercurial > hg > freeDiameter
annotate libfdproto/log.c @ 990:efb4e4ce2a6f
Include level string even with color output to ease copy/paste
author | Sebastien Decugis <sdecugis@freediameter.net> |
---|---|
date | Sun, 17 Mar 2013 14:52:20 +0100 |
parents | 46c675a28e40 |
children | 0117a7746b21 |
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 |
941
64088de91f7f
Initial logging rework by Thomas
Sebastien Decugis <sdecugis@freediameter.net>
parents:
909
diff
changeset
|
46 static void fd_internal_logger( int, const char *, va_list ); |
966
8862d9dece66
Use color output on consoles
Sebastien Decugis <sdecugis@freediameter.net>
parents:
965
diff
changeset
|
47 static int use_colors = 0; /* 0: not init, 1: yes, 2: no */ |
941
64088de91f7f
Initial logging rework by Thomas
Sebastien Decugis <sdecugis@freediameter.net>
parents:
909
diff
changeset
|
48 |
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
|
49 /* 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
|
50 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
|
51 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
|
52 |
183
960764fbc68f
Fix compilation error
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
174
diff
changeset
|
53 /* 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
|
54 int fd_breaks = 0; |
960764fbc68f
Fix compilation error
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
174
diff
changeset
|
55 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
|
56 |
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
|
57 /* Allow passing of the log and debug information from base stack to extensions */ |
941
64088de91f7f
Initial logging rework by Thomas
Sebastien Decugis <sdecugis@freediameter.net>
parents:
909
diff
changeset
|
58 void (*fd_logger)( int loglevel, const char * format, va_list args ) = fd_internal_logger; |
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
|
59 |
941
64088de91f7f
Initial logging rework by Thomas
Sebastien Decugis <sdecugis@freediameter.net>
parents:
909
diff
changeset
|
60 /* Register an external call back for tracing and debug */ |
64088de91f7f
Initial logging rework by Thomas
Sebastien Decugis <sdecugis@freediameter.net>
parents:
909
diff
changeset
|
61 int fd_log_handler_register( void (*logger)(int loglevel, const char * format, va_list args) ) |
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
|
62 { |
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 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
|
64 |
941
64088de91f7f
Initial logging rework by Thomas
Sebastien Decugis <sdecugis@freediameter.net>
parents:
909
diff
changeset
|
65 if ( fd_logger != fd_internal_logger ) |
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
|
66 { |
905 | 67 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
|
68 } |
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 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
|
70 { |
941
64088de91f7f
Initial logging rework by Thomas
Sebastien Decugis <sdecugis@freediameter.net>
parents:
909
diff
changeset
|
71 fd_logger = logger; |
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
|
72 } |
941
64088de91f7f
Initial logging rework by Thomas
Sebastien Decugis <sdecugis@freediameter.net>
parents:
909
diff
changeset
|
73 |
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
|
74 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
|
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 |
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 /* 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
|
78 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
|
79 { |
941
64088de91f7f
Initial logging rework by Thomas
Sebastien Decugis <sdecugis@freediameter.net>
parents:
909
diff
changeset
|
80 fd_logger = fd_internal_logger; |
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
|
81 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
|
82 } |
e87d083d0342
Added ability to register another log function; thanks to Zack for the code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
740
diff
changeset
|
83 |
200
ff557d8b48d7
Fix deadlock in some circumstances
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
183
diff
changeset
|
84 static void fd_cleanup_mutex_silent( void * mutex ) |
ff557d8b48d7
Fix deadlock in some circumstances
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
183
diff
changeset
|
85 { |
ff557d8b48d7
Fix deadlock in some circumstances
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
183
diff
changeset
|
86 (void)pthread_mutex_unlock((pthread_mutex_t *)mutex); |
ff557d8b48d7
Fix deadlock in some circumstances
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
183
diff
changeset
|
87 } |
ff557d8b48d7
Fix deadlock in some circumstances
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
183
diff
changeset
|
88 |
941
64088de91f7f
Initial logging rework by Thomas
Sebastien Decugis <sdecugis@freediameter.net>
parents:
909
diff
changeset
|
89 |
64088de91f7f
Initial logging rework by Thomas
Sebastien Decugis <sdecugis@freediameter.net>
parents:
909
diff
changeset
|
90 static void fd_internal_logger( int loglevel, const char *format, va_list ap ) |
64088de91f7f
Initial logging rework by Thomas
Sebastien Decugis <sdecugis@freediameter.net>
parents:
909
diff
changeset
|
91 { |
945
284608b307ee
Add timestamp to default logger.
Thomas Klausner <tk@giga.or.at>
parents:
941
diff
changeset
|
92 char buf[25]; |
941
64088de91f7f
Initial logging rework by Thomas
Sebastien Decugis <sdecugis@freediameter.net>
parents:
909
diff
changeset
|
93 FILE *fstr = fd_g_debug_fstr ?: stdout; |
966
8862d9dece66
Use color output on consoles
Sebastien Decugis <sdecugis@freediameter.net>
parents:
965
diff
changeset
|
94 int local_use_color = 0; |
941
64088de91f7f
Initial logging rework by Thomas
Sebastien Decugis <sdecugis@freediameter.net>
parents:
909
diff
changeset
|
95 |
64088de91f7f
Initial logging rework by Thomas
Sebastien Decugis <sdecugis@freediameter.net>
parents:
909
diff
changeset
|
96 /* logging has been decided by macros outside already */ |
945
284608b307ee
Add timestamp to default logger.
Thomas Klausner <tk@giga.or.at>
parents:
941
diff
changeset
|
97 |
284608b307ee
Add timestamp to default logger.
Thomas Klausner <tk@giga.or.at>
parents:
941
diff
changeset
|
98 /* add timestamp */ |
966
8862d9dece66
Use color output on consoles
Sebastien Decugis <sdecugis@freediameter.net>
parents:
965
diff
changeset
|
99 fprintf(fstr, "%s ", fd_log_time(NULL, buf, sizeof(buf))); |
8862d9dece66
Use color output on consoles
Sebastien Decugis <sdecugis@freediameter.net>
parents:
965
diff
changeset
|
100 |
8862d9dece66
Use color output on consoles
Sebastien Decugis <sdecugis@freediameter.net>
parents:
965
diff
changeset
|
101 /* Use colors on stdout ? */ |
8862d9dece66
Use color output on consoles
Sebastien Decugis <sdecugis@freediameter.net>
parents:
965
diff
changeset
|
102 if (!use_colors) { |
8862d9dece66
Use color output on consoles
Sebastien Decugis <sdecugis@freediameter.net>
parents:
965
diff
changeset
|
103 if (isatty(STDOUT_FILENO)) |
8862d9dece66
Use color output on consoles
Sebastien Decugis <sdecugis@freediameter.net>
parents:
965
diff
changeset
|
104 use_colors = 1; |
8862d9dece66
Use color output on consoles
Sebastien Decugis <sdecugis@freediameter.net>
parents:
965
diff
changeset
|
105 else |
8862d9dece66
Use color output on consoles
Sebastien Decugis <sdecugis@freediameter.net>
parents:
965
diff
changeset
|
106 use_colors = 2; |
8862d9dece66
Use color output on consoles
Sebastien Decugis <sdecugis@freediameter.net>
parents:
965
diff
changeset
|
107 } |
8862d9dece66
Use color output on consoles
Sebastien Decugis <sdecugis@freediameter.net>
parents:
965
diff
changeset
|
108 |
8862d9dece66
Use color output on consoles
Sebastien Decugis <sdecugis@freediameter.net>
parents:
965
diff
changeset
|
109 /* now, this time log, do we use colors? */ |
8862d9dece66
Use color output on consoles
Sebastien Decugis <sdecugis@freediameter.net>
parents:
965
diff
changeset
|
110 if ((fstr == stdout) && (use_colors == 1)) |
8862d9dece66
Use color output on consoles
Sebastien Decugis <sdecugis@freediameter.net>
parents:
965
diff
changeset
|
111 local_use_color = 1; |
8862d9dece66
Use color output on consoles
Sebastien Decugis <sdecugis@freediameter.net>
parents:
965
diff
changeset
|
112 |
965
9b37f34c1b1f
Some modifications to logging code:
Sebastien Decugis <sdecugis@freediameter.net>
parents:
961
diff
changeset
|
113 switch(loglevel) { |
990
efb4e4ce2a6f
Include level string even with color output to ease copy/paste
Sebastien Decugis <sdecugis@freediameter.net>
parents:
986
diff
changeset
|
114 case FD_LOG_DEBUG: fprintf(fstr, "%s DBG ", local_use_color ? "\e[0;37m" : ""); break; |
efb4e4ce2a6f
Include level string even with color output to ease copy/paste
Sebastien Decugis <sdecugis@freediameter.net>
parents:
986
diff
changeset
|
115 case FD_LOG_NOTICE: fprintf(fstr, "%sNOTI ", local_use_color ? "\e[1;37m" : ""); break; |
efb4e4ce2a6f
Include level string even with color output to ease copy/paste
Sebastien Decugis <sdecugis@freediameter.net>
parents:
986
diff
changeset
|
116 case FD_LOG_ERROR: fprintf(fstr, "%sERROR ", local_use_color ? "\e[0;31m" : ""); break; |
efb4e4ce2a6f
Include level string even with color output to ease copy/paste
Sebastien Decugis <sdecugis@freediameter.net>
parents:
986
diff
changeset
|
117 default: fprintf(fstr, "%s ??? ", local_use_color ? "\e[0;31m" : ""); |
965
9b37f34c1b1f
Some modifications to logging code:
Sebastien Decugis <sdecugis@freediameter.net>
parents:
961
diff
changeset
|
118 } |
966
8862d9dece66
Use color output on consoles
Sebastien Decugis <sdecugis@freediameter.net>
parents:
965
diff
changeset
|
119 vfprintf(fstr, format, ap); |
8862d9dece66
Use color output on consoles
Sebastien Decugis <sdecugis@freediameter.net>
parents:
965
diff
changeset
|
120 if (local_use_color) |
8862d9dece66
Use color output on consoles
Sebastien Decugis <sdecugis@freediameter.net>
parents:
965
diff
changeset
|
121 fprintf(fstr, "\e[00m"); |
986
46c675a28e40
Send \n after last control data
Sebastien Decugis <sdecugis@freediameter.net>
parents:
980
diff
changeset
|
122 fprintf(fstr, "\n"); |
966
8862d9dece66
Use color output on consoles
Sebastien Decugis <sdecugis@freediameter.net>
parents:
965
diff
changeset
|
123 |
8862d9dece66
Use color output on consoles
Sebastien Decugis <sdecugis@freediameter.net>
parents:
965
diff
changeset
|
124 fflush(fstr); |
941
64088de91f7f
Initial logging rework by Thomas
Sebastien Decugis <sdecugis@freediameter.net>
parents:
909
diff
changeset
|
125 } |
64088de91f7f
Initial logging rework by Thomas
Sebastien Decugis <sdecugis@freediameter.net>
parents:
909
diff
changeset
|
126 |
0 | 127 /* Log a debug message */ |
941
64088de91f7f
Initial logging rework by Thomas
Sebastien Decugis <sdecugis@freediameter.net>
parents:
909
diff
changeset
|
128 void fd_log ( int loglevel, const char * format, ... ) |
0 | 129 { |
130 va_list ap; | |
131 | |
132 (void)pthread_mutex_lock(&fd_log_lock); | |
133 | |
200
ff557d8b48d7
Fix deadlock in some circumstances
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
183
diff
changeset
|
134 pthread_cleanup_push(fd_cleanup_mutex_silent, &fd_log_lock); |
0 | 135 |
136 va_start(ap, format); | |
941
64088de91f7f
Initial logging rework by Thomas
Sebastien Decugis <sdecugis@freediameter.net>
parents:
909
diff
changeset
|
137 fd_logger(loglevel, format, ap); |
0 | 138 va_end(ap); |
139 | |
140 pthread_cleanup_pop(0); | |
141 | |
142 (void)pthread_mutex_unlock(&fd_log_lock); | |
143 } | |
144 | |
941
64088de91f7f
Initial logging rework by Thomas
Sebastien Decugis <sdecugis@freediameter.net>
parents:
909
diff
changeset
|
145 /* Log debug message to file. */ |
64088de91f7f
Initial logging rework by Thomas
Sebastien Decugis <sdecugis@freediameter.net>
parents:
909
diff
changeset
|
146 void fd_log_debug_fstr( FILE * fstr, const char * format, ... ) |
64088de91f7f
Initial logging rework by Thomas
Sebastien Decugis <sdecugis@freediameter.net>
parents:
909
diff
changeset
|
147 { |
64088de91f7f
Initial logging rework by Thomas
Sebastien Decugis <sdecugis@freediameter.net>
parents:
909
diff
changeset
|
148 va_list ap; |
64088de91f7f
Initial logging rework by Thomas
Sebastien Decugis <sdecugis@freediameter.net>
parents:
909
diff
changeset
|
149 |
64088de91f7f
Initial logging rework by Thomas
Sebastien Decugis <sdecugis@freediameter.net>
parents:
909
diff
changeset
|
150 va_start(ap, format); |
64088de91f7f
Initial logging rework by Thomas
Sebastien Decugis <sdecugis@freediameter.net>
parents:
909
diff
changeset
|
151 vfprintf(fstr, format, ap); |
64088de91f7f
Initial logging rework by Thomas
Sebastien Decugis <sdecugis@freediameter.net>
parents:
909
diff
changeset
|
152 va_end(ap); |
64088de91f7f
Initial logging rework by Thomas
Sebastien Decugis <sdecugis@freediameter.net>
parents:
909
diff
changeset
|
153 } |
64088de91f7f
Initial logging rework by Thomas
Sebastien Decugis <sdecugis@freediameter.net>
parents:
909
diff
changeset
|
154 |
0 | 155 /* 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
|
156 void fd_log_threadname ( const char * name ) |
0 | 157 { |
158 void * val = NULL; | |
159 | |
160 TRACE_ENTRY("%p(%s)", name, name?:"/"); | |
161 | |
162 /* First, check if a value is already assigned to the current thread */ | |
163 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
|
164 if (TRACE_BOOL(ANNOYING)) { |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
687
diff
changeset
|
165 if (val) { |
974
2091bf698fb1
Remove newlines from fd_log_debug, TRACE_DEBUG, TRACE_ERROR, and TRACE_DEBUG_ERROR
Thomas Klausner <tk@giga.or.at>
parents:
966
diff
changeset
|
166 fd_log_debug("(Thread '%s' renamed to '%s')", (char *)val, name?:"(nil)"); |
706
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
687
diff
changeset
|
167 } else { |
974
2091bf698fb1
Remove newlines from fd_log_debug, TRACE_DEBUG, TRACE_ERROR, and TRACE_DEBUG_ERROR
Thomas Klausner <tk@giga.or.at>
parents:
966
diff
changeset
|
168 fd_log_debug("(Thread %p named '%s')", pthread_self(), name?:"(nil)"); |
706
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
687
diff
changeset
|
169 } |
4ffbc9f1e922
Large UNTESTED commit with the following changes:
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
687
diff
changeset
|
170 } |
0 | 171 if (val != NULL) { |
172 free(val); | |
173 } | |
174 | |
175 /* Now create the new string */ | |
176 if (name == NULL) { | |
177 CHECK_POSIX_DO( pthread_setspecific(fd_log_thname, NULL), /* continue */); | |
178 return; | |
179 } | |
180 | |
181 CHECK_MALLOC_DO( val = strdup(name), return ); | |
182 | |
183 CHECK_POSIX_DO( pthread_setspecific(fd_log_thname, val), /* continue */); | |
184 return; | |
185 } | |
186 | |
22
0b3b46da2c12
Progress on server code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
2
diff
changeset
|
187 /* Write time into a buffer */ |
0b3b46da2c12
Progress on server code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
2
diff
changeset
|
188 char * fd_log_time ( struct timespec * ts, char * buf, size_t len ) |
0 | 189 { |
190 int ret; | |
191 size_t offset = 0; | |
192 struct timespec tp; | |
193 struct tm tm; | |
194 | |
195 /* Get current time */ | |
22
0b3b46da2c12
Progress on server code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
2
diff
changeset
|
196 if (!ts) { |
0b3b46da2c12
Progress on server code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
2
diff
changeset
|
197 ret = clock_gettime(CLOCK_REALTIME, &tp); |
0b3b46da2c12
Progress on server code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
2
diff
changeset
|
198 if (ret != 0) { |
0b3b46da2c12
Progress on server code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
2
diff
changeset
|
199 snprintf(buf, len, "%s", strerror(ret)); |
0b3b46da2c12
Progress on server code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
2
diff
changeset
|
200 return buf; |
0b3b46da2c12
Progress on server code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
2
diff
changeset
|
201 } |
0b3b46da2c12
Progress on server code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
2
diff
changeset
|
202 ts = &tp; |
0 | 203 } |
204 | |
22
0b3b46da2c12
Progress on server code
Sebastien Decugis <sdecugis@nict.go.jp>
parents:
2
diff
changeset
|
205 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
|
206 offset += snprintf(buf + offset, len - offset, ".%6.6ld", ts->tv_nsec / 1000); |
0 | 207 |
208 return buf; | |
209 } |