diff --git a/README.md b/README.md index 5e11b9e..85af64e 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,13 @@ JSON Logger ====== -An easy-to-use, small, fast, and portable JSON builder and logger for IoT firmware data gathering. +An easy-to-use, small, fast and portable JSON builder and logger for IoT logging, data acquisition and analytics. ### Usage: Special prefixes to indicate data types: ```javascript -s| i| d|# b| o| -s[ i[ d[# b[ o[ +s| i| f|# b| o| +s[ i[ f[# b[ o[ -{ +| ``` @@ -22,11 +22,11 @@ s[ i[ d[# b[ o[ // s|key: value is a string (can be used to escape prefix: // e.g. "s|i|..." if you want your key to start with i|) // i|key: value is an integer (32 bits) - // d|#key: value is a floating number with 1 to 17 significant + // f|#key: value is a floating number with 1 to 17 significant // digits (a is 10, b is 11 ... h is 17). // b|key: value is a boolean // o|key: value is anything else (object, array, null) - json(buf256, "str_key1", "str1", "i|int_key1", 7, "d|3double_key1", 3.14159, + json(buf256, "str_key1", "str1", "i|int_key1", 7, "f|3double_key1", 3.14159, "b|boolean_key1", 1, "o|object_key1", "{}", "o|array_key1", "[]", "o|null_key1", "null"); // => {"str_key1":"str1","int_key1":7,"double_key1":3.14,"boolean_key1":true, // "object_key1":{},"array_key1":[],"null_key1":null} @@ -53,13 +53,13 @@ s[ i[ d[# b[ o[ json(buf64, "i[", 3, 0, -2147483648, 2147483647); // => [0,-2147483648,2147483647] - // "d[#": a floating number array with 1 to 17 significant + // "f[#": a floating number array with 1 to 17 significant // digits (a is 10, b is 11 ... h is 17) - json(buf64, "d[1", 4, 0.0, 0.01, 4.44, 1.2345678901234567890); + json(buf64, "f[1", 4, 0.0, 0.01, 4.44, 1.2345678901234567890); // => [0,0.01,4,1] - json(buf64, "d[7", 4, 0.0, 0.01, 4.44, 1.2345678901234567890); + json(buf64, "f[7", 4, 0.0, 0.01, 4.44, 1.2345678901234567890); // => [0,0.01,4.44,1.234568] - json(buf64, "d[h", 4, 0.0, 0.01, 4.44, -1.2345678901234567890); + json(buf64, "f[h", 4, 0.0, 0.01, 4.44, -1.2345678901234567890); // => [0,0.01,4.4400000000000004,-1.2345678901234567] // "b[": a boolean array @@ -76,7 +76,7 @@ s[ i[ d[# b[ o[ #include void to_console(int level, const char* json) { - char mod[LOG_MAX_LEN]; + char mof[LOG_MAX_LEN]; strcpy(mod, json); logModifyForHuman(level, mod); @@ -96,7 +96,7 @@ void to_mqtt(int level, const char* json) { logTrace("should not be logged at all if LOG_MIN_LEVEL is not changed to 0"); logDebug("Debug"); - logInfo("i|status", -1, "d|5pi", 3.14159, "Info"); + logInfo("i|status", -1, "f|5pi", 3.14159, "Info"); logWarn("Warning"); logError("Error"); logFatal("Fatal"); diff --git a/examples/JsonBuilder/JsonBuilder.ino b/examples/JsonBuilder/JsonBuilder.ino index d346cf2..65064ef 100644 --- a/examples/JsonBuilder/JsonBuilder.ino +++ b/examples/JsonBuilder/JsonBuilder.ino @@ -9,10 +9,10 @@ void setup() { char buf3[3], buf256[256], buf64[64], buf512[512]; // key: value is a string // i|key: value is an integer - // d|#key: value is a floating number with 1 to 17 significant digits (a is 10, b is 11 ... h is 17). + // f|#key: value is a floating number with 1 to 17 significant digits (a is 10, b is 11 ... h is 17). // b|key: value is a boolean // o|key: value is anything else (object, array, null) - int json_len = json(buf256, "str_key1", "str1", "i|int_key1", 7, "d|3double_key1", 3.14159, + int json_len = json(buf256, "str_key1", "str1", "i|int_key1", 7, "f|3double_key1", 3.14159, "b|boolean_key1", 1, "o|object_key1", "{}", "o|array_key1", "[]", "o|null_key1", "null"); Serial.println(String(buf256) + " length: " + json_len); // {"str_key1":"str1","int_key1":7,"double_key1":3.14,"boolean_key1":true, @@ -45,8 +45,8 @@ void setup() { json(buf64, "i[", 3, 0, 10, 20); Serial.println(buf64); // [0,10,20] - // "d[#": a floating number array with 1 to 17 significant digits (a is 10, b is 11 ... h is 17) - json(buf64, "d[7", 4, 0.0, 0.01, 4.44, 1.2345678901234567890); + // "f[#": a floating number array with 1 to 17 significant digits (a is 10, b is 11 ... h is 17) + json(buf64, "f[7", 4, 0.0, 0.01, 4.44, 1.2345678901234567890); Serial.println(buf64); // [0,0.01,4.44,1.234568] // "b[": a boolean array diff --git a/examples/JsonLogger/JsonLogger.ino b/examples/JsonLogger/JsonLogger.ino index 3d6beac..34aff4c 100644 --- a/examples/JsonLogger/JsonLogger.ino +++ b/examples/JsonLogger/JsonLogger.ino @@ -28,7 +28,7 @@ void setup() { Serial.println(); logDebug("log to terminal, but not to file"); Serial.println(); - logInfo("i|status", -1, "d|5pi", 3.14159, "log to both terminal and file"); + logInfo("i|status", -1, "f|5pi", 3.14159, "log to both terminal and file"); Serial.println(); logWarn("Warning"); Serial.println(); diff --git a/library.json b/library.json index 53fa3d3..c25f1bd 100644 --- a/library.json +++ b/library.json @@ -1,8 +1,8 @@ { "name": "JsonLogger", - "version": "1.0.7", + "version": "1.1.0", "keywords": ["communication"], - "description": "An easy-to-use, small, fast, and portable JSON builder and logger for IoT firmware data gathering. Useful for logging json data to terminal, file, and cloud.", + "description": "An easy-to-use, small, fast and portable JSON builder and logger for IoT logging, data acquisition and analytics", "frameworks": "*", "platforms": "*", "authors": { diff --git a/library.properties b/library.properties index 06f7add..49ed8d7 100644 --- a/library.properties +++ b/library.properties @@ -1,8 +1,8 @@ name=JsonLogger -version=1.0.7 +version=1.1.0 author=Alan Chen maintainer=Alan Chen -sentence=An easy-to-use, small, fast, and portable JSON builder and logger for IoT firmware data gathering. +sentence=An easy-to-use, small, fast and portable JSON builder and logger for IoT logging, data acquisition and analytics. paragraph=Useful for logging json data to terminal, file, and cloud category=Communication url=https://github.com/ravelab/JsonLogger diff --git a/src/Builder.c b/src/Builder.c index 977c2c1..fc52f24 100644 --- a/src/Builder.c +++ b/src/Builder.c @@ -206,7 +206,7 @@ int vbuild_json(char* json, size_t buf_size, const char* item, va_list arg) { case 'i': array = INT_ARRAY; break; - case 'd': + case 'f': array = DOUBLE_ARRAY; doubleArrayPrecisionChar = item[2]; break; @@ -256,7 +256,7 @@ int vbuild_json(char* json, size_t buf_size, const char* item, va_list arg) { if (item[1] == '|' && item[0] == 'i') { // integer addKey(&item[2]); addInt(); - } else if (item[1] == '|' && item[0] == 'd') { // double + } else if (item[1] == '|' && item[0] == 'f') { // double addKey(&item[3]); addDouble(item[2]); } else if (item[1] == '|' && item[0] == 'b') { // boolean @@ -368,7 +368,7 @@ int main() { int len; char buf3[3], buf256[256], buf64[64]; - len = json(buf256, "str_key1", "str1", "i|int_key1", 7, "d|3double_key1", 3.14159, + len = json(buf256, "str_key1", "str1", "i|int_key1", 7, "f|3double_key1", 3.14159, "b|boolean_key1", 1, "o|object_key1", "{}", "o|array_key1", "[]", "o|null_key1", "null"); printf("%s\n", buf256); assert(!strcmp(buf256, "{\"str_key1\":\"str1\",\"int_key1\":7,\"double_key1\":3.14,\"boolean_key1\":true,\"object_key1\":{},\"array_key1\":[],\"null_key1\":null}")); @@ -401,18 +401,18 @@ int main() { assert(!strcmp(buf64, "[0,-2147483648,2147483647]")); assert(len == strlen(buf64)); - len = json(buf64, "d[1", 4, 0.0, 0.01, 4.44, 1.2345678901234567890); + len = json(buf64, "f[1", 4, 0.0, 0.01, 4.44, 1.2345678901234567890); printf("%s\n", buf64); assert(!strcmp(buf64, "[0,0.01,4,1]")); assert(len == strlen(buf64)); - // up to 17 significant number, the valid values for the * in "d[*" are 1 to h (a is 10, b is 11 ... h is 17) - len = json(buf64, "d[7", 4, 0.0, 0.01, 4.44, 1.2345678901234567890); + // up to 17 significant number, the valid values for the # in "f[#" are 1 to h (a is 10, b is 11 ... h is 17) + len = json(buf64, "f[7", 4, 0.0, 0.01, 4.44, 1.2345678901234567890); printf("%s\n", buf64); assert(!strcmp(buf64, "[0,0.01,4.44,1.234568]")); assert(len == strlen(buf64)); - len = json(buf256, "d[h", 6, 0.0, 0.01, 4.44, -1.2345678901234567890, -0x1.fffffffffffffp+1023, -2.2250738585072014e-308); + len = json(buf256, "f[h", 6, 0.0, 0.01, 4.44, -1.2345678901234567890, -0x1.fffffffffffffp+1023, -2.2250738585072014e-308); printf("%s\n", buf256); assert(!strcmp(buf256, "[0,0.01,4.4400000000000004,-1.2345678901234567,-1.7976931348623157e+308,-2.2250738585072014e-308]")); assert(len == strlen(buf256)); @@ -497,7 +497,7 @@ int main() { assert(len == strlen(buf64)); // compile with -O2 gets segmentation fault 11 - // len = json(buf64, "d|2"); + // len = json(buf64, "f|2"); // printf("%s\n", buf64); // gonna be random value converted from 64-bit number len = json(buf64, "b|"); diff --git a/src/JsonLogger.h b/src/JsonLogger.h index 9e37ca6..b8de63a 100644 --- a/src/JsonLogger.h +++ b/src/JsonLogger.h @@ -1,7 +1,6 @@ #ifndef json_builder_h #define json_builder_h -// stuff you should know #define json(buf, ...) build_json(buf, sizeof(buf), __VA_ARGS__, NULL) // returns json length if all good, negative number if error #define jsonHeap(buf, size, ...) build_json(buf, size, __VA_ARGS__, NULL) // same as json() but user supplies buffer size for malloc-ed buffer @@ -17,15 +16,20 @@ extern "C" { #endif // define LOG_ID_KEY (e.g. -D LOG_ID_KEY="i") and implement getLogId() if you want to log id +//#define LOG_ID_KEY "i" #ifdef LOG_ID_KEY extern const char* getLogId(); #endif -// define LOG_ID_KEY (e.g. -D LOG_TIME_KEY="t") and implement getLogTime() if you want to log id +// define LOG_TIME_KEY (e.g. -D LOG_TIME_KEY="t") and implement getLogTime() if you want to log id +//#define LOG_TIME_KEY "t" #ifdef LOG_TIME_KEY extern const char* getLogTime(); #endif +// define LOG_SOURCE_KEY (e.g. -D LOG_SOURCE_KEY="s") if you want to log source file, line # and function name +//#define LOG_SOURCE_KEY "s" + void logAddSender(void (*sender)(int level, const char* json)); void logModifyForHuman(int level, char* json); @@ -37,10 +41,6 @@ extern const char* LOG_LEVELS[]; #define JSON_ERR_BUF_SIZE -1 -#ifndef EMPTY_KEY -#define EMPTY_KEY "_" -#endif - #ifndef LOG_MIN_LEVEL #define LOG_MIN_LEVEL 1 #endif @@ -49,18 +49,18 @@ extern const char* LOG_LEVELS[]; #define LOG_MAX_LEN 512 #endif -#ifndef LOG_LEVEL_KEY -#define LOG_LEVEL_KEY "l" -#endif - -#ifndef LOG_SOURCE_KEY -#define LOG_SOURCE_KEY "s" +#ifndef EMPTY_KEY +#define EMPTY_KEY "_" #endif #ifndef LOG_FUNC_KEY #define LOG_FUNC_KEY "f" #endif +#ifndef LOG_LEVEL_KEY +#define LOG_LEVEL_KEY "l" +#endif + #define LEVEL_FATAL 5 #define LEVEL_ERROR 4 #define LEVEL_WARN 3 @@ -68,9 +68,13 @@ extern const char* LOG_LEVELS[]; #define LEVEL_DEBUG 1 #define LEVEL_TRACE 0 -// stuff you shouldn't know +#ifdef LOG_SOURCE_KEY #define logJson(level, ...) \ - if (level >= LOG_MIN_LEVEL) log_json(level, "", "s", __FILE__ ":" TOSTRING(__LINE__), "f", __func__, __VA_ARGS__, NULL) + if (level >= LOG_MIN_LEVEL) log_json(level, "", LOG_SOURCE_KEY, __FILE__ ":" TOSTRING(__LINE__), LOG_FUNC_KEY, __func__, __VA_ARGS__, NULL) +#else +#define logJson(level, ...) \ + if (level >= LOG_MIN_LEVEL) log_json(level, "", __VA_ARGS__, NULL) +#endif #define STRINGIFY(x) #x #define TOSTRING(x) STRINGIFY(x) diff --git a/src/Logger.c b/src/Logger.c index 7a29546..0a366a7 100644 --- a/src/Logger.c +++ b/src/Logger.c @@ -26,10 +26,12 @@ void logModifyForHuman(int level, char* mod) { str_replace(mod, buf, ""); #endif str_replace(mod, "\",\"" LOG_LEVEL_KEY "\":", strlen(LOG_LEVELS[level]) == 5 ? "" : " "); +#ifdef LOGO_SOURCE_KEY str_replace(mod, "\",\"" LOG_SOURCE_KEY "\":\"", " "); str_replace(mod, "\",\"" LOG_FUNC_KEY "\":\"", " "); +#endif + str_replace(mod, "\\\"", "'"); str_replace(mod, "\"", " "); - // mod[strlen(mod) - 1] = '\0'; } void log_json(int level, const char* placeholder, ...) { @@ -53,8 +55,11 @@ void log_json(int level, const char* placeholder, ...) { } #ifdef LOGGER_TEST + // gcc -Os -DLOGGER_TEST '-DLOG_TIME_KEY="t"' src/*.c; ./a.out; rm ./a.out // gcc -Os -DLOGGER_TEST '-DLOG_ID_KEY="i"' src/*.c; ./a.out; rm ./a.out +// gcc -Os -DLOGGER_TEST '-DLOG_SOURCE_KEY="s"' src/*.c; ./a.out; rm ./a.out +// gcc -Os -DLOGGER_TEST src/*.c; ./a.out; rm ./a.out const char* getLogTime() { return "1970-01-01T00:00:00Z"; @@ -87,7 +92,7 @@ int main() { printf("\n"); logDebug("log to terminal, but not to file"); printf("\n"); - logInfo("i|status", -1, "d|5pi", 3.14159, "log to both terminal and file"); + logInfo("i|status", -1, "f|5pi", 3.14159, "log to both \"terminal\" and \"file\""); printf("\n"); logWarn("Warning"); printf("\n");