VCOM Linux Driver
Advantech Inc.
Loading...
Searching...
No Matches
vcom_monitor_dbg.h
Go to the documentation of this file.
1#ifndef _VCOM_MONITOR_H
2#define _VCOM_MONITOR_H
3#define MSIZE 2048 // 2K size file
4#define FNAME_LEN 256
5#define CUTTER "> "
6#define MON_MSGLEN_MAX 128
7
8extern void * stk_mon;
9
10struct vc_monitor{
11 void * addr;
12 int fd;
13 int pid;
16 char fname[FNAME_LEN];
17};
18
19extern struct vc_monitor vc_mon;
20
21static inline int mon_init(char * fname)
22{
23 vc_mon.fd = -1;
24 vc_mon.addr = 0;
25 vc_mon.max_statl = 0;
26 vc_mon.dbg_first = 1;
27
28 if(fname == 0)
29 return 0;
30
31 vc_mon.pid = getpid();
32 snprintf(vc_mon.fname, FNAME_LEN, "%s", fname);
33
34 vc_mon.fd = open(vc_mon.fname, O_RDWR | O_CREAT | O_TRUNC, S_IRWXO|S_IRWXG|S_IRWXU);
35 if(vc_mon.fd < 0){
36 printf("create log fail...\n");
37 return -1;
38 }
39
40 ftruncate(vc_mon.fd, MSIZE);
41 vc_mon.addr = mmap(0, MSIZE, PROT_READ | PROT_WRITE, MAP_SHARED, vc_mon.fd, 0);
42 if(vc_mon.addr == MAP_FAILED){
43 printf("mmap fail\n");
44 munmap(vc_mon.addr, MSIZE);
45 close(vc_mon.fd);
46 vc_mon.fd = 0;
47 return -1;
48 }
49 memset(vc_mon.addr, '\r', MSIZE);
50 stk_mon = &vc_mon;
51 return 0;
52}
53
54static inline int time2str(char *buf, int len)
55{
56 int tm_strlen;
57 struct tm tm_buf;
58 struct timeval tv;
59
60 gettimeofday(&tv, 0);
61 localtime_r(&tv.tv_sec, &tm_buf);
62 tm_strlen = strftime(buf, len, "%F|%T:", &tm_buf);
63
64 return tm_strlen;
65}
66
67static inline int mon_update(struct stk_vc * stk, int sig, const char * dbg)
68{
69 char * mem;
70 char * stat;
71 char tmp[MON_MSGLEN_MAX];
72 int len;
73 int statl;
74
75 if(vc_mon.fd < 0){
76 return 0;
77 }
78
79 if(stk_empty(stk)){
80 printf("stack empty ...\n");
81 return -1;
82 }
83
84 stat = stk_curnt(stk)->name();
85 mem = (char *)vc_mon.addr;
86 memset(tmp, ' ', sizeof(tmp));
87 statl = snprintf(tmp, sizeof(tmp), "Pid %d|State[%s]\n",
88 vc_mon.pid, stat);
89
90 len = MSIZE - statl;
91 if(len <= 1){
92 printf("%s len <= 1\n", __func__);
93 return -1;
94 }
95
96 if(statl > vc_mon.max_statl){
97 memmove(mem+statl, mem+vc_mon.max_statl, MSIZE-statl);
98 vc_mon.max_statl = statl;
99 }
100 memcpy(mem, tmp, vc_mon.max_statl);
101 memset(tmp, ' ', sizeof(tmp));
102 /* for record debug message */
103 if(dbg != 0 ){
104 char * ptr;
105 int msglen;
106 int dbgl;
107 msglen = 0;
108
109 ptr = mem + vc_mon.max_statl;
110
111// msglen = snprintf(tmp, sizeof(tmp), "\n");
112
113 dbgl = time2str(&tmp[msglen], sizeof(tmp) -msglen);
114 msglen += dbgl;
115
116 dbgl = snprintf(&tmp[msglen], sizeof(tmp)- msglen, "%s%s\n", CUTTER, dbg);
117 msglen += dbgl;
118
119 len = MSIZE - msglen - vc_mon.max_statl;
120 if(len <= 1){
121 printf("%s len <= 1\n", __func__);
122 return -1;
123 }
124
125 if(!vc_mon.dbg_first){
126
127 memmove(ptr+msglen, ptr, MSIZE-statl-msglen);
128 }else{
129 vc_mon.dbg_first = 0;
130 }
131
132 memcpy(ptr, tmp, msglen);
133 //memset(ptr+msglen, ' ', 1);
134 }
135 /* Trigger the inotify */
136 if(sig){
137 msync(mem, MSIZE, MS_SYNC);
138 ftruncate(vc_mon.fd, MSIZE);
139 }else{
140 msync(mem, MSIZE, MS_ASYNC);
141 }
142
143 return 0;
144}
145
146#define muc2(a, b) \
147 do{if(stk_mon) \
148 mon_update(a, b, 0); \
149 }while(0)
150
151
152#define muc3(a, b, c) \
153 do{if(stk_mon) \
154 mon_update(a, b, c); \
155 }while(0)
156
157#define muc_ovrld(_1, _2, _3, func, ...) func
158#define mon_update_check(args...) muc_ovrld(args, muc3, muc2,...)(args)
159
160#endif
Definition vcom.h:25
Definition vcom_monitor.h:8
char fname[FNAME_LEN]
Definition vcom_monitor.h:13
int fd
Definition vcom_monitor.h:10
int dbg_first
Definition vcom_monitor_dbg.h:15
void * addr
Definition vcom_monitor.h:9
int max_statl
Definition vcom_monitor_dbg.h:14
int pid
Definition vcom_monitor.h:11
#define mon_init(a)
Definition vcom.h:97
#define stk_empty(a)
Definition vcom.h:23
#define mon_update(...)
Definition vcom.h:96
struct vc_monitor vc_mon
Definition vcom_monitor.h:15
#define FNAME_LEN
Definition vcom_monitor_dbg.h:4
#define MON_MSGLEN_MAX
Definition vcom_monitor_dbg.h:6
#define CUTTER
Definition vcom_monitor_dbg.h:5
#define MSIZE
Definition vcom_monitor_dbg.h:3
void * stk_mon
Definition vcom_client.c:33
#define FNAME_LEN
Definition vcom_monitor_pre_stat.h:4
unsigned short len
Definition vcom_proto.h:3
char ptr[4]
Definition vcom_proto.h:2