Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optionally remove OS dependencies #28

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/utils/assert.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
extern "C" {
#endif

#define __ASSERT_PRINT(fmt, ...) printf(fmt, ##__VA_ARGS__)
#define __ASSERT_PRINT(fmt, ...) __logger_log(NULL, LOG_EMERG, __FILE__, __LINE__, fmt, ##__VA_ARGS__)

#define __ASSERT_LOC(test) \
__ASSERT_PRINT("ASSERTION FAIL [%s] @ %s:%d\n", \
Expand Down
16 changes: 16 additions & 0 deletions src/logger.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ logger_t default_logger = {
.flags = LOGGER_FLAG_NONE,
.log_level = LOG_INFO,
.name = "GLOBAL",
#if defined(NO_OS)
.puts = NULL,
#else
.puts = puts,
#endif
.root_path = NULL,
.cb = NULL,
};
Expand All @@ -59,6 +63,10 @@ static const char *log_level_names[LOG_MAX_LEVEL] = {

static inline void logger_log_set_color(logger_t *ctx, const char *color)
{
#if defined(NO_OS)
ARG_UNUSED(ctx);
ARG_UNUSED(color);
#else
size_t ret, len;

if (ctx->flags & LOGGER_FLAG_NO_COLORS)
Expand All @@ -70,6 +78,7 @@ static inline void logger_log_set_color(logger_t *ctx, const char *color)
assert(ret == len);
ARG_UNUSED(ret); /* squash warning in Release builds */
}
#endif
}

static const char *get_tstamp()
Expand Down Expand Up @@ -134,10 +143,17 @@ int __logger_log(logger_t *ctx, int log_level, const char *file, unsigned long l
ctx->cb(log_level, file, line, buf);
} else {
logger_log_set_color(ctx, log_level_colors[log_level]);
#if defined(NO_OS)
if (ctx->puts)
ctx->puts(buf);
else
return -1;
#else
if (ctx->file)
fputs(buf, ctx->file);
else
ctx->puts(buf);
#endif
logger_log_set_color(ctx, RESET);
}

Expand Down
19 changes: 19 additions & 0 deletions src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,26 @@ int add_iso8601_utc_datetime(char *buf, size_t size)
return strftime(buf, size, "%Y-%m-%dT%H:%M:%SZ", &timeinfo);
}

#elif defined(NO_OS)

int add_iso8601_utc_datetime(char *buf, size_t size)
{
//TODO: Timestamp Generation
ARG_UNUSED(buf);
ARG_UNUSED(size);
return 0;
}

// Expect user to provide this function
extern int64_t usec_now();

#else

#error Platform test failed

#endif

#if !defined(NO_OS)
int64_t usec_now()
{
int64_t usec;
Expand All @@ -173,6 +187,7 @@ void get_time(uint32_t *seconds, uint32_t *micro_seconds)
*seconds = tv.tv_sec;
*micro_seconds = tv.tv_usec;
}
#endif

int64_t usec_since(int64_t last)
{
Expand Down Expand Up @@ -205,6 +220,10 @@ void dump_trace(void)
puts("");
free(strings);
}
#elif defined(NO_OS)
void dump_trace(void)
{
}
#else
void dump_trace(void)
{
Expand Down
Loading