Commit graph

36 commits

Author SHA1 Message Date
Andrei Alexeyev
ad08ddcd7b
log: fix gcc warnings 2023-05-26 02:25:00 +02:00
Andrei Alexeyev
f265f38adc
log: cotask support; rewrite console formatter 2023-03-30 04:37:57 +02:00
Andrei Alexeyev
c197d224e7
log: log originating thread names 2023-03-29 20:47:27 +02:00
Andrei Alexeyev
e957f11790
thread: add over-engineered wrapper for thread management 2023-03-29 20:47:27 +02:00
Andrei Alexeyev
7c19322940
log: flush buffers after assert failure 2023-03-26 01:07:17 +01:00
Andrei Alexeyev
b6978178b1
memory: use custom memory allocation wrappers
Introduces wrappers around memory allocation functions in `memory.h`
that should be used instead of the standard C ones.

These never return NULL and, with the exception of `mem_realloc()`,
zero-initialize the allocated memory like `calloc()` does.

All allocations made with the memory.h API must be deallocated with
`mem_free()`. Although standard `free()` will work on some platforms,
it's not portable (currently it won't work on Windows). Likewise,
`mem_free()` must not be used to free foreign allocations.

The standard C allocation functions are now diagnosed as deprecated.
They are, however, available with the `libc_` prefix in case interfacing
with foreign APIs is required. So far they are only used to implement
`memory.h`.

Perhaps the most important change is the introduction of the `ALLOC()`,
`ALLOC_ARRAY()`, and `ALLOC_FLEX()` macros. They take a type as a
parameter, and allocate enough memory with the correct alignment for
that type. That includes overaligned types as well. In most
circumstances you should prefer to use these macros. See the `memory.h`
header for some usage examples.
2023-01-18 13:23:22 +01:00
Andrei Alexeyev
fe2b69144c
log: fixup module name separators on windows 2023-01-10 06:18:44 +01:00
Andrei Alexeyev
26662582df
log: add categories and filtering functionality
A "category" (or "module") is now displayed with each log message
together with the source function. The name of the category is derived
from the source file name (see modname() in log.c).

Taisei now also loads a "logfilter" file from the storage directory that
controls which messages are propagated to the console. You can filter by
log level, category, and function. Simple glob pattern matching is
supported. Each line contains a new rule; empty lines and lines starting
with # are ignored. Rules are processed from top to bottom.

