c84355f80c874948b6614d1cd08e7097063b9fc3
[mirror/winof/.git] / ulp / opensm / user / include / opensm / osm_log.h
1 /*
2  * Copyright (c) 2004, 2005 Voltaire, Inc. All rights reserved.
3  * Copyright (c) 2002-2005 Mellanox Technologies LTD. All rights reserved.
4  * Copyright (c) 1996-2003 Intel Corporation. All rights reserved.
5  *
6  * This software is available to you under the OpenIB.org BSD license
7  * below:
8  *
9  *     Redistribution and use in source and binary forms, with or
10  *     without modification, are permitted provided that the following
11  *     conditions are met:
12  *
13  *      - Redistributions of source code must retain the above
14  *        copyright notice, this list of conditions and the following
15  *        disclaimer.
16  *
17  *      - Redistributions in binary form must reproduce the above
18  *        copyright notice, this list of conditions and the following
19  *        disclaimer in the documentation and/or other materials
20  *        provided with the distribution.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
26  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
27  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
28  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29  * SOFTWARE.
30  *
31  * $Id$
32  */
33
34
35 /*
36  * Abstract:
37  *      Declaration of osm_log_t.
38  *      This object represents the log file.
39  *      This object is part of the OpenSM family of objects.
40  *
41  * Environment:
42  *      Linux User Mode
43  *
44  * $Revision: 1.6 $
45  */
46
47
48 #ifndef _OSM_LOG_H_
49 #define _OSM_LOG_H_
50
51 #ifndef __WIN__
52 #include <syslog.h>
53 #endif
54 #include <complib/cl_log.h>
55 #include <complib/cl_spinlock.h>
56 #include <opensm/osm_base.h>
57 #include <iba/ib_types.h>
58 #include <stdio.h>
59
60 #ifdef __cplusplus
61 #  define BEGIN_C_DECLS extern "C" {
62 #  define END_C_DECLS   }
63 #else /* !__cplusplus */
64 #  define BEGIN_C_DECLS
65 #  define END_C_DECLS
66 #endif /* __cplusplus */
67
68 BEGIN_C_DECLS
69
70 #ifndef PRIx64
71 #if __WORDSIZE == 64
72 #define __PRI64_PREFIX  "l"
73 #else
74 #define __PRI64_PREFIX  "L"
75 #endif
76
77 #define PRId64          __PRI64_PREFIX"d"
78 #define PRIo64          __PRI64_PREFIX"o"
79 #define PRIu64          __PRI64_PREFIX"u"
80 #define PRIx64          __PRI64_PREFIX"x"
81 #endif
82
83 #define LOG_ENTRY_SIZE_MAX              4096
84 #define BUF_SIZE                                LOG_ENTRY_SIZE_MAX
85
86 #define OSM_LOG_DEFINE_FUNC( NAME ) \
87         static const char osm_log_func_name[] = #NAME
88
89 #define OSM_LOG_ENTER( OSM_LOG_PTR, NAME ) \
90         OSM_LOG_DEFINE_FUNC( NAME ); \
91         osm_log( OSM_LOG_PTR, OSM_LOG_FUNCS, \
92                  "%s: [\n", osm_log_func_name );
93
94 #define OSM_LOG_EXIT( OSM_LOG_PTR ) \
95         osm_log( OSM_LOG_PTR, OSM_LOG_FUNCS, \
96                  "%s: ]\n", osm_log_func_name );
97
98 /****h* OpenSM/Log
99 * NAME
100 *       Log
101 *
102 * DESCRIPTION
103 *
104 * AUTHOR
105 *
106 *********/
107 typedef uint8_t osm_log_level_t;
108
109 #define OSM_LOG_NONE                       0x00
110 #define OSM_LOG_ERROR                   0x01
111 #define OSM_LOG_INFO                       0x02
112 #define OSM_LOG_VERBOSE                 0x04
113 #define OSM_LOG_DEBUG                   0x08
114 #define OSM_LOG_FUNCS                   0x10
115 #define OSM_LOG_FRAMES                  0x20
116 #define OSM_LOG_ROUTING                 0x40
117 #define OSM_LOG_SYS           0x80
118
119 /*
120         DEFAULT - turn on ERROR and INFO only
121 */
122 #define OSM_LOG_DEFAULT_LEVEL           OSM_LOG_ERROR | OSM_LOG_INFO
123
124 /****s* OpenSM: MAD Wrapper/osm_log_t
125 * NAME
126 *       osm_log_t
127 *
128 * DESCRIPTION
129 *
130 * SYNOPSIS
131 */
132 typedef struct _osm_log
133 {
134         osm_log_level_t                 level;
135         cl_spinlock_t                   lock;
136         boolean_t                               flush;
137     FILE*                out_port;
138 } osm_log_t;
139 /*********/
140
141
142 /****f* OpenSM: Log/osm_log_construct
143 * NAME
144 *       osm_log_construct
145 *
146 * DESCRIPTION
147 *       This function constructs a Log object.
148 *
149 * SYNOPSIS
150 */
151 static inline void
152 osm_log_construct(
153         IN osm_log_t* const p_log )
154 {
155         cl_spinlock_construct( &p_log->lock );
156 }
157 /*
158 * PARAMETERS
159 *       p_log
160 *               [in] Pointer to a Log object to construct.
161 *
162 * RETURN VALUE
163 *       This function does not return a value.
164 *
165 * NOTES
166 *       Allows calling osm_log_init, osm_log_destroy
167 *
168 *       Calling osm_log_construct is a prerequisite to calling any other
169 *       method except osm_log_init.
170 *
171 * SEE ALSO
172 *       Log object, osm_log_init,
173 *       osm_log_destroy
174 *********/
175
176 /****f* OpenSM: Log/osm_log_destroy
177 * NAME
178 *       osm_log_destroy
179 *
180 * DESCRIPTION
181 *       The osm_log_destroy function destroys the object, releasing
182 *       all resources.
183 *
184 * SYNOPSIS
185 */
186 static inline void
187 osm_log_destroy(
188         IN osm_log_t* const p_log )
189 {
190         cl_spinlock_destroy( &p_log->lock );
191         if (p_log->out_port != stdout) {
192           fclose(p_log->out_port);
193           p_log->out_port = stdout;
194         }
195    closelog();
196 }
197
198 /*
199 * PARAMETERS
200 *       p_log
201 *               [in] Pointer to the object to destroy.
202 *
203 * RETURN VALUE
204 *       This function does not return a value.
205 *
206 * NOTES
207 *       Performs any necessary cleanup of the specified
208 *       Log object.
209 *       Further operations should not be attempted on the destroyed object.
210 *       This function should only be called after a call to
211 *       osm_log_construct or osm_log_init.
212 *
213 * SEE ALSO
214 *       Log object, osm_log_construct,
215 *       osm_log_init
216 *********/
217
218 /****f* OpenSM: Log/osm_log_init
219 * NAME
220 *       osm_log_init
221 *
222 * DESCRIPTION
223 *       The osm_log_init function initializes a
224 *       Log object for use.
225 *
226 * SYNOPSIS
227 */
228 static inline ib_api_status_t
229 osm_log_init(
230   IN osm_log_t* const p_log,
231   IN const boolean_t flush,
232   IN const uint8_t log_flags,
233   IN const char *log_file)
234 {
235   p_log->level = log_flags;
236   p_log->flush = flush;
237
238   if (log_file == NULL)
239   {
240     p_log->out_port = stdout;
241   }
242   else
243   {
244     p_log->out_port = fopen(log_file,"a+");
245     if (!p_log->out_port)
246     {
247       printf("Cannot open %s for appending. Permission denied\n", log_file);
248       return(IB_UNKNOWN_ERROR);
249     }
250   }
251   openlog("OpenSM", LOG_CONS | LOG_PID, LOG_USER);
252
253   if (cl_spinlock_init( &p_log->lock ) == CL_SUCCESS) 
254     return IB_SUCCESS;
255   else
256     return IB_ERROR;
257 }
258 /*
259 * PARAMETERS
260 *       p_log
261 *               [in] Pointer to the log object.
262 *
263 *       flush
264 *               [in] Set to TRUE directs the log to flush all log messages
265 *               immediately.  This severely degrades log performance,
266 *               and is normally used for debugging only.
267 *
268 *  log_flags
269 *     [in] The log verbosity level to be used.
270 *
271 *  log_file
272 *     [in] if not NULL defines the name of the log file. Otherwise it is stdout.
273 *
274 * RETURN VALUES
275 *       CL_SUCCESS if the Log object was initialized
276 *       successfully.
277 *
278 * NOTES
279 *       Allows calling other Log methods.
280 *
281 * SEE ALSO
282 *       Log object, osm_log_construct,
283 *       osm_log_destroy
284 *********/
285
286 /****f* OpenSM: Log/osm_log_get_level
287 * NAME
288 *       osm_log_get_level
289 *
290 * DESCRIPTION
291 *       Returns the current log level.
292 *
293 * SYNOPSIS
294 */
295 static inline osm_log_level_t
296 osm_log_get_level(
297         IN const osm_log_t* const p_log )
298 {
299         return( p_log->level );
300 }
301 /*
302 * PARAMETERS
303 *       p_log
304 *               [in] Pointer to the log object.
305 *
306 * RETURN VALUES
307 *       Returns the current log level.
308 *
309 * NOTES
310 *
311 * SEE ALSO
312 *       Log object, osm_log_construct,
313 *       osm_log_destroy
314 *********/
315 /****f* OpenSM: Log/osm_log_set_level
316 * NAME
317 *       osm_log_set_level
318 *
319 * DESCRIPTION
320 *       Sets the current log level.
321 *
322 * SYNOPSIS
323 */
324 static inline void
325 osm_log_set_level(
326         IN osm_log_t* const p_log,
327         IN const osm_log_level_t level )
328 {
329         p_log->level = level;
330 }
331 /*
332 * PARAMETERS
333 *       p_log
334 *               [in] Pointer to the log object.
335 *
336 *       level
337 *               [in] New level to set.
338 *
339 * RETURN VALUES
340 *       Returns the current log level.
341 *
342 * NOTES
343 *
344 * SEE ALSO
345 *       Log object, osm_log_construct,
346 *       osm_log_destroy
347 *********/
348 /****f* OpenSM: Log/osm_log_is_active
349 * NAME
350 *       osm_log_is_active
351 *
352 * DESCRIPTION
353 *       Returns TRUE if the specified log level would be logged.
354 *       FALSE otherwise.
355 *
356 * SYNOPSIS
357 */
358 static inline boolean_t
359 osm_log_is_active(
360         IN const osm_log_t* const p_log,
361         IN const osm_log_level_t level )
362 {
363         return( (p_log->level & level) != 0 );
364 }
365 /*
366 * PARAMETERS
367 *       p_log
368 *               [in] Pointer to the log object.
369 *
370 *       level
371 *               [in] Level to check.
372 *
373 * RETURN VALUES
374 *       Returns TRUE if the specified log level would be logged.
375 *       FALSE otherwise.
376 *
377 * NOTES
378 *
379 * SEE ALSO
380 *       Log object, osm_log_construct,
381 *       osm_log_destroy
382 *********/
383
384
385 void
386 osm_log(
387         IN osm_log_t* const p_log,
388         IN const osm_log_level_t verbosity,
389         IN const char *p_str, ... );
390
391 void
392 osm_log_raw(
393         IN osm_log_t* const p_log,
394         IN const osm_log_level_t verbosity,
395         IN const char *p_buf );
396
397 #define DBG_CL_LOCK 0
398
399 #define CL_PLOCK_EXCL_ACQUIRE( __exp__ )  \
400 {                                                                                                       \
401    if (DBG_CL_LOCK)                      \
402      printf("cl_plock_excl_acquire: Acquiring %p file %s, line %d\n", \
403           __exp__,__FILE__, __LINE__);            \
404    cl_plock_excl_acquire( __exp__ );      \
405    if (DBG_CL_LOCK)                      \
406      printf("cl_plock_excl_acquire: Acquired  %p file %s, line %d\n", \
407           __exp__,__FILE__, __LINE__);            \
408 }
409
410 #define CL_PLOCK_ACQUIRE( __exp__ )  \
411 {                                                                                                       \
412    if (DBG_CL_LOCK)                      \
413      printf("cl_plock_acquire: Acquiring %p file %s, line %d\n", \
414           __exp__,__FILE__, __LINE__);            \
415    cl_plock_acquire( __exp__ );      \
416    if (DBG_CL_LOCK)                      \
417      printf("cl_plock_acquire: Acquired  %p file %s, line %d\n", \
418           __exp__,__FILE__, __LINE__);            \
419 }
420
421 #define CL_PLOCK_RELEASE( __exp__ )  \
422 {                                                                                                       \
423    if (DBG_CL_LOCK)                      \
424      printf("cl_plock_release: Releasing %p file %s, line %d\n", \
425           __exp__,__FILE__, __LINE__);            \
426    cl_plock_release( __exp__ );      \
427    if (DBG_CL_LOCK)                      \
428      printf("cl_plock_release: Released  %p file %s, line %d\n", \
429           __exp__,__FILE__, __LINE__);            \
430 }
431
432 #define DBG_CL_SPINLOCK 0
433 #define CL_SPINLOCK_RELEASE( __exp__ )  \
434 {                                                                                                       \
435    if (DBG_CL_SPINLOCK)                      \
436      printf("cl_spinlock_release: Releasing %p file %s, line %d\n", \
437           __exp__,__FILE__, __LINE__);            \
438    cl_spinlock_release( __exp__ );      \
439    if (DBG_CL_SPINLOCK)                      \
440      printf("cl_spinlock_release: Released  %p file %s, line %d\n", \
441           __exp__,__FILE__, __LINE__);            \
442 }
443
444 #define CL_SPINLOCK_ACQUIRE( __exp__ )  \
445 {                                                                                                       \
446    if (DBG_CL_SPINLOCK)                      \
447      printf("cl_spinlock_acquire: Acquire %p file %s, line %d\n", \
448           __exp__,__FILE__, __LINE__);            \
449    cl_spinlock_acquire( __exp__ );      \
450    if (DBG_CL_SPINLOCK)                      \
451      printf("cl_spinlock_acquire: Acquired  %p file %s, line %d\n", \
452           __exp__,__FILE__, __LINE__);            \
453 }
454
455 /****f* OpenSM: Helper/osm_is_debug
456 * NAME
457 *       osm_is_debug
458 *
459 * DESCRIPTION
460 *       The osm_is_debug function returns TRUE if the opensm was compiled
461 *  in debug mode, and FALSE otherwise.
462 *
463 * SYNOPSIS
464 */
465 boolean_t
466 osm_is_debug(void);
467 /*
468 * PARAMETERS
469 *    None
470 *
471 * RETURN VALUE
472 *         TRUE if compiled in debug version. FALSE otherwise.
473 *
474 * NOTES
475 *
476 *********/
477
478 END_C_DECLS
479
480 #endif          /* _OSM_LOG_H_ */