Changeset View
Changeset View
Standalone View
Standalone View
intern/clog/clog.c
| Show First 20 Lines • Show All 72 Lines • ▼ Show 20 Lines | |||||
| typedef struct CLogContext { | typedef struct CLogContext { | ||||
| /** Single linked list of types. */ | /** Single linked list of types. */ | ||||
| CLG_LogType *types; | CLG_LogType *types; | ||||
| /* exclude, include filters. */ | /* exclude, include filters. */ | ||||
| CLG_IDFilter *filters[2]; | CLG_IDFilter *filters[2]; | ||||
| bool use_color; | bool use_color; | ||||
| bool use_basename; | bool use_basename; | ||||
| bool use_timestamp; | bool use_timestamp; | ||||
| bool use_stdout; | |||||
| /** Borrowed, not owned. */ | /** Borrowed, not owned. */ | ||||
| int output; | int output; | ||||
| FILE *output_file; | FILE *output_file; | ||||
| /** For timer (use_timestamp). */ | /** For timer (use_timestamp). */ | ||||
| uint64_t timestamp_tick_start; | uint64_t timestamp_tick_start; | ||||
| ▲ Show 20 Lines • Show All 261 Lines • ▼ Show 20 Lines | static void clg_ctx_fatal_action(CLogContext *ctx) | ||||
| abort(); | abort(); | ||||
| } | } | ||||
| static void clg_ctx_backtrace(CLogContext *ctx) | static void clg_ctx_backtrace(CLogContext *ctx) | ||||
| { | { | ||||
| /* Note: we avoid writing fo 'FILE', for backtrace we make an exception, | /* Note: we avoid writing fo 'FILE', for backtrace we make an exception, | ||||
| * if necessary we could have a version of the callback that writes to file descriptor all at once. */ | * if necessary we could have a version of the callback that writes to file descriptor all at once. */ | ||||
| ctx->callbacks.backtrace_fn(ctx->output_file); | ctx->callbacks.backtrace_fn(ctx->output_file); | ||||
| fflush(ctx->output_file); | fflush(ctx->output_file); | ||||
LazyDodo: This will use the system timer which has 10-16ms precision. seems fine for some casual logging… | |||||
| } | } | ||||
| static uint64_t clg_timestamp_ticks_get(void) | static uint64_t clg_timestamp_ticks_get(void) | ||||
| { | { | ||||
| uint64_t tick; | uint64_t tick; | ||||
| #if defined(_MSC_VER) | #if defined(_MSC_VER) | ||||
| tick = GetTickCount64(); | tick = GetTickCount64(); | ||||
| #else | #else | ||||
| ▲ Show 20 Lines • Show All 83 Lines • ▼ Show 20 Lines | write_type(&cstr, lg); | ||||
| clg_str_append(&cstr, message); | clg_str_append(&cstr, message); | ||||
| } | } | ||||
| clg_str_append(&cstr, "\n"); | clg_str_append(&cstr, "\n"); | ||||
| /* could be optional */ | /* could be optional */ | ||||
| int bytes_written = write(lg->ctx->output, cstr.data, cstr.len); | int bytes_written = write(lg->ctx->output, cstr.data, cstr.len); | ||||
| (void)bytes_written; | (void)bytes_written; | ||||
| if (lg->ctx->use_stdout && lg->ctx->output_file != stdout) { | |||||
| write(fileno(stdout), cstr.data, cstr.len); | |||||
| } | |||||
| clg_str_free(&cstr); | clg_str_free(&cstr); | ||||
| if (lg->ctx->callbacks.backtrace_fn) { | if (lg->ctx->callbacks.backtrace_fn) { | ||||
| clg_ctx_backtrace(lg->ctx); | clg_ctx_backtrace(lg->ctx); | ||||
| } | } | ||||
| if (severity == CLG_SEVERITY_FATAL) { | if (severity == CLG_SEVERITY_FATAL) { | ||||
| clg_ctx_fatal_action(lg->ctx); | clg_ctx_fatal_action(lg->ctx); | ||||
| Show All 24 Lines | write_type(&cstr, lg); | ||||
| va_end(ap); | va_end(ap); | ||||
| } | } | ||||
| clg_str_append(&cstr, "\n"); | clg_str_append(&cstr, "\n"); | ||||
| /* could be optional */ | /* could be optional */ | ||||
| int bytes_written = write(lg->ctx->output, cstr.data, cstr.len); | int bytes_written = write(lg->ctx->output, cstr.data, cstr.len); | ||||
| (void)bytes_written; | (void)bytes_written; | ||||
| if (lg->ctx->use_stdout && lg->ctx->output_file != stdout) { | |||||
| write(fileno(stdout), cstr.data, cstr.len); | |||||
| } | |||||
| clg_str_free(&cstr); | clg_str_free(&cstr); | ||||
| if (lg->ctx->callbacks.backtrace_fn) { | if (lg->ctx->callbacks.backtrace_fn) { | ||||
| clg_ctx_backtrace(lg->ctx); | clg_ctx_backtrace(lg->ctx); | ||||
| } | } | ||||
| if (severity == CLG_SEVERITY_FATAL) { | if (severity == CLG_SEVERITY_FATAL) { | ||||
| clg_ctx_fatal_action(lg->ctx); | clg_ctx_fatal_action(lg->ctx); | ||||
| Show All 23 Lines | |||||
| static void CLG_ctx_output_use_timestamp_set(CLogContext *ctx, int value) | static void CLG_ctx_output_use_timestamp_set(CLogContext *ctx, int value) | ||||
| { | { | ||||
| ctx->use_timestamp = (bool)value; | ctx->use_timestamp = (bool)value; | ||||
| if (ctx->use_timestamp) { | if (ctx->use_timestamp) { | ||||
| ctx->timestamp_tick_start = clg_timestamp_ticks_get(); | ctx->timestamp_tick_start = clg_timestamp_ticks_get(); | ||||
| } | } | ||||
| } | } | ||||
| static void CLG_ctx_output_use_stdout_set(CLogContext *ctx, int value) | |||||
| { | |||||
| ctx->use_stdout = (bool)value; | |||||
| } | |||||
| /** Action on fatal severity. */ | /** Action on fatal severity. */ | ||||
| static void CLG_ctx_fatal_fn_set(CLogContext *ctx, void (*fatal_fn)(void *file_handle)) | static void CLG_ctx_fatal_fn_set(CLogContext *ctx, void (*fatal_fn)(void *file_handle)) | ||||
| { | { | ||||
| ctx->callbacks.fatal_fn = fatal_fn; | ctx->callbacks.fatal_fn = fatal_fn; | ||||
| } | } | ||||
| static void CLG_ctx_backtrace_fn_set(CLogContext *ctx, void (*backtrace_fn)(void *file_handle)) | static void CLG_ctx_backtrace_fn_set(CLogContext *ctx, void (*backtrace_fn)(void *file_handle)) | ||||
| { | { | ||||
| ▲ Show 20 Lines • Show All 91 Lines • ▼ Show 20 Lines | void CLG_output_use_basename_set(int value) | ||||
| CLG_ctx_output_use_basename_set(g_ctx, value); | CLG_ctx_output_use_basename_set(g_ctx, value); | ||||
| } | } | ||||
| void CLG_output_use_timestamp_set(int value) | void CLG_output_use_timestamp_set(int value) | ||||
| { | { | ||||
| CLG_ctx_output_use_timestamp_set(g_ctx, value); | CLG_ctx_output_use_timestamp_set(g_ctx, value); | ||||
| } | } | ||||
| void CLG_output_use_stdout_set(int value) | |||||
| { | |||||
| CLG_ctx_output_use_stdout_set(g_ctx, value); | |||||
| } | |||||
| void CLG_fatal_fn_set(void (*fatal_fn)(void *file_handle)) | void CLG_fatal_fn_set(void (*fatal_fn)(void *file_handle)) | ||||
| { | { | ||||
| CLG_ctx_fatal_fn_set(g_ctx, fatal_fn); | CLG_ctx_fatal_fn_set(g_ctx, fatal_fn); | ||||
| } | } | ||||
| void CLG_backtrace_fn_set(void (*fatal_fn)(void *file_handle)) | void CLG_backtrace_fn_set(void (*fatal_fn)(void *file_handle)) | ||||
| { | { | ||||
| CLG_ctx_backtrace_fn_set(g_ctx, fatal_fn); | CLG_ctx_backtrace_fn_set(g_ctx, fatal_fn); | ||||
| Show All 33 Lines | |||||
This will use the system timer which has 10-16ms precision. seems fine for some casual logging, @Campbell Barton (campbellbarton) will have to decide here.