Last active
September 28, 2022 23:34
-
-
Save javiermon/54a3b51948cfe20f21a6 to your computer and use it in GitHub Desktop.
va_args logging
This file contains 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
#include <stdio.h> | |
#include <string.h> | |
#include <stdlib.h> | |
#include <errno.h> | |
#include <unistd.h> | |
#include <stdarg.h> | |
typedef signed int Int; | |
typedef unsigned int Uint; | |
typedef uint8_t Uint8; | |
typedef uint32_t Uint32; | |
typedef uint64_t Uint64; | |
typedef int32_t Int32; | |
typedef enum | |
{ | |
FALSE, /**< False value */ | |
TRUE /**< True value */ | |
} Boolean; | |
#define MEMMGR_ALLOC(size) (malloc(size)) | |
#define MEMMGR_FREE(ptr) (free(ptr)) | |
void Log(const char *func, uint32_t line, const char *preamble, const char *msg, ...) __attribute__ ((format (printf, 4, 5))); | |
#define LOG_DEBUG(msg, ...) Log(__func__, __LINE__, "DEBUG", msg, ##__VA_ARGS__) | |
#define LOG_ERROR(msg, ...) Log(__func__, __LINE__, "ERROR", msg, ##__VA_ARGS__) | |
#define LOG_ABORT(msg, ...) (Log(__func__, __LINE__, "ABORT", msg, ##__VA_ARGS__), exit(-1)) | |
void Log(const char *func, uint32_t line, const char *preamble, const char *msg, ...) | |
{ | |
char format[512]; | |
va_list arg; | |
sprintf(format, "%s(%u) %s: %s\n", func, line, preamble, msg); | |
va_start(arg, msg); | |
vfprintf(stderr, format, arg); | |
va_end(arg); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment