Skip to content

Commit

Permalink
1.1.0 change d| to f|
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan Chen committed Sep 1, 2019
1 parent 3165fc3 commit 3f0c5f5
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 44 deletions.
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -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[
-{ +|
```
Expand All @@ -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}
Expand All @@ -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
Expand All @@ -76,7 +76,7 @@ s[ i[ d[# b[ o[
#include <JsonLogger.h>

void to_console(int level, const char* json) {
char mod[LOG_MAX_LEN];
char mof[LOG_MAX_LEN];
strcpy(mod, json);

logModifyForHuman(level, mod);
Expand All @@ -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");
Expand Down
8 changes: 4 additions & 4 deletions examples/JsonBuilder/JsonBuilder.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/JsonLogger/JsonLogger.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions library.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
4 changes: 2 additions & 2 deletions library.properties
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
name=JsonLogger
version=1.0.7
version=1.1.0
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.
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
Expand Down
16 changes: 8 additions & 8 deletions src/Builder.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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}"));
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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|");
Expand Down
32 changes: 18 additions & 14 deletions src/JsonLogger.h
Original file line number Diff line number Diff line change
@@ -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

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

Expand All @@ -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
Expand All @@ -49,28 +49,32 @@ 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
#define LEVEL_INFO 2
#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)
Expand Down
9 changes: 7 additions & 2 deletions src/Logger.c
Original file line number Diff line number Diff line change
Expand Up @@ -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, ...) {
Expand All @@ -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";
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit 3f0c5f5

Please sign in to comment.