2 * Copyright (c) 2004-2008 Voltaire, Inc. All rights reserved.
3 * Copyright (c) 2002-2006 Mellanox Technologies LTD. All rights reserved.
4 * Copyright (c) 1996-2003 Intel Corporation. All rights reserved.
6 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
20 * - Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials
23 * provided with the distribution.
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38 * Declaration of osm_log_t.
39 * This object represents the log file.
40 * This object is part of the OpenSM family of objects.
49 #include <complib/cl_spinlock.h>
50 #include <opensm/osm_base.h>
51 #include <iba/ib_types.h>
55 #define STRICT_OSM_LOG_FORMAT __attribute__((format(printf, 3, 4)))
57 #define STRICT_OSM_LOG_FORMAT
61 # define BEGIN_C_DECLS extern "C" {
62 # define END_C_DECLS }
63 #else /* !__cplusplus */
64 # define BEGIN_C_DECLS
66 #endif /* __cplusplus */
69 #define LOG_ENTRY_SIZE_MAX 4096
70 #define BUF_SIZE LOG_ENTRY_SIZE_MAX
71 #define __func__ __FUNCTION__
72 #define OSM_LOG_ENTER( OSM_LOG_PTR ) \
73 osm_log( OSM_LOG_PTR, OSM_LOG_FUNCS, \
75 #define OSM_LOG_EXIT( OSM_LOG_PTR ) \
76 osm_log( OSM_LOG_PTR, OSM_LOG_FUNCS, \
87 typedef uint8_t osm_log_level_t;
89 #define OSM_LOG_NONE 0x00
90 #define OSM_LOG_ERROR 0x01
91 #define OSM_LOG_INFO 0x02
92 #define OSM_LOG_VERBOSE 0x04
93 #define OSM_LOG_DEBUG 0x08
94 #define OSM_LOG_FUNCS 0x10
95 #define OSM_LOG_FRAMES 0x20
96 #define OSM_LOG_ROUTING 0x40
97 #define OSM_LOG_ALL 0x7f
98 #define OSM_LOG_SYS 0x80
101 DEFAULT - turn on ERROR and INFO only
103 #define OSM_LOG_DEFAULT_LEVEL OSM_LOG_ERROR | OSM_LOG_INFO
105 /****s* OpenSM: MAD Wrapper/osm_log_t
113 typedef struct osm_log {
114 osm_log_level_t level;
117 unsigned long max_size;
120 boolean_t accum_log_file;
126 /****f* OpenSM: Log/osm_log_construct
131 * This function constructs a Log object.
135 static inline void osm_log_construct(IN osm_log_t * const p_log)
137 cl_spinlock_construct(&p_log->lock);
143 * [in] Pointer to a Log object to construct.
146 * This function does not return a value.
149 * Allows calling osm_log_init, osm_log_init_v2, osm_log_destroy
151 * Calling osm_log_construct is a prerequisite to calling any other
152 * method except osm_log_init or osm_log_init_v2.
155 * Log object, osm_log_init, osm_log_init_v2,
159 /****f* OpenSM: Log/osm_log_destroy
164 * The osm_log_destroy function destroys the object, releasing
169 static inline void osm_log_destroy(IN osm_log_t * const p_log)
171 cl_spinlock_destroy(&p_log->lock);
172 if (p_log->out_port != stdout) {
173 fclose(p_log->out_port);
174 p_log->out_port = stdout;
182 * [in] Pointer to the object to destroy.
185 * This function does not return a value.
188 * Performs any necessary cleanup of the specified
190 * Further operations should not be attempted on the destroyed object.
191 * This function should only be called after a call to
192 * osm_log_construct, osm_log_init, or osm_log_init_v2.
195 * Log object, osm_log_construct,
196 * osm_log_init, osm_log_init_v2
199 /****f* OpenSM: Log/osm_log_init_v2
204 * The osm_log_init_v2 function initializes a
205 * Log object for use.
210 osm_log_init_v2(IN osm_log_t * const p_log,
211 IN const boolean_t flush,
212 IN const uint8_t log_flags,
213 IN const char *log_file,
214 IN const unsigned long max_size,
215 IN const boolean_t accum_log_file);
219 * [in] Pointer to the log object.
222 * [in] Set to TRUE directs the log to flush all log messages
223 * immediately. This severely degrades log performance,
224 * and is normally used for debugging only.
227 * [in] The log verbosity level to be used.
230 * [in] if not NULL defines the name of the log file. Otherwise
234 * CL_SUCCESS if the Log object was initialized
238 * Allows calling other Log methods.
241 * Log object, osm_log_construct,
245 /****f* OpenSM: Log/osm_log_reopen_file
247 * osm_log_reopen_file
250 * The osm_log_reopen_file function reopens the log file
254 int osm_log_reopen_file(osm_log_t * p_log);
258 * [in] Pointer to the log object.
261 * 0 on success or nonzero value otherwise.
264 /****f* OpenSM: Log/osm_log_init
269 * The osm_log_init function initializes a
270 * Log object for use. It is a wrapper for osm_log_init_v2().
275 osm_log_init(IN osm_log_t * const p_log,
276 IN const boolean_t flush,
277 IN const uint8_t log_flags,
278 IN const char *log_file, IN const boolean_t accum_log_file);
280 * Same as osm_log_init_v2() but without max_size parameter
284 osm_log(IN osm_log_t * const p_log,
285 IN const osm_log_level_t verbosity,
286 IN const char *p_str, ...) STRICT_OSM_LOG_FORMAT;
288 /****f* OpenSM: Log/osm_log_get_level
293 * Returns the current log level.
297 static inline osm_log_level_t
298 osm_log_get_level(IN const osm_log_t * const p_log)
300 return (p_log->level);
306 * [in] Pointer to the log object.
309 * Returns the current log level.
314 * Log object, osm_log_construct,
318 /****f* OpenSM: Log/osm_log_set_level
323 * Sets the current log level.
328 osm_log_set_level(IN osm_log_t * const p_log, IN const osm_log_level_t level)
330 p_log->level = level;
331 osm_log(p_log, OSM_LOG_ALL, "Setting log level to: 0x%02x\n", level);
337 * [in] Pointer to the log object.
340 * [in] New level to set.
343 * Returns the current log level.
348 * Log object, osm_log_construct,
352 /****f* OpenSM: Log/osm_log_is_active
357 * Returns TRUE if the specified log level would be logged.
362 static inline boolean_t
363 osm_log_is_active(IN const osm_log_t * const p_log,
364 IN const osm_log_level_t level)
366 return ((p_log->level & level) != 0);
372 * [in] Pointer to the log object.
375 * [in] Level to check.
378 * Returns TRUE if the specified log level would be logged.
384 * Log object, osm_log_construct,
388 extern void osm_log_msg_box(osm_log_t *log, osm_log_level_t level,
389 const char *func_name, const char *msg);
390 extern void osm_log_raw(IN osm_log_t * const p_log,
391 IN const osm_log_level_t verbosity, IN const char *p_buf);
393 #define OSM_LOG(log, level, fmt, arg...) do { \
394 if (osm_log_is_active(log, (level))) \
395 osm_log(log, level, "%s: " fmt, __func__, ##arg); \
398 #define OSM_LOG_MSG_BOX(log, level, msg) \
399 osm_log_msg_box(log, level, __func__, msg)
401 #define DBG_CL_LOCK 0
403 #define CL_PLOCK_EXCL_ACQUIRE( __exp__ ) \
406 printf("cl_plock_excl_acquire: Acquiring %p file %s, line %d\n", \
407 __exp__,__FILE__, __LINE__); \
408 cl_plock_excl_acquire( __exp__ ); \
410 printf("cl_plock_excl_acquire: Acquired %p file %s, line %d\n", \
411 __exp__,__FILE__, __LINE__); \
414 #define CL_PLOCK_ACQUIRE( __exp__ ) \
417 printf("cl_plock_acquire: Acquiring %p file %s, line %d\n", \
418 __exp__,__FILE__, __LINE__); \
419 cl_plock_acquire( __exp__ ); \
421 printf("cl_plock_acquire: Acquired %p file %s, line %d\n", \
422 __exp__,__FILE__, __LINE__); \
425 #define CL_PLOCK_RELEASE( __exp__ ) \
428 printf("cl_plock_release: Releasing %p file %s, line %d\n", \
429 __exp__,__FILE__, __LINE__); \
430 cl_plock_release( __exp__ ); \
432 printf("cl_plock_release: Released %p file %s, line %d\n", \
433 __exp__,__FILE__, __LINE__); \
436 #define DBG_CL_SPINLOCK 0
437 #define CL_SPINLOCK_RELEASE( __exp__ ) \
439 if (DBG_CL_SPINLOCK) \
440 printf("cl_spinlock_release: Releasing %p file %s, line %d\n", \
441 __exp__,__FILE__, __LINE__); \
442 cl_spinlock_release( __exp__ ); \
443 if (DBG_CL_SPINLOCK) \
444 printf("cl_spinlock_release: Released %p file %s, line %d\n", \
445 __exp__,__FILE__, __LINE__); \
448 #define CL_SPINLOCK_ACQUIRE( __exp__ ) \
450 if (DBG_CL_SPINLOCK) \
451 printf("cl_spinlock_acquire: Acquiring %p file %s, line %d\n", \
452 __exp__,__FILE__, __LINE__); \
453 cl_spinlock_acquire( __exp__ ); \
454 if (DBG_CL_SPINLOCK) \
455 printf("cl_spinlock_acquire: Acquired %p file %s, line %d\n", \
456 __exp__,__FILE__, __LINE__); \
459 /****f* OpenSM: Helper/osm_is_debug
464 * The osm_is_debug function returns TRUE if the opensm was compiled
465 * in debug mode, and FALSE otherwise.
469 boolean_t osm_is_debug(void);
475 * TRUE if compiled in debug version. FALSE otherwise.
482 #endif /* _OSM_LOG_H_ */