From 06d709cf2dcfc38874f79a2f0be1d02c1e768ee1 Mon Sep 17 00:00:00 2001 From: Alan Chen Date: Fri, 30 Aug 2019 20:01:45 -0700 Subject: [PATCH] 1.0.5 add library.json and fix ino examples --- README.md | 11 +++++++---- examples/JsonBuilder/JsonBuilder.ino | 20 ++++++++++--------- examples/JsonLogger/JsonLogger.ino | 29 +++++++++------------------- library.json | 16 +++++++++++++++ library.properties | 2 +- src/JsonLogger.h | 10 +++++++++- 6 files changed, 53 insertions(+), 35 deletions(-) create mode 100644 library.json diff --git a/README.md b/README.md index a92f6df..221ec84 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ An easy-to-use, small, fast, and portable JSON builder and logger for IoT firmwa // if an argument has no matching pair, is not a fragment or part of an array, it will be a value to empty key json(buf512, "value only"); - // => {"":"value only"} + // => {"_":"value only"} // "-{": build a fragment (starts with +|) that can be inserted into a json json(buf64, "-{", "str_key2", "str2", "i|int_key2", 8); @@ -63,9 +63,12 @@ An easy-to-use, small, fast, and portable JSON builder and logger for IoT firmwa #include void to_console(int level, const char* json) { - if (level >= LEVEL_INFO) { - printf("%s\n", json); - } + char mod[LOG_MAX_LEN]; + strcpy(mod, json); + + logModifyForHuman(level, mod); + + Serial.println(mod); } void to_mqtt(int level, const char* json) { diff --git a/examples/JsonBuilder/JsonBuilder.ino b/examples/JsonBuilder/JsonBuilder.ino index 40bede0..680bf6b 100644 --- a/examples/JsonBuilder/JsonBuilder.ino +++ b/examples/JsonBuilder/JsonBuilder.ino @@ -9,23 +9,23 @@ 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; argument should be a double + // d|*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, "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,"object_key1":{},"array_key1":[],"null_key1":null} + Serial.println(String(buf256) + " length: " + json_len); + // {"str_key1":"str1","int_key1":7,"double_key1":3.14,"boolean_key1":true, + // "object_key1":{},"array_key1":[],"null_key1":null} length: 121 // return value: length of the json string json_len = json(buf3, "k", "v"); // need buffer size to be 10 to hold {"k":"v"} - if (json_len != JSON_ERR_BUF_SIZE) { - Serial.println("ERROR json_len is " + String(json_len)); - } Serial.println(String(buf3) + " length: " + json_len); - // if an argument has no matching pair, is not a fragment or part of an array, it will be a value to empty key + // If an argument has no matching pair and is not a fragment or part of an array, + // it will be a value for _ key json(buf512, "value only"); - Serial.println(buf512); // {"":"value only"} + Serial.println(buf512); // {"_":"value only"} // "-{": build a fragment (starts with +|) that can be inserted into a json json(buf64, "-{", "str_key2", "str2", "i|int_key2", 8); @@ -33,7 +33,9 @@ void setup() { // build a json with an object json and a fragment json(buf512, "o|obj", buf256, buf64); - Serial.println(buf512); // {"obj":{"str_key1":"str1","int_key1":7,"double_key1":3.14,"boolean_key1":true,"object_key1":{},"array_key1":[],"null_key1":null},"str_key2":"str2","int_key2":8} + Serial.println(buf512); + // {"obj":{"str_key1":"str1","int_key1":7,"double_key1":3.14,"boolean_key1":true, + // "object_key1":{},"array_key1":[],"null_key1":null},"str_key2":"str2","int_key2":8} // "s[": a string array, the next argument is the number of items json(buf64, "s[", 2, "str3", "str4\"inquote\""); @@ -43,7 +45,7 @@ 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); arguments should be doubles + // "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); Serial.println(buf64); // [0,0.01,4.44,1.234568] diff --git a/examples/JsonLogger/JsonLogger.ino b/examples/JsonLogger/JsonLogger.ino index 7a03d83..3d6beac 100644 --- a/examples/JsonLogger/JsonLogger.ino +++ b/examples/JsonLogger/JsonLogger.ino @@ -4,25 +4,14 @@ void send_console(int level, const char* json) { char mod[LOG_MAX_LEN]; strcpy(mod, json); - str_replace(mod, "{\"" LOG_TIME_KEY "\":\"", ""); -#ifdef LOG_ID_VALUE - str_replace(mod, ",\"" LOG_ID_KEY "\":", ""); - char buf[128]; - sprintf(buf, "\"%s\"", LOG_ID_VALUE); - str_replace(mod, buf, ""); -#endif - str_replace(mod, "\",\"" LOG_LEVEL_KEY "\":", strlen(LEVELS[level]) == 5 ? "" : " "); - str_replace(mod, "\",\"" LOG_SOURCE_KEY "\":\"", " "); - str_replace(mod, "\",\"" LOG_FUNC_KEY "\":\"", " "); - str_replace(mod, "\"", " "); - mod[strlen(mod) - 1] = '\0'; - - Serial.printf("terminal: %s\n", mod); + logModifyForHuman(level, mod); + + Serial.println(mod); } void send_file(int level, const char* json) { if (level >= LEVEL_INFO) { - Serial.printf("file : %s\n", json); + Serial.println(json); } } @@ -36,15 +25,15 @@ void setup() { logAddSender(send_file); logTrace("should not be logged at all if LOG_MIN_LEVEL is not changed to 0"); - Serial.printf("\n"); + Serial.println(); logDebug("log to terminal, but not to file"); - Serial.printf("\n"); + Serial.println(); logInfo("i|status", -1, "d|5pi", 3.14159, "log to both terminal and file"); - Serial.printf("\n"); + Serial.println(); logWarn("Warning"); - Serial.printf("\n"); + Serial.println(); logError("Error"); - Serial.printf("\n"); + Serial.println(); logFatal("Fatal"); } diff --git a/library.json b/library.json new file mode 100644 index 0000000..da559ce --- /dev/null +++ b/library.json @@ -0,0 +1,16 @@ +{ + "name": "JsonLogger", + "version": "1.0.5", + "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.", + "frameworks": "*", + "platforms": "*", + "authors": { + "email": "alanpc@gmail.com", + "name": "Alan Chen" + }, + "repository": { + "type": "git", + "url": "https://github.com/ravelab/JsonLogger" + } +} diff --git a/library.properties b/library.properties index cebabe4..4a2950e 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=JsonLogger -version=1.0.4 +version=1.0.5 author=Alan Chen maintainer=Alan Chen sentence=An easy-to-use, small, fast, and portable JSON builder and logger for IoT firmware data gathering. diff --git a/src/JsonLogger.h b/src/JsonLogger.h index 9e37ca6..cc75cbb 100644 --- a/src/JsonLogger.h +++ b/src/JsonLogger.h @@ -70,11 +70,19 @@ extern const char* LOG_LEVELS[]; // stuff you shouldn't know #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, "", "s", SOURCE, "f", __func__, __VA_ARGS__, NULL) #define STRINGIFY(x) #x #define TOSTRING(x) STRINGIFY(x) +#define FILELINE __FILE__ ":" TOSTRING(__LINE__) + +#if _WIN32 +#define SOURCE strrchr("\\" FILELINE, '\\') + 1 +#else +#define SOURCE FILELINE +#endif + #include #include #include