Anjay Lite
Loading...
Searching...
No Matches
log.h File Reference
#include <anj/init.h>
#include <stdio.h>
#include <anj/compat/log_impl_decls.h>
#include <anj/utils.h>
#include <anj_internal/log/log_filtering_utils.h>

Go to the source code of this file.

Macros

#define ANJ_INTERNAL_INCLUDE_LOG_FILTERING_UTILS
 
#define ANJ_LOG_PRINTF_ARG_CHECK(...)   ((void) sizeof(printf(__VA_ARGS__)))
 
#define ANJ_LOG_ENFORCE_FORMAT_INLINE_LITERAL(...)   "" __VA_ARGS__
 
#define ANJ_LOG_COMPILE_TIME_CHECK(...)
 
#define ANJ_LOG_IF_ALLOWED(Module, LogLevel, ...)   ((void) 0)
 
#define anj_log(Module, LogLevel, ...)
 
#define ANJ_LOG_DISPOSABLE_IMPL(Arg)   " "
 
#define ANJ_LOG_DISPOSABLE(Arg)   ANJ_LOG_DISPOSABLE_IMPL(Arg)
 

Macro Definition Documentation

◆ ANJ_INTERNAL_INCLUDE_LOG_FILTERING_UTILS

#define ANJ_INTERNAL_INCLUDE_LOG_FILTERING_UTILS

◆ anj_log

#define anj_log (   Module,
  LogLevel,
  ... 
)
Value:
((void) (ANJ_LOG_IF_ALLOWED(Module, LogLevel, __VA_ARGS__), \
ANJ_LOG_COMPILE_TIME_CHECK(__VA_ARGS__)))
#define ANJ_LOG_IF_ALLOWED(Module, LogLevel,...)
Definition log.h:87

Logs a message.

Log statements are a subject to compile-time filtering. The level of this statement must be equal or higher than the configured level of the module, if defined, or the default level ( ANJ_LOG_LEVEL_DEFAULT ).

anj_log(my_module, L_DEBUG, "Hello %s, %d!", "world", 42);
#define anj_log(Module, LogLevel,...)
Definition log.h:114

NOTE: Only following format specifiers are allowed in the format string:

  • %%, %s, %f,
  • %d, %ld, %lld, %zd,
  • %u, %lu, %llu, %zu. Behavior if other format specifiers are provided is undefined.
Parameters
ModuleName of the module that generates the message, given as a raw token.
LogLevelLog level, specified as a name of anj_log_level_t (other than L_MUTED) with the leading ANJ_LOG_LEVEL_ omitted.

◆ ANJ_LOG_COMPILE_TIME_CHECK

#define ANJ_LOG_COMPILE_TIME_CHECK (   ...)
Value:
#define ANJ_LOG_ENFORCE_FORMAT_INLINE_LITERAL(...)
Definition log.h:41
#define ANJ_LOG_PRINTF_ARG_CHECK(...)
Definition log.h:35

This macro is a best-effort check that:

  • only a string literal is a valid argument as a format, so that the line number of a log statement can be used to determine the provided format string if it's stripped from the application
  • enables emitting warnings when the provided format arguments do not match the format string, if the compiler supports it

◆ ANJ_LOG_DISPOSABLE

#define ANJ_LOG_DISPOSABLE (   Arg)    ANJ_LOG_DISPOSABLE_IMPL(Arg)

Replaces a string constant with " " if ANJ_LOG_STRIP_CONSTANTS is enabled. This is useful for wrapping constant parts of log messages, to shorten them, and therefore reduce the size of the binary.

anj_log(my_module, L_DEBUG, ANJ_LOG_DISPOSABLE("The result is: ") "%d", 42);
#define ANJ_LOG_DISPOSABLE(Arg)
Definition log.h:137

NOTE: Provided string constants shall not contain any format specifiers.

Parameters
ArgA string constant to be potentially replaced with " ".

◆ ANJ_LOG_DISPOSABLE_IMPL

#define ANJ_LOG_DISPOSABLE_IMPL (   Arg)    " "

◆ ANJ_LOG_ENFORCE_FORMAT_INLINE_LITERAL

#define ANJ_LOG_ENFORCE_FORMAT_INLINE_LITERAL (   ...)    "" __VA_ARGS__

Enforces the format to be an inline string literal, by concatenating it (the first vararg) with an empty string literal.

◆ ANJ_LOG_IF_ALLOWED

#define ANJ_LOG_IF_ALLOWED (   Module,
  LogLevel,
  ... 
)    ((void) 0)

◆ ANJ_LOG_PRINTF_ARG_CHECK

#define ANJ_LOG_PRINTF_ARG_CHECK (   ...)    ((void) sizeof(printf(__VA_ARGS__)))

Makes compiler emit warnings for provided format string and arguments without any overhead for the resulting application, as whole expression is wrapped into sizeof(...) that if calculated at compile time.

Value of sizeof(...) is later discarded by using comma operator.