Coder Social home page Coder Social logo

arduino-log's Introduction

ArduinoLog - C++ Log library for Arduino devices

Build Status License

An minimalistic Logging framework for Arduino-compatible embedded systems.

ArduinoLog is a minimalistic framework to help the programmer output log statements to an output of choice, fashioned after extensive logging libraries such as log4cpp ,log4j and log4net. In case of problems with an application, it is helpful to enable logging so that the problem can be located. ArduinoLog is designed so that log statements can remain in the code with minimal performance cost. In order to facilitate this the loglevel can be adjusted, and (if your code is completely tested) all logging code can be compiled out.

Features

  • Different log levels (Error, Info, Warn, Debug, Verbose )
  • Supports multiple variables
  • Supports formatted strings
  • Supports formatted strings from flash memory
  • Fixed memory allocation (zero malloc)
  • MIT License

Tested for

  • All Arduino boards (Uno, Due, Mini, Micro, Yun...)
  • ESP8266
  • ESP32

Downloading

This package has been published to the Arduino & PlatformIO package managers, but you can also download it from GitHub.

  • By directly loading fetching the Archive from GitHub:
  1. Go to https://github.com/thijse/Arduino-Log
  2. Click the DOWNLOAD ZIP button in the panel on the
  3. Rename the uncompressed folder Arduino-Log-master to Arduino-Log.
  4. You may need to create the libraries subfolder if its your first library.
  5. Place the Arduino-Log library folder in your /libraries/ folder.
  6. Restart the IDE.
  7. For more information, read this extended manual

Quick start

    Serial.begin(9600);

    // Initialize with log level and log output.
    Log.begin   (LOG_LEVEL_VERBOSE, &Serial);

    // Start logging text and formatted values
    Log.error   (  "Log as Error   with binary values             : %b, %B"CR  , 23  , 345808);
    Log.warning (F("Log as Warning with integer values from Flash : %d, %d"CR) , 34  , 799870);

See examples/Log/Log.ino

Usage

Initialisation

The log library needs to be initialized with the log level of messages to show and the log output. The latter will often be the Serial interface. Optionally, you can indicate whether to show the log type (error, debug, etc) for each line.

begin(int level, Print* logOutput, bool showLevel)
begin(int level, Print* logOutput)

The loglevels available are

* 0 - LOG_LEVEL_SILENT     no output
* 1 - LOG_LEVEL_FATAL      fatal errors
* 2 - LOG_LEVEL_ERROR      all errors  
* 3 - LOG_LEVEL_WARNING    errors, and warnings
* 4 - LOG_LEVEL_NOTICE     errors, warnings and notices
* 5 - LOG_LEVEL_TRACE      errors, warnings, notices & traces
* 6 - LOG_LEVEL_VERBOSE    all

example

    Log.begin(LOG_LEVEL_ERROR, &Serial, true);

if you want to fully remove all logging code, uncomment #define DISABLE_LOGGING in ArduinoLog.h, this may significantly reduce your sketch/library size.

Log events

The library allows you to log on different levels by the following functions

void fatal   (const char *format, va_list logVariables);
void error   (const char *format, va_list logVariables);
void warning (const char *format, va_list logVariables);
void notice  (const char *format, va_list logVariables);
void trace   (const char *format, va_list logVariables);
void verbose (const char *format, va_list logVariables);

where the format string can be used to format the log variables

* %s	display as string (char*)
* %c	display as single character
* %d	display as integer value
* %l	display as long value
* %x	display as hexadecimal value
* %X	display as hexadecimal value prefixed by `0x`
* %b	display as  binary number
* %B	display as  binary number, prefixed by `0b'
* %t	display as boolean value "t" or "f"
* %T	display as boolean value "true" or "false"
* %D,%F display as double value

The format string may come from flash memory.

examples

    Log.fatal   (F("Log as Fatal   with string value from Flash   : %s"CR    ) , "value"     );
    Log.error   (  "Log as Error   with binary values             : %b, %B"CR  , 23  , 345808);
    Log.warning (F("Log as Warning with integer values from Flash : %d, %d"CR) , 34  , 799870);
    Log.notice  (  "Log as Notice  with hexadecimal values        : %x, %X"CR  , 21  , 348972);
    Log.verbose (F("Log as Verbose with bool value from Flash     : %t, %T"CR) , true, false );

Log level change

Runtime log level change is supported by calling Log.setLogLevel(int LOG_LEVEL_x)

examples

int logLevel = LOG_LEVEL_ERROR;
void setup() {
    Serial.begin(9600);
    Log.begin(logLevel, &Serial);
}

void loop() {
    Log.notice("Log as Notice for test\n");
    if (Serial.available()) {
        switch (Serial.read()) {
            case 'm': // mute: be less verbose
                logLevel = max((logLevel - 1), LOG_LEVEL_SILENT);
                Log.setLogLevel(logLevel); break;
            case 'v': // be more verbose
                logLevel = min((logLevel + 1), LOG_LEVEL_VERBOSE);
                Log.setLogLevel(logLevel); break;
        }
    }
}

Disable library

(if your code is completely tested) all logging code can be compiled out. Do this by uncommenting

#define DISABLE_LOGGING

in Logging.h. This may significantly reduce your project size.

Credit

Based on library by

Bugfixes & features by

On using and modifying libraries

Copyright

ArduinoLog is provided Copyright © 2017 under MIT License.

arduino-log's People

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.