Virgil IoT KIT
Macros | Enumerations | Functions
logger.h File Reference

Logger implementation. More...

#include <logger-config.h>
#include <stdarg.h>
#include <string.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib-config.h>

Go to the source code of this file.

Macros

#define __FILENAME__   (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
 
#define VS_LOG_GET_LOGLEVEL(LOGLEV_VARIABLE)   (LOGLEV_VARIABLE) = vs_logger_get_loglev()
 Get current logging level to LOGLEV_VARIABLE. More...
 
#define VS_LOG_IS_LOGLEVEL(LOGLEV_VALUE)   vs_logger_is_loglev(LOGLEV_VALUE)
 Is LOGLEV_VALUE logging level is enabled. More...
 
#define VS_LOG_SET_LOGLEVEL(LOGLEV_VALUE)   vs_logger_set_loglev(LOGLEV_VALUE)
 Set LOGLEV_VALUE as current logging level. More...
 
#define VS_LOG(LGLVL, FRMT, ...)   vs_logger_message((LGLVL), __FILENAME__, __LINE__, (FRMT), ##__VA_ARGS__)
 Log message. More...
 
#define VS_LOG_HEX(LGLVL, PREFIX, BUF, SIZE)   vs_logger_message_hex((LGLVL), __FILENAME__, __LINE__, (PREFIX), (BUF), (SIZE))
 Log HEX buffer. More...
 
#define VS_LOG_MAKE_LEVEL(LEVEL)   LEVEL
 
#define VS_LOG_INFO(FRMT, ...)   vs_logger_message(VS_LOG_MAKE_LEVEL(VS_LOGLEV_INFO), __FILENAME__, __LINE__, (FRMT), ##__VA_ARGS__)
 Log message with VS_LOGLEV_INFO level. More...
 
#define VS_LOG_FATAL(FRMT, ...)   vs_logger_message(VS_LOG_MAKE_LEVEL(VS_LOGLEV_FATAL), __FILENAME__, __LINE__, (FRMT), ##__VA_ARGS__)
 Log message with VS_LOGLEV_FATAL level. More...
 
#define VS_LOG_ALERT(FRMT, ...)   vs_logger_message(VS_LOG_MAKE_LEVEL(VS_LOGLEV_ALERT), __FILENAME__, __LINE__, (FRMT), ##__VA_ARGS__)
 Log message with VS_LOGLEV_ALERT level. More...
 
#define VS_LOG_CRITICAL(FRMT, ...)   vs_logger_message(VS_LOG_MAKE_LEVEL(VS_LOGLEV_CRITICAL), __FILENAME__, __LINE__, (FRMT), ##__VA_ARGS__)
 Log message with VS_LOGLEV_CRITICAL level. More...
 
#define VS_LOG_ERROR(FRMT, ...)   vs_logger_message(VS_LOG_MAKE_LEVEL(VS_LOGLEV_ERROR), __FILENAME__, __LINE__, (FRMT), ##__VA_ARGS__)
 Log message with VS_LOGLEV_ERROR level. More...
 
#define VS_LOG_WARNING(FRMT, ...)   vs_logger_message(VS_LOG_MAKE_LEVEL(VS_LOGLEV_WARNING), __FILENAME__, __LINE__, (FRMT), ##__VA_ARGS__)
 Log message with VS_LOGLEV_WARNING level. More...
 
#define VS_LOG_NOTICE(FRMT, ...)   vs_logger_message(VS_LOG_MAKE_LEVEL(VS_LOGLEV_NOTICE), __FILENAME__, __LINE__, (FRMT), ##__VA_ARGS__)
 Log message with VS_LOGLEV_NOTICE level. More...
 
#define VS_LOG_TRACE(FRMT, ...)   vs_logger_message(VS_LOG_MAKE_LEVEL(VS_LOGLEV_TRACE), __FILENAME__, __LINE__, (FRMT), ##__VA_ARGS__)
 Log message with VS_LOGLEV_TRACE level. More...
 
#define VS_LOG_DEBUG(FRMT, ...)   vs_logger_message(VS_LOG_MAKE_LEVEL(VS_LOGLEV_DEBUG), __FILENAME__, __LINE__, (FRMT), ##__VA_ARGS__)
 Log message with VS_LOGLEV_DEBUG level. More...
 

