Skip to content

Commit

Permalink
log levels added to yasmin logs
Browse files Browse the repository at this point in the history
  • Loading branch information
mgonzs13 committed Feb 2, 2025
1 parent 47201cc commit cf76faf
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 8 deletions.
45 changes: 45 additions & 0 deletions yasmin/include/yasmin/logs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,37 @@ extern LogFunction log_warn; ///< Pointer to the warning logging function
extern LogFunction log_info; ///< Pointer to the info logging function
extern LogFunction log_debug; ///< Pointer to the debug logging function

/**
* @brief Enum representing different log levels for controlling log verbosity.
*
* This enum defines the severity levels of logs that can be used to control
* which log messages should be displayed. The levels are ordered from most
* severe to least severe. Only logs at or above the current log level will be
* shown.
*/
enum LogLevel {
/// Log level for error messages. Only critical errors should be logged.
ERROR = 0,
/// Log level for warning messages. Indicate potential issues that are not
/// critical.
WARN,
/// Log level for informational messages. General runtime information about
/// the system's state.
INFO,
/// Log level for debug messages. Used for detailed information, mainly for
/// developers.
DEBUG
};

/**
* @brief The current log level for the application.
*
* This global variable holds the current log level, which determines the
* verbosity of the logs. Logs at or above this level will be displayed. The
* default level is set to INFO.
*/
extern LogLevel log_level;

/**
* @brief Extracts the filename from a given file path.
*
Expand All @@ -63,15 +94,19 @@ inline const char *extract_filename(const char *path) {

// Macros for logging with automatic file and function information
#define YASMIN_LOG_ERROR(text, ...) \
if (yasmin::log_level >= yasmin::ERROR) \
yasmin::log_error(extract_filename(__FILE__), __FUNCTION__, __LINE__, text, \
##__VA_ARGS__)
#define YASMIN_LOG_WARN(text, ...) \
if (yasmin::log_level >= yasmin::WARN) \
yasmin::log_warn(extract_filename(__FILE__), __FUNCTION__, __LINE__, text, \
##__VA_ARGS__)
#define YASMIN_LOG_INFO(text, ...) \
if (yasmin::log_level >= yasmin::INFO) \
yasmin::log_info(extract_filename(__FILE__), __FUNCTION__, __LINE__, text, \
##__VA_ARGS__)
#define YASMIN_LOG_DEBUG(text, ...) \
if (yasmin::log_level >= yasmin::DEBUG) \
yasmin::log_debug(extract_filename(__FILE__), __FUNCTION__, __LINE__, text, \
##__VA_ARGS__)

Expand All @@ -98,6 +133,16 @@ void set_loggers(LogFunction error, LogFunction warn, LogFunction info,
*/
void set_default_loggers();

/**
* @brief Sets the log level for the logs.
*
* This function allows the user to specify the log level error, warning, info,
* or debug.
*
* @param log_level Log level.
*/
void set_log_level(LogLevel log_level);

} // namespace yasmin

#endif // YASMIN__LOGS_HPP
5 changes: 5 additions & 0 deletions yasmin/src/yasmin/logs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,9 @@ void set_default_loggers() {
default_log_debug);
}

// Initialize the log level to INFO
LogLevel log_level = INFO;

void set_log_level(LogLevel log_level) { log_level = log_level; }

} // namespace yasmin
3 changes: 3 additions & 0 deletions yasmin/yasmin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
from yasmin.state_machine import StateMachine

from yasmin.logs import (
LogLevel,
set_log_level,
log_level,
set_loggers,
YASMIN_LOG_ERROR,
YASMIN_LOG_WARN,
Expand Down
59 changes: 51 additions & 8 deletions yasmin/yasmin/logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@
import os
import inspect
import logging
from enum import IntEnum
from typing import Callable

import yasmin

__all__ = [
"LogLevel",
"set_log_level",
"log_level",
"set_loggers",
"YASMIN_LOG_ERROR",
"YASMIN_LOG_WARN",
Expand All @@ -33,6 +37,41 @@
logging.basicConfig(level=logging.NOTSET, format="%(message)s")


class LogLevel(IntEnum):
"""
@enum LogLevel
@brief Enumeration for different log levels.
Defines the available log levels for controlling verbosity in the
logging system.
"""

## Log level for error messages. Only critical errors should be logged.
ERROR = 0
## Log level for warning messages. Indicate potential issues that are not critical.
WARN = 1
## Log level for informational messages. General runtime information about the system's state.
INFO = 2
## Log level for debug messages. Used for detailed information, mainly for developers.
DEBUG = 3


## The current log level for the application.
log_level = LogLevel.DEBUG


## Sets the log level for the logs.
def set_log_level(level: LogLevel) -> None:
"""
@brief Set the log level for the YASMIN framework.
Adjusts the log level to control the verbosity of logged messages.
@param level The new log level to be set.
"""
yasmin.log_level = level


def get_caller_info():
"""
Retrieve information about the caller of the current function.
Expand Down Expand Up @@ -62,8 +101,9 @@ def YASMIN_LOG_ERROR(text: str) -> None:
@return: None
"""
file, function, line = get_caller_info()
logging.error(f"[ERROR] [{file}:{function}:{line}] {text}")
if yasmin.log_level >= LogLevel.ERROR:
file, function, line = get_caller_info()
logging.error(f"[ERROR] [{file}:{function}:{line}] {text}")


def YASMIN_LOG_WARN(text: str) -> None:
Expand All @@ -78,8 +118,9 @@ def YASMIN_LOG_WARN(text: str) -> None:
@return: None
"""
file, function, line = get_caller_info()
logging.warning(f"[WARN] [{file}:{function}:{line}] {text}")
if yasmin.log_level >= LogLevel.WARN:
file, function, line = get_caller_info()
logging.warning(f"[WARN] [{file}:{function}:{line}] {text}")


def YASMIN_LOG_INFO(text: str) -> None:
Expand All @@ -94,8 +135,9 @@ def YASMIN_LOG_INFO(text: str) -> None:
@return: None
"""
file, function, line = get_caller_info()
logging.info(f"[INFO] [{file}:{function}:{line}] {text}")
if yasmin.log_level >= LogLevel.INFO:
file, function, line = get_caller_info()
logging.info(f"[INFO] [{file}:{function}:{line}] {text}")


def YASMIN_LOG_DEBUG(text: str) -> None:
Expand All @@ -110,8 +152,9 @@ def YASMIN_LOG_DEBUG(text: str) -> None:
@return: None
"""
file, function, line = get_caller_info()
logging.debug(f"[DEBUG] [{file}:{function}:{line}] {text}")
if yasmin.log_level >= LogLevel.DEBUG:
file, function, line = get_caller_info()
logging.debug(f"[DEBUG] [{file}:{function}:{line}] {text}")


def set_loggers(
Expand Down

0 comments on commit cf76faf

Please sign in to comment.