NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
doca_log.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2021-2023 NVIDIA CORPORATION & AFFILIATES, ALL RIGHTS RESERVED.
3  *
4  * This software product is a proprietary product of NVIDIA CORPORATION &
5  * AFFILIATES (the "Company") and all right, title, and interest in and to the
6  * software product, including all associated intellectual property rights, are
7  * and shall remain exclusively with the Company.
8  *
9  * This software product is governed by the End User License Agreement
10  * provided with the software product.
11  *
12  */
13 
26 #ifndef DOCA_LOG_H_
27 #define DOCA_LOG_H_
28 
29 #include <stddef.h>
30 #include <stdio.h>
31 #include <stdint.h>
32 
33 #include <doca_compat.h>
34 #include <doca_error.h>
35 
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
51 };
52 
56 struct doca_log_backend;
57 
61 typedef void (*log_flush_callback)(char *buf);
62 
77 doca_error_t doca_log_backend_set_level_lower_limit(struct doca_log_backend *backend, uint32_t level);
78 
93 doca_error_t doca_log_backend_set_level_upper_limit(struct doca_log_backend *backend, uint32_t upper_limit);
94 
108 
122 
140 
157 
170 
183 
200 doca_error_t doca_log_register_source(const char *source_name, int *source);
201 
216 
230 doca_error_t doca_log_backend_create_with_file(FILE *fptr, struct doca_log_backend **backend);
231 
245 doca_error_t doca_log_backend_create_with_fd(int fd, struct doca_log_backend **backend);
246 
266  size_t capacity,
267  log_flush_callback handler,
268  struct doca_log_backend **backend);
269 
283 doca_error_t doca_log_backend_create_with_syslog(const char *name, struct doca_log_backend **backend);
284 
298 doca_error_t doca_log_backend_set_sdk_level(struct doca_log_backend *backend, uint32_t level);
299 
313 doca_error_t doca_log_backend_create_with_file_sdk(FILE *fptr, struct doca_log_backend **backend);
314 
328 doca_error_t doca_log_backend_create_with_fd_sdk(int fd, struct doca_log_backend **backend);
329 
349  size_t capacity,
350  log_flush_callback handler,
351  struct doca_log_backend **backend);
352 
366 doca_error_t doca_log_backend_create_with_syslog_sdk(const char *name, struct doca_log_backend **backend);
367 
384 
397 
410 
432 doca_error_t doca_log(uint32_t level, int source, const char *fname, int line, const char *func, const char *format, ...)
433  __attribute__((format(printf, 6, 7)));
434 
446 #define DOCA_LOG(level, format, ...) doca_log(level, log_source, __FILE__, __LINE__, __func__, format, ##__VA_ARGS__)
447 
456 #define DOCA_LOG_CRIT(format, ...) DOCA_LOG(DOCA_LOG_LEVEL_CRIT, format, ##__VA_ARGS__)
457 
466 #define DOCA_LOG_ERR(format, ...) DOCA_LOG(DOCA_LOG_LEVEL_ERROR, format, ##__VA_ARGS__)
467 
476 #define DOCA_LOG_WARN(format, ...) DOCA_LOG(DOCA_LOG_LEVEL_WARNING, format, ##__VA_ARGS__)
477 
486 #define DOCA_LOG_INFO(format, ...) DOCA_LOG(DOCA_LOG_LEVEL_INFO, format, ##__VA_ARGS__)
487 
496 #define DOCA_LOG_DBG(format, ...) DOCA_LOG(DOCA_LOG_LEVEL_DEBUG, format, ##__VA_ARGS__)
497 
510 #ifdef DOCA_LOGGING_ALLOW_TRACE
511 #define DOCA_LOG_TRC(format, ...) DOCA_LOG(DOCA_LOG_LEVEL_TRACE, format, ##__VA_ARGS__)
512 #else /* DOCA_LOGGING_ALLOW_TRACE */
513 #define DOCA_LOG_TRC(format, ...) \
514  do { \
515  } while (0)
516 #endif /* DOCA_LOGGING_ALLOW_TRACE */
517 
536 #ifdef __linux__
537 
538 #if defined(__INTELLISENSE__)
539 #pragma diag_suppress 1094 /* attribute "constructor" does not take arguments */
540 #endif
541 
542 #define DOCA_LOG_REGISTER(source) \
543  static int log_source; \
544  /* Use the highest priority so other Ctors will be able to use the log */ \
545  static void __attribute__((constructor(101), used)) DOCA_LOG_CTOR_##__FILE__(void) \
546  { \
547  doca_log_register_source(#source, &log_source); \
548  }
549 
550 #else /* implicit windows */
551 
552 #ifdef __cplusplus
553 
554 class doca_log_registrator {
555 public:
556  doca_log_registrator(const char *source_name, int &log_source) noexcept
557  {
558  doca_log_register_source(source_name, &log_source);
559  m_log_source = log_source;
560  }
561 
562 private:
563  int m_log_source{0};
564 };
565 
566 #define DOCA_LOG_REGISTER(source) \
567  static int log_source{0}; \
568  static doca_log_registrator g_register_struct(#source, log_source)
569 
570 #else /* __cplusplus */
571 
579 #pragma section(".CRT$XCU", read)
580 #define DOCA_LOG_REGISTER(source) \
581  static int log_source = 0; \
582  static void _log_ctor_func(void); \
583  __pragma(data_seg(".CRT$XCU")) static void (*__doca_log_initializer)() = _log_ctor_func; \
584  __pragma(data_seg()) static void _log_ctor_func(void) \
585  { \
586  doca_log_register_source(#source, &log_source); \
587  }
588 
589 #endif /* __cplusplus */
590 
591 #endif /* __linux__ */
592 
593 #ifdef __cplusplus
594 } /* extern "C" */
595 #endif
596 
599 #endif /* DOCA_LOG_H_ */
#define __attribute__(_x_)
To allow compiling functions and structs that are using GCC attributes using attribute() in compilers...
Definition: doca_compat.h:81
#define DOCA_EXPERIMENTAL
To set a Symbol (or specifically a function) as experimental.
Definition: doca_compat.h:103
enum doca_error doca_error_t
DOCA API return codes.
DOCA_EXPERIMENTAL uint32_t doca_log_level_get_global_sdk_limit(void)
Get the global log level for SDK messages.
DOCA_EXPERIMENTAL doca_error_t doca_log_backend_set_level_upper_limit_strict(struct doca_log_backend *backend)
Mark the upper log level limit of a specific application logging backend for application messages as ...
DOCA_EXPERIMENTAL doca_error_t doca_log_level_set_global_lower_limit(uint32_t level)
Set the log level of ALL logging backends for application messages.
DOCA_EXPERIMENTAL doca_error_t doca_log_backend_create_standard(void)
Create default, non configurable backend for application messages.
doca_log_level
log levels, sorted by verbosity level from high to low
Definition: doca_log.h:43
DOCA_EXPERIMENTAL doca_error_t doca_log_backend_set_level_lower_limit_strict(struct doca_log_backend *backend)
Mark the lower log level limit of a specific logging backend for application messages as strict.
DOCA_EXPERIMENTAL doca_error_t doca_log_register_source(const char *source_name, int *source)
Register a log source.
DOCA_EXPERIMENTAL uint32_t doca_log_level_get_global_upper_limit(void)
Get the global upper log level for application messages.
void(* log_flush_callback)(char *buf)
logging backend flush() handler
Definition: doca_log.h:61
DOCA_EXPERIMENTAL uint32_t doca_log_level_get_global_lower_limit(void)
Get the global log level for application messages.
DOCA_EXPERIMENTAL doca_error_t doca_log_backend_create_with_fd(int fd, struct doca_log_backend **backend)
Create a logging backend for application messages with an fd stream.
DOCA_EXPERIMENTAL doca_error_t doca_log_backend_create_with_buf_sdk(char *buf, size_t capacity, log_flush_callback handler, struct doca_log_backend **backend)
Create a logging backend with a char buffer stream for SDK messages.
DOCA_EXPERIMENTAL doca_error_t doca_log_backend_create_with_syslog(const char *name, struct doca_log_backend **backend)
Create a logging backend for application messages with a syslog output.
DOCA_EXPERIMENTAL doca_error_t doca_log_level_set_global_sdk_limit(uint32_t level)
Set the log level of ALL logging backends for SDK messages.
DOCA_EXPERIMENTAL doca_error_t doca_log_level_set_global_upper_limit(uint32_t upper_limit)
Set the log upper level limit of ALL logging backends for application messages.
DOCA_EXPERIMENTAL doca_error_t doca_log_backend_create_with_fd_sdk(int fd, struct doca_log_backend **backend)
Create a logging backend with an fd stream for SDK messages.
DOCA_EXPERIMENTAL doca_error_t doca_log_backend_create_with_file_sdk(FILE *fptr, struct doca_log_backend **backend)
Create a logging backend with a FILE* stream for SDK messages.
DOCA_EXPERIMENTAL doca_error_t doca_log_backend_create_with_file(FILE *fptr, struct doca_log_backend **backend)
Create a logging backend for application messages with a FILE* stream.
DOCA_EXPERIMENTAL doca_error_t doca_log_unregister_source(int source)
Unregister a log source.
DOCA_EXPERIMENTAL doca_error_t doca_log(uint32_t level, int source, const char *fname, int line, const char *func, const char *format,...) __attribute__((format(printf
Generates an application log message.
DOCA_EXPERIMENTAL doca_error_t doca_log_backend_create_with_syslog_sdk(const char *name, struct doca_log_backend **backend)
Create a logging backend with a syslog output for SDK messages.
DOCA_EXPERIMENTAL doca_error_t doca_log_backend_set_sdk_level(struct doca_log_backend *backend, uint32_t level)
Set the log level limit for SDK logging backends.
DOCA_EXPERIMENTAL doca_error_t doca_log_backend_create_with_buf(char *buf, size_t capacity, log_flush_callback handler, struct doca_log_backend **backend)
Create a logging backend for application messages with a char buffer stream.
DOCA_EXPERIMENTAL doca_error_t doca_log_backend_set_level_upper_limit(struct doca_log_backend *backend, uint32_t upper_limit)
Set the upper log level limit of a specific logging backend for application messages.
DOCA_EXPERIMENTAL doca_error_t doca_log_backend_set_level_lower_limit(struct doca_log_backend *backend, uint32_t level)
Set the lower log level of a specific logging backend for application messages.
@ DOCA_LOG_LEVEL_TRACE
Definition: doca_log.h:50
@ DOCA_LOG_LEVEL_ERROR
Definition: doca_log.h:46
@ DOCA_LOG_LEVEL_WARNING
Definition: doca_log.h:47
@ DOCA_LOG_LEVEL_DISABLE
Definition: doca_log.h:44
@ DOCA_LOG_LEVEL_INFO
Definition: doca_log.h:48
@ DOCA_LOG_LEVEL_DEBUG
Definition: doca_log.h:49
@ DOCA_LOG_LEVEL_CRIT
Definition: doca_log.h:45