applog 调试通过

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// 你必须定义一个 `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;
}

 

posted on   lydstory  阅读(129)  评论(0编辑  收藏  举报

编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2018-01-04 hg 添加用户

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示