Enumerations

enum  vs_log_level_t {
  VS_LOGLEV_UNKNOWN = 0xFF, VS_LOGLEV_NO_LOGGER = 0xFE, VS_LOGLEV_INFO = 0x00, VS_LOGLEV_FATAL = 0x10,
  VS_LOGLEV_ALERT = 0x20, VS_LOGLEV_CRITICAL = 0x30, VS_LOGLEV_ERROR = 0x40, VS_LOGLEV_WARNING = 0x50,
  VS_LOGLEV_NOTICE = 0x60, VS_LOGLEV_TRACE = 0x70, VS_LOGLEV_DEBUG = 0xFD
}
 Logging level. More...
 

Functions

bool vs_log_thread_descriptor (const char *description)
 Set current thread description. More...
 
bool vs_logger_last_result (void)
 Last result. More...
 
void vs_logger_message (vs_log_level_t level, const char *cur_filename, uint32_t line_num, const char *log_format,...)
 Log text message. More...
 
void vs_logger_init (vs_log_level_t log_level)
 Initialize logging level. More...
 
vs_log_level_t vs_logger_set_loglev (vs_log_level_t new_level)
 Set current logging level. More...
 
vs_log_level_t vs_logger_get_loglev (void)
 Get current logging level. More...
 
bool vs_logger_is_loglev (vs_log_level_t level)
 Check that specified logging level is enabled. More...
 
void vs_logger_message_hex (vs_log_level_t level, const char *cur_filename, uint32_t line_num, const char *prefix, const void *data_buf, const uint16_t data_size)
 Log HEX buffer. More...
 

Detailed Description

Logger implementation.

Logger allows to log messages to output stream that can be file or screen. User can enable different logging levels like debug, info, error etc. (see vs_log_level_t). In future versions sending report through network is planned

Logger Usage

See the following snippet for the logger usage examples :

vs_logger_init( VS_LOGLEV_WARNING ); // Warning level and upper ones are enabled
VS_LOG_FATAL("Serious error"); // Outputs fatal error if enabled
// Hex data output
const uint8_t raw_data[] = "Some raw data";
VS_LOG_HEX(VS_LOGLEV_DEBUG, "Raw data : ", raw_data, sizeof(raw_data));
// Some operation with false positive errors, need to temporary disable error messages
vs_log_level_t prev_log_level;
prev_log_level = vs_logger_set_loglev(VS_LOGLEV_FATAL); // Enable fatal level and upper ones
// ...
vs_logger_set_loglev(prev_log_level); // Restore previous logger level
Warning
If VS_IOT_LOGGER_USE_LIBRARY == 1, vs_logger_output_hal implementation is required. See Logger HAL Implementation for details.

Macro Definition Documentation

◆ __FILENAME__

#define __FILENAME__   (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)

◆ VS_LOG

