This repository has been archived by the owner on Jun 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
msg.c
157 lines (136 loc) · 3.29 KB
/
msg.c
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
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <pthread.h>
#include "xk.h"
#include "error.h"
#include "sleep.h"
#include "msg.h"
static volatile int cancel;
static pthread_t tid;
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static xk_msg *head = NULL, *tail = NULL; /* protected by mutex */
static void real_output_msg(const xk_msg *ptr)
{
/*
XK_MSG_DEBUG,
XK_MSG_INFO,
XK_MSG_NOTICE
XK_MSG_GOOD,
XK_MSG_BAD,
XK_MSG_ERROR
*/
const char *head[] = {"\033[0m", "\033[0m", "\033[34m", "\033[32m", "\033[31m", "\033[31m"};
printf("%s[%10d.%09d][%d] %s%s\n", head[ptr->type], (int) ptr->ts.tv_sec, (int) ptr->ts.tv_nsec, ptr->type, ptr->str, "\033[0m");
}
static void *msg_flush_thread(void *arg)
{
while (1) {
msg_flush();
if (cancel) break;
sleep_ms(XK_MSG_FLUSHINTERVAL);
}
return (void *) 0;
}
static void msg_doit(xk_msg_type type, const char *fmt, va_list ap)
{
xk_msg *ptr;
char buf[XK_MSG_MAXLINE];
int len;
/* construct the message string */
len = vsnprintf(buf, sizeof(buf), fmt, ap);
if (len < 0) err_sys("vsnprintf error");
/* construct list node */
if ((ptr = malloc(sizeof(xk_msg))) == NULL)
err_sys("malloc error");
if ((ptr->str = malloc(len + 1)) == NULL)
err_sys("malloc error");
strcpy(ptr->str, buf); /* buffer have enough space */
ptr->type = type;
clock_gettime(CLOCK_REALTIME, &ptr->ts);
ptr->next = NULL;
/* insert to the linked list */
pthread_mutex_lock(&mutex); /* enter critical section */
if (tail != NULL)
tail->next = ptr, tail = ptr;
else
head = tail = ptr;
pthread_mutex_unlock(&mutex); /* leave critical section */
}
void msg_flush()
{
xk_msg *ptr, *next;
/* take out old linked list */
pthread_mutex_lock(&mutex); /* enter critical section */
ptr = head;
head = tail = NULL;
pthread_mutex_unlock(&mutex); /* leave critical section */
while (ptr != NULL) {
real_output_msg(ptr);
next = ptr->next;
free(ptr->str);
free(ptr);
ptr = next;
}
}
void create_msg_flush_thread()
{
cancel = 0;
if (pthread_create(&tid, NULL, msg_flush_thread, NULL) != 0)
err_quit("pthread_create error");
}
void cancel_msg_flush_thread()
{
cancel = 1;
pthread_join(tid, NULL);
}
void msg(xk_msg_type type, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
msg_doit(type, fmt, ap);
va_end(ap);
}
void msg_dbg(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
msg_doit(XK_MSG_DEBUG, fmt, ap);
va_end(ap);
}
void msg_info(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
msg_doit(XK_MSG_INFO, fmt, ap);
va_end(ap);
}
void msg_notice(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
msg_doit(XK_MSG_NOTICE, fmt, ap);
va_end(ap);
}
void msg_good(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
msg_doit(XK_MSG_GOOD, fmt, ap);
va_end(ap);
}
void msg_bad(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
msg_doit(XK_MSG_BAD, fmt, ap);
va_end(ap);
}
void msg_err(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
msg_doit(XK_MSG_ERROR, fmt, ap);
va_end(ap);
}