mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-01-31 19:43:32 +00:00
80b211e314
This change introduces a `-W /dev/pts/1` flag to redbean. What it does is use the mincore() system call to create a dual-screen terminal display that lets you troubleshoot the virtual address space. This is useful since page faults are an important thing to consider when using a forking web server. Now we have a colorful visualization of which pages are going to fault and which ones are resident in memory. The memory monitor, if enabled, spawns as a thread that just outputs ANSI codes to the second terminal in a loop. In order to make this happen using the new clone() polyfill, stdio is now thread safe. This change also introduces some new demo pages to redbean. It also polishes the demos we already have, to look a bit nicer and more presentable for the upcoming release, with better explanations too.
256 lines
13 KiB
C
256 lines
13 KiB
C
#ifndef COSMOPOLITAN_LIBC_LOG_LOG_H_
|
|
#define COSMOPOLITAN_LIBC_LOG_LOG_H_
|
|
#include "libc/bits/likely.h"
|
|
#include "libc/bits/weaken.h"
|
|
#include "libc/calls/struct/rusage.h"
|
|
#include "libc/calls/struct/sigset.h"
|
|
#include "libc/calls/struct/winsize.h"
|
|
#include "libc/errno.h"
|
|
#include "libc/nexgen32e/stackframe.h"
|
|
#include "libc/runtime/runtime.h"
|
|
#include "libc/stdio/stdio.h"
|
|
/*───────────────────────────────────────────────────────────────────────────│─╗
|
|
│ cosmopolitan § liblog ─╬─│┼
|
|
╚────────────────────────────────────────────────────────────────────────────│*/
|
|
|
|
#define kLogFatal 0
|
|
#define kLogError 1
|
|
#define kLogWarn 2
|
|
#define kLogInfo 3
|
|
#define kLogVerbose 4
|
|
#define kLogDebug 5
|
|
#define kLogNoise 6
|
|
|
|
/**
|
|
* Log level for compile-time DCE.
|
|
*/
|
|
#ifndef LOGGABLELEVEL
|
|
#ifndef TINY
|
|
#define LOGGABLELEVEL kLogNoise
|
|
/* #elif IsTiny() */
|
|
/* #define LOGGABLELEVEL kLogInfo */
|
|
#else
|
|
#define LOGGABLELEVEL kLogVerbose
|
|
#endif
|
|
#endif
|
|
|
|
#if !(__ASSEMBLER__ + __LINKER__ + 0)
|
|
COSMOPOLITAN_C_START_
|
|
|
|
extern FILE *__log_file;
|
|
|
|
void perror(const char *) relegated; /* print the last system error */
|
|
void __die(void) relegated wontreturn; /* print backtrace and abort() */
|
|
void meminfo(int); /* shows malloc statistics &c. */
|
|
void memsummary(int); /* light version of same thing */
|
|
uint16_t getttycols(uint16_t);
|
|
int getttysize(int, struct winsize *) paramsnonnull();
|
|
bool IsTerminalInarticulate(void) nosideeffect;
|
|
const char *commandvenv(const char *, const char *);
|
|
const char *GetAddr2linePath(void);
|
|
const char *GetGdbPath(void);
|
|
const char *GetCallerName(const struct StackFrame *);
|
|
|
|
void ShowCrashReports(void);
|
|
void callexitontermination(struct sigset *);
|
|
bool32 IsDebuggerPresent(bool);
|
|
bool IsRunningUnderMake(void);
|
|
const char *GetSiCodeName(int, int);
|
|
void AppendResourceReport(char **, struct rusage *, const char *);
|
|
char *__get_symbol_by_addr(int64_t);
|
|
void PrintGarbage(void);
|
|
void PrintGarbageNumeric(FILE *);
|
|
void CheckForMemoryLeaks(void);
|
|
|
|
#define showcrashreports() ShowCrashReports()
|
|
|
|
/*───────────────────────────────────────────────────────────────────────────│─╗
|
|
│ cosmopolitan § liblog » logging ─╬─│┼
|
|
╚────────────────────────────────────────────────────────────────────────────│*/
|
|
#ifndef __STRICT_ANSI__
|
|
|
|
extern unsigned __log_level; /* log level for runtime check */
|
|
|
|
#define LOGGABLE(LEVEL) \
|
|
((!__builtin_constant_p(LEVEL) || (LEVEL) <= LOGGABLELEVEL) && \
|
|
(LEVEL) <= __log_level)
|
|
|
|
// log a message with the specified log level (not checking if LOGGABLE)
|
|
#define LOGF(LEVEL, FMT, ...) \
|
|
do { \
|
|
__atomic_fetch_sub(&g_ftrace, 1, __ATOMIC_RELAXED); \
|
|
flogf(LEVEL, __FILE__, __LINE__, NULL, FMT, ##__VA_ARGS__); \
|
|
__atomic_fetch_add(&g_ftrace, 1, __ATOMIC_RELAXED); \
|
|
} while (0)
|
|
|
|
// die with an error message without backtrace and debugger invocation
|
|
#define DIEF(FMT, ...) \
|
|
do { \
|
|
__atomic_fetch_sub(&g_ftrace, 1, __ATOMIC_RELAXED); \
|
|
flogf(kLogError, __FILE__, __LINE__, NULL, FMT, ##__VA_ARGS__); \
|
|
if (weaken(__die)) weaken(__die)(); \
|
|
exit(1); \
|
|
unreachable; \
|
|
} while (0)
|
|
|
|
#define FATALF(FMT, ...) \
|
|
do { \
|
|
__atomic_fetch_sub(&g_ftrace, 1, __ATOMIC_RELAXED); \
|
|
ffatalf(kLogFatal, __FILE__, __LINE__, NULL, FMT, ##__VA_ARGS__); \
|
|
unreachable; \
|
|
} while (0)
|
|
|
|
#define ERRORF(FMT, ...) \
|
|
do { \
|
|
if (LOGGABLE(kLogError)) { \
|
|
__atomic_fetch_sub(&g_ftrace, 1, __ATOMIC_RELAXED); \
|
|
flogf(kLogError, __FILE__, __LINE__, NULL, FMT, ##__VA_ARGS__); \
|
|
__atomic_fetch_add(&g_ftrace, 1, __ATOMIC_RELAXED); \
|
|
} \
|
|
} while (0)
|
|
|
|
#define WARNF(FMT, ...) \
|
|
do { \
|
|
if (LOGGABLE(kLogWarn)) { \
|
|
__atomic_fetch_sub(&g_ftrace, 1, __ATOMIC_RELAXED); \
|
|
flogf(kLogWarn, __FILE__, __LINE__, NULL, FMT, ##__VA_ARGS__); \
|
|
__atomic_fetch_add(&g_ftrace, 1, __ATOMIC_RELAXED); \
|
|
} \
|
|
} while (0)
|
|
|
|
#define INFOF(FMT, ...) \
|
|
do { \
|
|
if (LOGGABLE(kLogInfo)) { \
|
|
__atomic_fetch_sub(&g_ftrace, 1, __ATOMIC_RELAXED); \
|
|
flogf(kLogInfo, __FILE__, __LINE__, NULL, FMT, ##__VA_ARGS__); \
|
|
__atomic_fetch_add(&g_ftrace, 1, __ATOMIC_RELAXED); \
|
|
} \
|
|
} while (0)
|
|
|
|
#define VERBOSEF(FMT, ...) \
|
|
do { \
|
|
if (LOGGABLE(kLogVerbose)) { \
|
|
__atomic_fetch_sub(&g_ftrace, 1, __ATOMIC_RELAXED); \
|
|
fverbosef(kLogVerbose, __FILE__, __LINE__, NULL, FMT, ##__VA_ARGS__); \
|
|
__atomic_fetch_add(&g_ftrace, 1, __ATOMIC_RELAXED); \
|
|
} \
|
|
} while (0)
|
|
|
|
#define DEBUGF(FMT, ...) \
|
|
do { \
|
|
if (UNLIKELY(LOGGABLE(kLogDebug))) { \
|
|
__atomic_fetch_sub(&g_ftrace, 1, __ATOMIC_RELAXED); \
|
|
fdebugf(kLogDebug, __FILE__, __LINE__, NULL, FMT, ##__VA_ARGS__); \
|
|
__atomic_fetch_add(&g_ftrace, 1, __ATOMIC_RELAXED); \
|
|
} \
|
|
} while (0)
|
|
|
|
#define NOISEF(FMT, ...) \
|
|
do { \
|
|
if (UNLIKELY(LOGGABLE(kLogNoise))) { \
|
|
__atomic_fetch_sub(&g_ftrace, 1, __ATOMIC_RELAXED); \
|
|
fnoisef(kLogNoise, __FILE__, __LINE__, NULL, FMT, ##__VA_ARGS__); \
|
|
__atomic_fetch_add(&g_ftrace, 1, __ATOMIC_RELAXED); \
|
|
} \
|
|
} while (0)
|
|
|
|
#define FLOGF(F, FMT, ...) \
|
|
do { \
|
|
if (LOGGABLE(kLogInfo)) { \
|
|
__atomic_fetch_sub(&g_ftrace, 1, __ATOMIC_RELAXED); \
|
|
flogf(kLogInfo, __FILE__, __LINE__, F, FMT, ##__VA_ARGS__); \
|
|
__atomic_fetch_add(&g_ftrace, 1, __ATOMIC_RELAXED); \
|
|
} \
|
|
} while (0)
|
|
|
|
#define FWARNF(F, FMT, ...) \
|
|
do { \
|
|
if (LOGGABLE(kLogWarn)) { \
|
|
__atomic_fetch_sub(&g_ftrace, 1, __ATOMIC_RELAXED); \
|
|
flogf(kLogWarn, __FILE__, __LINE__, F, FMT, ##__VA_ARGS__); \
|
|
__atomic_fetch_add(&g_ftrace, 1, __ATOMIC_RELAXED); \
|
|
} \
|
|
} while (0)
|
|
|
|
#define FFATALF(F, FMT, ...) \
|
|
do { \
|
|
__atomic_fetch_sub(&g_ftrace, 1, __ATOMIC_RELAXED); \
|
|
ffatalf(kLogFatal, __FILE__, __LINE__, F, FMT, ##__VA_ARGS__); \
|
|
unreachable; \
|
|
} while (0)
|
|
|
|
#define FDEBUGF(F, FMT, ...) \
|
|
do { \
|
|
if (UNLIKELY(LOGGABLE(kLogDebug))) { \
|
|
__atomic_fetch_sub(&g_ftrace, 1, __ATOMIC_RELAXED); \
|
|
fdebugf(kLogDebug, __FILE__, __LINE__, F, FMT, ##__VA_ARGS__); \
|
|
__atomic_fetch_add(&g_ftrace, 1, __ATOMIC_RELAXED); \
|
|
} \
|
|
} while (0)
|
|
|
|
#define FNOISEF(F, FMT, ...) \
|
|
do { \
|
|
if (UNLIKELY(LOGGABLE(kLogNoise))) { \
|
|
__atomic_fetch_sub(&g_ftrace, 1, __ATOMIC_RELAXED); \
|
|
fnoisef(kLogNoise, __FILE__, __LINE__, F, FMT, ##__VA_ARGS__); \
|
|
__atomic_fetch_add(&g_ftrace, 1, __ATOMIC_RELAXED); \
|
|
} \
|
|
} while (0)
|
|
|
|
/*───────────────────────────────────────────────────────────────────────────│─╗
|
|
│ cosmopolitan § liblog » on error resume next ─╬─│┼
|
|
╚────────────────────────────────────────────────────────────────────────────│*/
|
|
|
|
#define LOGIFNEG1(FORM) \
|
|
({ \
|
|
int e = errno; \
|
|
autotype(FORM) Ax = (FORM); \
|
|
if (UNLIKELY(Ax == (typeof(Ax))(-1)) && LOGGABLE(kLogWarn)) { \
|
|
__atomic_fetch_sub(&g_ftrace, 1, __ATOMIC_RELAXED); \
|
|
__logerrno(__FILE__, __LINE__, #FORM); \
|
|
__atomic_fetch_add(&g_ftrace, 1, __ATOMIC_RELAXED); \
|
|
errno = e; \
|
|
} \
|
|
Ax; \
|
|
})
|
|
|
|
#define LOGIFNULL(FORM) \
|
|
({ \
|
|
int e = errno; \
|
|
autotype(FORM) Ax = (FORM); \
|
|
if (Ax == NULL && LOGGABLE(kLogWarn)) { \
|
|
__atomic_fetch_sub(&g_ftrace, 1, __ATOMIC_RELAXED); \
|
|
__logerrno(__FILE__, __LINE__, #FORM); \
|
|
__atomic_fetch_add(&g_ftrace, 1, __ATOMIC_RELAXED); \
|
|
errno = e; \
|
|
} \
|
|
Ax; \
|
|
})
|
|
|
|
/*───────────────────────────────────────────────────────────────────────────│─╗
|
|
│ cosmopolitan § liblog » implementation details ─╬─│┼
|
|
╚────────────────────────────────────────────────────────────────────────────│*/
|
|
|
|
void __logerrno(const char *, int, const char *) relegated;
|
|
|
|
#define ARGS unsigned, const char *, int, FILE *, const char *
|
|
#define ATTR paramsnonnull((5)) printfesque(5)
|
|
#define ATTRV paramsnonnull((5, 6))
|
|
void flogf(ARGS, ...) ATTR libcesque;
|
|
void vflogf(ARGS, va_list) ATTRV libcesque;
|
|
void fverbosef(ARGS, ...) asm("flogf") ATTR relegated libcesque;
|
|
void vfverbosef(ARGS, va_list) asm("vflogf") ATTRV relegated libcesque;
|
|
void fdebugf(ARGS, ...) asm("flogf") ATTR relegated libcesque;
|
|
void vfdebugf(ARGS, va_list) asm("vflogf") ATTRV relegated libcesque;
|
|
void fnoisef(ARGS, ...) asm("flogf") ATTR relegated libcesque;
|
|
void vfnoisef(ARGS, va_list) asm("vflogf") ATTRV relegated libcesque;
|
|
void ffatalf(ARGS, ...) asm("flogf") ATTR relegated wontreturn libcesque;
|
|
void vffatalf(ARGS, va_list) asm("vflogf") ATTRV relegated wontreturn libcesque;
|
|
#undef ARGS
|
|
#undef ATTR
|
|
#undef ATTRV
|
|
|
|
#endif /* __STRICT_ANSI__ */
|
|
COSMOPOLITAN_C_END_
|
|
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
|
|
#endif /* COSMOPOLITAN_LIBC_LOG_LOG_H_ */
|