CNDP  22.08.0
cne_log.h File Reference
#include <stdio.h>
#include <stdarg.h>
#include <stdint.h>
#include <cne_common.h>
#include <cne_stdio.h>
#include "cne_build_config.h"

Go to the source code of this file.

Macros

#define CNE_LOG(f, ...)   cne_log(CNE_LOG_##f, __func__, __LINE__, #f ": " __VA_ARGS__)
 
#define CNE_PRINT(f, args...)   cne_print(f, ##args)
 
#define CNE_EMERG(...)   CNE_LOG(EMERG, __VA_ARGS__)
 
#define CNE_ERR_RET_VAL(_val, ...)
 
#define CNE_RET(...)   CNE_ERR_RET_VAL(, __VA_ARGS__)
 
#define CNE_ERR_RET(...)   CNE_ERR_RET_VAL(-1, __VA_ARGS__)
 
#define CNE_NULL_RET(...)   CNE_ERR_RET_VAL(NULL, __VA_ARGS__)
 
#define CNE_ERR_GOTO(lbl, ...)
 
#define cne_panic(format, args...)   __cne_panic(__func__, __LINE__, format "\n", ##args)
 
#define cne_exit(format, args...)   __cne_exit(__func__, __LINE__, format "\n", ##args)
 
#define CNE_VERIFY(exp)
 

Functions

CNDP_API void cne_log_set_level (uint32_t level)
 
CNDP_API int cne_log_set_level_str (char *log_level)
 
CNDP_API uint32_t cne_log_get_level (void)
 
CNDP_API int cne_log (uint32_t level, const char *func, int line, const char *format,...)
 
CNDP_API int cne_print (const char *format,...)
 
CNDP_API int cne_vlog (uint32_t level, const char *func, int line, const char *format, va_list ap)
 
CNDP_API void cne_dump_stack (void)
 
CNDP_API void __cne_panic (const char *funcname, int line, const char *format,...)
 
CNDP_API void __cne_exit (const char *func, int line, const char *format,...)
 

Detailed Description

CNE Logs API

This file provides a log API to CNE applications.

Definition in file cne_log.h.

Macro Definition Documentation

◆ CNE_LOG

#define CNE_LOG (   f,
  ... 
)    cne_log(CNE_LOG_##f, __func__, __LINE__, #f ": " __VA_ARGS__)

Generates a log message.

The CNE_LOG() macro is a helper that prefixes the string with the log level, function name, line number, and calls cne_log().

Parameters
fLog level. A value between EMERG (1) and DEBUG (8). The short name is expanded by the macro, so it cannot be an integer value.
...The fmt string, as in printf(3), followed by the variable arguments required by the format.
Returns
  • The number of characters printed on success.
  • A negative value on error.

Definition at line 184 of file cne_log.h.

◆ CNE_PRINT

#define CNE_PRINT (   f,
  args... 
)    cne_print(f, ##args)

Generates a log message regardless of log level.

Parameters
fThe fmt string, as in printf(3), followed by the variable arguments required by the format.
argsVariable arguments depend on Application.
Returns
  • The number of characters printed on success.
  • A negative value on error.

Definition at line 198 of file cne_log.h.

◆ CNE_EMERG

#define CNE_EMERG (   ...)    CNE_LOG(EMERG, __VA_ARGS__)

Macros for the normal cases with CNE_LOG() levels

Definition at line 203 of file cne_log.h.

◆ CNE_ERR_RET_VAL

#define CNE_ERR_RET_VAL (   _val,
  ... 
)
Value:
do { \
CNE_ERR(__VA_ARGS__); \
return _val; \
} while ((0))

Generate an Error log message and return value

Same as CNE_LOG(ERR,...) define, but returns -1 to enable this style of coding. if (val == error) { CNE_ERR("Error: Failed\n"); return -1; } Returning _val to the calling function.

Definition at line 222 of file cne_log.h.

◆ CNE_RET

#define CNE_RET (   ...)    CNE_ERR_RET_VAL(, __VA_ARGS__)

Generate an Error log message and return

Same as CNE_LOG(ERR,...) define, but returns to enable this style of coding. if (val == error) { CNE_ERR("Error: Failed\n"); return; } Returning to the calling function.

Examples
examples/cnet-graph/cnet-graph.c, examples/cnet-quic/cnet-quic.c, examples/cnet-quic/quic-cli.c, examples/ibroker/main.c, examples/phil/main.c, and examples/phil/phil.c.

Definition at line 238 of file cne_log.h.

◆ CNE_ERR_RET

◆ CNE_NULL_RET

#define CNE_NULL_RET (   ...)    CNE_ERR_RET_VAL(NULL, __VA_ARGS__)

Generate an Error log message and return NULL

Same as CNE_LOG(ERR,...) define, but returns NULL to enable this style of coding. if (val == error) { CNE_ERR("Error: Failed\n"); return NULL; } Returning a NULL to the calling function.

Examples
examples/cli/main.c.

Definition at line 262 of file cne_log.h.

◆ CNE_ERR_GOTO

#define CNE_ERR_GOTO (   lbl,
  ... 
)
Value:
do { \
CNE_ERR(__VA_ARGS__); \
goto lbl; \
} while ((0))

Generate a Error log message and goto label

