Commit 88477545 authored by Eric Cano's avatar Eric Cano
Browse files

Catch up with master branch

parents 25307b53 5d860ddf
......@@ -38,79 +38,80 @@
#include <fcntl.h>
#include <stdio.h>
namespace castor {
namespace log {
namespace {
/**
* Default size of a syslog message.
*/
static const size_t DEFAULT_SYSLOG_MSGLEN = 1024;
const size_t DEFAULT_SYSLOG_MSGLEN = 1024;
/**
* Default size of a rsyslog message.
*/
static const size_t DEFAULT_RSYSLOG_MSGLEN = 2000;
const size_t DEFAULT_RSYSLOG_MSGLEN = 2000;
/**
* Maximum length of a parameter name.
*/
static const size_t LOG_MAX_PARAMNAMELEN = 20;
const size_t LOG_MAX_PARAMNAMELEN = 20;
/**
* Maximum length of a string value.
*/
static size_t LOG_MAX_PARAMSTRLEN = 1024;
const size_t LOG_MAX_PARAMSTRLEN = 1024;
/**
* Maximum length of an ident/facility.
* Maximum length of the program name to be prepended to every log message.
*/
static const size_t LOG_MAX_IDENTLEN = 20;
const size_t LOG_MAX_PROGNAMELEN = 20;
/**
* Maximum length of a log message.
*/
static size_t LOG_MAX_LINELEN = 8192;
const size_t LOG_MAX_LINELEN = 8192;
/**
* True if the interface of the CASTOR logging system has been initialized.
*/
static bool s_initialized = false;
bool s_initialized = false;
/**
* The file descriptor of the log.
*/
static int s_logFile = -1;
int s_logFile = -1;
/**
* AF_UNIX address of local logger.
*/
static struct sockaddr_un s_syslogAddr;
struct sockaddr_un s_syslogAddr;
/**
* True if the the syslog connect has bveen done.
*/
static bool s_connected = false;
bool s_connected = false;
/**
* Mutex used to synchronize syslog client.
*/
static pthread_mutex_t s_syslogMutex;
pthread_mutex_t s_syslogMutex;
/**
* ident/facility.
* The name of the program to be prepended to every log message.
*/
static const char* s_progname = 0;
const char* s_progname = 0;
/**
* The maximum message size that the client syslog server can handle.
*/
static int s_maxmsglen = DEFAULT_SYSLOG_MSGLEN;
int s_maxmsglen = DEFAULT_SYSLOG_MSGLEN;
static struct {
struct PrioritySyslogMapping {
const char *name; // Name of the priority
int value; // The priority's numeric representation in syslog
const char *text; // Textual representation of the priority
} s_prioritylist[] = {
};
PrioritySyslogMapping s_prioritylist[] = {
{ "LOG_EMERG", LOG_EMERG, "Emerg" },
{ "LOG_ALERT", LOG_ALERT, "Alert" },
{ "LOG_CRIT", LOG_CRIT, "Crit" },
......@@ -125,7 +126,7 @@ static struct {
//-----------------------------------------------------------------------------
// castor_openlog
//-----------------------------------------------------------------------------
static void castor_openlog() {
void castor_openlog() {
if(-1 == s_logFile) {
s_syslogAddr.sun_family = AF_UNIX;
strncpy(s_syslogAddr.sun_path, _PATH_LOG, sizeof(s_syslogAddr.sun_path));
......@@ -157,86 +158,10 @@ 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) > LOG_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 <= LOG_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
//-----------------------------------------------------------------------------
static void castor_closelog() {
void castor_closelog() {
if (!s_connected) return;
close (s_logFile);
s_logFile = -1;
......@@ -246,7 +171,7 @@ static void castor_closelog() {
//-----------------------------------------------------------------------------
// castor_syslog
//-----------------------------------------------------------------------------
static void castor_syslog(const char *const msg, const int msgLen) throw() {
void castor_syslog(const char *const msg, const int msgLen) throw() {
int send_flags = 0;
#ifndef __APPLE__
// MAC has has no MSG_NOSIGNAL
......@@ -281,7 +206,7 @@ static void castor_syslog(const char *const msg, const int msgLen) throw() {
//-----------------------------------------------------------------------------
// build_syslog_header
//-----------------------------------------------------------------------------
static int build_syslog_header(char *const buffer,
int build_syslog_header(char *const buffer,
const int buflen,
const int priority,
const struct timeval &timeStamp,
......@@ -306,7 +231,7 @@ static int build_syslog_header(char *const buffer,
//-----------------------------------------------------------------------------
// _clean_string
//-----------------------------------------------------------------------------
static std::string _clean_string(const std::string &s, const bool underscore) {
std::string _clean_string(const std::string &s, const bool underscore) {
char *str = strdup(s.c_str());
// Return an empty string if the strdup() failed
......@@ -351,8 +276,83 @@ static std::string _clean_string(const std::string &s, const bool underscore) {
return result;
}
} // namespace log
} // namespace castor
} // unnamed namespace
//-----------------------------------------------------------------------------
// initLog
//-----------------------------------------------------------------------------
int castor::log::initLog(const char *const progname) {
/* 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 progname is too big */
if (strlen(progname) > LOG_MAX_PROGNAMELEN) {
errno = EINVAL;
return (-1);
}
s_progname = progname;
/* 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 <= LOG_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);
}
//-----------------------------------------------------------------------------
// writeMsg
......
......@@ -50,8 +50,8 @@ namespace log {
/**
* 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.
* @param progname The name of the program to be prepended to every log
* message.
*
* @return On success zero is returned, On error, -1 is returned, and errno is
* set appropriately.
......@@ -63,7 +63,7 @@ namespace log {
*
* @see openlog(), setlogmask()
*/
int initLog (const char *ident);
int initLog (const char *progname);
/**
* Writes a message into the CASTOR logging system. Note that no exception
......
......@@ -25,17 +25,152 @@
// Include Files
#include <time.h>
#include <sstream>
#include <string.h>
#include <iomanip>
#include "castor/log/Param.hpp"
#include "castor/ObjectSet.hpp"
#include "castor/IObject.hpp"
#include <string.h>
//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
castor::log::Param::Param(const std::string &name, const std::string &value)
throw():
m_name(name),
m_type(name == "TPVID" ? LOG_MSG_PARAM_TPVID : LOG_MSG_PARAM_STR),
m_strValue(value), m_intValue(0), m_uint64Value(0), m_doubleValue(0.0),
m_uuidValue(nullCuuid) {
}
//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
castor::log::Param::Param(const std::string &name, const Cuuid_t value) throw():
m_name(name), m_type(LOG_MSG_PARAM_UUID),
m_strValue(), m_intValue(0), m_uint64Value(0), m_doubleValue(0.0),
m_uuidValue(value) {
}
//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
castor::log::Param::Param(const Cuuid_t &value) throw() :
m_name(""), m_type(LOG_MSG_PARAM_UUID),
m_strValue(), m_intValue(0), m_uint64Value(0), m_doubleValue(0.0),
m_uuidValue(value) {
}
//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
#if defined __x86_64__
castor::log::Param::Param(const std::string &name, const long int value)
throw():
m_name(name), m_type(LOG_MSG_PARAM_INT64),
m_strValue(), m_intValue(0), m_uint64Value(value), m_doubleValue(0.0),
m_uuidValue(nullCuuid) {
}
#else
castor::log::Param::Param(const std::string &name, const long int value)
throw():
m_name(name), m_type(LOG_MSG_PARAM_INT),
m_strValue(), m_intValue(value), m_uint64Value(0), m_doubleValue(0.0),
m_uuidValue(nullCuuid) {
}
#endif
//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
#if defined __x86_64__
castor::log::Param::Param(const std::string &name,
const long unsigned int value) throw():
m_name(name), m_type(LOG_MSG_PARAM_INT64),
m_strValue(), m_intValue(0), m_uint64Value(value), m_doubleValue(0.0),
m_uuidValue(nullCuuid) {
}
#else
castor::log::Param::Param(const std::string &name,
const long unsigned int value) throw():
m_name(name), m_type(LOG_MSG_PARAM_INT),
m_strValue(), m_intValue(value), m_uint64Value(0), m_doubleValue(0.0),
m_uuidValue(nullCuuid) {
}
#endif
//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
castor::log::Param::Param(const std::string &name, const int value) throw():
m_name(name), m_type(LOG_MSG_PARAM_INT),
m_strValue(), m_intValue(value), m_uint64Value(0), m_doubleValue(0.0),
m_uuidValue(nullCuuid) {
}
//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
castor::log::Param::Param(const std::string &name, const unsigned int value)
throw():
m_name(name), m_type(LOG_MSG_PARAM_INT),
m_strValue(), m_intValue(value), m_uint64Value(0), m_doubleValue(0.0),
m_uuidValue(nullCuuid) {
}
//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
castor::log::Param::Param(const std::string &name, const u_signed64 value)
throw():
m_name(name), m_type(LOG_MSG_PARAM_INT64),
m_strValue(), m_intValue(0), m_uint64Value(value), m_doubleValue(0.0),
m_uuidValue(nullCuuid) {
}
//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
castor::log::Param::Param(const std::string &name, const float value)
throw():
m_name(name), m_type(LOG_MSG_PARAM_DOUBLE),
m_strValue(), m_intValue(0), m_uint64Value(0), m_doubleValue(value),
m_uuidValue(nullCuuid) {
}
//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
castor::log::Param::Param(const std::string &name, const double value)
throw():
m_name(name), m_type(LOG_MSG_PARAM_DOUBLE),
m_strValue(), m_intValue(0), m_uint64Value(0), m_doubleValue(value),
m_uuidValue(nullCuuid) {
}
//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
castor::log::Param::Param(const std::string &name,
const castor::stager::TapeVid &value) throw():
m_name(name), m_type(LOG_MSG_PARAM_TPVID),
m_strValue(0 != value.vid() ? value.vid() : ""), m_intValue(0),
m_uint64Value(0), m_doubleValue(0.0), m_uuidValue(nullCuuid) {
}
//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
castor::log::Param::Param(const std::string &rawParams) throw():
m_name(""), m_type(LOG_MSG_PARAM_RAW),
m_strValue(rawParams), m_intValue(0), m_uint64Value(0), m_doubleValue(0.0),
m_uuidValue(nullCuuid) {
};
//-----------------------------------------------------------------------------
// Constructor
//-----------------------------------------------------------------------------
castor::log::Param::Param(const std::string &name,
const castor::IObject *const value) :
const castor::IObject *const value) throw():
m_name(name), m_type(LOG_MSG_PARAM_STR),
m_intValue(0), m_uint64Value(0), m_doubleValue(0.0), m_uuidValue(nullCuuid) {
std::ostringstream oss;
......@@ -48,7 +183,7 @@ castor::log::Param::Param(const std::string &name,
// Constructor
//-----------------------------------------------------------------------------
castor::log::Param::Param(const std::string &name,
const castor::log::IPAddress &value) :
const castor::log::IPAddress &value) throw():
m_name(name), m_type(LOG_MSG_PARAM_STR),
m_intValue(0), m_uint64Value(0), m_doubleValue(0.0), m_uuidValue(nullCuuid) {
std::ostringstream oss;
......@@ -63,7 +198,7 @@ castor::log::Param::Param(const std::string &name,
// Constructor
//-----------------------------------------------------------------------------
castor::log::Param::Param(const std::string &name,
const castor::log::TimeStamp &value) :
const castor::log::TimeStamp &value) throw():
m_name(name), m_type(LOG_MSG_PARAM_STR),
m_intValue(0), m_uint64Value(0), m_doubleValue(0.0), m_uuidValue(nullCuuid) {
time_t time = value.time();
......@@ -79,3 +214,72 @@ castor::log::Param::Param(const std::string &name,
<< ":" << tmstruc.tm_sec;
m_strValue = oss.str();
}
//------------------------------------------------------------------------------
// getName
//------------------------------------------------------------------------------
const std::string &castor::log::Param::getName() const throw() {
return m_name;
}
//------------------------------------------------------------------------------
// getType
//------------------------------------------------------------------------------
int castor::log::Param::getType() const throw() {
return m_type;
}
//------------------------------------------------------------------------------
// getStrValue
//------------------------------------------------------------------------------
const std::string &castor::log::Param::getStrValue() const throw() {
if(LOG_MSG_PARAM_STR == m_type) {
return m_strValue;
} else {
return m_emptyStr;
}
}
//------------------------------------------------------------------------------
// getIntValue
//------------------------------------------------------------------------------
int castor::log::Param::getIntValue() const throw() {
if(LOG_MSG_PARAM_INT == m_type) {
return m_intValue;
} else {
return 0;
}
}
//------------------------------------------------------------------------------
// getUint64Value
//------------------------------------------------------------------------------
u_signed64 castor::log::Param::getUint64Value() const throw() {
if(LOG_MSG_PARAM_INT64 == m_type) {
return m_uint64Value;
} else {
return (u_signed64)0;
}
}
//------------------------------------------------------------------------------
// getDoubleValue
//------------------------------------------------------------------------------
double castor::log::Param::getDoubleValue() const throw() {
if(LOG_MSG_PARAM_DOUBLE == m_type) {
return m_doubleValue;
} else {
return (double)0.0;
}
}
//------------------------------------------------------------------------------
// getUuidValue
//------------------------------------------------------------------------------
const Cuuid_t &castor::log::Param::getUuidValue() const throw() {
if(LOG_MSG_PARAM_UUID == m_type) {
return m_uuidValue;
} else {
return nullCuuid;
}
}
......@@ -61,215 +61,124 @@ class Param {
public:
/**
* Constructor for strings
* Constructor for strings.
*/
Param(const std::string &name, const std::string &value) :
m_name(name),
m_type(name == "TPVID" ? LOG_MSG_PARAM_TPVID : LOG_MSG_PARAM_STR),
m_strValue(value), m_intValue(0), m_uint64Value(0), m_doubleValue(0.0),
m_uuidValue(nullCuuid) {
}
Param(const std::string &name, const std::string &value) throw();
/**
* Constructor for uuids
* Constructor for uuids.
*/
Param(const std::string &name, const Cuuid_t value) :
m_name(name), m_type(LOG_MSG_PARAM_UUID),
m_strValue(), m_intValue(0), m_uint64Value(0), m_doubleValue(0.0),
m_uuidValue(value) {
}
Param(const std::string &name, const Cuuid_t value) throw();
/**
* Constructor for SubRequest uuids
* Constructor for SubRequest uuids.
*/
Param(const Cuuid_t value) :
m_name(""), m_type(LOG_MSG_PARAM_UUID),
m_strValue(), m_intValue(0), m_uint64Value(0), m_doubleValue(0.0),
m_uuidValue(value) {
}
Param(const Cuuid_t &value) throw();
/**
* Constructor for long int
* Constructor for long int.
*/
#if defined __x86_64__
Param(const std::string &name, const long int value) :
m_name(name), m_type(LOG_MSG_PARAM_INT64),
m_strValue(), m_intValue(0), m_uint64Value(value), m_doubleValue(0.0),
m_uuidValue(nullCuuid) {
}
Param(const std::string &name, const long int value) throw();
#else
Param(const std::string &name, const long int value) :
m_name(name), m_type(LOG_MSG_PARAM_INT),