Add -e flag to main in order to enable starting a new log file,
[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   IN const boolean_t accum_log_file )
235 {
236   p_log->level = log_flags;
237   p_log->flush = flush;
238
239   if (log_file == NULL)
240   {
241     p_log->out_port = stdout;
242   }
243   else
244   {
245     if (accum_log_file)
246     p_log->out_port = fopen(log_file,"a+");
247     else
248       p_log->out_port = fopen(log_file,"w+");
249     
250     if (!p_log->out_port)
251     {
252       if (accum_log_file)
253       printf("Cannot open %s for appending. Permission denied\n", log_file);
254       else
255         printf("Cannot open %s for writing. Permission denied\n", log_file);
256
257       return(IB_UNKNOWN_ERROR);
258     }
259   }
260   openlog("OpenSM", LOG_CONS | LOG_PID, LOG_USER);
261
262   if (cl_spinlock_init( &p_log->lock ) == CL_SUCCESS) 
263     return IB_SUCCESS;
264   else
265     return IB_ERROR;
266 }
267 /*
268 * PARAMETERS
269 *       p_log
270 *               [in] Pointer to the log object.
271 *
272 *       flush
273 *               [in] Set to TRUE directs the log to flush all log messages
274 *               immediately.  This severely degrades log performance,
275 *               and is normally used for debugging only.
276 *
277 *  log_flags
278 *     [in] The log verbosity level to be used.
279 *
280 *  log_file
281 *     [in] if not NULL defines the name of the log file. Otherwise it is stdout.
282 *
283 * RETURN VALUES
284 *       CL_SUCCESS if the Log object was initialized
285 *       successfully.
286 *
287 * NOTES
288 *       Allows calling other Log methods.
289 *
290 * SEE ALSO
291 *       Log object, osm_log_construct,
292 *       osm_log_destroy
293 *********/
294
295 /****f* OpenSM: Log/osm_log_get_level
296 * NAME
297 *       osm_log_get_level
298 *
299 * DESCRIPTION
300 *       Returns the current log level.
301 *
302 * SYNOPSIS
303 */
304 static inline osm_log_level_t
305 osm_log_get_level(
306         IN const osm_log_t* const p_log )
307 {
308         return( p_log->level );
309 }
310 /*
311 * PARAMETERS
312 *       p_log
313 *               [in] Pointer to the log object.
314 *
315 * RETURN VALUES
316 *       Returns the current log level.
317 *
318 * NOTES
319 *
320 * SEE ALSO
321 *       Log object, osm_log_construct,
322 *       osm_log_destroy
323 *********/
324 /****f* OpenSM: Log/osm_log_set_level
325 * NAME
326 *       osm_log_set_level
327 *
328 * DESCRIPTION
329 *       Sets the current log level.
330 *
331 * SYNOPSIS
332 */
333 static inline void
334 osm_log_set_level(
335         IN osm_log_t* const p_log,
336         IN const osm_log_level_t level )
337 {
338         p_log->level = level;
339 }
340 /*
341 * PARAMETERS
342 *       p_log
343 *               [in] Pointer to the log object.
344 *
345 *       level
346 *               [in] New level to set.
347 *
348 * RETURN VALUES
349 *       Returns the current log level.
350 *
351 * NOTES
352 *
353 * SEE ALSO
354 *       Log object, osm_log_construct,
355 *       osm_log_destroy
356 *********/
357 /****f* OpenSM: Log/osm_log_is_active
358 * NAME
359 *       osm_log_is_active
360 *
361 * DESCRIPTION
362 *       Returns TRUE if the specified log level would be logged.
363 *       FALSE otherwise.
364 *
365 * SYNOPSIS
366 */
367 static inline boolean_t
368 osm_log_is_active(
369         IN const osm_log_t* const p_log,
370         IN const osm_log_level_t level )
371 {
372         return( (p_log->level & level) != 0 );
373 }
374 /*
375 * PARAMETERS
376 *       p_log
377 *               [in] Pointer to the log object.
378 *
379 *       level
380 *               [in] Level to check.
381 *
382 * RETURN VALUES
383 *       Returns TRUE if the specified log level would be logged.
384 *       FALSE otherwise.
385 *
386 * NOTES
387 *
388 * SEE ALSO
389 *       Log object, osm_log_construct,
390 *       osm_log_destroy
391 *********/
392
393
394 void
395 osm_log(
396         IN osm_log_t* const p_log,
397         IN const osm_log_level_t verbosity,
398         IN const char *p_str, ... );
399
400 void
401 osm_log_raw(
402         IN osm_log_t* const p_log,
403         IN const osm_log_level_t verbosity,
404         IN const char *p_buf );
405
406 #define DBG_CL_LOCK 0
407
408 #define CL_PLOCK_EXCL_ACQUIRE( __exp__ )  \
409 {                                                                                                       \
410    if (DBG_CL_LOCK)                      \
411      printf("cl_plock_excl_acquire: Acquiring %p file %s, line %d\n", \
412           __exp__,__FILE__, __LINE__);            \
413    cl_plock_excl_acquire( __exp__ );      \
414    if (DBG_CL_LOCK)                      \
415      printf("cl_plock_excl_acquire: Acquired  %p file %s, line %d\n", \
416           __exp__,__FILE__, __LINE__);            \
417 }
418
419 #define CL_PLOCK_ACQUIRE( __exp__ )  \
420 {                                                                                                       \
421    if (DBG_CL_LOCK)                      \
422      printf("cl_plock_acquire: Acquiring %p file %s, line %d\n", \
423           __exp__,__FILE__, __LINE__);            \
424    cl_plock_acquire( __exp__ );      \
425    if (DBG_CL_LOCK)                      \
426      printf("cl_plock_acquire: Acquired  %p file %s, line %d\n", \
427           __exp__,__FILE__, __LINE__);            \
428 }
429
430 #define CL_PLOCK_RELEASE( __exp__ )  \
431 {                                                                                                       \
432    if (DBG_CL_LOCK)                      \
433      printf("cl_plock_release: Releasing %p file %s, line %d\n", \
434           __exp__,__FILE__, __LINE__);            \
435    cl_plock_release( __exp__ );      \
436    if (DBG_CL_LOCK)                      \
437      printf("cl_plock_release: Released  %p file %s, line %d\n", \
438           __exp__,__FILE__, __LINE__);            \
439 }
440
441 #define DBG_CL_SPINLOCK 0
442 #define CL_SPINLOCK_RELEASE( __exp__ )  \
443 {                                                                                                       \
444    if (DBG_CL_SPINLOCK)                      \
445      printf("cl_spinlock_release: Releasing %p file %s, line %d\n", \
446           __exp__,__FILE__, __LINE__);            \
447    cl_spinlock_release( __exp__ );      \
448    if (DBG_CL_SPINLOCK)                      \
449      printf("cl_spinlock_release: Released  %p file %s, line %d\n", \
450           __exp__,__FILE__, __LINE__);            \
451 }
452
453 #define CL_SPINLOCK_ACQUIRE( __exp__ )  \
454 {                                                                                                       \
455    if (DBG_CL_SPINLOCK)                      \
456      printf("cl_spinlock_acquire: Acquire %p file %s, line %d\n", \
457           __exp__,__FILE__, __LINE__);            \
458    cl_spinlock_acquire( __exp__ );      \
459    if (DBG_CL_SPINLOCK)                      \
460      printf("cl_spinlock_acquire: Acquired  %p file %s, line %d\n", \
461           __exp__,__FILE__, __LINE__);            \
462 }
463
464 /****f* OpenSM: Helper/osm_is_debug
465 * NAME
466 *       osm_is_debug
467 *
468 * DESCRIPTION
469 *       The osm_is_debug function returns TRUE if the opensm was compiled
470 *  in debug mode, and FALSE otherwise.
471 *
472 * SYNOPSIS
473 */
474 boolean_t
475 osm_is_debug(void);
476 /*
477 * PARAMETERS
478 *    None
479 *
480 * RETURN VALUE
481 *         TRUE if compiled in debug version. FALSE otherwise.
482 *
483 * NOTES
484 *
485 *********/
486
487 END_C_DECLS
488
489 #endif          /* _OSM_LOG_H_ */