Example logfilter file:

        # [mask]         [category]       [function]
        # Hide debug messages by default:
        -d               *                *
        # Show debug messages from all of the rendering-related modules:
        +d               renderer/*       *
        # Also show debug messages from my specific function:
        +d               *                my_function
        # Hide info about resource loading/unloading:
        -i               resource         unload_resource
        -i               resource         load_resource_finish
        # Suppress everything from a specific module:
        -a               spam             *

Note that only the console output is filtered; the log file still
contains all messages. In addition to that, the log file now also
contains the filename and line number of the original log call with
every message.
2022-09-01 06:02:29 +02:00
Andrei Alexeyev
c668267888
log: sync outputs before shutdown or crash
Previously this half-worked: fflush() was called on log_fatal, but not
in case of assertion failure, leaving the on-disk log truncated.
2022-07-12 01:57:35 +03:00
Alice D
b5190e1634
fix small build warnings for clang on macOS (#272) 2020-12-24 00:45:15 +02:00
Andrei Alexeyev
bb3a809117
Asynchronous logging (#260)
* Implement asynchronous logging

Put all log messages into a queue. A background thread will format and
dispatch the messages to loggers in a first-in-first-out fashion. This
is mostly useful for windows/wine, or in cases *very* spammy logging.
This adds some malloc overhead, however.

Can be disabled with TAISEI_LOG_ASYNC=0

* log: unfuck memory management

* log: add TAISEI_LOG_ASYNC_FAST_SHUTDOWN env variable

If true, don't wait for the logging queue to process every message
before shutting down.

* strbuf: fix off-by-one error

* log: sync queue before crashing on assertion failure

* docs/ENVIRON: update logging docs
2020-11-24 05:31:51 +02:00
Andrei Alexeyev
5a23fb95fc
make upkeep script preserve existing copyrights 2019-08-03 20:44:22 +03:00
Andrei Alexeyev
b61d1b3254
backport improved assertions from the lua branch 2019-08-03 19:57:02 +03:00
Samuel P
469d6e2f48 Switch homebrew port (#173)
* Initial port

* Switch specific video modes

* Handle clean exit

* Hide option to disable gamepads, force it enabled

* Match buttons layout with Switch controllers

* Hide player name setting (to avoid getting stuck)

* Switch specific VFS setup instead of env bootstrap

* Clean up, get rid of warnings and a few ifdefs

* Add Switch specific build script and assets

* Make vfs setup mount packages

* Re-enable packaging on Switch

* Transpile shaders to es and install them

* Add applet warning and shader deps to README

* Remove script; instead using meson build scripts

* Strict prototypes

* Build script compat with project minimum meson ver

Refactor of pack.py exclude option

* Uniformise header inclusion on arch_switch.c

* Allow input for any dev; hide the option on Switch

* Silence unsused function warnings
2019-08-02 21:38:33 +03:00
Andrei Alexeyev
3055901998
update my email 2019-07-03 21:00:56 +03:00
Andrei Alexeyev
02afb993a2
Define _POSIX_C_SOURCE if compiling for a POSIX-like platform
Do not try to declare random POSIX functions ourselves. Fixes #165
2019-05-01 07:05:39 +03:00
Andrei Alexeyev
c2949c47d8
log: fix colorless console formatter 2019-02-17 21:21:26 +02:00
Andrei Alexeyev
b91b85c42a
Logging: colors, non-fatal error level, alt. format for log file
Also removed traceback support entirely as it never produced useful
output.
2019-02-15 01:58:40 +02:00
Andrei Alexeyev
4159ea1249
'upkeep' target for maintenance tasks; back to include guards; happy new year! 2019-01-23 22:10:43 +02:00
Andrei Alexeyev
59cf8f6300
Rendering system rewrite, tons of refactoring, optimizations, and other cool stuff (#116) 2018-04-12 17:08:48 +03:00
Andrei Alexeyev
513d613387
Consistent indentation: indent with tabs, align with spaces (#104)
I would've preferred to just go with 4-spaces for indent and no tabs,
but lao is a bit conservative about it. :^)

Still, this is a ton better than mixing different styles all over the
place, especially within the same file.
2018-01-12 20:26:07 +02:00
Andrei Alexeyev
485c9a8ed6
Happy New Year! 2018-01-04 19:14:31 +02:00
Andrei Alexeyev
e355e57fb5
make the list api require less insane casts all over the place
by sealing the spirit of insanity in its header file, that is
2017-12-24 08:16:25 +02:00
Andrei Alexeyev
29acd5f58a meson: intel intrinsics, various improvements 2017-12-21 03:58:54 +01:00
Andrei Alexeyev
7b53d9e731
redesign list api 2017-11-21 16:45:01 +02:00
Andrei Alexeyev
76ee80b6da
(attempt to) prevent draw code from altering state 2017-11-16 22:25:03 +02:00
Andrei Alexeyev
d19aa3c8c5
flush stdio output streams on logging shutdown 2017-11-03 13:27:38 +02:00
Andrei "Akari" Alexeyev
7c9e54a71d
update copyright and credits 2017-09-12 04:28:15 +03:00
Andrei "Akari" Alexeyev
457ca7bf10 fix a use-after-free 2017-03-25 03:26:48 +02:00
Andrei "Akari" Alexeyev
455039edb4 Implemented a custom assert() that uses the log system if possible
Bonus: no need to include assert.h
2017-03-15 11:21:39 +02:00
Andrei "Akari" Alexeyev
6530a39bd2 log: support for dumping backtraces in debug builds
Enabled only if the environment has the execinfo.h interface.
By default dumps the backtrace only on log_fatal, use.
TAISEI_LOGLVLS_BACKTRACE to customize that behaviour, e.g.:

    TAISEI_LOGLVLS_BACKTRACE=+w taisei

to also enable it for warnings.
2017-03-15 10:42:00 +02:00
Andrei "Akari" Alexeyev
3b83228d50 fixed an assert 2017-03-13 19:16:54 +02:00
Andrei "Akari" Alexeyev
3a957cb3f2 More control over logging output via env variables and macros
In release builds, everything is logged to the log file by default, and
only warnings and errors go to the console (stderr). In debug builds,
all output goes both to the console (stdout/stderr) and the log file.

You can override this behaviour with environment variables. Some
examples:

    Don't log anything to the console at all:
        TAISEI_LOGLVLS_CONSOLE=-a

    Suppress info and debug output in the log file:
        TAISEI_LOGLVLS_FILE=-di

    More verbose console output in release builds:
        TAISEI_LOGLVLS_CONSOLE=+i

    Override defaults and enable all enable the wanted loglevels explicitly:
        TAISEI_LOGLVLS_CONSOLE=-a+diwe

    Redirect debug output to stderr:
        TAISEI_LOGLVLS_STDOUT=-d
        TAISEI_LOGLVLS_STDERR=+d
2017-03-13 18:57:44 +02:00
Andrei "Akari" Alexeyev
b0c959f1c7 log: use timestamps in ms relative to program startup 2017-03-13 18:17:05 +02:00
Andrei "Akari" Alexeyev
3d5344de3b Renamed log_err to log_fatal 2017-03-13 18:03:51 +02:00
Andrei "Akari" Alexeyev
45da155cb2 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 07:45:01 +02:00