Created
April 11, 2018 04:43
-
-
Save incrediblejr/5a9d260f56c44a3e3a54a41188b9f43a to your computer and use it in GitHub Desktop.
quick and dirty printf-logging with (compile time) 'multiple outputs'
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* clang-format off */ | |
/* | |
ijsl : IncredibleJunior Simple Log | |
quick and dirty printf-logging with (compile time) 'multiple outputs' implemented | |
as a stb-style header-file library[1] which means that in *ONE* source file declare | |
the implementation (see example) and the other source files should just include | |
this file. | |
example setup for Windows (declare OutputDebugStringA ourselves to avoid including windows.h) | |
#ifdef __cplusplus | |
extern "C" { | |
#endif | |
__declspec(dllimport) void __stdcall OutputDebugStringA(const char *lpOutputString); | |
#ifdef __cplusplus | |
} | |
#endif | |
#include <stdio.h> | |
#define IJSL_log_impl(s) printf("%s", (s)), OutputDebugStringA((s)) | |
#define IJSL_IMPLEMENTATION | |
#include "ijsl.h" | |
License: | |
This software is dual-licensed to the public domain and under the following | |
license: you are granted a perpetual, irrevocable license to copy, modify, | |
publish, and distribute this file as you see fit. | |
References: | |
[1] https://github.com/nothings/stb | |
*/ | |
#ifndef IJSL_INCLUDED_H | |
#define IJSL_INCLUDED_H | |
#ifdef __cplusplus | |
extern "C" { | |
#endif | |
extern void ijsl_log(const char *format, ...); | |
#define IJSL_LOG(s, ...) ijsl_log(s "\n", ##__VA_ARGS__) | |
#ifdef __cplusplus | |
} | |
#endif | |
#endif | |
#ifdef IJSL_IMPLEMENTATION | |
#ifndef IJSL_TEMP_BUFFER_SIZE | |
#define IJSL_TEMP_BUFFER_SIZE (32 * 1024) | |
#endif | |
#ifndef IJSL_snprintf | |
#include <stdio.h> | |
#include <stdarg.h> | |
#define IJSL_snprintf(s, n, f, v) _vsnprintf_s((s), (n), _TRUNCATE, (f), (v)) | |
#endif | |
#ifndef IJSL_log_impl | |
#error IJSL_log_impl must be defined | |
/* examples | |
#define IJSL_log_impl(s) printf("%s", (s)), OutputDebugStringA((s)) | |
if file logging is also wanted: | |
FILE *logfile; | |
#define IJSL_log_impl(s) printf("%s", (s)), OutputDebugStringA((s)), (logfile ? fprintf(logfile, "%s", (s)) : 0) | |
*/ | |
#endif | |
extern void ijsl_log(const char *msg_format, ...) | |
{ | |
char buffer[IJSL_TEMP_BUFFER_SIZE]; | |
va_list args; | |
va_start(args, msg_format); | |
if (0 > IJSL_snprintf(buffer, IJSL_TEMP_BUFFER_SIZE, msg_format, args)) | |
buffer[IJSL_TEMP_BUFFER_SIZE-1] = 0; | |
IJSL_log_impl(buffer); | |
va_end(args); | |
} | |
#endif | |
/* clang-format on */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment