Belle II Software  release-06-02-00
RFLogManager.cc
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 
9 #include "daq/rfarm/manager/RFLogManager.h"
10 #include "daq/rfarm/manager/RFNSM.h"
11 
12 #include <nsm2/belle2nsm.h>
13 
14 #include <fcntl.h>
15 #include <sys/time.h>
16 #include <time.h>
17 #include <unistd.h>
18 
19 #include <cstdarg>
20 #include <cstring>
21 #include <iostream>
22 
23 using namespace Belle2;
24 using namespace std;
25 
26 #define VSNPRINTF(s,l,f,a) va_start(a,f);vsnprintf(s,l,f,a);va_end(a)
27 
28 // Constructor/Destructor
29 
30 RFLogManager::RFLogManager(const char* id, const char* lognode, const char* logdir)
31 {
32  strcpy(m_id, id);
33 
34  if (lognode != NULL)
35  strcpy(m_lognode, lognode);
36  else
37  strcpy(m_lognode, "LOGC");
38 
39  if (logdir != NULL)
40  strcpy(m_logdir, logdir);
41  else
42  m_logdir[0] = 0;
43 
44  m_logdate = today();
45  OpenLogFile(m_logdate);
46 }
47 
48 RFLogManager::~RFLogManager()
49 {
50  close(m_fd);
51 }
52 
53 // Basic Function
54 
55 int RFLogManager::today()
56 {
57  time_t today = time(NULL);
58  struct tm* tmtoday = gmtime(&today);
59  int day = tmtoday->tm_mday;
60  int month = tmtoday->tm_mon + 1;
61  int year = tmtoday->tm_year - 100;
62  // printf ( "RFLogManager: day = %d, month = %d, year = %d\n", day, month, year );
63  return day + month * 100 + year * 10000;
64 }
65 
66 int RFLogManager::OpenLogFile(int today)
67 {
68  char filename[1024];
69  if (m_logdir[0] != 0)
70  sprintf(filename, "%s/%s_d%6.6d.log", m_logdir, m_id, today);
71  else
72  sprintf(filename, "%s_d%6.6d.log", m_id, today);
73 
74  m_fd = open(filename, O_RDWR | O_CREAT | O_APPEND, 0644);
75  if (m_fd < 0) {
76  fprintf(stderr, "RfLogManager(%s) : error to open file %s\n",
77  m_id, filename);
78  perror("RFLogManager");
79  } else {
80  // Make symbolic link to "latest.log"
81  char slinkname[1024];
82  sprintf(slinkname, "latest.log");
83  unlink(slinkname);
84  symlink(filename, slinkname);
85  printf("RFLogManager: symbolic link to %s\n", slinkname);
86  }
87  return m_fd;
88 }
89 
90 int RFLogManager::SwitchLogFile()
91 {
92  int myday = today();
93  if (myday != m_logdate) {
94  close(m_fd);
95  OpenLogFile(myday);
96  m_logdate = myday;
97  }
98  return 0;
99 }
100 
101 
102 int RFLogManager::WriteLog(const char* prefix, const char* msg)
103 {
104  SwitchLogFile();
105  char wbuf[1024];
106  timestamp(wbuf);
107  sprintf(&wbuf[strlen(wbuf) - 1], "%s %s\n", prefix, msg);
108  int st = write(m_fd, wbuf, strlen(wbuf));
109  return st;
110 }
111 
112 void RFLogManager::timestamp(char* buf)
113 {
114  struct timeval tb;
115  struct tm* tp;
116  // struct tm result;
117  gettimeofday(&tb, NULL);
118  tp = localtime(&tb.tv_sec);
119  sprintf(buf, "[%04d-%02d-%02d %02d:%02d:%02d.%03d] ",
120  tp->tm_year + 1900, tp->tm_mon + 1, tp->tm_mday,
121  tp->tm_hour, tp->tm_min, tp->tm_sec, (int)(tb.tv_usec / 1000));
122  return;
123 }
124 
125 char* RFLogManager::BuildMessage(const char* fmt, ...)
126 {
127  va_list arg;
128 
129  m_strbuf[sizeof(m_strbuf) - 1] = 0;
130 
131  VSNPRINTF(m_strbuf, sizeof(m_strbuf), fmt, arg);
132 
133  return m_strbuf;
134 
135  /*
136  int len = strlen(m_strbuf);
137  while(m_strbuf[len-1] == '\n' ) m_strbuf[--len] = 0;
138  m_strbuf[len++] = '\n';
139  m_strbuf[len] = 0;
140  return m_strbuf;
141  */
142 }
143 
144 void RFLogManager::Log(const char* fmt, ...)
145 {
146  va_list ap;
147  char msg[1000];
148 
149  msg[sizeof(msg) - 1] = 0;
150  VSNPRINTF(msg, sizeof(msg), fmt, ap);
151  WriteLog("", msg);
152 }
153 
154 void RFLogManager::Info(const char* fmt, ...)
155 {
156  va_list ap;
157  char msg[1000];
158 
159  msg[sizeof(msg) - 1] = 0;
160  VSNPRINTF(msg, sizeof(msg), fmt, ap);
161  WriteLog("[INFO]", msg);
162  if (RFNSM_Status::Instance().get_state() == RFSTATE_RUNNING) {
163  int pars[2];
164  pars[0] = 2;
165  pars[1] = (int)time(NULL);
166  b2nsm_sendany(m_lognode, "LOG", 2, pars, strlen(msg) + 1, msg, NULL);
167  }
168 }
169 
170 void RFLogManager::Warning(const char* fmt, ...)
171 {
172  va_list ap;
173  char msg[1000];
174 
175  msg[sizeof(msg) - 1] = 0;
176  VSNPRINTF(msg, sizeof(msg), fmt, ap);
177  WriteLog("[WARNING]", msg);
178  if (RFNSM_Status::Instance().get_state() == RFSTATE_RUNNING) {
179  int pars[2];
180  pars[0] = 4;
181  pars[1] = (int)time(NULL);
182  b2nsm_sendany(m_lognode, "LOG", 2, pars, strlen(msg) + 1, msg, NULL);
183  }
184 }
185 
186 void RFLogManager::Error(const char* fmt, ...)
187 {
188  va_list ap;
189  char msg[1000];
190 
191  msg[sizeof(msg) - 1] = 0;
192  VSNPRINTF(msg, sizeof(msg), fmt, ap);
193  WriteLog("[ERROR]", msg);
194  if (RFNSM_Status::Instance().get_state() == RFSTATE_RUNNING) {
195  int pars[2];
196  pars[0] = 5;
197  pars[1] = (int)time(NULL);
198  b2nsm_sendany(m_lognode, "LOG", 2, pars, strlen(msg) + 1, msg, NULL);
199  }
200 }
201 
202 void RFLogManager::Fatal(const char* fmt, ...)
203 {
204  va_list ap;
205  char msg[1000];
206 
207  msg[sizeof(msg) - 1] = 0;
208  VSNPRINTF(msg, sizeof(msg), fmt, ap);
209  WriteLog("[FATAL]", msg);
210  if (RFNSM_Status::Instance().get_state() == RFSTATE_RUNNING) {
211  int pars[2];
212  pars[0] = 6;
213  pars[1] = (int)time(NULL);
214  b2nsm_sendany(m_lognode, "LOG", 2, pars, strlen(msg) + 1, msg, NULL);
215  }
216 }
217 
218 void RFLogManager::Abort(const char* fmt, ...)
219 {
220  va_list ap;
221  char msg[1000];
222 
223  msg[sizeof(msg) - 1] = 0;
224  VSNPRINTF(msg, sizeof(msg), fmt, ap);
225  WriteLog("[ABORT]", msg);
226  b2nsm_sendany(m_lognode, "LOG", 0, NULL, strlen(msg), msg, NULL);
227  abort();
228 }
229 
230 // Process Log
231 
232 int RFLogManager::ProcessLog(int fd)
233 {
234  char* p, *q, buf[4000000];
235  int len;
236  int siz = sizeof(buf) - 1;
237  int toolong = 0;
238 
239  p = buf;
240  fd_set fdset;
241  do {
242  FD_ZERO(&fdset);
243  if (fd <= 0) {
244  printf("Pipe for log is not availablle\n");
245  Abort("No pipe available");
246  }
247  FD_SET(fd, &fdset);
248  struct timeval tv;
249  tv.tv_sec = 0;
250  tv.tv_usec = 0;
251  if (select(fd + 1, &fdset, NULL, NULL, &tv) < 0) {
252  switch (errno) {
253  case EINTR: continue;
254  case EAGAIN: continue;
255  default:
256  perror("select");
257  return -1;
258  }
259  return -1;
260  } else if (!(fd > 0 && FD_ISSET(fd, &fdset))) {
261  return 0;
262  }
263  if ((len = read(fd, p, siz)) < 0) {
264  if (errno == EINTR) continue;
265  if (errno == EPIPE) {
266  Log("broken pipe fd=%d", fd);
267  close(fd);
268  return -1;
269  } else if (errno == EBADF) {
270  Log("bad fd=%d", fd);
271  return -1;
272  }
273  perror("read");
274  Abort("read");
275  } else if (len == 0) {
276  close(fd);
277  return -1;
278  } else {
279  p[len] = 0;
280  len += p - buf;
281  p = buf;
282  do {
283  if (!(q = strchr(p, '\n'))) {
284  if (p == buf && len == siz) {
285  if (! toolong)
286  Warning("too long message from fd=%d", fd);
287  toolong = 1;
288  } else {
289  len -= p - buf;
290  memmove(buf, p, len);
291  siz = sizeof(buf) - len;
292  p = buf + len;
293  }
294  break;
295  }
296  *q++ = 0;
297  if (! toolong) {
298  if (strlen(p) > 13 &&
299  p[2] == ':' && p[5] == ':' && p[8] == '.' && p[12] == ' ') {
300  p += 13;
301  }
302  if (strncmp(p, "[FATAL] ", 8) == 0) {
303  Fatal("%s", p + 8);
304  } else if (strncmp(p, "[ERROR] ", 8) == 0) {
305  Error("%s", p + 8);
306  } else if (strncmp(p, "[WARNING] ", 10) == 0) {
307  Warning("%s", p + 10);
308  } else if (strncmp(p, "[INFO] ", 7) == 0) {
309  Info("%s", p + 7);
310  } else if (strncmp(p, "[ABORT] ", 8) == 0) {
311  Fatal("abort - %s", p + 8);
312  } else if (strncmp(p, "[sysexit] ", 10) == 0) {
313  Fatal("sysexit - %s", p + 10);
314  } else {
315  Log("%s", p);
316  }
317  }
318  toolong = 0;
319  p = q;
320  if (! *p) return 0;
321  } while (*p);
322  }
323  } while (1);
324 }
325 
326 
327 
328 
329 
330 
Abstract base class for different kinds of events.