2020-06-15 14:18:57 +00:00
|
|
|
#ifndef COSMOPOLITAN_LIBC_LOG_LOG_H_
|
|
|
|
#define COSMOPOLITAN_LIBC_LOG_LOG_H_
|
2021-08-13 18:18:25 +00:00
|
|
|
#include "libc/calls/struct/rusage.h"
|
2021-02-08 17:19:00 +00:00
|
|
|
#include "libc/calls/struct/sigset.h"
|
|
|
|
#include "libc/calls/struct/winsize.h"
|
2022-07-09 12:49:19 +00:00
|
|
|
#include "libc/dce.h"
|
2022-04-17 03:29:08 +00:00
|
|
|
#include "libc/errno.h"
|
2022-08-14 00:20:50 +00:00
|
|
|
#include "libc/intrin/likely.h"
|
2021-07-19 21:55:20 +00:00
|
|
|
#include "libc/nexgen32e/stackframe.h"
|
|
|
|
#include "libc/runtime/runtime.h"
|
2021-02-08 17:19:00 +00:00
|
|
|
#include "libc/stdio/stdio.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
|
2021-02-08 17:19:00 +00:00
|
|
|
#define kLogFatal 0
|
|
|
|
#define kLogError 1
|
|
|
|
#define kLogWarn 2
|
|
|
|
#define kLogInfo 3
|
|
|
|
#define kLogVerbose 4
|
|
|
|
#define kLogDebug 5
|
2021-07-12 06:17:47 +00:00
|
|
|
#define kLogNoise 6
|
2020-06-15 14:18:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Log level for compile-time DCE.
|
|
|
|
*/
|
|
|
|
#ifndef LOGGABLELEVEL
|
2020-10-06 06:11:49 +00:00
|
|
|
#ifndef TINY
|
2021-07-19 21:55:20 +00:00
|
|
|
#define LOGGABLELEVEL kLogNoise
|
2020-08-25 11:23:25 +00:00
|
|
|
/* #elif IsTiny() */
|
|
|
|
/* #define LOGGABLELEVEL kLogInfo */
|
2020-06-15 14:18:57 +00:00
|
|
|
#else
|
2020-08-27 06:08:08 +00:00
|
|
|
#define LOGGABLELEVEL kLogVerbose
|
2020-06-15 14:18:57 +00:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !(__ASSEMBLER__ + __LINKER__ + 0)
|
|
|
|
COSMOPOLITAN_C_START_
|
|
|
|
|
2021-03-01 07:42:35 +00:00
|
|
|
extern FILE *__log_file;
|
2020-06-15 14:18:57 +00:00
|
|
|
|
2022-08-15 22:18:37 +00:00
|
|
|
int __watch(void *, size_t);
|
2020-12-05 20:20:41 +00:00
|
|
|
void __die(void) relegated wontreturn; /* print backtrace and abort() */
|
|
|
|
void meminfo(int); /* shows malloc statistics &c. */
|
|
|
|
void memsummary(int); /* light version of same thing */
|
2020-06-15 14:18:57 +00:00
|
|
|
uint16_t getttycols(uint16_t);
|
|
|
|
int getttysize(int, struct winsize *) paramsnonnull();
|
2021-02-01 11:33:13 +00:00
|
|
|
bool IsTerminalInarticulate(void) nosideeffect;
|
2021-02-03 06:17:53 +00:00
|
|
|
const char *commandvenv(const char *, const char *);
|
2020-08-25 11:23:25 +00:00
|
|
|
const char *GetAddr2linePath(void);
|
|
|
|
const char *GetGdbPath(void);
|
2021-07-19 21:55:20 +00:00
|
|
|
const char *GetCallerName(const struct StackFrame *);
|
2020-06-15 14:18:57 +00:00
|
|
|
|
2021-08-18 21:21:30 +00:00
|
|
|
void ShowCrashReports(void);
|
2020-06-15 14:18:57 +00:00
|
|
|
void callexitontermination(struct sigset *);
|
|
|
|
bool32 IsDebuggerPresent(bool);
|
2021-02-01 11:33:13 +00:00
|
|
|
bool IsRunningUnderMake(void);
|
2021-08-13 18:18:25 +00:00
|
|
|
const char *GetSiCodeName(int, int);
|
|
|
|
void AppendResourceReport(char **, struct rusage *, const char *);
|
2021-10-14 00:27:13 +00:00
|
|
|
char *__get_symbol_by_addr(int64_t);
|
|
|
|
void PrintGarbage(void);
|
2021-08-19 15:30:00 +00:00
|
|
|
void PrintGarbageNumeric(FILE *);
|
2022-03-18 09:33:37 +00:00
|
|
|
void CheckForMemoryLeaks(void);
|
2020-06-15 14:18:57 +00:00
|
|
|
|
2021-08-18 21:21:30 +00:00
|
|
|
#define showcrashreports() ShowCrashReports()
|
|
|
|
|
2020-12-01 11:43:40 +00:00
|
|
|
#ifndef __STRICT_ANSI__
|
2020-06-15 14:18:57 +00:00
|
|
|
|
2021-03-01 07:42:35 +00:00
|
|
|
extern unsigned __log_level; /* log level for runtime check */
|
2020-06-15 14:18:57 +00:00
|
|
|
|
2020-12-24 07:42:56 +00:00
|
|
|
#define LOGGABLE(LEVEL) \
|
|
|
|
((!__builtin_constant_p(LEVEL) || (LEVEL) <= LOGGABLELEVEL) && \
|
2021-03-01 07:42:35 +00:00
|
|
|
(LEVEL) <= __log_level)
|
2020-06-15 14:18:57 +00:00
|
|
|
|
2021-08-23 03:16:10 +00:00
|
|
|
// log a message with the specified log level (not checking if LOGGABLE)
|
2021-09-04 04:14:26 +00:00
|
|
|
#define LOGF(LEVEL, FMT, ...) \
|
2021-08-23 03:16:10 +00:00
|
|
|
do { \
|
2022-07-09 12:49:19 +00:00
|
|
|
if (!IsTiny()) --__ftrace; \
|
2022-04-23 01:55:28 +00:00
|
|
|
flogf(LEVEL, __FILE__, __LINE__, NULL, FMT, ##__VA_ARGS__); \
|
2022-07-09 12:49:19 +00:00
|
|
|
if (!IsTiny()) ++__ftrace; \
|
2021-08-23 03:16:10 +00:00
|
|
|
} while (0)
|
|
|
|
|
2022-06-27 04:25:02 +00:00
|
|
|
// report an error without backtrace and debugger invocation
|
|
|
|
#define FATALF(FMT, ...) \
|
2021-08-22 22:01:52 +00:00
|
|
|
do { \
|
2022-07-09 12:49:19 +00:00
|
|
|
if (!IsTiny()) --__ftrace; \
|
2021-08-22 22:01:52 +00:00
|
|
|
flogf(kLogError, __FILE__, __LINE__, NULL, FMT, ##__VA_ARGS__); \
|
2022-05-22 15:13:13 +00:00
|
|
|
__restorewintty(); \
|
|
|
|
_Exit(1); \
|
2021-08-22 22:01:52 +00:00
|
|
|
unreachable; \
|
|
|
|
} while (0)
|
|
|
|
|
2022-06-27 04:25:02 +00:00
|
|
|
#define DIEF(FMT, ...) \
|
2021-07-12 06:17:47 +00:00
|
|
|
do { \
|
2022-07-09 12:49:19 +00:00
|
|
|
if (!IsTiny()) --__ftrace; \
|
2021-07-12 06:17:47 +00:00
|
|
|
ffatalf(kLogFatal, __FILE__, __LINE__, NULL, FMT, ##__VA_ARGS__); \
|
|
|
|
unreachable; \
|
|
|
|
} while (0)
|
|
|
|
|
2022-07-09 12:49:19 +00:00
|
|
|
#define ERRORF(FMT, ...) \
|
|
|
|
do { \
|
|
|
|
if (LOGGABLE(kLogError)) { \
|
|
|
|
LOGF(kLogError, FMT, ##__VA_ARGS__); \
|
|
|
|
} \
|
2021-08-19 11:14:08 +00:00
|
|
|
} while (0)
|
|
|
|
|
2022-07-09 12:49:19 +00:00
|
|
|
#define WARNF(FMT, ...) \
|
|
|
|
do { \
|
|
|
|
if (LOGGABLE(kLogWarn)) { \
|
|
|
|
LOGF(kLogWarn, FMT, ##__VA_ARGS__); \
|
|
|
|
} \
|
2021-07-12 06:17:47 +00:00
|
|
|
} while (0)
|
|
|
|
|
2022-07-09 12:49:19 +00:00
|
|
|
#define INFOF(FMT, ...) \
|
|
|
|
do { \
|
|
|
|
if (LOGGABLE(kLogInfo)) { \
|
|
|
|
LOGF(kLogInfo, FMT, ##__VA_ARGS__); \
|
|
|
|
} \
|
2020-06-15 14:18:57 +00:00
|
|
|
} while (0)
|
|
|
|
|
2021-07-12 06:17:47 +00:00
|
|
|
#define VERBOSEF(FMT, ...) \
|
|
|
|
do { \
|
|
|
|
if (LOGGABLE(kLogVerbose)) { \
|
2022-07-09 12:49:19 +00:00
|
|
|
if (!IsTiny()) --__ftrace; \
|
2022-04-23 01:55:28 +00:00
|
|
|
fverbosef(kLogVerbose, __FILE__, __LINE__, NULL, FMT, ##__VA_ARGS__); \
|
2022-07-09 12:49:19 +00:00
|
|
|
if (!IsTiny()) ++__ftrace; \
|
2021-07-12 06:17:47 +00:00
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define DEBUGF(FMT, ...) \
|
|
|
|
do { \
|
2021-07-19 21:55:20 +00:00
|
|
|
if (UNLIKELY(LOGGABLE(kLogDebug))) { \
|
2022-07-09 12:49:19 +00:00
|
|
|
if (!IsTiny()) --__ftrace; \
|
2022-04-23 01:55:28 +00:00
|
|
|
fdebugf(kLogDebug, __FILE__, __LINE__, NULL, FMT, ##__VA_ARGS__); \
|
2022-07-09 12:49:19 +00:00
|
|
|
if (!IsTiny()) ++__ftrace; \
|
2021-07-12 06:17:47 +00:00
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define NOISEF(FMT, ...) \
|
|
|
|
do { \
|
2021-07-19 21:55:20 +00:00
|
|
|
if (UNLIKELY(LOGGABLE(kLogNoise))) { \
|
2022-07-09 12:49:19 +00:00
|
|
|
if (!IsTiny()) --__ftrace; \
|
2022-04-23 01:55:28 +00:00
|
|
|
fnoisef(kLogNoise, __FILE__, __LINE__, NULL, FMT, ##__VA_ARGS__); \
|
2022-07-09 12:49:19 +00:00
|
|
|
if (!IsTiny()) ++__ftrace; \
|
2021-07-12 06:17:47 +00:00
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
2020-06-15 14:18:57 +00:00
|
|
|
#define FLOGF(F, FMT, ...) \
|
|
|
|
do { \
|
|
|
|
if (LOGGABLE(kLogInfo)) { \
|
2022-07-09 12:49:19 +00:00
|
|
|
if (!IsTiny()) --__ftrace; \
|
2022-04-23 01:55:28 +00:00
|
|
|
flogf(kLogInfo, __FILE__, __LINE__, F, FMT, ##__VA_ARGS__); \
|
2022-07-09 12:49:19 +00:00
|
|
|
if (!IsTiny()) ++__ftrace; \
|
2020-06-15 14:18:57 +00:00
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define FWARNF(F, FMT, ...) \
|
|
|
|
do { \
|
|
|
|
if (LOGGABLE(kLogWarn)) { \
|
2022-07-09 12:49:19 +00:00
|
|
|
if (!IsTiny()) --__ftrace; \
|
2022-04-23 01:55:28 +00:00
|
|
|
flogf(kLogWarn, __FILE__, __LINE__, F, FMT, ##__VA_ARGS__); \
|
2022-07-09 12:49:19 +00:00
|
|
|
if (!IsTiny()) ++__ftrace; \
|
2020-06-15 14:18:57 +00:00
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
2022-07-09 12:49:19 +00:00
|
|
|
#define FFATALF(F, FMT, ...) \
|
|
|
|
do { \
|
|
|
|
if (!IsTiny()) --__ftrace; \
|
|
|
|
flogf(kLogError, __FILE__, __LINE__, F, FMT, ##__VA_ARGS__); \
|
|
|
|
__restorewintty(); \
|
|
|
|
_Exit(1); \
|
|
|
|
unreachable; \
|
2020-06-15 14:18:57 +00:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define FDEBUGF(F, FMT, ...) \
|
|
|
|
do { \
|
2021-07-19 21:55:20 +00:00
|
|
|
if (UNLIKELY(LOGGABLE(kLogDebug))) { \
|
2022-07-09 12:49:19 +00:00
|
|
|
if (!IsTiny()) --__ftrace; \
|
2022-04-23 01:55:28 +00:00
|
|
|
fdebugf(kLogDebug, __FILE__, __LINE__, F, FMT, ##__VA_ARGS__); \
|
2022-07-09 12:49:19 +00:00
|
|
|
if (!IsTiny()) ++__ftrace; \
|
2020-06-15 14:18:57 +00:00
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
2021-07-12 06:17:47 +00:00
|
|
|
#define FNOISEF(F, FMT, ...) \
|
|
|
|
do { \
|
2021-07-19 21:55:20 +00:00
|
|
|
if (UNLIKELY(LOGGABLE(kLogNoise))) { \
|
2022-07-09 12:49:19 +00:00
|
|
|
if (!IsTiny()) --__ftrace; \
|
2022-04-23 01:55:28 +00:00
|
|
|
fnoisef(kLogNoise, __FILE__, __LINE__, F, FMT, ##__VA_ARGS__); \
|
2022-07-09 12:49:19 +00:00
|
|
|
if (!IsTiny()) ++__ftrace; \
|
2021-07-12 06:17:47 +00:00
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
2021-07-19 21:55:20 +00:00
|
|
|
#define LOGIFNEG1(FORM) \
|
|
|
|
({ \
|
2022-04-17 03:29:08 +00:00
|
|
|
int e = errno; \
|
2021-07-19 21:55:20 +00:00
|
|
|
autotype(FORM) Ax = (FORM); \
|
|
|
|
if (UNLIKELY(Ax == (typeof(Ax))(-1)) && LOGGABLE(kLogWarn)) { \
|
2022-07-09 12:49:19 +00:00
|
|
|
if (!IsTiny()) --__ftrace; \
|
2022-04-23 01:55:28 +00:00
|
|
|
__logerrno(__FILE__, __LINE__, #FORM); \
|
2022-07-09 12:49:19 +00:00
|
|
|
if (!IsTiny()) ++__ftrace; \
|
2022-04-17 03:29:08 +00:00
|
|
|
errno = e; \
|
2021-07-19 21:55:20 +00:00
|
|
|
} \
|
|
|
|
Ax; \
|
2020-06-15 14:18:57 +00:00
|
|
|
})
|
|
|
|
|
2022-05-18 23:41:29 +00:00
|
|
|
#define LOGIFNULL(FORM) \
|
|
|
|
({ \
|
|
|
|
int e = errno; \
|
|
|
|
autotype(FORM) Ax = (FORM); \
|
|
|
|
if (Ax == NULL && LOGGABLE(kLogWarn)) { \
|
2022-07-09 12:49:19 +00:00
|
|
|
if (!IsTiny()) --__ftrace; \
|
2022-05-18 23:41:29 +00:00
|
|
|
__logerrno(__FILE__, __LINE__, #FORM); \
|
2022-07-09 12:49:19 +00:00
|
|
|
if (!IsTiny()) ++__ftrace; \
|
2022-05-18 23:41:29 +00:00
|
|
|
errno = e; \
|
|
|
|
} \
|
|
|
|
Ax; \
|
2020-06-15 14:18:57 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
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;
|
2020-08-27 06:08:08 +00:00
|
|
|
void fverbosef(ARGS, ...) asm("flogf") ATTR relegated libcesque;
|
|
|
|
void vfverbosef(ARGS, va_list) asm("vflogf") ATTRV relegated libcesque;
|
2020-06-15 14:18:57 +00:00
|
|
|
void fdebugf(ARGS, ...) asm("flogf") ATTR relegated libcesque;
|
|
|
|
void vfdebugf(ARGS, va_list) asm("vflogf") ATTRV relegated libcesque;
|
2021-07-12 06:17:47 +00:00
|
|
|
void fnoisef(ARGS, ...) asm("flogf") ATTR relegated libcesque;
|
|
|
|
void vfnoisef(ARGS, va_list) asm("vflogf") ATTRV relegated libcesque;
|
2020-12-05 20:20:41 +00:00
|
|
|
void ffatalf(ARGS, ...) asm("flogf") ATTR relegated wontreturn libcesque;
|
|
|
|
void vffatalf(ARGS, va_list) asm("vflogf") ATTRV relegated wontreturn libcesque;
|
2020-06-15 14:18:57 +00:00
|
|
|
#undef ARGS
|
|
|
|
#undef ATTR
|
|
|
|
#undef ATTRV
|
|
|
|
|
2020-12-01 11:43:40 +00:00
|
|
|
#endif /* __STRICT_ANSI__ */
|
2020-06-15 14:18:57 +00:00
|
|
|
COSMOPOLITAN_C_END_
|
|
|
|
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
|
|
|
|
#endif /* COSMOPOLITAN_LIBC_LOG_LOG_H_ */
|