mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2025-06-23 18:17:59 +02:00
replaced old logger
This commit is contained in:
@ -65,7 +65,7 @@ class UdpRxSocket {
|
||||
if (current < buffer_size) {
|
||||
setBufferSize(buffer_size);
|
||||
if (getBufferSize() / 2 < buffer_size) {
|
||||
FILE_LOG(logWARNING)
|
||||
LOG(logWARNING)
|
||||
<< "Could not set buffer size. Got: "
|
||||
<< getBufferSize() / 2 << " instead of " << buffer_size;
|
||||
}
|
||||
@ -107,7 +107,7 @@ class UdpRxSocket {
|
||||
constexpr ssize_t eiger_header_packet =
|
||||
40; // only detector that has this
|
||||
if (r == eiger_header_packet) {
|
||||
FILE_LOG(logWARNING) << "Got header pkg";
|
||||
LOG(logWARNING) << "Got header pkg";
|
||||
r = recvfrom(fd, dst, packet_size, 0, nullptr, nullptr);
|
||||
}
|
||||
return r;
|
||||
|
@ -204,17 +204,17 @@ public:
|
||||
// get host info into res
|
||||
int errcode = getaddrinfo (hostname, NULL, &hints, res);
|
||||
if (errcode != 0) {
|
||||
FILE_LOG(logERROR) << "Error: Could not convert hostname " << hostname << " to internet address (zmq):"
|
||||
LOG(logERROR) << "Error: Could not convert hostname " << hostname << " to internet address (zmq):"
|
||||
<< gai_strerror(errcode);
|
||||
} else {
|
||||
if (*res == NULL) {
|
||||
FILE_LOG(logERROR) << "Could not convert hostname " << hostname << " to internet address (zmq): "
|
||||
LOG(logERROR) << "Could not convert hostname " << hostname << " to internet address (zmq): "
|
||||
"gettaddrinfo returned null";
|
||||
} else{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
FILE_LOG(logERROR) << "Could not convert hostname to internet address";
|
||||
LOG(logERROR) << "Could not convert hostname to internet address";
|
||||
return 1;
|
||||
};
|
||||
|
||||
@ -232,7 +232,7 @@ public:
|
||||
freeaddrinfo(res);
|
||||
return 0;
|
||||
}
|
||||
FILE_LOG(logERROR) << "Could not convert internet address to ip string";
|
||||
LOG(logERROR) << "Could not convert internet address to ip string";
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -381,7 +381,7 @@ public:
|
||||
int length = zmq_msg_recv (&message, sockfd.socketDescriptor, 0);
|
||||
if (length == -1) {
|
||||
PrintError ();
|
||||
FILE_LOG(logERROR) << "Could not read header for socket " << index;
|
||||
LOG(logERROR) << "Could not read header for socket " << index;
|
||||
}
|
||||
#ifdef VERBOSE
|
||||
else
|
||||
@ -449,7 +449,7 @@ public:
|
||||
Document& document, bool& dummy, uint32_t version)
|
||||
{
|
||||
if ( document.Parse( buff, length).HasParseError() ) {
|
||||
FILE_LOG(logERROR) << index << " Could not parse. len:" << length << ": Message:" << buff;
|
||||
LOG(logERROR) << index << " Could not parse. len:" << length << ": Message:" << buff;
|
||||
fflush ( stdout );
|
||||
// char* buf = (char*) zmq_msg_data (&message);
|
||||
for ( int i= 0; i < length; ++i ) {
|
||||
@ -461,7 +461,7 @@ public:
|
||||
}
|
||||
|
||||
if (document["jsonversion"].GetUint() != version) {
|
||||
FILE_LOG(logERROR) << "version mismatch. required " << version << ", got " << document["jsonversion"].GetUint();
|
||||
LOG(logERROR) << "version mismatch. required " << version << ", got " << document["jsonversion"].GetUint();
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -504,7 +504,7 @@ public:
|
||||
}
|
||||
//incorrect size (larger)
|
||||
else {
|
||||
FILE_LOG(logERROR) << "Received weird packet size " << length << " for socket " << index;
|
||||
LOG(logERROR) << "Received weird packet size " << length << " for socket " << index;
|
||||
memset(buf,0xFF,size);
|
||||
}
|
||||
|
||||
@ -520,52 +520,52 @@ public:
|
||||
void PrintError () {
|
||||
switch (errno) {
|
||||
case EINVAL:
|
||||
FILE_LOG(logERROR) << "The socket type/option or value/endpoint supplied is invalid (zmq)";
|
||||
LOG(logERROR) << "The socket type/option or value/endpoint supplied is invalid (zmq)";
|
||||
break;
|
||||
case EAGAIN:
|
||||
FILE_LOG(logERROR) << "Non-blocking mode was requested and the message cannot be sent/available at the moment (zmq)";
|
||||
LOG(logERROR) << "Non-blocking mode was requested and the message cannot be sent/available at the moment (zmq)";
|
||||
break;
|
||||
case ENOTSUP:
|
||||
FILE_LOG(logERROR) << "The zmq_send()/zmq_msg_recv() operation is not supported by this socket type (zmq)";
|
||||
LOG(logERROR) << "The zmq_send()/zmq_msg_recv() operation is not supported by this socket type (zmq)";
|
||||
break;
|
||||
case EFSM:
|
||||
FILE_LOG(logERROR) << "The zmq_send()/zmq_msg_recv() unavailable now as socket in inappropriate state (eg. ZMQ_REP). Look up messaging patterns (zmq)";
|
||||
LOG(logERROR) << "The zmq_send()/zmq_msg_recv() unavailable now as socket in inappropriate state (eg. ZMQ_REP). Look up messaging patterns (zmq)";
|
||||
break;
|
||||
case EFAULT:
|
||||
FILE_LOG(logERROR) << "The provided context/message is invalid (zmq)";
|
||||
LOG(logERROR) << "The provided context/message is invalid (zmq)";
|
||||
break;
|
||||
case EMFILE:
|
||||
FILE_LOG(logERROR) << "The limit on the total number of open ØMQ sockets has been reached (zmq)";
|
||||
LOG(logERROR) << "The limit on the total number of open ØMQ sockets has been reached (zmq)";
|
||||
break;
|
||||
case EPROTONOSUPPORT:
|
||||
FILE_LOG(logERROR) << "The requested transport protocol is not supported (zmq)";
|
||||
LOG(logERROR) << "The requested transport protocol is not supported (zmq)";
|
||||
break;
|
||||
case ENOCOMPATPROTO:
|
||||
FILE_LOG(logERROR) << "The requested transport protocol is not compatible with the socket type (zmq)";
|
||||
LOG(logERROR) << "The requested transport protocol is not compatible with the socket type (zmq)";
|
||||
break;
|
||||
case EADDRINUSE:
|
||||
FILE_LOG(logERROR) << "The requested address is already in use (zmq)";
|
||||
LOG(logERROR) << "The requested address is already in use (zmq)";
|
||||
break;
|
||||
case EADDRNOTAVAIL:
|
||||
FILE_LOG(logERROR) << "The requested address was not local (zmq)";
|
||||
LOG(logERROR) << "The requested address was not local (zmq)";
|
||||
break;
|
||||
case ENODEV:
|
||||
FILE_LOG(logERROR) << "The requested address specifies a nonexistent interface (zmq)";
|
||||
LOG(logERROR) << "The requested address specifies a nonexistent interface (zmq)";
|
||||
break;
|
||||
case ETERM:
|
||||
FILE_LOG(logERROR) << "The ØMQ context associated with the specified socket was terminated (zmq)";
|
||||
LOG(logERROR) << "The ØMQ context associated with the specified socket was terminated (zmq)";
|
||||
break;
|
||||
case ENOTSOCK:
|
||||
FILE_LOG(logERROR) << "The provided socket was invalid (zmq)";
|
||||
LOG(logERROR) << "The provided socket was invalid (zmq)";
|
||||
break;
|
||||
case EINTR:
|
||||
FILE_LOG(logERROR) << "The operation was interrupted by delivery of a signal (zmq)";
|
||||
LOG(logERROR) << "The operation was interrupted by delivery of a signal (zmq)";
|
||||
break;
|
||||
case EMTHREAD:
|
||||
FILE_LOG(logERROR) << "No I/O thread is available to accomplish the task (zmq)";
|
||||
LOG(logERROR) << "No I/O thread is available to accomplish the task (zmq)";
|
||||
break;
|
||||
default:
|
||||
FILE_LOG(logERROR) << "Unknown socket error (zmq)";
|
||||
LOG(logERROR) << "Unknown socket error (zmq)";
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
@ -150,7 +150,7 @@ public:
|
||||
sockfd.fd = socket(AF_INET, getProtocol(),0); //tcp
|
||||
|
||||
if (sockfd.fd < 0) {
|
||||
FILE_LOG(logERROR) << "Can not create socket";
|
||||
LOG(logERROR) << "Can not create socket";
|
||||
sockfd.fd =-1;
|
||||
throw SocketError("Can not create socket");
|
||||
}
|
||||
@ -173,7 +173,7 @@ public:
|
||||
int val=1;
|
||||
if (setsockopt(sockfd.fd,SOL_SOCKET,SO_REUSEADDR,
|
||||
&val,sizeof(int)) == -1) {
|
||||
FILE_LOG(logERROR) << "setsockopt REUSEADDR failed";
|
||||
LOG(logERROR) << "setsockopt REUSEADDR failed";
|
||||
sockfd.fd =-1;
|
||||
throw SocketError("setsockopt REUSEADDR failed");
|
||||
}
|
||||
@ -188,33 +188,33 @@ public:
|
||||
|
||||
// confirm if sufficient
|
||||
if (getsockopt(sockfd.fd, SOL_SOCKET, SO_RCVBUF, &ret_size, &optlen) == -1) {
|
||||
FILE_LOG(logWARNING) << "[Port " << port_number << "] "
|
||||
LOG(logWARNING) << "[Port " << port_number << "] "
|
||||
"Could not get rx socket receive buffer size";
|
||||
} else if (ret_size >= real_size) {
|
||||
actual_udp_socket_buffer_size = ret_size;
|
||||
FILE_LOG(logINFO) << "[Port " << port_number << "] "
|
||||
LOG(logINFO) << "[Port " << port_number << "] "
|
||||
"UDP rx socket real buffer size is sufficient (" << ret_size << ")";
|
||||
}
|
||||
|
||||
// not sufficient, enhance size
|
||||
else {
|
||||
FILE_LOG(logINFO) << "[Port " << port_number << "] UDP rx socket real buffer size to be modified from " << ret_size << " to " << real_size;
|
||||
LOG(logINFO) << "[Port " << port_number << "] UDP rx socket real buffer size to be modified from " << ret_size << " to " << real_size;
|
||||
// set buffer size (could not set)
|
||||
if (setsockopt(sockfd.fd, SOL_SOCKET, SO_RCVBUF,
|
||||
&desired_size, optlen) == -1) {
|
||||
FILE_LOG(logWARNING) << "[Port " << port_number << "] "
|
||||
LOG(logWARNING) << "[Port " << port_number << "] "
|
||||
"Could not set rx socket buffer size to "
|
||||
<< desired_size << ". (No Root Privileges?)";
|
||||
}
|
||||
// confirm size
|
||||
else if (getsockopt(sockfd.fd, SOL_SOCKET, SO_RCVBUF,
|
||||
&ret_size, &optlen) == -1) {
|
||||
FILE_LOG(logWARNING) << "[Port " << port_number << "] "
|
||||
LOG(logWARNING) << "[Port " << port_number << "] "
|
||||
"Could not get rx socket buffer size";
|
||||
}
|
||||
else if (ret_size >= real_size) {
|
||||
actual_udp_socket_buffer_size = ret_size;
|
||||
FILE_LOG(logINFO) << "[Port " << port_number << "] "
|
||||
LOG(logINFO) << "[Port " << port_number << "] "
|
||||
"UDP rx socket buffer size modified to " << ret_size;
|
||||
}
|
||||
// buffer size too large
|
||||
@ -227,14 +227,14 @@ public:
|
||||
getsockopt(sockfd.fd, SOL_SOCKET, SO_RCVBUF,
|
||||
&ret_size, &optlen);
|
||||
if (ret == -1) {
|
||||
FILE_LOG(logWARNING) << "[Port " << port_number << "] "
|
||||
LOG(logWARNING) << "[Port " << port_number << "] "
|
||||
"Could not force rx socket buffer size to "
|
||||
<< desired_size << ".\n Real size: " << ret_size <<
|
||||
". (No Root Privileges?)\n"
|
||||
" To remove this warning: set rx_udpsocksize from client to <= " <<
|
||||
(ret_size/2) << " (Real size:" << ret_size << ").";
|
||||
} else {
|
||||
FILE_LOG(logINFO) << "[Port " << port_number << "] "
|
||||
LOG(logINFO) << "[Port " << port_number << "] "
|
||||
"UDP rx socket buffer size (force) modified to " << ret_size;
|
||||
}
|
||||
}
|
||||
@ -243,7 +243,7 @@ public:
|
||||
|
||||
|
||||
if(bind(sockfd.fd,(struct sockaddr *) &serverAddress,sizeof(serverAddress))<0){
|
||||
FILE_LOG(logERROR) << "Can not bind socket. Please check if another process is running.";
|
||||
LOG(logERROR) << "Can not bind socket. Please check if another process is running.";
|
||||
sockfd.fd =-1;
|
||||
throw SocketError("Can not bind socket. Please check if another process is running.");
|
||||
}
|
||||
@ -312,7 +312,7 @@ public:
|
||||
case UDP:
|
||||
return SOCK_DGRAM;
|
||||
default:
|
||||
FILE_LOG(logERROR) << "unknown protocol: " << p;
|
||||
LOG(logERROR) << "unknown protocol: " << p;
|
||||
return -1;
|
||||
}
|
||||
};
|
||||
@ -364,70 +364,70 @@ public:
|
||||
if(is_a_server && protocol==TCP){ //server tcp; the server will wait for the clients connection
|
||||
if (sockfd.fd>0) {
|
||||
if ((sockfd.newfd = accept(sockfd.fd,(struct sockaddr *) &clientAddress, &clientAddress_length)) < 0) {
|
||||
FILE_LOG(logERROR) << "with server accept, connection refused";
|
||||
LOG(logERROR) << "with server accept, connection refused";
|
||||
switch(errno) {
|
||||
case EWOULDBLOCK:
|
||||
FILE_LOG(logERROR) << "ewouldblock eagain";
|
||||
LOG(logERROR) << "ewouldblock eagain";
|
||||
break;
|
||||
case EBADF:
|
||||
FILE_LOG(logERROR) << "ebadf";
|
||||
LOG(logERROR) << "ebadf";
|
||||
break;
|
||||
case ECONNABORTED:
|
||||
FILE_LOG(logERROR) << "econnaborted";
|
||||
LOG(logERROR) << "econnaborted";
|
||||
break;
|
||||
case EFAULT:
|
||||
FILE_LOG(logERROR) << "efault";
|
||||
LOG(logERROR) << "efault";
|
||||
break;
|
||||
case EINTR:
|
||||
FILE_LOG(logERROR) << "eintr";
|
||||
LOG(logERROR) << "eintr";
|
||||
break;
|
||||
case EINVAL:
|
||||
FILE_LOG(logERROR) << "einval";
|
||||
LOG(logERROR) << "einval";
|
||||
break;
|
||||
case EMFILE:
|
||||
FILE_LOG(logERROR) << "emfile";
|
||||
LOG(logERROR) << "emfile";
|
||||
break;
|
||||
case ENFILE:
|
||||
FILE_LOG(logERROR) << "enfile";
|
||||
LOG(logERROR) << "enfile";
|
||||
break;
|
||||
case ENOTSOCK:
|
||||
FILE_LOG(logERROR) << "enotsock";
|
||||
LOG(logERROR) << "enotsock";
|
||||
break;
|
||||
case EOPNOTSUPP:
|
||||
FILE_LOG(logERROR) << "eOPNOTSUPP";
|
||||
LOG(logERROR) << "eOPNOTSUPP";
|
||||
break;
|
||||
case ENOBUFS:
|
||||
FILE_LOG(logERROR) << "ENOBUFS";
|
||||
LOG(logERROR) << "ENOBUFS";
|
||||
break;
|
||||
case ENOMEM:
|
||||
FILE_LOG(logERROR) << "ENOMEM";
|
||||
LOG(logERROR) << "ENOMEM";
|
||||
break;
|
||||
case ENOSR:
|
||||
FILE_LOG(logERROR) << "ENOSR";
|
||||
LOG(logERROR) << "ENOSR";
|
||||
break;
|
||||
case EPROTO:
|
||||
FILE_LOG(logERROR) << "EPROTO";
|
||||
LOG(logERROR) << "EPROTO";
|
||||
break;
|
||||
default:
|
||||
FILE_LOG(logERROR) << "unknown error";
|
||||
LOG(logERROR) << "unknown error";
|
||||
}
|
||||
}
|
||||
else{
|
||||
inet_ntop(AF_INET, &(clientAddress.sin_addr), dummyClientIP, INET_ADDRSTRLEN);
|
||||
FILE_LOG(logDEBUG1) << "client connected " << sockfd.newfd;
|
||||
LOG(logDEBUG1) << "client connected " << sockfd.newfd;
|
||||
}
|
||||
}
|
||||
FILE_LOG(logDEBUG1) << "fd " << sockfd.newfd;
|
||||
LOG(logDEBUG1) << "fd " << sockfd.newfd;
|
||||
return sockfd.newfd;
|
||||
} else {
|
||||
if (sockfd.fd<=0)
|
||||
sockfd.fd = socket(AF_INET, getProtocol(),0);
|
||||
// SetTimeOut(10);
|
||||
if (sockfd.fd < 0){
|
||||
FILE_LOG(logERROR) << "Can not create socket";
|
||||
LOG(logERROR) << "Can not create socket";
|
||||
} else {
|
||||
if(connect(sockfd.fd,(struct sockaddr *) &serverAddress,sizeof(serverAddress))<0){
|
||||
FILE_LOG(logERROR) << "Can not connect to socket";
|
||||
LOG(logERROR) << "Can not connect to socket";
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
@ -465,13 +465,13 @@ public:
|
||||
tout.tv_usec = 0;
|
||||
if(::setsockopt(sockfd.fd, SOL_SOCKET, SO_RCVTIMEO,
|
||||
&tout, sizeof(struct timeval)) <0) {
|
||||
FILE_LOG(logERROR) << "setsockopt SO_RCVTIMEO " << 0;
|
||||
LOG(logERROR) << "setsockopt SO_RCVTIMEO " << 0;
|
||||
}
|
||||
tout.tv_sec = ts;
|
||||
tout.tv_usec = 0;
|
||||
if(::setsockopt(sockfd.fd, SOL_SOCKET, SO_SNDTIMEO,
|
||||
&tout, sizeof(struct timeval)) < 0) {
|
||||
FILE_LOG(logERROR) << "setsockopt SO_SNDTIMEO " << ts;
|
||||
LOG(logERROR) << "setsockopt SO_SNDTIMEO " << ts;
|
||||
}
|
||||
return 0;
|
||||
};
|
||||
@ -609,17 +609,17 @@ public:
|
||||
// get host info into res
|
||||
int errcode = getaddrinfo (hostname, NULL, &hints, res);
|
||||
if (errcode != 0) {
|
||||
FILE_LOG(logERROR) << "Could not convert hostname (" << hostname << ") to internet address (zmq):" <<
|
||||
LOG(logERROR) << "Could not convert hostname (" << hostname << ") to internet address (zmq):" <<
|
||||
gai_strerror(errcode);
|
||||
} else {
|
||||
if (*res == NULL) {
|
||||
FILE_LOG(logERROR) << "Could not converthostname (" << hostname << ") to internet address (zmq):"
|
||||
LOG(logERROR) << "Could not converthostname (" << hostname << ") to internet address (zmq):"
|
||||
"gettaddrinfo returned null";
|
||||
} else{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
FILE_LOG(logERROR) << "Could not convert hostname to internet address";
|
||||
LOG(logERROR) << "Could not convert hostname to internet address";
|
||||
return 1;
|
||||
};
|
||||
|
||||
@ -637,7 +637,7 @@ public:
|
||||
freeaddrinfo(res);
|
||||
return 0;
|
||||
}
|
||||
FILE_LOG(logERROR) << "Could not convert internet address to ip string";
|
||||
LOG(logERROR) << "Could not convert internet address to ip string";
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -691,7 +691,7 @@ public:
|
||||
continue;
|
||||
if(nsent != nsending){
|
||||
if(nsent && (nsent != -1)) {
|
||||
FILE_LOG(logERROR) << "Incomplete Packet size " << nsent;
|
||||
LOG(logERROR) << "Incomplete Packet size " << nsent;
|
||||
}
|
||||
break;
|
||||
}
|
||||
@ -710,7 +710,7 @@ public:
|
||||
break;
|
||||
//incomplete packets or header packets ignored and read buffer again
|
||||
if(nsent != packet_size && nsent != header_packet_size) {
|
||||
FILE_LOG(logERROR) << portno << ": Incomplete Packet size " << nsent;
|
||||
LOG(logERROR) << portno << ": Incomplete Packet size " << nsent;
|
||||
}
|
||||
}
|
||||
//nsent = 1040;
|
||||
@ -720,7 +720,7 @@ public:
|
||||
default:
|
||||
;
|
||||
}
|
||||
FILE_LOG(logDEBUG1) << "sent " << total_sent << " Bytes";
|
||||
LOG(logDEBUG1) << "sent " << total_sent << " Bytes";
|
||||
return total_sent;
|
||||
}
|
||||
|
||||
@ -731,7 +731,7 @@ public:
|
||||
* @returns size of data sent
|
||||
*/
|
||||
int SendDataOnly(void *buf, int length) {
|
||||
FILE_LOG(logDEBUG1) << "want to send " << length << " Bytes";
|
||||
LOG(logDEBUG1) << "want to send " << length << " Bytes";
|
||||
if (buf==NULL) return -1;
|
||||
|
||||
total_sent=0;
|
||||
@ -746,7 +746,7 @@ public:
|
||||
nsending = (length>packet_size) ? packet_size:length;
|
||||
nsent = write(tcpfd,(char*)buf+total_sent,nsending);
|
||||
if(is_a_server && nsent < 0) {
|
||||
FILE_LOG(logERROR) << "Could not write to socket. Possible client socket crash";
|
||||
LOG(logERROR) << "Could not write to socket. Possible client socket crash";
|
||||
break;
|
||||
}
|
||||
if(!nsent) break;
|
||||
@ -768,7 +768,7 @@ public:
|
||||
default:
|
||||
;
|
||||
}
|
||||
FILE_LOG(logDEBUG1) << "sent "<< total_sent << " Bytes";
|
||||
LOG(logDEBUG1) << "sent "<< total_sent << " Bytes";
|
||||
return total_sent;
|
||||
}
|
||||
|
||||
|
248
slsSupportLib/include/logger.h
Executable file → Normal file
248
slsSupportLib/include/logger.h
Executable file → Normal file
@ -1,30 +1,19 @@
|
||||
#pragma once
|
||||
/*Utility to log to console*/
|
||||
|
||||
#include <ansi.h>
|
||||
#include "ansi.h" //Colors
|
||||
#include <sys/time.h>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <stdio.h>
|
||||
#include <string>
|
||||
#include <unistd.h>
|
||||
|
||||
enum TLogLevel {logERROR, logWARNING, logINFOBLUE, logINFOGREEN, logINFORED, logINFO,
|
||||
logDEBUG, logDEBUG1, logDEBUG2, logDEBUG3, logDEBUG4, logDEBUG5};
|
||||
|
||||
|
||||
#ifdef FIFODEBUG
|
||||
#define FILELOG_MAX_LEVEL logDEBUG5
|
||||
#elif VERYVERBOSE
|
||||
#define FILELOG_MAX_LEVEL logDEBUG4
|
||||
#elif VERBOSE
|
||||
#define FILELOG_MAX_LEVEL logDEBUG
|
||||
// Compiler should optimize away anything below this value
|
||||
#ifndef LOG_MAX_REPORTING_LEVEL
|
||||
#define LOG_MAX_REPORTING_LEVEL logINFO
|
||||
#endif
|
||||
|
||||
#ifndef FILELOG_MAX_LEVEL
|
||||
#define FILELOG_MAX_LEVEL logINFO
|
||||
// #define FILELOG_MAX_LEVEL logDEBUG5
|
||||
#endif
|
||||
|
||||
|
||||
#define STRINGIFY(x) #x
|
||||
#define TOSTRING(x) STRINGIFY(x)
|
||||
#define MYCONCAT(x,y)
|
||||
#define __AT__ std::string(__FILE__) + std::string("::") + std::string(__func__) + std::string("(): ")
|
||||
#define __SHORT_FORM_OF_FILE__ \
|
||||
(strrchr(__FILE__,'/') \
|
||||
@ -34,159 +23,72 @@
|
||||
#define __SHORT_AT__ std::string(__SHORT_FORM_OF_FILE__) + std::string("::") + std::string(__func__) + std::string("(): ")
|
||||
|
||||
|
||||
namespace sls {
|
||||
class Logger {
|
||||
std::ostringstream os;
|
||||
TLogLevel level = LOG_MAX_REPORTING_LEVEL;
|
||||
|
||||
|
||||
inline std::string NowTime();
|
||||
// 1 normal debug, 3 function names, 5 fifodebug
|
||||
enum TLogLevel {logERROR, logWARNING, logINFOBLUE, logINFOGREEN, logINFORED, logINFO,
|
||||
logDEBUG, logDEBUG1, logDEBUG2, logDEBUG3, logDEBUG4, logDEBUG5};
|
||||
|
||||
template <typename T> class Log{
|
||||
public:
|
||||
Log();
|
||||
virtual ~Log();
|
||||
std::ostringstream& Get(TLogLevel level = logINFO);
|
||||
static TLogLevel& ReportingLevel();
|
||||
static std::string ToString(TLogLevel level);
|
||||
static TLogLevel FromString(const std::string& level);
|
||||
protected:
|
||||
std::ostringstream os;
|
||||
TLogLevel lev;
|
||||
private:
|
||||
Log(const Log&);
|
||||
Log& operator =(const Log&);
|
||||
};
|
||||
|
||||
|
||||
class Output2FILE {
|
||||
public:
|
||||
static FILE*& Stream();
|
||||
static void Output(const std::string& msg);
|
||||
static void Output(const std::string& msg, TLogLevel level);
|
||||
};
|
||||
|
||||
|
||||
#define FILELOG_DECLSPEC
|
||||
|
||||
class FILELOG_DECLSPEC FILELog : public Log<Output2FILE> {};
|
||||
|
||||
|
||||
#define FILE_LOG(level) \
|
||||
if (level > FILELOG_MAX_LEVEL) ; \
|
||||
else if (level > FILELog::ReportingLevel() || !Output2FILE::Stream()) ; \
|
||||
else FILELog().Get(level)
|
||||
|
||||
|
||||
#include <sys/time.h>
|
||||
|
||||
inline std::string NowTime()
|
||||
{
|
||||
char buffer[12];
|
||||
const int buffer_len = sizeof(buffer);
|
||||
time_t t;
|
||||
time(&t);
|
||||
tm r;
|
||||
strftime(buffer, buffer_len, "%X", localtime_r(&t, &r));
|
||||
buffer[buffer_len - 1] = 0;
|
||||
struct timeval tv;
|
||||
gettimeofday(&tv, nullptr);
|
||||
char result[100];
|
||||
const int result_len = sizeof(result);
|
||||
snprintf(result, result_len, "%s.%03ld", buffer, (long)tv.tv_usec / 1000);
|
||||
result[result_len - 1] = 0;
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
template <typename T> Log<T>::Log():lev(logDEBUG){}
|
||||
|
||||
template <typename T> std::ostringstream& Log<T>::Get(TLogLevel level)
|
||||
{
|
||||
lev = level;
|
||||
os << "- " << NowTime();
|
||||
os << " " << ToString(level) << ": ";
|
||||
if (level > logDEBUG)
|
||||
os << std::string(level - logDEBUG, ' ');
|
||||
return os;
|
||||
}
|
||||
|
||||
template <typename T> Log<T>::~Log()
|
||||
{
|
||||
os << std::endl;
|
||||
T::Output( os.str(),lev); // T::Output( os.str());
|
||||
}
|
||||
|
||||
template <typename T> TLogLevel& Log<T>::ReportingLevel()
|
||||
{
|
||||
static TLogLevel reportingLevel = logDEBUG5;
|
||||
return reportingLevel;
|
||||
}
|
||||
|
||||
template <typename T> std::string Log<T>::ToString(TLogLevel level)
|
||||
{
|
||||
static const char* const buffer[] = {
|
||||
"ERROR", "WARNING", "INFO", "INFO", "INFO", "INFO",
|
||||
"DEBUG", "DEBUG1", "DEBUG2", "DEBUG3", "DEBUG4","DEBUG5"};
|
||||
return buffer[level];
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
TLogLevel Log<T>::FromString(const std::string& level)
|
||||
{
|
||||
if (level == "DEBUG5")
|
||||
return logDEBUG5;
|
||||
if (level == "DEBUG4")
|
||||
return logDEBUG4;
|
||||
if (level == "DEBUG3")
|
||||
return logDEBUG3;
|
||||
if (level == "DEBUG2")
|
||||
return logDEBUG2;
|
||||
if (level == "DEBUG1")
|
||||
return logDEBUG1;
|
||||
if (level == "DEBUG")
|
||||
return logDEBUG;
|
||||
if (level == "INFO")
|
||||
return logINFO;
|
||||
if (level == "WARNING")
|
||||
return logWARNING;
|
||||
if (level == "ERROR")
|
||||
return logERROR;
|
||||
Log<T>().Get(logWARNING) << "Unknown logging level '" << level << "'. Using INFO level as default.";
|
||||
return logINFO;
|
||||
}
|
||||
|
||||
|
||||
inline FILE*& Output2FILE::Stream()
|
||||
{
|
||||
static FILE* pStream = stderr;
|
||||
return pStream;
|
||||
}
|
||||
|
||||
inline void Output2FILE::Output(const std::string& msg)
|
||||
{
|
||||
FILE* pStream = Stream();
|
||||
if (!pStream)
|
||||
return;
|
||||
fprintf(pStream, "%s", msg.c_str());
|
||||
fflush(pStream);
|
||||
}
|
||||
|
||||
inline void Output2FILE::Output(const std::string& msg, TLogLevel level)
|
||||
{
|
||||
FILE* pStream = Stream();
|
||||
if (!pStream)
|
||||
return;
|
||||
bool out = true;
|
||||
switch(level){
|
||||
case logERROR: cprintf(RED BOLD,"%s",msg.c_str()); break;
|
||||
case logWARNING: cprintf(YELLOW BOLD,"%s",msg.c_str()); break;
|
||||
case logINFO: cprintf(RESET,"%s",msg.c_str()); break;
|
||||
case logINFOBLUE: cprintf(BLUE,"%s",msg.c_str()); break;
|
||||
case logINFORED: cprintf(RED,"%s",msg.c_str()); break;
|
||||
case logINFOGREEN: cprintf(GREEN,"%s",msg.c_str()); break;
|
||||
default: fprintf(pStream,"%s",msg.c_str()); out = false; break;
|
||||
public:
|
||||
Logger() = default;
|
||||
explicit Logger(TLogLevel level) : level(level){};
|
||||
~Logger() {
|
||||
// output in the destructor to allow for << syntax
|
||||
os << RESET << '\n';
|
||||
std::clog << os.str() << std::flush; // Single write
|
||||
}
|
||||
fflush(out ? stdout : pStream);
|
||||
}
|
||||
|
||||
#include "logger2.h"
|
||||
static TLogLevel &ReportingLevel() { // singelton eeh
|
||||
static TLogLevel reportingLevel = logINFO;
|
||||
return reportingLevel;
|
||||
}
|
||||
|
||||
// Danger this buffer need as many elements as TLogLevel
|
||||
static const char *Color(TLogLevel level) noexcept {
|
||||
static const char *const colors[] = {
|
||||
RED BOLD, YELLOW BOLD, BLUE, GREEN, RED, RESET,
|
||||
RESET, RESET, RESET, RESET, RESET, RESET};
|
||||
return colors[level];
|
||||
}
|
||||
|
||||
// Danger this buffer need as many elements as TLogLevel
|
||||
static std::string ToString(TLogLevel level) {
|
||||
static const char *const buffer[] = {
|
||||
"ERROR", "WARNING", "INFO", "INFO", "INFO", "INFO",
|
||||
"DEBUG", "DEBUG1", "DEBUG2", "DEBUG3", "DEBUG4", "DEBUG5"};
|
||||
return buffer[level];
|
||||
}
|
||||
|
||||
std::ostringstream &Get() {
|
||||
os << Color(level) << "- " << Timestamp() << " " << ToString(level)
|
||||
<< ": ";
|
||||
return os;
|
||||
}
|
||||
|
||||
static std::string Timestamp() {
|
||||
constexpr size_t buffer_len = 12;
|
||||
char buffer[buffer_len];
|
||||
time_t t;
|
||||
::time(&t);
|
||||
tm r;
|
||||
strftime(buffer, buffer_len, "%X", localtime_r(&t, &r));
|
||||
buffer[buffer_len - 1] = '\0';
|
||||
struct timeval tv;
|
||||
gettimeofday(&tv, nullptr);
|
||||
constexpr size_t result_len = 100;
|
||||
char result[result_len];
|
||||
snprintf(result, result_len, "%s.%03ld", buffer,
|
||||
(long)tv.tv_usec / 1000);
|
||||
result[result_len - 1] = '\0';
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
#define LOG(level) \
|
||||
if (level > LOG_MAX_REPORTING_LEVEL) \
|
||||
; \
|
||||
else if (level > sls::Logger::ReportingLevel()) \
|
||||
; \
|
||||
else \
|
||||
sls::Logger(level).Get()
|
||||
|
||||
} // namespace sls
|
||||
|
@ -1,82 +0,0 @@
|
||||
#pragma once
|
||||
/*Utility to log to console*/
|
||||
|
||||
#include "ansi.h" //Colors
|
||||
// #include "logger.h" //for enum, to be removed
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
// Compiler should optimize away anything below this value
|
||||
#ifndef LOG_MAX_REPORTING_LEVEL
|
||||
#define LOG_MAX_REPORTING_LEVEL logINFO
|
||||
#endif
|
||||
|
||||
namespace sls {
|
||||
class Logger {
|
||||
std::ostringstream os;
|
||||
TLogLevel level = LOG_MAX_REPORTING_LEVEL;
|
||||
|
||||
public:
|
||||
Logger() = default;
|
||||
explicit Logger(TLogLevel level) : level(level){};
|
||||
~Logger() {
|
||||
// output in the destructor to allow for << syntax
|
||||
os << RESET << '\n';
|
||||
std::clog << os.str() << std::flush; // Single write
|
||||
}
|
||||
|
||||
static TLogLevel &ReportingLevel() { // singelton eeh
|
||||
static TLogLevel reportingLevel = logINFO;
|
||||
return reportingLevel;
|
||||
}
|
||||
|
||||
// Danger this buffer need as many elements as TLogLevel
|
||||
static const char *Color(TLogLevel level) noexcept {
|
||||
static const char *const colors[] = {
|
||||
RED BOLD, YELLOW BOLD, BLUE, GREEN, RED, RESET,
|
||||
RESET, RESET, RESET, RESET, RESET, RESET};
|
||||
return colors[level];
|
||||
}
|
||||
|
||||
// Danger this buffer need as many elements as TLogLevel
|
||||
static std::string ToString(TLogLevel level) {
|
||||
static const char *const buffer[] = {
|
||||
"ERROR", "WARNING", "INFO", "INFO", "INFO", "INFO",
|
||||
"DEBUG", "DEBUG1", "DEBUG2", "DEBUG3", "DEBUG4", "DEBUG5"};
|
||||
return buffer[level];
|
||||
}
|
||||
|
||||
std::ostringstream &Get() {
|
||||
os << Color(level) << "- " << Timestamp() << " " << ToString(level)
|
||||
<< ": ";
|
||||
return os;
|
||||
}
|
||||
|
||||
std::string Timestamp() {
|
||||
constexpr size_t buffer_len = 12;
|
||||
char buffer[buffer_len];
|
||||
time_t t;
|
||||
::time(&t);
|
||||
tm r;
|
||||
strftime(buffer, buffer_len, "%X", localtime_r(&t, &r));
|
||||
buffer[buffer_len - 1] = '\0';
|
||||
struct timeval tv;
|
||||
gettimeofday(&tv, nullptr);
|
||||
constexpr size_t result_len = 100;
|
||||
char result[result_len];
|
||||
snprintf(result, result_len, "%s.%03ld", buffer,
|
||||
(long)tv.tv_usec / 1000);
|
||||
result[result_len - 1] = '\0';
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
#define LOG(level) \
|
||||
if (level > LOG_MAX_REPORTING_LEVEL) \
|
||||
; \
|
||||
else if (level > sls::Logger::ReportingLevel()) \
|
||||
; \
|
||||
else \
|
||||
sls::Logger(level).Get()
|
||||
|
||||
} // namespace sls
|
@ -9,13 +9,13 @@ namespace sls{
|
||||
struct RuntimeError : public std::runtime_error {
|
||||
public:
|
||||
RuntimeError(): runtime_error("SLS Detector Package Failed") {
|
||||
FILE_LOG(logERROR) << "SLS Detector Package Failed";
|
||||
LOG(logERROR) << "SLS Detector Package Failed";
|
||||
}
|
||||
RuntimeError(const std::string& msg): runtime_error(msg) {
|
||||
FILE_LOG(logERROR) << msg;
|
||||
LOG(logERROR) << msg;
|
||||
}
|
||||
RuntimeError(const char* msg): runtime_error(msg) {
|
||||
FILE_LOG(logERROR) << msg;
|
||||
LOG(logERROR) << msg;
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -107,7 +107,7 @@ int DataSocket::setTimeOut(int t_seconds) {
|
||||
// Receive timeout indefinet
|
||||
if (::setsockopt(getSocketId(), SOL_SOCKET, SO_RCVTIMEO, &t,
|
||||
sizeof(struct timeval)) < 0) {
|
||||
FILE_LOG(logERROR) << "setsockopt SO_RCVTIMEO " << 0;
|
||||
LOG(logERROR) << "setsockopt SO_RCVTIMEO " << 0;
|
||||
}
|
||||
|
||||
t.tv_sec = t_seconds;
|
||||
@ -115,7 +115,7 @@ int DataSocket::setTimeOut(int t_seconds) {
|
||||
// Sending timeout in seconds
|
||||
if (::setsockopt(getSocketId(), SOL_SOCKET, SO_SNDTIMEO, &t,
|
||||
sizeof(struct timeval)) < 0) {
|
||||
FILE_LOG(logERROR) << "setsockopt SO_SNDTIMEO " << t_seconds;
|
||||
LOG(logERROR) << "setsockopt SO_SNDTIMEO " << t_seconds;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
@ -12,7 +12,7 @@ int ServerInterface::sendResult(int ret, void *retval, int retvalSize,
|
||||
if (mess != nullptr) {
|
||||
write(mess, MAX_STR_LENGTH);
|
||||
} else {
|
||||
FILE_LOG(logERROR) << "No error message provided for this "
|
||||
LOG(logERROR) << "No error message provided for this "
|
||||
"failure. Will mess up TCP\n";
|
||||
}
|
||||
} else {
|
||||
|
@ -46,7 +46,7 @@ int readDataFile(std::string fname, short int *data, int nch) {
|
||||
iline=readDataFile(infile, data, nch, 0);
|
||||
infile.close();
|
||||
} else {
|
||||
FILE_LOG(logERROR) << "Could not read file " << fname;
|
||||
LOG(logERROR) << "Could not read file " << fname;
|
||||
return -1;
|
||||
}
|
||||
return iline;
|
||||
@ -73,7 +73,7 @@ int writeDataFile(std::string fname,int nch, short int *data) {
|
||||
outfile.close();
|
||||
return slsDetectorDefs::OK;
|
||||
} else {
|
||||
FILE_LOG(logERROR) << "Could not open file " << fname << "for writing";
|
||||
LOG(logERROR) << "Could not open file " << fname << "for writing";
|
||||
return slsDetectorDefs::FAIL;
|
||||
}
|
||||
}
|
||||
|
@ -14,7 +14,7 @@
|
||||
#include <sys/prctl.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include <unistd.h>
|
||||
#include "network_utils.h"
|
||||
|
||||
namespace sls {
|
||||
|
Reference in New Issue
Block a user