Code Monkey home page Code Monkey logo

library-esp32-logger's Introduction

MyLogging Library

Esta libreria es un complemento simple al Serial.x(Arduino) o log_x(ESP) para habilitar dinamicamente niveles de logging en el codigo.

Warning Documentacion en desarrollo

Caracteristicas

  • Five log levels: NONE, ERROR, WARN, INFO, DEBUG, and VERBOSE.
  • Flexible configuration to control the log level for each logger individually.
  • Optional mutex support for thread safety in multithreaded applications.
  • Easy integration into Arduino projects.
  • Logger list creation as map , to change logging levels dynamically.

Instalacion

PlatformIO

  1. añadir a platformio.ini:

     lib_deps = https://github.com/manuelgonzalvez/library-esp32-logger.git
    

Usage

Creating Loggers

Si se van a crear varias instancias del objeto, es conveniente declararlas previamente como externas en una clase en comun que permita realizar operaciones sobre las mismas.

Para crear un objeto "logger", añadir el header de la libreria

+-- [helpers.h]

#include "ESP32LoggingHelper.h"

extern logger myLoggerA;
extern logger myLoggerB;

classA.cpp

#include "helpers.h"

// Create a logger with a specific log level and name, and add it to the loggerList array
logger myLoggerA(LOGGER_DEBUG, "myLoggerA",true);

classB.cpp

#include "helpers.h"

// Create a logger with a specific log level and name, logger not added to the list.
logger myLoggerB(LOGGER_DEBUG, "myLoggerB");

Logging Messages

Once you have created a logger, you can use it to log messages at different log levels. The library provides five log levels: ERROR, WARN, INFO, DEBUG, and VERBOSE. The log level of each logger determines which messages will be printed.

if(myLogger.printD()) // Log an error message
{
log_d("Uptime: %zu", ScaleData.uptime);
}

resultara en:

[nodos] [D][myNodes.cpp:932] checkNodeUptime(): Uptime: 9172 

Setting Log Levels

You can change the log level of a logger at any time using the setLevel() method. This allows you to adjust the verbosity of your output dynamically.

myLogger.setLevel(LOGGER_WARN); // Set the log level to WARN

Thread Safety (Mutex)

Warning If you are working in a multithreaded environment, you can enable the mutex option to ensure thread safety. When the mutex is enabled, a lock will be acquired before printing each log message, preventing multiple threads from interfering with each other.

To enable the mutex, uncomment the #define USE_MUTEX line in the mylogging.h file.

#define USE_MUTEX // Enable mutex for thread safety

Note: Enabling the mutex may introduce additional execution overhead, so use it only if necessary in your specific application.

Accessing Loggers

The loggerList class provides methods to access the created loggers. You can retrieve loggers by their tag name or by their index in the list of loggers.

// Retrieve a logger by tag name
logger* myLogger = loggerList::getLogger("myLogger");

// Retrieve a logger by index
logger* myLogger = loggerList::getLoggerByIndex(0);

Obtaining Logger Information

The loggerList class also allows you to get information about the loggers, such as the number of loggers and the log level of a specific logger.

int count = loggerList::getLoggersCount();       // Get the number of loggers
const char* tagName = loggerList::getLoggerTagByIndex(0);  // Get the tag name of the first logger
uint8_t level = loggerList::getLoggerLevelByIndex(0);     // Get the log level of the first logger

License

This library is released under the MIT License.

Credits

The MyLogging library was developed by Manuel Gonzalvez. If you have any questions or suggestions, please feel free to contact me at [email protected].

library-esp32-logger's People

Contributors

manuelgonzalvez avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.