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