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