Servostar / gemstone

Gemstone programming language compiler (GSC)
GNU General Public License v2.0
3 stars 0 forks source link

3 add logging api #7

Closed Servostar closed 4 months ago

Servostar commented 4 months ago

Logging API

Added a simple logger API. This is meant to be used as compiler INTERNAL logging NOT for printing messages about syntax errors.

Logging

for using the logger include <sys/log.h>. Then call one of the following macros in order to make a log message:

Each of those functions takes arguments similar to printf. A format string and optional arguments. They can be used in the following ways:

#include <sys/log.h>

void foo(void)
{
    ERROR("this is an error message!");
    DEBUG("this a debug message!");

    int a = 10;
    float d = 0.3;

   INFO("formatted output: %d, %f", a, d);
}

By setting the macro LOG_LEVEL only messages with a higher log level will be logged. If set to LOG_LEVEL_WARN only errors and warnings will be printed. Default level for now is LOG_LEVEL_DEBUG. Logs which not get printed should get optimized out of the binary.

Crashes

There are two special functions: PANIC and FATAL both take the same arguments as the other logging functions. PANIC will print only to stderr and the call exit, closing the program. All functions registered with atexit will be run. FATAL will also print only to stderr but will cal abort terminating immediately. ONLY use these two for critical sections in which no recovery is possible. Otherwise use one of the others.

Custom logger

Other output streams can be registered with log_register_stream. Example:


FILE* file = fopen("gemstone.log", "w");

log_register_stream(file);

DEBUG("log to stderr and a file!");