Skip to content

Commit

Permalink
1.1.1 improve logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan Chen committed Sep 4, 2019
1 parent 3f0c5f5 commit e12dad2
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "JsonLogger",
"version": "1.1.0",
"version": "1.1.1",
"keywords": ["communication"],
"description": "An easy-to-use, small, fast and portable JSON builder and logger for IoT logging, data acquisition and analytics",
"frameworks": "*",
Expand Down
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.1.0
version=1.1.1
author=Alan Chen
maintainer=Alan Chen <[email protected]>
sentence=An easy-to-use, small, fast and portable JSON builder and logger for IoT logging, data acquisition and analytics.
Expand Down
3 changes: 1 addition & 2 deletions src/JsonLogger.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#define logInfo(...) logJson(LEVEL_INFO, __VA_ARGS__)
#define logDebug(...) logJson(LEVEL_DEBUG, __VA_ARGS__)
#define logTrace(...) logJson(LEVEL_TRACE, __VA_ARGS__)
#define logLevel(level, ...) logJson(level, __VA_ARGS__)

#ifdef __cplusplus
extern "C" {
Expand All @@ -33,8 +34,6 @@ extern const char* getLogTime();
void logAddSender(void (*sender)(int level, const char* json));
void logModifyForHuman(int level, char* json);

extern const char* LOG_LEVELS[];

#ifdef __cplusplus
}
#endif
Expand Down
25 changes: 19 additions & 6 deletions src/Logger.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,23 @@ void logModifyForHuman(int level, char* mod) {
#else
str_replace(mod, "\"" LOG_ID_KEY "\":", "");
#endif
char buf[128];
char buf[64];
sprintf(buf, "\"%s", getLogId());
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 "\":\"", " ");

if (level < sizeof(LOG_LEVELS) / sizeof(LOG_LEVELS[0])) {
char buf[64];
#if defined(LOG_TIME_KEY) || defined(LOG_ID_KEY)
sprintf(buf, ",\"" LOG_LEVEL_KEY "\":%d,", level);
#else
sprintf(buf, "\"" LOG_LEVEL_KEY "\":%d,", level);
#endif
str_replace(mod, buf, LOG_LEVELS[level]);
}

#ifdef LOG_SOURCE_KEY
str_replace(mod, "\"" LOG_SOURCE_KEY "\":\"", " ");
str_replace(mod, "\",\"" LOG_FUNC_KEY "\":\"", " ");
#endif
str_replace(mod, "\\\"", "'");
Expand All @@ -43,7 +53,7 @@ void log_json(int level, const char* placeholder, ...) {
#ifdef LOG_ID_KEY
LOG_ID_KEY, getLogId(),
#endif
LOG_LEVEL_KEY, LOG_LEVELS[level]);
"i|" LOG_LEVEL_KEY, level);
va_list args;
va_start(args, placeholder);
vbuild_json(json, LOG_MAX_LEN, fragment, args);
Expand All @@ -56,10 +66,11 @@ void log_json(int level, const char* placeholder, ...) {

#ifdef LOGGER_TEST

// gcc -Os -DLOGGER_TEST '-DLOG_ID_KEY="i"' '-DLOG_TIME_KEY="t"' '-DLOG_SOURCE_KEY="s"' src/*.c; ./a.out; rm ./a.out
// 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
// gcc -Os -DLOGGER_TEST '-DLOG_MIN_LEVEL=0' src/*.c; ./a.out; rm ./a.out

const char* getLogTime() {
return "1970-01-01T00:00:00Z";
Expand Down Expand Up @@ -99,6 +110,8 @@ int main() {
logError("Error");
printf("\n");
logFatal("Fatal");
printf("\n");
logLevel(8, "DATA");

return 0;
}
Expand Down

0 comments on commit e12dad2

Please sign in to comment.