DI-Guy SDK Documentation  13.5
libbdilog.h
Go to the documentation of this file.
1 
2 /*********************************************************************
3  ** Copyright (c) 1992-2020 MAK Technologies, Inc.
4  ** All rights reserved.
5  *********************************************************************/
6 
7 /**********************************************************************
8  **
9  *t Boston Dynamics Log Library
10  **
11  ** The Boston Dynamics log library is used by most of Boston Dynamics'
12  ** software. It allows programs to write errors, debugging
13  ** information, etc., to zero, one, or more outputs including
14  ** stderr, a file, or a user-supplied output.
15  */
16 
17 #ifndef __LIBBDILOG_H
18 #define __LIBBDILOG_H
19 
20 #ifdef SWIG
21 %module libbdilog
22 #define CHECK_PRINTF_FORMAT
23 #else
24 #define CPLUSPLUS_ONLY
25 #if defined(__GNUC__) && (__GNUC__ > 2) || ((__GNUC__ == 2) && (__GNUC_MINOR__ > 4))
26 #define CHECK_PRINTF_FORMAT __attribute__ ((format (printf, 2, 3)))
27 #else
28 #define CHECK_PRINTF_FORMAT /* No support for non-GCC compilers */
29 #endif
30 #endif
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
36 #include <declspec_bdiutil.h>
37 
38 /*****************************************************************************
39  **
40  *2 Constants and Types
41  */
42 
43 #ifdef CPLUSPLUS_ONLY
44 
45 typedef int bdiCallbackHandle;
46 
47 #define BDI_LOG_MAX_CALLBACKS (8)
48 #define BDI_LOG_BAD_CALLBACK_HANDLE (-1)
49 
50 #endif
51 
52 /*l
53  ** Log notify levels. A lower number indicates higher importance.
54  */
55 #define BDI_LOG_NOTHING (-1)
56 #define BDI_LOG_ALWAYS (0)
57 #define BDI_LOG_FATAL (1)
58 #define BDI_LOG_ERROR (2)
59 #define BDI_LOG_WARN (3)
60 #define BDI_LOG_INFO (4)
61 #define BDI_LOG_DEBUG (5)
62 #define BDI_LOG_DEBUG_LV1 (6)
63 #define BDI_LOG_DEBUG_LV2 (7)
64 #define BDI_LOG_DEBUG_LV3 (8)
65 #define BDI_LOG_DEBUG_LV4 (9)
66 #define BDI_LOG_DEBUG_LV5 (10)
67 #define BDI_LOG_DEBUG_LV6 (11)
68 #define BDI_LOG_DEBUG_LV7 (12)
69 #define BDI_LOG_DEBUG_LV8 (13)
70 #define BDI_LOG_DEBUG_LV9 (14)
71 #define BDI_LOG_EVERYTHING (15)
72 
73 /*
74  * Callback prototype.
75  */
76 #ifdef CPLUSPLUS_ONLY
77 
78 typedef int bdiLogCallbackFunction(int notify_level, const char* string, void* user_data);
79 typedef int bdiLogFatalExitFunction(void);
80 typedef const char* bdiLogPrefixFunction(void* user_data);
81 
82 #endif
83 
84 
85 /*****************************************************************************
86  **
87  *2 Standard stderr/stdout Callbacks
88  */
89 
90 /*l
91  *b Description:
92  **
93  ** This function will route a copy of appropriate log messages to
94  ** stdio's stderr.
95  **
96  *b Arguments and Return Value:
97  **
98  *a return value - 0 on success, -1 on failure
99  *a notify_level - only messages whose notify level is at least as important
100  *a as this will appear in this log
101  */
102 BDI_DECLSPEC_bdiutil
103 int bdi_log_stderr_enable(int notify_level);
104 
105 /*l
106  *b Description:
107  **
108  ** This function shuts down the log opened by bdi_log_stderr_enable().
109  */
110 BDI_DECLSPEC_bdiutil
111 int bdi_log_stderr_disable(void);
112 
113 /*l
114  *b Description:
115  **
116  ** This function will route a copy of appropriate log messages to
117  ** stdio's stdout.
118  **
119  *b Arguments and Return Value:
120  **
121  *a return value - 0 on success, -1 on failure
122  *a notify_level - only messages whose notify level is at least as important
123  *a as this will appear in this log
124  */
125 BDI_DECLSPEC_bdiutil
126 int bdi_log_stdout_enable(int notify_level);
127 
128 /*l
129  *b Description:
130  **
131  ** This function shuts down the log opened by bdi_log_stdout_enable().
132  **
133  *b Arguments and Return Value:
134  **
135  *a return value - 0 on success, -1 on failure
136  */
137 BDI_DECLSPEC_bdiutil
138 int bdi_log_stdout_disable(void);
139 
140 
141 /*****************************************************************************
142  **
143  *2 File Callbacks
144  */
145 
146 /*l
147  *b Description:
148  **
149  ** This function will route a copy of appropriate log messages to
150  ** the named file.
151  **
152  *b Arguments and Return Value:
153  **
154  *a return value - 0 on success, -1 on failure
155  *a notify_level - only messages whose notify level is at least as important
156  *a as this will appear in this log
157  *a filename - name of the file in which messages will appear
158  *a clear_file - 0 or 1; 1 means clear out prior contents of file,
159  *a 0 means append to the file
160  */
161 BDI_DECLSPEC_bdiutil
162 int bdi_log_file_enable(int notify_level, const char* filename, int clear_file);
163 
164 /*l
165  *b Description:
166  **
167  ** This function shuts down the log opened by bdi_log_file_enable().
168  **
169  *b Arguments and Return Value:
170  **
171  *a return value - 0 on success, -1 on failure
172  */
173 BDI_DECLSPEC_bdiutil
174 int bdi_log_file_disable(void);
175 
176 
177 /*****************************************************************************
178  **
179  *2 Print Functions
180  */
181 
182 /*l
183  *b Description:
184  **
185  ** This function sends the specified string to all open logs. Whether or not
186  ** the string is actually printed depends the specified notify_level in
187  ** this call and the notify_level with which each log was opened.
188  **
189  *b Arguments and Return Value:
190  **
191  *a return value - 0 on success, -1 on failure
192  *a string - message to be (potentially) printed
193  *a notify_level - "importance" of message; lower is more important
194  */
195 BDI_DECLSPEC_bdiutil
196 int bdi_log_print(int notify_level, const char* string);
197 
198 /*l
199  *b Description:
200  **
201  ** printf-like version of bdi_log_print(). Use just like printf, but add
202  ** the notify_level as the first argument.
203  **
204  *b Arguments and Return Value:
205  **
206  *a return value - 0 on success, -1 on failure
207  */
208 #ifdef CPLUSPLUS_ONLY
209 
210 BDI_DECLSPEC_bdiutil
211 int bdi_log_printf(int notify_level, const char* format, ...)
213 
214 #endif
215 
216 /*l
217  *b Description:
218  **
219  ** This function sets the global notify level. Messages whose notify_level
220  ** argument is less important than this will not be printed, regardless of
221  ** the notify_level used to open logs.
222  **
223  ** The starting global notify level is BDI_LOG_EVERYTHING.
224  **
225  *b Arguments and Return Value:
226  **
227  *a return value - 0 on success, -1 on failure
228  *a notify_level - global level of importance
229  **
230  *b Example:
231  **
232  *e // open a file log with notify level BDI_LOG_WARN
233  *e bdi_log_file_enable(BDI_LOG_WARN, "errors.txt", 1);
234  *e
235  *e // this message WILL appear in the log
236  *e bdi_log_print(BDI_LOG_WARN, "Message 1.\n");
237  *e
238  *e // set the global notify level to be more strict
239  *e bdi_log_set_global_notify_level(BDI_LOG_ERROR);
240  *e
241  *e // this message WILL NOT appear in the log
242  *e // (log messages of importance less than BDI_LOG_ERROR are ignored.)
243  *e bdi_log_print(BDI_LOG_WARN, "Message 2.\n");
244  */
245 BDI_DECLSPEC_bdiutil
246 int bdi_log_set_global_notify_level(int notify_level);
247 
248 /*l
249  *b Description:
250  **
251  ** Returns the most recent setting of bdi_log_set_global_notify_level().
252  **
253  *b Arguments and Return Value:
254  **
255  *a return value - most recent setting
256  */
257 BDI_DECLSPEC_bdiutil
259 
260 
261 /*****************************************************************************
262  **
263  *2 Registration Functions
264  */
265 
266 /*l
267  *b Description:
268  **
269  ** This function registers a callback for the specified notify_level.
270  **
271  *b Arguments and Return Value:
272  **
273  *a return value - a handle
274  *a callback - pointer to the callback function
275  *a notify_level - level of "importance" the callback will be
276  *a called for
277  *a user_data - pointer that will be supplied to the callback
278  *a each time it is called.
279  */
280 #ifdef CPLUSPLUS_ONLY
281 
282 BDI_DECLSPEC_bdiutil
283 bdiCallbackHandle bdi_log_register_callback(int notify_level,
284  bdiLogCallbackFunction* callback,
285  void* user_data);
286 
287 #endif
288 
289 /*l
290  *b Description:
291  **
292  ** This function unregisters a previously registered callback.
293  **
294  *b Arguments and Return Value:
295  **
296  *a return value - 0 on success, -1 on failure
297  *a handle - the handle returned by bdi_log_register_callback()
298  */
299 #ifdef CPLUSPLUS_ONLY
300 
301 BDI_DECLSPEC_bdiutil
302 int bdi_log_unregister_callback(bdiCallbackHandle handle);
303 
304 #endif
305 
306 /*l
307  *b Description:
308  **
309  ** This function returns the number of logs currently open.
310  **
311  *b Arguments and Return Value:
312  **
313  *a return value - number of logs open
314  */
315 #ifdef CPLUSPLUS_ONLY
316 
317 BDI_DECLSPEC_bdiutil
319 
320 #endif
321 
322 /*l
323  *b Description:
324  **
325  ** This function changes the notify level of a registered callback.
326  **
327  *b Arguments and Return Value:
328  **
329  *a return value - 0 on success, -1 on failure
330  *a handle - the handle returned by bdi_log_register_callback()
331  *a notify_level - the new notify level
332  */
333 BDI_DECLSPEC_bdiutil
334 int bdi_log_set_notify_level(bdiCallbackHandle handle, int notify_level);
335 
336 /*l
337  *b Description:
338  **
339  ** This function returns the notify level of a registered callback.
340  **
341  *b Arguments and Return Value:
342  **
343  *a return value - most recent setting or BDI_LOG_NOTHING if handle is invalid
344  *a handle - the handle returned by bdi_log_register_callback()
345  */
346 BDI_DECLSPEC_bdiutil
347 int bdi_log_get_notify_level(bdiCallbackHandle handle);
348 
349 /*l
350  *b Description:
351  **
352  ** Derives a string from the notify level and returns it in the passed
353  ** result buffer.
354  **
355  *b Arguments:
356  *a
357  *a notify_level - notify level to parse
358  *a result - character buffer for result
359  *a result_max_len - result buffer size
360  */
361 BDI_DECLSPEC_bdiutil
362 void bdi_log_get_string_from_notify_level(int notify_level,
363  char* result,
364  int result_buf_size);
365 
366 /*l
367  *b Description:
368  **
369  ** This function registers a function that will be called if a
370  ** log message is sent with level BDI_LOG_FATAL. The function
371  ** should whatever it takes to shut down gracefully.
372  **
373  ** If no fatal exit function is registered, exit(EXIT_FAILURE) will be
374  ** called by default.
375  */
376 #ifdef CPLUSPLUS_ONLY
377 
378 BDI_DECLSPEC_bdiutil
380 
381 /*l
382  *b Description:
383  **
384  ** This function prints using OutputDebugString on Windows, which will
385  ** show up in the Output window in Microsoft Developer Studio.
386  */
387 BDI_DECLSPEC_bdiutil
388 int bdi_log_print_to_output_window(char* format, ...);
389 
390 #endif
393 /*l
394  *b Description:
395  **
396  ** This function grabs the lock of the logging system. This is pretty hacky.
397  ** If you do not release this lock, you're in trouble. This can be useful for
398  ** certain specialized interactions with log callback objects that rely on the
399  ** libbdilog lock for their synchronization.
400  */
401 BDI_DECLSPEC_bdiutil
404 /*l
405  *b Description:
406  **
407  ** This function releases the lock of the logging system. This is pretty hacky.
408  ** If you do not release this lock, you're in trouble. This can be useful for
409  ** certain specialized interactions with log callback objects that rely on the
410  ** libbdilog lock for their synchronization.
411  */
412 BDI_DECLSPEC_bdiutil
413 void bdi_log_unlock();
414 
415 /*
416  * Debug macros. Using these macros allows for liberal use of debugging
417  * output and assertions for debug versions, with zero runtime performance
418  * hit for release versions (similar to use of standard assertions).
419  */
420 #ifdef CPLUSPLUS_ONLY
421 #ifndef NDEBUG
422 
423 #define bdi_log_assert(assertion) if (!(assertion)) {bdi_log_printf(BDI_LOG_ERROR, "ERROR: Assertion '%s' failed in file %s line %d\n", #assertion, __FILE__, __LINE__);}
424 #define bdi_log_assert_fatal(assertion) if (!(assertion)) {bdi_log_printf(BDI_LOG_FATAL, "FATAL: Assertion '%s' failed in file %s line %d\n", #assertion, __FILE__, __LINE__); exit(EXIT_FAILURE);}
425 #define bdi_log_assert_warn(assertion) if (!(assertion)) {bdi_log_printf(BDI_LOG_WARN, "WARNING: Assertion '%s' failed in file %s line %d\n", #assertion, __FILE__, __LINE__);}
426 #define bdi_log_assert_info(assertion) if (!(assertion)) {bdi_log_printf(BDI_LOG_INFO, "INFO: Assertion '%s' failed in file %s line %d\n", #assertion, __FILE__, __LINE__);}
427 #define bdi_log_assert_debug(assertion) if (!(assertion)) {bdi_log_printf(BDI_LOG_DEBUG, "DEBUG: Assertion '%s' failed in file %s line %d\n", #assertion, __FILE__, __LINE__);}
428 
429 #define bdi_log_debug(string) bdi_log_print(BDI_LOG_DEBUG, string)
430 #define bdi_log_debug_arg0(string) bdi_log_print(BDI_LOG_DEBUG, string)
431 #define bdi_log_debug_arg1(string, arg1) bdi_log_printf(BDI_LOG_DEBUG, string, arg1)
432 #define bdi_log_debug_arg2(string, arg1, arg2) bdi_log_printf(BDI_LOG_DEBUG, string, arg1, arg2)
433 #define bdi_log_debug_arg3(string, arg1, arg2, arg3) bdi_log_printf(BDI_LOG_DEBUG, string, arg1, arg2, arg3)
434 #define bdi_log_debug_arg4(string, arg1, arg2, arg3, arg4) bdi_log_printf(BDI_LOG_DEBUG, string, arg1, arg2, arg3, arg4)
435 #define bdi_log_debug_arg5(string, arg1, arg2, arg3, arg4, arg5) bdi_log_printf(BDI_LOG_DEBUG, string, arg1, arg2, arg3, arg4, arg5)
436 #define bdi_log_debug_arg6(string, arg1, arg2, arg3, arg4, arg5, arg6) bdi_log_printf(BDI_LOG_DEBUG, string, arg1, arg2, arg3, arg4, arg5, arg6)
437 
438 #define bdi_log_debug_lvn(level, string) bdi_log_print(BDI_LOG_DEBUG_LV##level, string)
439 #define bdi_log_debug_lvn_arg0(level, string) bdi_log_print(BDI_LOG_DEBUG_LV##level, string)
440 #define bdi_log_debug_lvn_arg1(level, string, arg1) bdi_log_printf(BDI_LOG_DEBUG_LV##level, string, arg1)
441 #define bdi_log_debug_lvn_arg2(level, string, arg1, arg2) bdi_log_printf(BDI_LOG_DEBUG_LV##level, string, arg1, arg2)
442 #define bdi_log_debug_lvn_arg3(level, string, arg1, arg2, arg3) bdi_log_printf(BDI_LOG_DEBUG_LV##level, string, arg1, arg2, arg3)
443 #define bdi_log_debug_lvn_arg4(level, string, arg1, arg2, arg3, arg4) bdi_log_printf(BDI_LOG_DEBUG_LV##level, string, arg1, arg2, arg3, arg4)
444 #define bdi_log_debug_lvn_arg5(level, string, arg1, arg2, arg3, arg4, arg5) bdi_log_printf(BDI_LOG_DEBUG_LV##level, string, arg1, arg2, arg3, arg4, arg5)
445 #define bdi_log_debug_lvn_arg6(level, string, arg1, arg2, arg3, arg4, arg5, arg6) bdi_log_printf(BDI_LOG_DEBUG_LV##level, string, arg1, arg2, arg3, arg4, arg5, arg6)
446 
447 #else
448 
449 #define bdi_log_assert(assertion) /* */
450 #define bdi_log_assert_fatal(assertion) /* */
451 #define bdi_log_assert_warn(assertion) /* */
452 #define bdi_log_assert_info(assertion) /* */
453 #define bdi_log_assert_debug(assertion) /* */
454 
455 #define bdi_log_debug(string) /* */
456 #define bdi_log_debug_arg0(string) /* */
457 #define bdi_log_debug_arg1(string, arg1) /* */
458 #define bdi_log_debug_arg2(string, arg1, arg2) /* */
459 #define bdi_log_debug_arg3(string, arg1, arg2, arg3) /* */
460 #define bdi_log_debug_arg4(string, arg1, arg2, arg3, arg4) /* */
461 #define bdi_log_debug_arg5(string, arg1, arg2, arg3, arg4, arg5) /* */
462 #define bdi_log_debug_arg6(string, arg1, arg2, arg3, arg4, arg5, arg6) /* */
463 
464 #define bdi_log_debug_lvn(level, string) /* */
465 #define bdi_log_debug_lvn_arg0(level, string) /* */
466 #define bdi_log_debug_lvn_arg1(level, string, arg1) /* */
467 #define bdi_log_debug_lvn_arg2(level, string, arg1, arg2) /* */
468 #define bdi_log_debug_lvn_arg3(level, string, arg1, arg2, arg3) /* */
469 #define bdi_log_debug_lvn_arg4(level, string, arg1, arg2, arg3, arg4) /* */
470 #define bdi_log_debug_lvn_arg5(level, string, arg1, arg2, arg3, arg4, arg5) /* */
471 #define bdi_log_debug_lvn_arg6(level, string, arg1, arg2, arg3, arg4, arg5, arg6) /* */
472 
473 
474 #endif
475 
476 #endif
477 
478 #ifdef __cplusplus
479 }
480 #endif
481 
482 #endif /* __LIBBDILOG_H */
483 
484 
485 /*********************************************************************
486  ** Copyright (c) 1992-2020 MAK Technologies, Inc.
487  ** All rights reserved.
488  *********************************************************************/
489 
int bdi_log_print(int notify_level, const char *string)
This function sends the specified string to all open logs.
int bdiLogCallbackFunction(int notify_level, const char *string, void *user_data)
Definition: libbdilog.h:77
void bdi_log_unlock()
This function releases the lock of the logging system.
void bdi_log_register_fatal_exit_function(bdiLogFatalExitFunction *fatal_exit_func)
This function registers a function that will be called if a log message is sent with level BDI_LOG_FA...
int bdi_log_stderr_disable(void)
This function shuts down the log opened by bdi_log_stderr_enable().
int bdi_log_set_notify_level(bdiCallbackHandle handle, int notify_level)
This function changes the notify level of a registered callback.
int bdi_log_print_to_output_window(char *format,...)
This function prints using OutputDebugString on Windows, which will show up in the Output window in M...
int bdi_log_stderr_enable(int notify_level)
This function will route a copy of appropriate log messages to stdio's stderr.
bdiCallbackHandle bdi_log_register_callback(int notify_level, bdiLogCallbackFunction *callback, void *user_data)
This function registers a callback for the specified notify_level.
void bdi_log_lock()
This function grabs the lock of the logging system.
int bdi_log_get_num_logs_open()
This function returns the number of logs currently open.
const char * bdiLogPrefixFunction(void *user_data)
Definition: libbdilog.h:79
int bdi_log_get_global_notify_level(void)
Returns the most recent setting of bdi_log_set_global_notify_level().
int bdi_log_file_enable(int notify_level, const char *filename, int clear_file)
This function will route a copy of appropriate log messages to the named file.
int bdi_log_file_disable(void)
This function shuts down the log opened by bdi_log_file_enable().
int bdiCallbackHandle
Definition: libbdilog.h:44
int bdiLogFatalExitFunction(void)
Definition: libbdilog.h:78
int bdi_log_unregister_callback(bdiCallbackHandle handle)
This function unregisters a previously registered callback.
int bdi_log_stdout_enable(int notify_level)
This function will route a copy of appropriate log messages to stdio's stdout.
void bdi_log_get_string_from_notify_level(int notify_level, char *result, int result_buf_size)
Derives a string from the notify level and returns it in the passed result buffer.
int bdi_log_stdout_disable(void)
This function shuts down the log opened by bdi_log_stdout_enable().
#define CHECK_PRINTF_FORMAT
Definition: libbdilog.h:27
int bdi_log_printf(int notify_level, const char *format,...) CHECK_PRINTF_FORMAT
printf-like version of bdi_log_print().
int bdi_log_set_global_notify_level(int notify_level)
This function sets the global notify level.
int bdi_log_get_notify_level(bdiCallbackHandle handle)
This function returns the notify level of a registered callback.