Commit 86faf9bf authored by Eric Cano's avatar Eric Cano
Browse files

Catch up with master branch.

parents 00d0824f dd202a9a
......@@ -17,8 +17,6 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* @(#)Dlf.cpp,v 1.1 $Release$ 2005/04/05 11:51:33 sponcec3
*
* Interface to the CASTOR logging system
*
* @author Sebastien Ponce
......@@ -26,7 +24,9 @@
// Include Files
#include "castor/log/Log.hpp"
#include "h/getconfent.h"
#include <errno.h>
#include <pthread.h>
#include <string.h>
#include <sys/un.h>
......@@ -38,6 +38,41 @@
namespace castor {
namespace log {
/**
* Default size of a syslog message.
*/
static const size_t DEFAULT_SYSLOG_MSGLEN = 1024;
/**
* Default size of a rsyslog message.
*/
static const size_t DEFAULT_RSYSLOG_MSGLEN = 2000;
/**
* Maximum length of a parameter name.
*/
static const size_t LOG_MAX_PARAMNAMELEN = 20;
/**
* Maximum length of a string value.
*/
//static size_t LOG_MAX_PARAMSTRLEN = 1024;
/**
* Maximum length of an ident/facility.
*/
static const size_t CASTOR_MAX_IDENTLEN = 20;
/**
* Maximum length of a log message.
*/
static size_t CASTOR_MAX_LINELEN = 8192;
/**
* True if the interface of the CASTOR logging system has been initialized.
*/
static bool s_initialized = false;
/**
* The file descriptor of the log.
*/
......@@ -58,6 +93,16 @@ static bool s_connected = false;
*/
static pthread_mutex_t s_syslogMutex;
/**
* ident/facility.
*/
static const char* s_progname = 0;
/**
* The maximum message size that the client syslog server can handle.
*/
static int s_maxmsglen = DEFAULT_SYSLOG_MSGLEN;
//-----------------------------------------------------------------------------
// castor_openlog
//-----------------------------------------------------------------------------
......@@ -93,6 +138,83 @@ static void castor_openlog() {
}
}
//-----------------------------------------------------------------------------
// initLog
//-----------------------------------------------------------------------------
int initLog(const char *ident) {
/* Variables */
FILE *fp = NULL;
char *p;
char buffer[1024];
size_t size = 0;
/* Check if already initialized */
if (s_initialized) {
errno = EPERM;
return (-1);
}
/* Check if the ident is too big */
if (strlen(ident) > CASTOR_MAX_IDENTLEN) {
errno = EINVAL;
return (-1);
}
s_progname = ident;
/* Determine the maximum message size that the client syslog server can
* handle.
*/
if ((p = getconfent("LOG", "MaxMessageSize", 0)) != NULL) {
size = atoi(p);
} else {
/* Determine the size automatically, this is not guaranteed to work! */
fp = fopen("/etc/rsyslog.conf", "r");
if (fp) {
/* The /etc/rsyslog.conf file exists so we assume the default message
* size of 2K.
*/
s_maxmsglen = DEFAULT_RSYSLOG_MSGLEN;
/* In rsyslog versions >= 3.21.4, the maximum size of a message became
* configurable through the $MaxMessageSize global config directive.
* Here we attempt to find out if the user has increased the size!
*/
while (fgets(buffer, sizeof(buffer), fp) != NULL) {
if (strncasecmp(buffer, "$MaxMessageSize", 15)) {
continue; /* Option not of interest */
}
size = atol(&buffer[15]);
}
fclose(fp);
} else {
/* The /etc/rsyslog.conf file is missing which implies that we are
* running on a stock syslogd system, therefore the message size is
* governed by the syslog RFC: http://www.faqs.org/rfcs/rfc3164.html
*/
}
}
/* Check that the size of messages falls within acceptable limits */
if ((size >= DEFAULT_SYSLOG_MSGLEN) &&
(size <= CASTOR_MAX_LINELEN)) {
s_maxmsglen = size;
}
/* create the syslog serialization lock */
if (pthread_mutex_init(&s_syslogMutex, NULL)) {
errno = ENOMEM;
return (-1);
}
/* Open syslog */
castor_openlog();
s_initialized = 1;
return (0);
}
//-----------------------------------------------------------------------------
// castor_closelog
//-----------------------------------------------------------------------------
......
......@@ -29,90 +29,119 @@
// Include Files
#include "castor/log/Param.hpp"
#include <syslog.h>
#include <sys/time.h>
namespace castor {
/* Priority levels */
#define LOG_LVL_EMERGENCY LOG_EMERG /* System is unusable */
#define LOG_LVL_ALERT LOG_ALERT /* Action must be taken immediately */
#define LOG_LVL_CRIT LOG_CRIT /* Critical conditions */
#define LOG_LVL_ERROR LOG_ERR /* Error conditions */
#define LOG_LVL_WARNING LOG_WARNING /* Warning conditions */
#define LOG_LVL_USER_ERROR LOG_NOTICE /* Normal but significant condition */
#define LOG_LVL_AUTH LOG_NOTICE
#define LOG_LVL_SECURITY LOG_NOTICE
#define LOG_LVL_SYSTEM LOG_INFO /* Informational */
#define LOG_LVL_DEBUG LOG_DEBUG /* Debug-level messages */
namespace log {
namespace castor {
namespace log {
/**
* Writes a message into the CASTOR logging system. Note that no exception
* will ever be thrown in case of failure. Failures will actually be
* silently ignored in order to not impact the processing.
*
* Note that this version of writeMsg() allows the caller to specify the
* time stamp of the log message.
*
* @param severity the severity of the message.
* @param msg the message.
* @param numParams the number of parameters in the message.
* @param params the parameters of the message, given as an array.
* @param timeStamp the time stamp of the log message.
*/
void writeMsg(const int severity,
const std::string &msg,
const int numParams,
const castor::log::Param params[],
const struct timeval &timeStamp) throw();
/**
* Initialize the CASTOR logging interface.
*
* @param ident The ident argument is a string that is prepended to every
* log message and identifiers the source application.
*
* @return On success zero is returned, On error, -1 is returned, and errno is
* set appropriately.
*
* Possible errors include:
* - EPERM The interface is already initialized!
* - EINVAL Invalid argument (refers to errors in parsing the LogMask
* configuration option in castor.conf for the daemon)
*
* @see openlog(), setlogmask()
*/
int initLog (const char *ident);
/**
* A template function that wraps writeMsg in order to get the compiler
* to automatically determine the size of the params parameter, therefore
*
* Note that this version of writeMsg() allows the caller to specify the
* time stamp of the log message.
*
* @param msg the message.
* @param params the parameters of the message, given as an array.
* @param timeStamp the time stamp of the log message.
*/
template<int numParams>
void writeMsg(const int severity,
const std::string &msg,
castor::log::Param(&params)[numParams],
const struct timeval &timeStamp) throw() {
writeMsg(severity, msg, numParams, params, timeStamp);
}
/**
* Writes a message into the CASTOR logging system. Note that no exception
* will ever be thrown in case of failure. Failures will actually be
* silently ignored in order to not impact the processing.
*
* Note that this version of writeMsg() allows the caller to specify the
* time stamp of the log message.
*
* @param severity the severity of the message.
* @param msg the message.
* @param numParams the number of parameters in the message.
* @param params the parameters of the message, given as an array.
* @param timeStamp the time stamp of the log message.
*/
void writeMsg(const int severity,
const std::string &msg,
const int numParams,
const Param params[],
const struct timeval &timeStamp) throw();
/**
* Writes a message into the CASTOR logging system. Note that no exception
* will ever be thrown in case of failure. Failures will actually be
* silently ignored in order to not impact the processing.
*
* Note that this version of writeMsg() implicitly uses the current time as
* the time stamp of the message.
*
* @param severity the severity of the message.
* @param msg the message.
* @param numParams the number of parameters in the message.
* @param params the parameters of the message, given as an array.
*/
void writeMsg(const int severity,
const std::string &msg,
const int numParams,
const castor::log::Param params[]) throw();
/**
* A template function that wraps writeMsg in order to get the compiler
* to automatically determine the size of the params parameter, therefore
*
* Note that this version of writeMsg() allows the caller to specify the
* time stamp of the log message.
*
* @param msg the message.
* @param params the parameters of the message, given as an array.
* @param timeStamp the time stamp of the log message.
*/
template<int numParams>
void writeMsg(const int severity,
const std::string &msg,
castor::log::Param(&params)[numParams],
const struct timeval &timeStamp) throw() {
writeMsg(severity, msg, numParams, params, timeStamp);
}
/**
* A template function that wraps writeMsg in order to get the compiler
* to automatically determine the size of the params parameter, therefore
* removing the need for the devloper to provide it explicity.
*
* Note that this version of writeMsg() implicitly uses the current time as
* the time stamp of the message.
*
* @param severity the severity of the message.
* @param msg the message.
* @param params the parameters of the message, given as an array.
*/
template<int numParams>
void writeMsg(const int severity,
const std::string &msg,
castor::log::Param(&params)[numParams]) throw() {
writeMsg(severity, msg, numParams, params);
}
/**
* Writes a message into the CASTOR logging system. Note that no exception
* will ever be thrown in case of failure. Failures will actually be
* silently ignored in order to not impact the processing.
*
* Note that this version of writeMsg() implicitly uses the current time as
* the time stamp of the message.
*
* @param severity the severity of the message.
* @param msg the message.
* @param numParams the number of parameters in the message.
* @param params the parameters of the message, given as an array.
*/
void writeMsg(const int severity,
const std::string &msg,
const int numParams,
const castor::log::Param params[]) throw();
} // end of namespace log
/**
* A template function that wraps writeMsg in order to get the compiler
* to automatically determine the size of the params parameter, therefore
* removing the need for the devloper to provide it explicity.
*
* Note that this version of writeMsg() implicitly uses the current time as
* the time stamp of the message.
*
* @param severity the severity of the message.
* @param msg the message.
* @param params the parameters of the message, given as an array.
*/
template<int numParams>
void writeMsg(const int severity,
const std::string &msg,
castor::log::Param(&params)[numParams]) throw() {
writeMsg(severity, msg, numParams, params);
}
} // end of namespace castor
} // namespace log
} // namespace castor
#endif // CASTOR_LOG_LOG_HPP
......@@ -17,8 +17,6 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* @(#)Param.cpp,v 1.2 $Release$ 2005/04/05 13:36:36 sponcec3
*
* A parameter for the CASTOR logging system
*
* @author castor dev team
......@@ -40,7 +38,7 @@ castor::log::Param::Param(const char* name,
castor::IObject* value) :
m_deallocate(true) {
m_cParam.name = (char*) name;
m_cParam.type = DLF_MSG_PARAM_STR;
m_cParam.type = LOG_MSG_PARAM_STR;
std::ostringstream s;
castor::ObjectSet set;
value->print(s, "", set);
......@@ -54,7 +52,7 @@ castor::log::Param::Param(const char* name,
castor::log::IPAddress value) :
m_deallocate(true) {
m_cParam.name = (char*) name;
m_cParam.type = DLF_MSG_PARAM_STR;
m_cParam.type = LOG_MSG_PARAM_STR;
std::ostringstream s;
s << ((value.ip() & 0xFF000000) >> 24) << "."
<< ((value.ip() & 0x00FF0000) >> 16) << "."
......@@ -70,7 +68,7 @@ castor::log::Param::Param(const char* name,
castor::log::TimeStamp value) :
m_deallocate(true) {
m_cParam.name = (char*) name;
m_cParam.type = DLF_MSG_PARAM_STR;
m_cParam.type = LOG_MSG_PARAM_STR;
time_t time = value.time();
// There is NO localtime_r() on Windows,
// so we will use non-reentrant version localtime().
......
......@@ -27,220 +27,248 @@
#define CASTOR_LOG_PARAM_HPP 1
// Include Files
#include "dlf_api.h"
#include "castor/IObject.hpp"
#include "castor/stager/TapeVid.hpp"
#include "castor/log/IPAddress.hpp"
#include "castor/log/TimeStamp.hpp"
#include "h/Cuuid.h"
#include <string.h>
#include <stdlib.h>
namespace castor {
/* Parameter types */
#define LOG_MSG_PARAM_DOUBLE 1 /* Double precision floating point value */
#define LOG_MSG_PARAM_INT64 2 /* 64 bit integers */
#define LOG_MSG_PARAM_STR 3 /* General purpose string */
#define LOG_MSG_PARAM_TPVID 4 /* Tape visual identifier string */
#define LOG_MSG_PARAM_UUID 5 /* Subrequest identifier */
#define LOG_MSG_PARAM_FLOAT 6 /* Single precision floating point value */
#define LOG_MSG_PARAM_INT 7 /* Integer parameter */
#define LOG_MSG_PARAM_UID 8
#define LOG_MSG_PARAM_GID 9
#define LOG_MSG_PARAM_STYPE 10
#define LOG_MSG_PARAM_SNAME 11
#define LOG_MSG_PARAM_RAW 12 /* raw (set of) parameters, in key=value format */
namespace log {
/**
* A parameter for the DLF.
*/
class Param {
public:
/**
* Constructor for strings
*/
Param(const char* name, std::string value) :
m_deallocate(true) {
m_cParam.name = (char*) name;
m_cParam.type = DLF_MSG_PARAM_STR;
if (!strcmp(name, "TPVID")) {
m_cParam.type = DLF_MSG_PARAM_TPVID;
}
m_cParam.value.par_string = strdup(value.c_str());
};
/**
* Constructor for C strings
*/
Param(const char* name, const char* value) :
m_deallocate(true) {
m_cParam.name = (char*) name;
m_cParam.type = DLF_MSG_PARAM_STR;
if (!strcmp(name, "TPVID")) {
m_cParam.type = DLF_MSG_PARAM_TPVID;
}
if (0 != value) {
m_cParam.value.par_string = strdup(value);
} else {
m_cParam.value.par_string = 0;
}
};
/**
* Constructor for uuids
*/
Param(const char* name, Cuuid_t value) :
m_deallocate(false) {
m_cParam.name = (char*) name;
m_cParam.type = DLF_MSG_PARAM_UUID;
m_cParam.value.par_uuid = value;
};
/**
* Constructor for SubRequest uuids
*/
Param(Cuuid_t value) :
m_deallocate(false) {
m_cParam.name = NULL;
m_cParam.type = DLF_MSG_PARAM_UUID;
m_cParam.value.par_uuid = value;
};
/**
* Constructor for int
*/
Param(const char* name, const long int value) :
m_deallocate(false) {
m_cParam.name = (char*) name;
m_cParam.type = DLF_MSG_PARAM_INT;
m_cParam.value.par_int = value;
};
/**
* Constructor for int
*/
Param(const char* name, const long unsigned int value) :
m_deallocate(false) {
m_cParam.name = (char*) name;
namespace castor {
namespace log {
/**
* Provided for C++ users. Refer to: castor/log/Param.hpp
*/
struct castor_write_param_t {
char *name; /* Name of the parameter */
int type; /* Parameter type, one of LOG_MSG_PARAM_* */
union {
char *par_string; /* Value for type LOG_PARAM_STRING */
int par_int; /* Value for type LOG_PARAM_INT */
u_signed64 par_u64; /* Value for type LOG_PARAM_INT64 */
double par_double; /* Value for type LOG_PARAM_DOUBLE */
Cuuid_t par_uuid; /* Value for type LOG_PARAM_UUID */
} value;
};
/**
* A parameter for the CASTOR logging system.
*/
class Param {
public:
/**
* Constructor for strings
*/
Param(const char* name, std::string value) :
m_deallocate(true) {
m_cParam.name = (char*) name;
m_cParam.type = LOG_MSG_PARAM_STR;
if (!strcmp(name, "TPVID")) {
m_cParam.type = LOG_MSG_PARAM_TPVID;
}
m_cParam.value.par_string = strdup(value.c_str());
};
/**
* Constructor for C strings
*/
Param(const char* name, const char* value) :
m_deallocate(true) {
m_cParam.name = (char*) name;
m_cParam.type = LOG_MSG_PARAM_STR;
if (!strcmp(name, "TPVID")) {
m_cParam.type = LOG_MSG_PARAM_TPVID;
}
if (0 != value) {
m_cParam.value.par_string = strdup(value);
} else {
m_cParam.value.par_string = 0;
}
};
/**
* Constructor for uuids
*/
Param(const char* name, Cuuid_t value) :
m_deallocate(false) {
m_cParam.name = (char*) name;
m_cParam.type = LOG_MSG_PARAM_UUID;
m_cParam.value.par_uuid = value;
};
/**
* Constructor for SubRequest uuids
*/
Param(Cuuid_t value) :
m_deallocate(false) {
m_cParam.name = NULL;
m_cParam.type = LOG_MSG_PARAM_UUID;
m_cParam.value.par_uuid = value;
};
/**
* Constructor for int
*/
Param(const char* name, const long int value) :
m_deallocate(false) {
m_cParam.name = (char*) name;
m_cParam.type = LOG_MSG_PARAM_INT;
m_cParam.value.par_int = value;
};
/**
* Constructor for int
*/
Param(const char* name, const long unsigned int value) :
m_deallocate(false) {
m_cParam.name = (char*) name;
#if defined __x86_64__
m_cParam.type = DLF_MSG_PARAM_INT64;
m_cParam.value.par_u64 = value;
m_cParam.type = LOG_MSG_PARAM_INT64;
m_cParam.value.par_u64 = value;
#else
m_cParam.type = DLF_MSG_PARAM_INT;
m_cParam.value.par_int = value;
m_cParam.type = LOG_MSG_PARAM_INT;
m_cParam.value.par_int = value;
#endif
};
/**
* Constructor for int
*/
Param(const char* name, const int value) :
m_deallocate(false) {
m_cParam.name = (char*) name;
m_cParam.type = DLF_MSG_PARAM_INT;
m_cParam.value.par_int = value;
};
/**
* Constructor for int
*/
Param(const char* name, const unsigned int value) :
m_deallocate(false) {
m_cParam.name = (char*) name;
m_cParam.type = DLF_MSG_PARAM_INT;
m_cParam.value.par_int = value;
};
/**
* Constructor for u_signed64
*/
Param(const char* name, u_signed64 value) :
m_deallocate(false) {
m_cParam.name = (char*) name;
m_cParam.type = DLF_MSG_PARAM_INT64;
m_cParam.value.par_u64 = value;
};
/**
* Constructor for floats
*/
Param(const char* name, float value) :