#define VS_LOG (   LGLVL,
  FRMT,
  ... 
)    vs_logger_message((LGLVL), __FILENAME__, __LINE__, (FRMT), ##__VA_ARGS__)

Log message.

Sends FRMT message by calling vs_logger_message function.

Parameters
[in]LGLVLLogging level.
[in]FRMTprintf-like format string and arguments.

◆ VS_LOG_ALERT

#define VS_LOG_ALERT (   FRMT,
  ... 
)    vs_logger_message(VS_LOG_MAKE_LEVEL(VS_LOGLEV_ALERT), __FILENAME__, __LINE__, (FRMT), ##__VA_ARGS__)

Log message with VS_LOGLEV_ALERT level.

◆ VS_LOG_CRITICAL

#define VS_LOG_CRITICAL (   FRMT,
  ... 
)    vs_logger_message(VS_LOG_MAKE_LEVEL(VS_LOGLEV_CRITICAL), __FILENAME__, __LINE__, (FRMT), ##__VA_ARGS__)

Log message with VS_LOGLEV_CRITICAL level.

◆ VS_LOG_DEBUG

#define VS_LOG_DEBUG (   FRMT,
  ... 
)    vs_logger_message(VS_LOG_MAKE_LEVEL(VS_LOGLEV_DEBUG), __FILENAME__, __LINE__, (FRMT), ##__VA_ARGS__)

Log message with VS_LOGLEV_DEBUG level.

◆ VS_LOG_ERROR

#define VS_LOG_ERROR (   FRMT,
  ... 
)    vs_logger_message(VS_LOG_MAKE_LEVEL(VS_LOGLEV_ERROR), __FILENAME__, __LINE__, (FRMT), ##__VA_ARGS__)

Log message with VS_LOGLEV_ERROR level.

◆ VS_LOG_FATAL

#define VS_LOG_FATAL (   FRMT,
  ... 
)    vs_logger_message(VS_LOG_MAKE_LEVEL(VS_LOGLEV_FATAL), __FILENAME__, __LINE__, (FRMT), ##__VA_ARGS__)

Log message with VS_LOGLEV_FATAL level.

◆ VS_LOG_GET_LOGLEVEL

#define VS_LOG_GET_LOGLEVEL (   LOGLEV_VARIABLE)    (LOGLEV_VARIABLE) = vs_logger_get_loglev()

Get current logging level to LOGLEV_VARIABLE.

◆ VS_LOG_HEX

#define VS_LOG_HEX (   LGLVL,
  PREFIX,
  BUF,
  SIZE 
)    vs_logger_message_hex((LGLVL), __FILENAME__, __LINE__, (PREFIX), (BUF), (SIZE))

Log HEX buffer.

Sends BUF as hex string by calling vs_logger_message function.

Parameters
[in]LGLVLLogging level
[in]PREFIXPrefix for output. Must not be NULL.
[in]BUFBuffer to be output as HEX. Must not be NULL.
[in]SIZESize of the array to be output as HEX. Must not be zero.

◆ VS_LOG_INFO

