Skip to content

Commit

Permalink
1.0.5 add library.json and fix ino examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan Chen committed Aug 31, 2019
1 parent 75ce1f3 commit 06d709c
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 35 deletions.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -63,9 +63,12 @@ An easy-to-use, small, fast, and portable JSON builder and logger for IoT firmwa
#include <JsonLogger.h>
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) {
Expand Down
20 changes: 11 additions & 9 deletions examples/JsonBuilder/JsonBuilder.ino
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,33 @@ 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);
Serial.println(buf64); // +|"str_key2":"str2","int_key2":8

// 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\"");
Expand All @@ -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]

Expand Down
29 changes: 9 additions & 20 deletions examples/JsonLogger/JsonLogger.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -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");
}

Expand Down
16 changes: 16 additions & 0 deletions library.json
Original file line number Diff line number Diff line change
@@ -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": "[email protected]",
"name": "Alan Chen"
},
"repository": {
"type": "git",
"url": "https://github.com/ravelab/JsonLogger"
}
}
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=JsonLogger
version=1.0.4
version=1.0.5
author=Alan Chen
maintainer=Alan Chen <[email protected]>
sentence=An easy-to-use, small, fast, and portable JSON builder and logger for IoT firmware data gathering.
Expand Down
10 changes: 9 additions & 1 deletion src/JsonLogger.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <inttypes.h>
#include <stdarg.h>
#include <stdio.h>
Expand Down

0 comments on commit 06d709c

Please sign in to comment.