9#include "daq/rfarm/manager/RFLogManager.h"
10#include "daq/rfarm/manager/RFNSM.h"
12#include <nsm2/belle2nsm.h>
26#define VSNPRINTF(s,l,f,a) va_start(a,f);vsnprintf(s,l,f,a);va_end(a)
30RFLogManager::RFLogManager(
const char*
id,
const char* lognode,
const char* logdir)
35 strcpy(m_lognode, lognode);
37 strcpy(m_lognode,
"LOGC");
40 strcpy(m_logdir, logdir);
45 OpenLogFile(m_logdate);
48RFLogManager::~RFLogManager()
55int RFLogManager::today()
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;
63 return day + month * 100 + year * 10000;
66int RFLogManager::OpenLogFile(
int today)
70 sprintf(filename,
"%s/%s_d%6.6d.log", m_logdir, m_id, today);
72 sprintf(filename,
"%s_d%6.6d.log", m_id, today);
74 m_fd = open(filename, O_RDWR | O_CREAT | O_APPEND, 0644);
76 fprintf(stderr,
"RfLogManager(%s) : error to open file %s\n",
78 perror(
"RFLogManager");
82 sprintf(slinkname,
"latest.log");
84 symlink(filename, slinkname);
85 printf(
"RFLogManager: symbolic link to %s\n", slinkname);
90int RFLogManager::SwitchLogFile()
93 if (myday != m_logdate) {
102int RFLogManager::WriteLog(
const char* prefix,
const char* msg)
107 sprintf(&wbuf[strlen(wbuf) - 1],
"%s %s\n", prefix, msg);
108 int st = write(m_fd, wbuf, strlen(wbuf));
112void RFLogManager::timestamp(
char* buf)
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));
125char* RFLogManager::BuildMessage(
const char* fmt, ...)
129 m_strbuf[
sizeof(m_strbuf) - 1] = 0;
131 VSNPRINTF(m_strbuf,
sizeof(m_strbuf), fmt, arg);
144void RFLogManager::Log(
const char* fmt, ...)
149 msg[
sizeof(msg) - 1] = 0;
150 VSNPRINTF(msg,
sizeof(msg), fmt, ap);
154void RFLogManager::Info(
const char* fmt, ...)
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) {
165 pars[1] = (int)time(NULL);
166 b2nsm_sendany(m_lognode,
"LOG", 2, pars, strlen(msg) + 1, msg, NULL);
170void RFLogManager::Warning(
const char* fmt, ...)
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) {
181 pars[1] = (int)time(NULL);
182 b2nsm_sendany(m_lognode,
"LOG", 2, pars, strlen(msg) + 1, msg, NULL);
186void RFLogManager::Error(
const char* fmt, ...)
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) {
197 pars[1] = (int)time(NULL);
198 b2nsm_sendany(m_lognode,
"LOG", 2, pars, strlen(msg) + 1, msg, NULL);
202void RFLogManager::Fatal(
const char* fmt, ...)
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) {
213 pars[1] = (int)time(NULL);
214 b2nsm_sendany(m_lognode,
"LOG", 2, pars, strlen(msg) + 1, msg, NULL);
218void RFLogManager::Abort(
const char* fmt, ...)
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);
232int RFLogManager::ProcessLog(
int fd)
234 const int bufSize = 4000000;
236 char*
const buf =
new char[bufSize];
238 int siz = bufSize *
sizeof(char) - 1;
246 printf(
"Pipe for log is not availablle\n");
247 Abort(
"No pipe available");
253 if (select(fd + 1, &fdset, NULL, NULL, &tv) < 0) {
255 case EINTR:
continue;
256 case EAGAIN:
continue;
262 }
else if (!(fd > 0 && FD_ISSET(fd, &fdset))) {
265 if ((len = read(fd, p, siz)) < 0) {
266 if (errno == EINTR)
continue;
267 if (errno == EPIPE) {
268 Log(
"broken pipe fd=%d", fd);
271 }
else if (errno == EBADF) {
272 Log(
"bad fd=%d", fd);
277 }
else if (len == 0) {
285 if (!(q = strchr(p,
'\n'))) {
286 if (p == buf && len == siz) {
288 Warning(
"too long message from fd=%d", fd);
292 memmove(buf, p, len);
293 siz = bufSize *
sizeof(char) - len;
300 if (strlen(p) > 13 &&
301 p[2] ==
':' && p[5] ==
':' && p[8] ==
'.' && p[12] ==
' ') {
304 if (strncmp(p,
"[FATAL] ", 8) == 0) {
306 }
else if (strncmp(p,
"[ERROR] ", 8) == 0) {
308 }
else if (strncmp(p,
"[WARNING] ", 10) == 0) {
309 Warning(
"%s", p + 10);
310 }
else if (strncmp(p,
"[INFO] ", 7) == 0) {
312 }
else if (strncmp(p,
"[ABORT] ", 8) == 0) {
313 Fatal(
"abort - %s", p + 8);
314 }
else if (strncmp(p,
"[sysexit] ", 10) == 0) {
315 Fatal(
"sysexit - %s", p + 10);
Abstract base class for different kinds of events.