From 1be40ad6fc7b8a3f1ead9bc67f557d29c44e3217 Mon Sep 17 00:00:00 2001 From: Gregg Date: Sun, 6 Mar 2022 07:48:12 -0600 Subject: [PATCH] Converted LOG1() and LOG2() to variadic macros! If LOG1() or LOG2() is only provided with a SINGLE argument, then Serial.print() is called. This allows you to continue using LOG1() and LOG2() to directly print any variable or object that is handled by Serial.print(), such as an int, double, or even an IPAddress. If LOG1() or LOG2() is provided with multiple arguments, the first is considered the format and Serial.printf(format...) is called. This allows you to use printf-like functionality within LOG1() and LOG2(). --- examples/19-WebLog/19-WebLog.ino | 2 +- examples/19-WebLog/DEV_LED.h | 2 ++ src/Settings.h | 4 ++-- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/examples/19-WebLog/19-WebLog.ino b/examples/19-WebLog/19-WebLog.ino index 32a4905..6278ff8 100644 --- a/examples/19-WebLog/19-WebLog.ino +++ b/examples/19-WebLog/19-WebLog.ino @@ -1,7 +1,7 @@ /********************************************************************************* * MIT License * - * Copyright (c) 2020 Gregg E. Berman + * Copyright (c) 2022 Gregg E. Berman * * https://github.com/HomeSpan/HomeSpan * diff --git a/examples/19-WebLog/DEV_LED.h b/examples/19-WebLog/DEV_LED.h index 07c7552..23f555c 100644 --- a/examples/19-WebLog/DEV_LED.h +++ b/examples/19-WebLog/DEV_LED.h @@ -28,6 +28,8 @@ struct DEV_LED : Service::LightBulb { // First we create a derived digitalWrite(ledPin,power->getNewVal()); // use a standard Arduino function to turn on/off ledPin based on the return of a call to power->getNewVal() (see below for more info) WEBLOG("LED on Pin %d: %s",ledPin,power->getNewVal()?"ON":"OFF"); + LOG1("LOG1: LED on Pin %d: %s",ledPin,power->getNewVal()?"ON":"OFF"); + LOG2("LOG2: LED on Pin %d: %s",ledPin,power->getNewVal()?"ON":"OFF"); return(true); // return true to indicate the update was successful (otherwise create code to return false if some reason you could not turn on the LED) diff --git a/src/Settings.h b/src/Settings.h index 9c1f345..ec43965 100644 --- a/src/Settings.h +++ b/src/Settings.h @@ -98,8 +98,8 @@ // Message Log Level Control Macros // // 0=Minimal, 1=Informative, 2=All // -#define LOG1(x) if(homeSpan.logLevel>0)Serial.print(x) -#define LOG2(x) if(homeSpan.logLevel>1)Serial.print(x) +#define LOG1(format,...) if(homeSpan.logLevel>0)Serial.print ##__VA_OPT__(f)(format __VA_OPT__(,) __VA_ARGS__) +#define LOG2(format,...) if(homeSpan.logLevel>1)Serial.print ##__VA_OPT__(f)(format __VA_OPT__(,) __VA_ARGS__) #define WEBLOG(format,...) homeSpan.webLog.addLog(format __VA_OPT__(,) __VA_ARGS__)