// 你必须定义一个 `main()` 函数入口。
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <sys/types.h>
#include <sys/times.h>
#include <pthread.h>
#include <stdarg.h>
#include <sys/time.h>
extern bool use_syslog;
extern bool opt_quiet;
pthread_mutex_t console_lock;
bool opt_debug = false;
bool opt_log_output = false;
bool opt_quiet;
bool use_syslog;
enum {
LOG_ERR,
LOG_WARNING,
LOG_NOTICE,
LOG_INFO,
LOG_DEBUG,
};
#define __maybe_unused __attribute__((unused))
#define unlikely(expr) (__builtin_expect(!!(expr), 0))
extern void quit(int status, const char *format, ...);
static inline void mutex_lock(pthread_mutex_t *lock)
{
if (unlikely(pthread_mutex_lock(lock)))
quit(1, "WTF MUTEX ERROR ON LOCK!");
}
static inline void mutex_unlock(pthread_mutex_t *lock)
{
if (unlikely(pthread_mutex_unlock(lock)))
quit(1, "WTF MUTEX ERROR ON UNLOCK!");
}
void cgtime(struct timeval *tv)
{
gettimeofday(tv, NULL);
}
static void my_log_curses(__maybe_unused int prio, char *f, va_list ap)
{
if (opt_quiet && prio != LOG_ERR)
return;
else
{
int len = strlen(f);
strcpy(f + len - 1, " \n");
mutex_lock(&console_lock);
vprintf(f, ap);
mutex_unlock(&console_lock);
}
}
static void log_generic(int prio, const char *fmt, va_list ap);
void vapplog(int prio, const char *fmt, va_list ap)
{
if (!opt_debug && prio == LOG_DEBUG)
return;
if (use_syslog || opt_log_output || prio <= LOG_NOTICE)
log_generic(prio, fmt, ap);
}
void _applog(int prio, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vapplog(prio, fmt, ap);
va_end(ap);
}
static void log_generic(int prio, const char *fmt, va_list ap)
{
if (0) {}
else {
char *f;
int len;
struct timeval tv = {0, 0};
struct tm *tm;
cgtime(&tv);
const time_t tmp_time = tv.tv_sec;
tm = localtime(&tmp_time);
len = 40 + strlen(fmt) + 22;
f = alloca(len);
sprintf(f, " [%d-%02d-%02d %02d:%02d:%02d] %s\n",
tm->tm_year + 1900,
tm->tm_mon + 1,
tm->tm_mday,
tm->tm_hour,
tm->tm_min,
tm->tm_sec,
fmt);
/* Only output to stderr if it's not going to the screen as well */
if (!isatty(fileno((FILE *)stderr))) {
va_list apc;
va_copy(apc, ap);
vfprintf(stderr, f, apc); /* atomic write to stderr */
va_end(apc);
fflush(stderr);
}
my_log_curses(prio, f, ap);
}
}
void quit(int status, const char *format, ...)
{
if (format) {
va_list ap;
va_start(ap, format);
vapplog(LOG_ERR, format, ap);
va_end(ap);
}
exit(status);
}
#define applog(prio, fmt, ...) do { \
char *tmp42; \
if (0) \
sprintf(tmp42, fmt, ##__VA_ARGS__); \
else \
_applog(prio, fmt, ##__VA_ARGS__); \
} while (0)
int main()
{
struct timeval now;
cgtime(&now);
applog(LOG_INFO, "Stopping mining threads");
system("ls");
printf("Hello, World!\n");
return 0;
}