Same as CNE_LOG(ERR,...) define, but goes to a label to enable this style of coding. if (error condition) { CNE_ERR("Error: Failed\n"); goto lbl; }

Examples
examples/cli/main.c, examples/cndpfwd/acl-func.c, examples/cndpfwd/main.c, examples/cnet-graph/cnet-graph.c, examples/cnet-quic/cnet-quic.c, examples/cnet-quic/parse-args.c, examples/cnet-quic/quic-cli.c, examples/ibroker/main.c, examples/l3fwd-graph/fwd.c, and examples/phil/phil.c.

Definition at line 273 of file cne_log.h.

◆ cne_panic

#define cne_panic (   format,
  args... 
)    __cne_panic(__func__, __LINE__, format "\n", ##args)

Provide notification of a critical non-recoverable error and terminate execution abnormally.

Display the format string and its expanded arguments (printf-like).

In a linux environment, this function dumps the stack and calls abort() resulting in a core dump if enabled.

The function never returns.

Parameters
formatThe format string, followed by the variable list of arguments.
argsVariable arguments depend on Application.
Examples
examples/helloworld/main.c.

Definition at line 300 of file cne_log.h.

◆ cne_exit

#define cne_exit (   format,
  args... 
)    __cne_exit(__func__, __LINE__, format "\n", ##args)

Provide notification of a critical non-recoverable error and terminate execution abnormally by calling exit(-1).

Display the format string and its expanded arguments (printf-like).

The function never returns.

Parameters
formatThe format string, followed by the variable list of arguments.
argsVariable arguments depend on Application.
Examples
examples/cndpfwd/main.c, and examples/dlb_test/dlb_test.c.

Definition at line 315 of file cne_log.h.

◆ CNE_VERIFY

#define CNE_VERIFY (   exp)
Value:
do { \
if (unlikely(!(exp))) \
cne_panic("assert \"%s\" failed", #exp); \
} while (0)
#define unlikely(x)

Verify an expression is true

Calls cne_panic() when the expression is false.

Parameters
expThe expression to test

Definition at line 342 of file cne_log.h.

Function Documentation

◆ cne_log_set_level()

CNDP_API void cne_log_set_level ( uint32_t  level)

Set the log level for a given type.

Parameters
levelThe level to be set.

◆ cne_log_set_level_str()

CNDP_API int cne_log_set_level_str ( char *  log_level)

Set the log level by string.

Parameters
log_levelThe level to be set.
Returns
0 for success, 1 on error.
Examples
examples/cndpfwd/parse-args.c, examples/cnet-graph/parse-args.c, examples/cnet-quic/parse-args.c, and examples/l3fwd-graph/parse-args.c.

◆ cne_log_get_level()

CNDP_API uint32_t cne_log_get_level ( void  )

Get the log level.

Returns
The current log level.
Examples
examples/cnet-graph/cnet-graph.c.

◆ cne_log()

CNDP_API int cne_log ( uint32_t  level,
const char *  func,
int  line,
const char *  format,
  ... 
)

Generates a log message.

The message will be sent to stdout.

The level argument determines if the log should be displayed or not, depending on the global cne_loglevel variable.

The preferred alternative is the CNE_LOG() macro because it adds the level in the logged string and sets the function name and line number automatically.

Parameters
levelLog level. A value between CNE_LOG_EMERG (1) and CNE_LOG_DEBUG (8).
funcFunction name.
lineLine Number.
formatThe format string, as in printf(3), followed by the variable arguments required by the format.
Returns
  • The number of characters printed on success.
  • A negative value on error.

◆ cne_print()

CNDP_API int cne_print ( const char *  format,
  ... 
)

Generates a log message regardless of log level.

The message will be sent to stdout.

Parameters
formatThe format string, as in printf(3), followed by the variable arguments required by the format.
Returns
  • The number of characters printed on success.
  • A negative value on error.

◆ cne_vlog()

CNDP_API int cne_vlog ( uint32_t  level,
const char *  func,
int  line,
const char *  format,
va_list  ap 
)

Generates a log message.

The message will be sent to stdout.

The level argument determines if the log should be displayed or not, depending on the global cne_loglevel variable.

The preferred alternative is the CNE_LOG() macro because it adds the level in the logged string and sets the function name and line number automatically.

Parameters
levelLog level. A value between CNE_LOG_EMERG (1) and CNE_LOG_DEBUG (8).
funcFunction name.
lineLine Number.
formatThe format string, as in printf(3), followed by the variable arguments required by the format.
apThe va_list of the variable arguments required by the format.
Returns
  • The number of characters printed on success.
  • A negative value on error.

◆ cne_dump_stack()

CNDP_API void cne_dump_stack ( void  )

Dump the stack of the calling core to the console.

◆ __cne_panic()

CNDP_API void __cne_panic ( const char *  funcname,
int  line,
const char *  format,
  ... 
)

Provide notification of a critical non-recoverable error and stop.

This function should not be called directly. Refer to cne_panic() macro documentation.

◆ __cne_exit()

CNDP_API void __cne_exit ( const char *  func,
int  line,
const char *  format,
  ... 
)

Provide notification of a critical non-recoverable error and stop.

This function should not be called directly. Refer to cne_exit() macro documentation.