2017-03-06 01:25:59 +01:00
|
|
|
|
|
|
|
#ifndef TSUTIL_H
|
|
|
|
#define TSUTIL_H
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
Implemented a simple and consistent logging subsystem
The goal of this change is mainly to clean up Taisei's codebase and
improve its console output. I've been frustrated by files littered with
inconsistent printf/fprintf/warnx/errx calls for a long time, and now I
actually did something about it.
All the above functions are now considered deprecated and result in a
compile-time warning when used. Instead, the following macros should be
used:
log_debug(format, ...)
log_info(format, ...)
log_warn(format, ...)
log_err(format, ...)
As you can see, all of them have the same printf-like interface. But
they have different functionality and purpose:
log_debug is intended for very verbose and specific information. It
does nothing in release builds, much like assert(), so don't use
expressions with side-effects in its arguments.
log_info is for various status updates that are expected during
normal operation of the program.
log_warn is for non-critical failures or other things that may be
worth investigating, but don't inherently render the program
non-functional.
log_err is for when the only choice is to give up. Like errx, it
also terminates the program. Unlike errx, it actually calls abort(),
which means the cleanup functions are not ran -- but on the other
hand, you get a debuggable backtrace. However, if you're trying to
catch programming errors, consider using assert() instead.
All of them produce output that contains a timestamp, the log level
identifier, the calling function's name, and the formatted message.
The newline at the end of the format string is not required -- no, it is
actually *prohibited*. The logging system will take care of the line
breaks by itself, don't litter the code with that shit.
Internally, the logging system is based on the SDL_RWops abstraction,
and may have multiple, configurable destinations. This makes it easily
extensible. Currently, log_debug and log_info are set to write to
stdout, log_warn and log_err to stderr, and all of them also to the file
log.txt in the Taisei config directory.
Consequently, the nasty freopen hacks we used to make Taisei write to
log files on Windows are no longer needed -- which is a very good thing,
considering they probably would break if the configdir path contains
UTF-8 characters. SDL_RWFromFile does not suffer this limitation.
As an added bonus, it's also thread-safe.
Note about printf and fprintf: in very few cases, the logging system is
not a good substitute for these functions. That is, when you care about
writing exactly to stdout/stderr and about exactly how the output looks.
However, I insist on keeping the deprecation warnings on them to not
tempt anyone to use them for logging/debugging out of habit and/or
laziness.
For this reason, I've added a tsfprintf function to util.c. It is
functionally identical to fprintf, except it returns void. Yes, the name
is deliberately ugly. Avoid using it if possible, but if you must, only
use it to write to stdout or stderr. Do not write to actual files with
it, use SDL_RWops.
2017-03-13 03:51:58 +01:00
|
|
|
#include <stdnoreturn.h>
|
2017-03-06 01:25:59 +01:00
|
|
|
#include <stdio.h>
|
Implemented a simple and consistent logging subsystem
The goal of this change is mainly to clean up Taisei's codebase and
improve its console output. I've been frustrated by files littered with
inconsistent printf/fprintf/warnx/errx calls for a long time, and now I
actually did something about it.
All the above functions are now considered deprecated and result in a
compile-time warning when used. Instead, the following macros should be
used:
log_debug(format, ...)
log_info(format, ...)
log_warn(format, ...)
log_err(format, ...)
As you can see, all of them have the same printf-like interface. But
they have different functionality and purpose:
log_debug is intended for very verbose and specific information. It
does nothing in release builds, much like assert(), so don't use
expressions with side-effects in its arguments.
log_info is for various status updates that are expected during
normal operation of the program.
log_warn is for non-critical failures or other things that may be
worth investigating, but don't inherently render the program
non-functional.
log_err is for when the only choice is to give up. Like errx, it
also terminates the program. Unlike errx, it actually calls abort(),
which means the cleanup functions are not ran -- but on the other
hand, you get a debuggable backtrace. However, if you're trying to
catch programming errors, consider using assert() instead.
All of them produce output that contains a timestamp, the log level
identifier, the calling function's name, and the formatted message.
The newline at the end of the format string is not required -- no, it is
actually *prohibited*. The logging system will take care of the line
breaks by itself, don't litter the code with that shit.
Internally, the logging system is based on the SDL_RWops abstraction,
and may have multiple, configurable destinations. This makes it easily
extensible. Currently, log_debug and log_info are set to write to
stdout, log_warn and log_err to stderr, and all of them also to the file
log.txt in the Taisei config directory.
Consequently, the nasty freopen hacks we used to make Taisei write to
log files on Windows are no longer needed -- which is a very good thing,
considering they probably would break if the configdir path contains
UTF-8 characters. SDL_RWFromFile does not suffer this limitation.
As an added bonus, it's also thread-safe.
Note about printf and fprintf: in very few cases, the logging system is
not a good substitute for these functions. That is, when you care about
writing exactly to stdout/stderr and about exactly how the output looks.
However, I insist on keeping the deprecation warnings on them to not
tempt anyone to use them for logging/debugging out of habit and/or
laziness.
For this reason, I've added a tsfprintf function to util.c. It is
functionally identical to fprintf, except it returns void. Yes, the name
is deliberately ugly. Avoid using it if possible, but if you must, only
use it to write to stdout or stderr. Do not write to actual files with
it, use SDL_RWops.
2017-03-13 03:51:58 +01:00
|
|
|
#include <string.h>
|
2017-03-06 01:25:59 +01:00
|
|
|
#include <zlib.h> // compiling under mingw may fail without this...
|
2017-03-12 22:07:21 +01:00
|
|
|
#include <png.h>
|
2017-03-11 22:10:05 +01:00
|
|
|
#include <SDL.h>
|
Implemented a simple and consistent logging subsystem
The goal of this change is mainly to clean up Taisei's codebase and
improve its console output. I've been frustrated by files littered with
inconsistent printf/fprintf/warnx/errx calls for a long time, and now I
actually did something about it.
All the above functions are now considered deprecated and result in a
compile-time warning when used. Instead, the following macros should be
used:
log_debug(format, ...)
log_info(format, ...)
log_warn(format, ...)
log_err(format, ...)
As you can see, all of them have the same printf-like interface. But
they have different functionality and purpose:
log_debug is intended for very verbose and specific information. It
does nothing in release builds, much like assert(), so don't use
expressions with side-effects in its arguments.
log_info is for various status updates that are expected during
normal operation of the program.
log_warn is for non-critical failures or other things that may be
worth investigating, but don't inherently render the program
non-functional.
log_err is for when the only choice is to give up. Like errx, it
also terminates the program. Unlike errx, it actually calls abort(),
which means the cleanup functions are not ran -- but on the other
hand, you get a debuggable backtrace. However, if you're trying to
catch programming errors, consider using assert() instead.
All of them produce output that contains a timestamp, the log level
identifier, the calling function's name, and the formatted message.
The newline at the end of the format string is not required -- no, it is
actually *prohibited*. The logging system will take care of the line
breaks by itself, don't litter the code with that shit.
Internally, the logging system is based on the SDL_RWops abstraction,
and may have multiple, configurable destinations. This makes it easily
extensible. Currently, log_debug and log_info are set to write to
stdout, log_warn and log_err to stderr, and all of them also to the file
log.txt in the Taisei config directory.
Consequently, the nasty freopen hacks we used to make Taisei write to
log files on Windows are no longer needed -- which is a very good thing,
considering they probably would break if the configdir path contains
UTF-8 characters. SDL_RWFromFile does not suffer this limitation.
As an added bonus, it's also thread-safe.
Note about printf and fprintf: in very few cases, the logging system is
not a good substitute for these functions. That is, when you care about
writing exactly to stdout/stderr and about exactly how the output looks.
However, I insist on keeping the deprecation warnings on them to not
tempt anyone to use them for logging/debugging out of habit and/or
laziness.
For this reason, I've added a tsfprintf function to util.c. It is
functionally identical to fprintf, except it returns void. Yes, the name
is deliberately ugly. Avoid using it if possible, but if you must, only
use it to write to stdout or stderr. Do not write to actual files with
it, use SDL_RWops.
2017-03-13 03:51:58 +01:00
|
|
|
#include "log.h"
|
2017-03-11 22:10:05 +01:00
|
|
|
#include "hashtable.h"
|
2017-04-18 21:48:18 +02:00
|
|
|
#include "vfs/public.h"
|
2017-03-06 01:25:59 +01:00
|
|
|
|
2017-03-06 02:51:27 +01:00
|
|
|
//
|
|
|
|
// compatibility
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef __GNUC__ // clang defines this too
|
2017-05-07 00:59:44 +02:00
|
|
|
#define __attribute__(...)
|
|
|
|
#define __extension__
|
|
|
|
#define PRAGMA(p)
|
|
|
|
#else
|
|
|
|
#define PRAGMA(p) _Pragma(#p)
|
2017-03-06 02:51:27 +01:00
|
|
|
#endif
|
|
|
|
|
2017-03-06 01:25:59 +01:00
|
|
|
//
|
|
|
|
// string utils
|
|
|
|
//
|
|
|
|
|
|
|
|
#undef strlcat
|
|
|
|
#undef strlcpy
|
|
|
|
#define strlcat SDL_strlcat
|
|
|
|
#define strlcpy SDL_strlcpy
|
|
|
|
|
|
|
|
char* copy_segment(const char *text, const char *delim, int *size);
|
2017-04-21 06:22:43 +02:00
|
|
|
bool strendswith(const char *s, const char *e) __attribute__((pure));
|
|
|
|
bool strstartswith(const char *s, const char *p) __attribute__((pure));
|
|
|
|
bool strendswith_any(const char *s, const char **earray) __attribute__((pure));
|
|
|
|
bool strstartswith_any(const char *s, const char **earray) __attribute__((pure));
|
2017-03-06 01:25:59 +01:00
|
|
|
void stralloc(char **dest, const char *src);
|
2017-03-06 02:51:27 +01:00
|
|
|
char* strjoin(const char *first, ...) __attribute__((sentinel));
|
2017-03-12 22:40:19 +01:00
|
|
|
char* vstrfmt(const char *fmt, va_list args);
|
2017-03-06 02:51:27 +01:00
|
|
|
char* strfmt(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
|
2017-03-06 14:07:51 +01:00
|
|
|
void strip_trailing_slashes(char *buf);
|
2017-03-13 06:44:39 +01:00
|
|
|
char* strtok_r(char *str, const char *delim, char **nextp);
|
2017-04-18 21:48:18 +02:00
|
|
|
char* strappend(char **dst, char *src);
|
2017-03-06 01:25:59 +01:00
|
|
|
#undef strdup
|
|
|
|
#define strdup SDL_strdup
|
|
|
|
|
|
|
|
//
|
|
|
|
// math utils
|
|
|
|
//
|
|
|
|
|
|
|
|
#include <complex.h>
|
|
|
|
|
|
|
|
// These definitions are common but non-standard, so we provide our own
|
|
|
|
#undef M_PI
|
|
|
|
#undef M_PI_2
|
|
|
|
#define M_PI 3.14159265358979323846
|
|
|
|
#define M_PI_2 1.57079632679489661923
|
|
|
|
|
|
|
|
// This is a workaround to properly specify the type of our "complex" variables...
|
|
|
|
// Taisei code always uses just "complex" when it actually means "complex double", which is not really correct...
|
|
|
|
// gcc doesn't seem to care, other compilers do (e.g. clang)
|
|
|
|
#ifdef complex
|
|
|
|
#undef complex
|
|
|
|
#define complex _Complex double
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// needed for mingw compatibility:
|
|
|
|
#undef min
|
|
|
|
#undef max
|
|
|
|
|
2017-03-06 02:51:27 +01:00
|
|
|
double min(double, double) __attribute__((const));
|
|
|
|
double max(double, double) __attribute__((const));
|
|
|
|
double clamp(double, double, double) __attribute__((const));
|
|
|
|
double approach(double v, double t, double d) __attribute__((const));
|
|
|
|
double psin(double) __attribute__((const));
|
2017-03-06 16:32:51 +01:00
|
|
|
int sign(double) __attribute__((const));
|
2017-03-11 02:49:09 +01:00
|
|
|
double swing(double x, double s) __attribute__((const));
|
2017-04-07 01:54:59 +02:00
|
|
|
unsigned int topow2(unsigned int x) __attribute__((const));
|
|
|
|
float ftopow2(float x) __attribute__((const));
|
2017-09-05 23:02:40 +02:00
|
|
|
float smooth(float x);
|
|
|
|
float smoothreclamp(float x, float old_min, float old_max, float new_min, float new_max);
|
2017-03-06 01:25:59 +01:00
|
|
|
|
|
|
|
//
|
|
|
|
// gl/video utils
|
|
|
|
//
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int fpstime; // frame counter
|
|
|
|
int fps;
|
|
|
|
int show_fps;
|
|
|
|
double stagebg_fps;
|
|
|
|
} FPSCounter;
|
|
|
|
|
2017-03-27 22:46:39 +02:00
|
|
|
void frame_rate(uint64_t *lasttime);
|
2017-03-21 03:28:35 +01:00
|
|
|
bool calc_fps(FPSCounter *fps);
|
2017-03-06 01:25:59 +01:00
|
|
|
void set_ortho(void);
|
2017-04-07 01:54:59 +02:00
|
|
|
void set_ortho_ex(float w, float h);
|
2017-03-06 01:25:59 +01:00
|
|
|
void colorfill(float r, float g, float b, float a);
|
|
|
|
void fade_out(float f);
|
|
|
|
|
|
|
|
//
|
|
|
|
// i/o utils
|
|
|
|
//
|
|
|
|
|
2017-03-11 22:10:05 +01:00
|
|
|
typedef void (*KVCallback)(const char *key, const char *value, void *data);
|
|
|
|
|
2017-03-06 01:25:59 +01:00
|
|
|
char* read_all(const char *filename, int *size);
|
2017-03-11 22:10:05 +01:00
|
|
|
bool parse_keyvalue_stream_cb(SDL_RWops *strm, KVCallback callback, void *data);
|
|
|
|
bool parse_keyvalue_file_cb(const char *filename, KVCallback callback, void *data);
|
2017-03-11 22:47:42 +01:00
|
|
|
Hashtable* parse_keyvalue_stream(SDL_RWops *strm, size_t tablesize);
|
|
|
|
Hashtable* parse_keyvalue_file(const char *filename, size_t tablesize);
|
2017-03-12 22:25:19 +01:00
|
|
|
void png_init_rwops_read(png_structp png, SDL_RWops *rwops);
|
|
|
|
void png_init_rwops_write(png_structp png, SDL_RWops *rwops);
|
2017-03-06 01:25:59 +01:00
|
|
|
|
2017-03-12 22:22:43 +01:00
|
|
|
char* SDL_RWgets(SDL_RWops *rwops, char *buf, size_t bufsize);
|
2017-03-12 22:40:19 +01:00
|
|
|
size_t SDL_RWprintf(SDL_RWops *rwops, const char* fmt, ...) __attribute__((format(printf, 2, 3)));
|
2017-03-12 22:22:43 +01:00
|
|
|
|
Implemented a simple and consistent logging subsystem
The goal of this change is mainly to clean up Taisei's codebase and
improve its console output. I've been frustrated by files littered with
inconsistent printf/fprintf/warnx/errx calls for a long time, and now I
actually did something about it.
All the above functions are now considered deprecated and result in a
compile-time warning when used. Instead, the following macros should be
used:
log_debug(format, ...)
log_info(format, ...)
log_warn(format, ...)
log_err(format, ...)
As you can see, all of them have the same printf-like interface. But
they have different functionality and purpose:
log_debug is intended for very verbose and specific information. It
does nothing in release builds, much like assert(), so don't use
expressions with side-effects in its arguments.
log_info is for various status updates that are expected during
normal operation of the program.
log_warn is for non-critical failures or other things that may be
worth investigating, but don't inherently render the program
non-functional.
log_err is for when the only choice is to give up. Like errx, it
also terminates the program. Unlike errx, it actually calls abort(),
which means the cleanup functions are not ran -- but on the other
hand, you get a debuggable backtrace. However, if you're trying to
catch programming errors, consider using assert() instead.
All of them produce output that contains a timestamp, the log level
identifier, the calling function's name, and the formatted message.
The newline at the end of the format string is not required -- no, it is
actually *prohibited*. The logging system will take care of the line
breaks by itself, don't litter the code with that shit.
Internally, the logging system is based on the SDL_RWops abstraction,
and may have multiple, configurable destinations. This makes it easily
extensible. Currently, log_debug and log_info are set to write to
stdout, log_warn and log_err to stderr, and all of them also to the file
log.txt in the Taisei config directory.
Consequently, the nasty freopen hacks we used to make Taisei write to
log files on Windows are no longer needed -- which is a very good thing,
considering they probably would break if the configdir path contains
UTF-8 characters. SDL_RWFromFile does not suffer this limitation.
As an added bonus, it's also thread-safe.
Note about printf and fprintf: in very few cases, the logging system is
not a good substitute for these functions. That is, when you care about
writing exactly to stdout/stderr and about exactly how the output looks.
However, I insist on keeping the deprecation warnings on them to not
tempt anyone to use them for logging/debugging out of habit and/or
laziness.
For this reason, I've added a tsfprintf function to util.c. It is
functionally identical to fprintf, except it returns void. Yes, the name
is deliberately ugly. Avoid using it if possible, but if you must, only
use it to write to stdout or stderr. Do not write to actual files with
it, use SDL_RWops.
2017-03-13 03:51:58 +01:00
|
|
|
// This is for the very few legitimate uses for printf/fprintf that shouldn't be replaced with log_*
|
|
|
|
void tsfprintf(FILE *out, const char *restrict fmt, ...) __attribute__((format(printf, 2, 3)));
|
2017-03-06 01:25:59 +01:00
|
|
|
|
|
|
|
//
|
|
|
|
// misc utils
|
|
|
|
//
|
|
|
|
|
2017-03-06 02:51:27 +01:00
|
|
|
int getenvint(const char *v) __attribute__((pure));
|
Implemented a simple and consistent logging subsystem
The goal of this change is mainly to clean up Taisei's codebase and
improve its console output. I've been frustrated by files littered with
inconsistent printf/fprintf/warnx/errx calls for a long time, and now I
actually did something about it.
All the above functions are now considered deprecated and result in a
compile-time warning when used. Instead, the following macros should be
used:
log_debug(format, ...)
log_info(format, ...)
log_warn(format, ...)
log_err(format, ...)
As you can see, all of them have the same printf-like interface. But
they have different functionality and purpose:
log_debug is intended for very verbose and specific information. It
does nothing in release builds, much like assert(), so don't use
expressions with side-effects in its arguments.
log_info is for various status updates that are expected during
normal operation of the program.
log_warn is for non-critical failures or other things that may be
worth investigating, but don't inherently render the program
non-functional.
log_err is for when the only choice is to give up. Like errx, it
also terminates the program. Unlike errx, it actually calls abort(),
which means the cleanup functions are not ran -- but on the other
hand, you get a debuggable backtrace. However, if you're trying to
catch programming errors, consider using assert() instead.
All of them produce output that contains a timestamp, the log level
identifier, the calling function's name, and the formatted message.
The newline at the end of the format string is not required -- no, it is
actually *prohibited*. The logging system will take care of the line
breaks by itself, don't litter the code with that shit.
Internally, the logging system is based on the SDL_RWops abstraction,
and may have multiple, configurable destinations. This makes it easily
extensible. Currently, log_debug and log_info are set to write to
stdout, log_warn and log_err to stderr, and all of them also to the file
log.txt in the Taisei config directory.
Consequently, the nasty freopen hacks we used to make Taisei write to
log files on Windows are no longer needed -- which is a very good thing,
considering they probably would break if the configdir path contains
UTF-8 characters. SDL_RWFromFile does not suffer this limitation.
As an added bonus, it's also thread-safe.
Note about printf and fprintf: in very few cases, the logging system is
not a good substitute for these functions. That is, when you care about
writing exactly to stdout/stderr and about exactly how the output looks.
However, I insist on keeping the deprecation warnings on them to not
tempt anyone to use them for logging/debugging out of habit and/or
laziness.
For this reason, I've added a tsfprintf function to util.c. It is
functionally identical to fprintf, except it returns void. Yes, the name
is deliberately ugly. Avoid using it if possible, but if you must, only
use it to write to stdout or stderr. Do not write to actual files with
it, use SDL_RWops.
2017-03-13 03:51:58 +01:00
|
|
|
void png_setup_error_handlers(png_structp png);
|
2017-03-29 00:11:39 +02:00
|
|
|
uint32_t crc32str(uint32_t crc, const char *str);
|
|
|
|
|
2017-03-15 10:21:39 +01:00
|
|
|
noreturn void _ts_assert_fail(const char *cond, const char *func, const char *file, int line, bool use_log);
|
|
|
|
|
|
|
|
#undef assert
|
|
|
|
|
|
|
|
#ifdef NDEBUG
|
|
|
|
#define _assert(cond,uselog)
|
|
|
|
#else
|
|
|
|
#define _assert(cond,uselog) ((cond) ? (void)0 : _ts_assert_fail(#cond, __func__, __FILE__, __LINE__, uselog))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define assert(cond) _assert(cond, true)
|
|
|
|
#define assert_nolog(cond) _assert(cond, false)
|
2017-03-06 01:25:59 +01:00
|
|
|
|
2017-03-12 21:26:10 +01:00
|
|
|
//
|
Implemented a simple and consistent logging subsystem
The goal of this change is mainly to clean up Taisei's codebase and
improve its console output. I've been frustrated by files littered with
inconsistent printf/fprintf/warnx/errx calls for a long time, and now I
actually did something about it.
All the above functions are now considered deprecated and result in a
compile-time warning when used. Instead, the following macros should be
used:
log_debug(format, ...)
log_info(format, ...)
log_warn(format, ...)
log_err(format, ...)
As you can see, all of them have the same printf-like interface. But
they have different functionality and purpose:
log_debug is intended for very verbose and specific information. It
does nothing in release builds, much like assert(), so don't use
expressions with side-effects in its arguments.
log_info is for various status updates that are expected during
normal operation of the program.
log_warn is for non-critical failures or other things that may be
worth investigating, but don't inherently render the program
non-functional.
log_err is for when the only choice is to give up. Like errx, it
also terminates the program. Unlike errx, it actually calls abort(),
which means the cleanup functions are not ran -- but on the other
hand, you get a debuggable backtrace. However, if you're trying to
catch programming errors, consider using assert() instead.
All of them produce output that contains a timestamp, the log level
identifier, the calling function's name, and the formatted message.
The newline at the end of the format string is not required -- no, it is
actually *prohibited*. The logging system will take care of the line
breaks by itself, don't litter the code with that shit.
Internally, the logging system is based on the SDL_RWops abstraction,
and may have multiple, configurable destinations. This makes it easily
extensible. Currently, log_debug and log_info are set to write to
stdout, log_warn and log_err to stderr, and all of them also to the file
log.txt in the Taisei config directory.
Consequently, the nasty freopen hacks we used to make Taisei write to
log files on Windows are no longer needed -- which is a very good thing,
considering they probably would break if the configdir path contains
UTF-8 characters. SDL_RWFromFile does not suffer this limitation.
As an added bonus, it's also thread-safe.
Note about printf and fprintf: in very few cases, the logging system is
not a good substitute for these functions. That is, when you care about
writing exactly to stdout/stderr and about exactly how the output looks.
However, I insist on keeping the deprecation warnings on them to not
tempt anyone to use them for logging/debugging out of habit and/or
laziness.
For this reason, I've added a tsfprintf function to util.c. It is
functionally identical to fprintf, except it returns void. Yes, the name
is deliberately ugly. Avoid using it if possible, but if you must, only
use it to write to stdout or stderr. Do not write to actual files with
it, use SDL_RWops.
2017-03-13 03:51:58 +01:00
|
|
|
// safeguards against some dangerous or otherwise undesirable practices
|
2017-03-12 21:26:10 +01:00
|
|
|
//
|
|
|
|
|
2017-05-07 00:59:44 +02:00
|
|
|
PRAGMA(GCC diagnostic push)
|
|
|
|
PRAGMA(GCC diagnostic ignored "-Wstrict-prototypes")
|
|
|
|
|
2017-03-12 21:26:10 +01:00
|
|
|
#undef fopen
|
Implemented a simple and consistent logging subsystem
The goal of this change is mainly to clean up Taisei's codebase and
improve its console output. I've been frustrated by files littered with
inconsistent printf/fprintf/warnx/errx calls for a long time, and now I
actually did something about it.
All the above functions are now considered deprecated and result in a
compile-time warning when used. Instead, the following macros should be
used:
log_debug(format, ...)
log_info(format, ...)
log_warn(format, ...)
log_err(format, ...)
As you can see, all of them have the same printf-like interface. But
they have different functionality and purpose:
log_debug is intended for very verbose and specific information. It
does nothing in release builds, much like assert(), so don't use
expressions with side-effects in its arguments.
log_info is for various status updates that are expected during
normal operation of the program.
log_warn is for non-critical failures or other things that may be
worth investigating, but don't inherently render the program
non-functional.
log_err is for when the only choice is to give up. Like errx, it
also terminates the program. Unlike errx, it actually calls abort(),
which means the cleanup functions are not ran -- but on the other
hand, you get a debuggable backtrace. However, if you're trying to
catch programming errors, consider using assert() instead.
All of them produce output that contains a timestamp, the log level
identifier, the calling function's name, and the formatted message.
The newline at the end of the format string is not required -- no, it is
actually *prohibited*. The logging system will take care of the line
breaks by itself, don't litter the code with that shit.
Internally, the logging system is based on the SDL_RWops abstraction,
and may have multiple, configurable destinations. This makes it easily
extensible. Currently, log_debug and log_info are set to write to
stdout, log_warn and log_err to stderr, and all of them also to the file
log.txt in the Taisei config directory.
Consequently, the nasty freopen hacks we used to make Taisei write to
log files on Windows are no longer needed -- which is a very good thing,
considering they probably would break if the configdir path contains
UTF-8 characters. SDL_RWFromFile does not suffer this limitation.
As an added bonus, it's also thread-safe.
Note about printf and fprintf: in very few cases, the logging system is
not a good substitute for these functions. That is, when you care about
writing exactly to stdout/stderr and about exactly how the output looks.
However, I insist on keeping the deprecation warnings on them to not
tempt anyone to use them for logging/debugging out of habit and/or
laziness.
For this reason, I've added a tsfprintf function to util.c. It is
functionally identical to fprintf, except it returns void. Yes, the name
is deliberately ugly. Avoid using it if possible, but if you must, only
use it to write to stdout or stderr. Do not write to actual files with
it, use SDL_RWops.
2017-03-13 03:51:58 +01:00
|
|
|
FILE* fopen() __attribute__((deprecated(
|
2017-04-18 21:48:18 +02:00
|
|
|
"Use vfs_open or SDL_RWFromFile instead")));
|
2017-03-12 21:26:10 +01:00
|
|
|
|
|
|
|
#undef strncat
|
Implemented a simple and consistent logging subsystem
The goal of this change is mainly to clean up Taisei's codebase and
improve its console output. I've been frustrated by files littered with
inconsistent printf/fprintf/warnx/errx calls for a long time, and now I
actually did something about it.
All the above functions are now considered deprecated and result in a
compile-time warning when used. Instead, the following macros should be
used:
log_debug(format, ...)
log_info(format, ...)
log_warn(format, ...)
log_err(format, ...)
As you can see, all of them have the same printf-like interface. But
they have different functionality and purpose:
log_debug is intended for very verbose and specific information. It
does nothing in release builds, much like assert(), so don't use
expressions with side-effects in its arguments.
log_info is for various status updates that are expected during
normal operation of the program.
log_warn is for non-critical failures or other things that may be
worth investigating, but don't inherently render the program
non-functional.
log_err is for when the only choice is to give up. Like errx, it
also terminates the program. Unlike errx, it actually calls abort(),
which means the cleanup functions are not ran -- but on the other
hand, you get a debuggable backtrace. However, if you're trying to
catch programming errors, consider using assert() instead.
All of them produce output that contains a timestamp, the log level
identifier, the calling function's name, and the formatted message.
The newline at the end of the format string is not required -- no, it is
actually *prohibited*. The logging system will take care of the line
breaks by itself, don't litter the code with that shit.
Internally, the logging system is based on the SDL_RWops abstraction,
and may have multiple, configurable destinations. This makes it easily
extensible. Currently, log_debug and log_info are set to write to
stdout, log_warn and log_err to stderr, and all of them also to the file
log.txt in the Taisei config directory.
Consequently, the nasty freopen hacks we used to make Taisei write to
log files on Windows are no longer needed -- which is a very good thing,
considering they probably would break if the configdir path contains
UTF-8 characters. SDL_RWFromFile does not suffer this limitation.
As an added bonus, it's also thread-safe.
Note about printf and fprintf: in very few cases, the logging system is
not a good substitute for these functions. That is, when you care about
writing exactly to stdout/stderr and about exactly how the output looks.
However, I insist on keeping the deprecation warnings on them to not
tempt anyone to use them for logging/debugging out of habit and/or
laziness.
For this reason, I've added a tsfprintf function to util.c. It is
functionally identical to fprintf, except it returns void. Yes, the name
is deliberately ugly. Avoid using it if possible, but if you must, only
use it to write to stdout or stderr. Do not write to actual files with
it, use SDL_RWops.
2017-03-13 03:51:58 +01:00
|
|
|
char* strncat() __attribute__((deprecated(
|
|
|
|
"This function likely doesn't do what you expect, use strlcat")));
|
2017-03-12 21:26:10 +01:00
|
|
|
|
|
|
|
#undef strncpy
|
Implemented a simple and consistent logging subsystem
The goal of this change is mainly to clean up Taisei's codebase and
improve its console output. I've been frustrated by files littered with
inconsistent printf/fprintf/warnx/errx calls for a long time, and now I
actually did something about it.
All the above functions are now considered deprecated and result in a
compile-time warning when used. Instead, the following macros should be
used:
log_debug(format, ...)
log_info(format, ...)
log_warn(format, ...)
log_err(format, ...)
As you can see, all of them have the same printf-like interface. But
they have different functionality and purpose:
log_debug is intended for very verbose and specific information. It
does nothing in release builds, much like assert(), so don't use
expressions with side-effects in its arguments.
log_info is for various status updates that are expected during
normal operation of the program.
log_warn is for non-critical failures or other things that may be
worth investigating, but don't inherently render the program
non-functional.
log_err is for when the only choice is to give up. Like errx, it
also terminates the program. Unlike errx, it actually calls abort(),
which means the cleanup functions are not ran -- but on the other
hand, you get a debuggable backtrace. However, if you're trying to
catch programming errors, consider using assert() instead.
All of them produce output that contains a timestamp, the log level
identifier, the calling function's name, and the formatted message.
The newline at the end of the format string is not required -- no, it is
actually *prohibited*. The logging system will take care of the line
breaks by itself, don't litter the code with that shit.
Internally, the logging system is based on the SDL_RWops abstraction,
and may have multiple, configurable destinations. This makes it easily
extensible. Currently, log_debug and log_info are set to write to
stdout, log_warn and log_err to stderr, and all of them also to the file
log.txt in the Taisei config directory.
Consequently, the nasty freopen hacks we used to make Taisei write to
log files on Windows are no longer needed -- which is a very good thing,
considering they probably would break if the configdir path contains
UTF-8 characters. SDL_RWFromFile does not suffer this limitation.
As an added bonus, it's also thread-safe.
Note about printf and fprintf: in very few cases, the logging system is
not a good substitute for these functions. That is, when you care about
writing exactly to stdout/stderr and about exactly how the output looks.
However, I insist on keeping the deprecation warnings on them to not
tempt anyone to use them for logging/debugging out of habit and/or
laziness.
For this reason, I've added a tsfprintf function to util.c. It is
functionally identical to fprintf, except it returns void. Yes, the name
is deliberately ugly. Avoid using it if possible, but if you must, only
use it to write to stdout or stderr. Do not write to actual files with
it, use SDL_RWops.
2017-03-13 03:51:58 +01:00
|
|
|
char* strncpy() __attribute__((deprecated(
|
|
|
|
"This function likely doesn't do what you expect, use strlcpy")));
|
|
|
|
|
|
|
|
#undef errx
|
|
|
|
noreturn void errx(int, const char*, ...) __attribute__((deprecated(
|
2017-03-13 17:03:51 +01:00
|
|
|
"Use log_fatal instead")));
|
Implemented a simple and consistent logging subsystem
The goal of this change is mainly to clean up Taisei's codebase and
improve its console output. I've been frustrated by files littered with
inconsistent printf/fprintf/warnx/errx calls for a long time, and now I
actually did something about it.
All the above functions are now considered deprecated and result in a
compile-time warning when used. Instead, the following macros should be
used:
log_debug(format, ...)
log_info(format, ...)
log_warn(format, ...)
log_err(format, ...)
As you can see, all of them have the same printf-like interface. But
they have different functionality and purpose:
log_debug is intended for very verbose and specific information. It
does nothing in release builds, much like assert(), so don't use
expressions with side-effects in its arguments.
log_info is for various status updates that are expected during
normal operation of the program.
log_warn is for non-critical failures or other things that may be
worth investigating, but don't inherently render the program
non-functional.
log_err is for when the only choice is to give up. Like errx, it
also terminates the program. Unlike errx, it actually calls abort(),
which means the cleanup functions are not ran -- but on the other
hand, you get a debuggable backtrace. However, if you're trying to
catch programming errors, consider using assert() instead.
All of them produce output that contains a timestamp, the log level
identifier, the calling function's name, and the formatted message.
The newline at the end of the format string is not required -- no, it is
actually *prohibited*. The logging system will take care of the line
breaks by itself, don't litter the code with that shit.
Internally, the logging system is based on the SDL_RWops abstraction,
and may have multiple, configurable destinations. This makes it easily
extensible. Currently, log_debug and log_info are set to write to
stdout, log_warn and log_err to stderr, and all of them also to the file
log.txt in the Taisei config directory.
Consequently, the nasty freopen hacks we used to make Taisei write to
log files on Windows are no longer needed -- which is a very good thing,
considering they probably would break if the configdir path contains
UTF-8 characters. SDL_RWFromFile does not suffer this limitation.
As an added bonus, it's also thread-safe.
Note about printf and fprintf: in very few cases, the logging system is
not a good substitute for these functions. That is, when you care about
writing exactly to stdout/stderr and about exactly how the output looks.
However, I insist on keeping the deprecation warnings on them to not
tempt anyone to use them for logging/debugging out of habit and/or
laziness.
For this reason, I've added a tsfprintf function to util.c. It is
functionally identical to fprintf, except it returns void. Yes, the name
is deliberately ugly. Avoid using it if possible, but if you must, only
use it to write to stdout or stderr. Do not write to actual files with
it, use SDL_RWops.
2017-03-13 03:51:58 +01:00
|
|
|
|
|
|
|
#undef warnx
|
|
|
|
void warnx(const char*, ...) __attribute__((deprecated(
|
|
|
|
"Use log_warn instead")));
|
|
|
|
|
|
|
|
#undef printf
|
|
|
|
int printf(const char*, ...) __attribute__((deprecated(
|
|
|
|
"Use log_info instead")));
|
|
|
|
|
|
|
|
#undef fprintf
|
|
|
|
int fprintf(FILE*, const char*, ...) __attribute__((deprecated(
|
|
|
|
"Use log_warn instead (or SDL_RWops if you want to write to a file)")));
|
2017-03-06 01:25:59 +01:00
|
|
|
|
2017-03-13 06:44:39 +01:00
|
|
|
#undef strtok
|
|
|
|
char* strtok() __attribute__((deprecated(
|
|
|
|
"Use strtok_r instead")));
|
|
|
|
|
2017-03-25 02:41:10 +01:00
|
|
|
#undef sprintf
|
|
|
|
int sprintf(char *, const char*, ...) __attribute__((deprecated(
|
|
|
|
"Use snprintf or strfmt instead")));
|
|
|
|
|
2017-05-07 00:59:44 +02:00
|
|
|
PRAGMA(GCC diagnostic pop)
|
|
|
|
|
2017-03-06 01:25:59 +01:00
|
|
|
#endif
|