#define VS_LOG_INFO (   FRMT,
  ... 
)    vs_logger_message(VS_LOG_MAKE_LEVEL(VS_LOGLEV_INFO), __FILENAME__, __LINE__, (FRMT), ##__VA_ARGS__)

Log message with VS_LOGLEV_INFO level.

◆ VS_LOG_IS_LOGLEVEL

#define VS_LOG_IS_LOGLEVEL (   LOGLEV_VALUE)    vs_logger_is_loglev(LOGLEV_VALUE)

Is LOGLEV_VALUE logging level is enabled.

◆ VS_LOG_MAKE_LEVEL

#define VS_LOG_MAKE_LEVEL (   LEVEL)    LEVEL

◆ VS_LOG_NOTICE

#define VS_LOG_NOTICE (   FRMT,
  ... 
)    vs_logger_message(VS_LOG_MAKE_LEVEL(VS_LOGLEV_NOTICE), __FILENAME__, __LINE__, (FRMT), ##__VA_ARGS__)

Log message with VS_LOGLEV_NOTICE level.

◆ VS_LOG_SET_LOGLEVEL

#define VS_LOG_SET_LOGLEVEL (   LOGLEV_VALUE)    vs_logger_set_loglev(LOGLEV_VALUE)

Set LOGLEV_VALUE as current logging level.

◆ VS_LOG_TRACE

#define VS_LOG_TRACE (   FRMT,
  ... 
)    vs_logger_message(VS_LOG_MAKE_LEVEL(VS_LOGLEV_TRACE), __FILENAME__, __LINE__, (FRMT), ##__VA_ARGS__)

Log message with VS_LOGLEV_TRACE level.

◆ VS_LOG_WARNING

#define VS_LOG_WARNING (   FRMT,
  ... 
)    vs_logger_message(VS_LOG_MAKE_LEVEL(VS_LOGLEV_WARNING), __FILENAME__, __LINE__, (FRMT), ##__VA_ARGS__)

Log message with VS_LOGLEV_WARNING level.

Enumeration Type Documentation

◆ vs_log_level_t

Logging level.

Enumerator
VS_LOGLEV_UNKNOWN 

Invalid logging level.

VS_LOGLEV_NO_LOGGER 

Logging is disabled.

VS_LOGLEV_INFO 

Information messages.

VS_LOGLEV_FATAL 

Fatal messages.

VS_LOGLEV_ALERT 

Alert messages.

VS_LOGLEV_CRITICAL 

Critical messages.

VS_LOGLEV_ERROR 

Error messages.

VS_LOGLEV_WARNING 

Warning messages.

VS_LOGLEV_NOTICE 

Notifications.

VS_LOGLEV_TRACE 

Trace messages.

VS_LOGLEV_DEBUG 

Debug messages.

Function Documentation

◆ vs_log_thread_descriptor()

bool vs_log_thread_descriptor ( const char *  description)

Set current thread description.

Note
This function does nothing if VS_IOT_LOGGER_OUTPUT_THREAD_ID is not set to 1
Parameters
descriptionHuman readable thread description.
Warning
description must be static string pointer valid during program functioning
Returns
false if no space is available to store thread description

◆ vs_logger_get_loglev()

vs_log_level_t vs_logger_get_loglev ( void  )

Get current logging level.

Returns
VS_LOGLEV_UNKNOWN in cse of error

◆ vs_logger_init()

void vs_logger_init ( vs_log_level_t  log_level)

Initialize logging level.

log_level is initialized and default title for thread is provided. You can change this name by vs_log_thread_descriptor call

Parameters
[int]log_level Message log level

◆ vs_logger_is_loglev()

bool vs_logger_is_loglev ( vs_log_level_t  level)

Check that specified logging level is enabled.

Parameters
[in]levelLogging level to be tested
Returns
true if specified logging level is enabled and there are now any error

◆ vs_logger_last_result()

bool vs_logger_last_result ( void  )

Last result.

Return true if there were no errors during last call and log level was appropriate.

Returns
true if last output was successful and false otherwise

◆ vs_logger_message()

void vs_logger_message ( vs_log_level_t  level,
const char *  cur_filename,
uint32_t  line_num,
const char *  log_format,
  ... 
)

Log text message.

Sends FRMT output message with current time if VS_IOT_LOGGER_OUTPUT_TIME is enabled, filename, line number and user message. Buffer size is limited by VS_IOT_LOGGER_MAX_BUFFER_SIZE define.

Parameters
[in]levelMessage log level
[in]cur_filenameSource code file name. If NULL, there will be no filename information.
[in]line_numSource code line number. If zero, there will be no line number.
[in]log_formatprintf like string
Returns
true if there were no errors and string has not been cut

◆ vs_logger_message_hex()

void vs_logger_message_hex ( vs_log_level_t  level,
const char *  cur_filename,
uint32_t  line_num,
const char *  prefix,
const void *  data_buf,
const uint16_t  data_size 
)

Log HEX buffer.

Sends BUF as hex string by calling vs_logger_message function.

Parameters
[in]levelLogging level
[in]cur_filenameSource code file name. If NULL, there will be no filename information.
[in]line_numSource code line number. If zero, there will be no line number.
[in]prefixPrefix for output. Must not be NULL.
[in]data_bufBuffer to be output as HEX. Must not be NULL.
[in]SIZESize of the array to be output as HEX. Must not be zero.

◆ vs_logger_set_loglev()

vs_log_level_t vs_logger_set_loglev ( vs_log_level_t  new_level)

Set current logging level.

Parameters
[in]new_levelNew logging level to be initialized
Returns
vs_log_level_t previous log level
VS_LOGLEV_WARNING
@ VS_LOGLEV_WARNING
Warning messages.
Definition: logger.h:91
VS_LOGLEV_DEBUG
@ VS_LOGLEV_DEBUG
Debug messages.
Definition: logger.h:95
VS_LOGLEV_FATAL
@ VS_LOGLEV_FATAL
Fatal messages.
Definition: logger.h:87
vs_logger_set_loglev
vs_log_level_t vs_logger_set_loglev(vs_log_level_t new_level)
Set current logging level.
vs_log_level_t
vs_log_level_t
Logging level.
Definition: logger.h:82
vs_logger_init
void vs_logger_init(vs_log_level_t log_level)
Initialize logging level.
VS_LOG_FATAL
#define VS_LOG_FATAL(FRMT,...)
Log message with VS_LOGLEV_FATAL level.
Definition: logger.h:158
VS_LOG_HEX
#define VS_LOG_HEX(LGLVL, PREFIX, BUF, SIZE)
Log HEX buffer.
Definition: logger.h:133