Commit 22f94b8d authored by Steven Murray's avatar Steven Murray
Browse files

This commit respresents the main simplification to the CASTOR logging facility.

The castor::log::Param class simply contains an std::string name and an
std::string value.  The new templated constructor of the Param class uses
std::ostringstream to convert a non-string value to a string one.
parent 7168aaab
......@@ -36,66 +36,6 @@ namespace log {
*/
const size_t LOG_MAX_PROGNAMELEN = 20;
/**
* Parameter type: Double precision floating point value.
*/
const int LOG_MSG_PARAM_DOUBLE = 1;
/**
* Parameter type: 64 bit integer.
*/
const int LOG_MSG_PARAM_INT64 = 2;
/**
* Parameter type: General purpose string.
*/
const int LOG_MSG_PARAM_STR = 3;
/**
* Parameter type: Tape volume identifier string.
*/
const int LOG_MSG_PARAM_TPVID = 4;
/**
* Parameter type: Subrequest identifier.
*/
const int LOG_MSG_PARAM_UUID = 5;
/**
* Parameter type: Single precision floating point value.
*/
const int LOG_MSG_PARAM_FLOAT = 6;
/**
* Parameter type: Integer.
*/
const int LOG_MSG_PARAM_INT = 7;
/**
* Parameter type: User identifier as in getuid().
*/
const int LOG_MSG_PARAM_UID = 8;
/**
* Parameter type: Group identifier as in getgid().
*/
const int LOG_MSG_PARAM_GID = 9;
/**
* Parameter type: STYPE?
*/
const int LOG_MSG_PARAM_STYPE = 10;
/**
* Parameter type: SNAME?
*/
const int LOG_MSG_PARAM_SNAME = 11;
/**
* Parameter type: Raw (set of) parameters, in key=value format.
*/
const int LOG_MSG_PARAM_RAW = 12;
} // namespace log
} // namespace castor
......
......@@ -289,69 +289,13 @@ void castor::log::LoggerImplementation::logMsg(
const std::string name = param.getName() == "" ? "Undefined" :
cleanString(param.getName(), true);
// Process the data type associated with the parameter
switch(params[i].getType()) {
// Strings
case LOG_MSG_PARAM_TPVID:
case LOG_MSG_PARAM_STR:
{
const std::string value = cleanString(param.getStrValue(), false);
if(LOG_MSG_PARAM_TPVID == param.getType()) {
len += snprintf(buffer + len, m_maxMsgLen - len, "TPVID=%.*s ",
CA_MAXVIDLEN, value.c_str());
} else {
len += snprintf(buffer + len, m_maxMsgLen - len, "%.*s=\"%.*s\" ",
(int)LOG_MAX_PARAMNAMELEN, name.c_str(),
(int)LOG_MAX_PARAMSTRLEN, value.c_str());
}
}
break;
// Numerical values
case LOG_MSG_PARAM_INT:
len += snprintf(buffer + len, m_maxMsgLen - len, "%.*s=%d ",
(int)LOG_MAX_PARAMNAMELEN, name.c_str(),
param.getIntValue());
break;
case LOG_MSG_PARAM_INT64:
len += snprintf(buffer + len, m_maxMsgLen - len, "%.*s=%lld ",
(int)LOG_MAX_PARAMNAMELEN, name.c_str(),
param.getUint64Value());
break;
case LOG_MSG_PARAM_DOUBLE:
len += snprintf(buffer + len, m_maxMsgLen - len, "%.*s=%f ",
(int)LOG_MAX_PARAMNAMELEN, name.c_str(),
param.getDoubleValue());
break;
// Subrequest uuid
case LOG_MSG_PARAM_UUID:
{
char uuidstr[CUUID_STRING_LEN + 1];
if(Cuuid2string(uuidstr, CUUID_STRING_LEN + 1,
&param.getUuidValue())) {
return;
}
len += snprintf(buffer + len, m_maxMsgLen - len, "SUBREQID=%.*s ",
CUUID_STRING_LEN, uuidstr);
}
break;
// Process the parameter value
const std::string value = cleanString(param.getValue(), false);
case LOG_MSG_PARAM_RAW:
len += snprintf(buffer + len, m_maxMsgLen - len, "%s ",
param.getStrValue().c_str());
break;
default:
// Please note that this case is used for normal program execution
// for the following parameter types:
//
// LOG_MSG_PARAM_UID
// LOG_MSG_PARAM_GID
// LOG_MSG_PARAM_STYPE
// LOG_MSG_PARAM_SNAME
break; // Nothing
}
// Write the name and value to the buffer
len += snprintf(buffer + len, m_maxMsgLen - len, "%.*s=\"%.*s\" ",
(int)LOG_MAX_PARAMNAMELEN, name.c_str(),
(int)LOG_MAX_PARAMSTRLEN, value.c_str());
// Check if there is enough space in the buffer
if(len >= m_maxMsgLen) {
......
......@@ -19,192 +19,10 @@
*
* A parameter for the CASTOR logging system
*
* @author castor dev team
* @author steve.murray@cern.ch
*****************************************************************************/
// Include Files
#include <time.h>
#include <sstream>
#include <string.h>
#include <iomanip>
#include "castor/log/Constants.hpp"
#include "castor/log/Param.hpp"
#include "castor/ObjectSet.hpp"
//------------------------------------------------------------------------------
// 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 &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) 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;
castor::ObjectSet set;
value->print(oss, "", set);
m_strValue = oss.str();
}
//-----------------------------------------------------------------------------
// Constructor
//-----------------------------------------------------------------------------
castor::log::Param::Param(const std::string &name,
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;
oss << ((value.ip() & 0xFF000000) >> 24) << "."
<< ((value.ip() & 0x00FF0000) >> 16) << "."
<< ((value.ip() & 0x0000FF00) >> 8) << "."
<< ((value.ip() & 0x000000FF));
m_strValue = oss.str();
}
//-----------------------------------------------------------------------------
// Constructor
//-----------------------------------------------------------------------------
castor::log::Param::Param(const std::string &name,
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();
// There is NO localtime_r() on Windows,
// so we will use non-reentrant version localtime().
struct tm tmstruc;
localtime_r (&time, &tmstruc);
std::ostringstream oss;
oss << std::setw(2) << tmstruc.tm_mon+1
<< "/" << tmstruc.tm_mday
<< " " << tmstruc.tm_hour
<< ":" << tmstruc.tm_min
<< ":" << tmstruc.tm_sec;
m_strValue = oss.str();
}
//------------------------------------------------------------------------------
// getName
......@@ -214,63 +32,8 @@ const std::string &castor::log::Param::getName() const throw() {
}
//------------------------------------------------------------------------------
// 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 || LOG_MSG_PARAM_RAW == 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
// getValue
//------------------------------------------------------------------------------
const Cuuid_t &castor::log::Param::getUuidValue() const throw() {
if(LOG_MSG_PARAM_UUID == m_type) {
return m_uuidValue;
} else {
return nullCuuid;
}
const std::string &castor::log::Param::getValue() const throw() {
return m_value;
}
......@@ -26,93 +26,32 @@
#ifndef CASTOR_LOG_PARAM_HPP
#define CASTOR_LOG_PARAM_HPP 1
#include "castor/IObject.hpp"
#include "castor/log/IPAddress.hpp"
#include "castor/log/TimeStamp.hpp"
#include "h/Cuuid.h"
#include <sstream>
#include <string.h>
#include <stdlib.h>
namespace castor {
namespace log {
/**
* A parameter for the CASTOR logging system.
* A name/value parameter for the CASTOR logging system.
*/
class Param {
public:
/**
* Constructor for strings.
*/
Param(const std::string &name, const std::string &value) throw();
/**
* Constructor for uuids.
*/
Param(const std::string &name, const Cuuid_t value) throw();
/**
* Constructor for SubRequest uuids.
*/
Param(const Cuuid_t &value) throw();
/**
* Constructor for long int.
*/
Param(const std::string &name, const long int value) throw();
/**
* Constructor for long unsigned int.
*/
Param(const std::string &name, const long unsigned int value) throw();
/**
* Constructor for int.
*/
Param(const std::string &name, const int value) throw();
/**
* Constructor for unsigned int.
*/
Param(const std::string &name, const unsigned int value) throw();
/**
* Constructor for u_signed64.
*/
Param(const std::string &name, const u_signed64 value) throw();
/**
* Constructor for floats.
*/
Param(const std::string &name, const float value) throw();
/**
* Constructor for doubles.
*/
Param(const std::string &name, const double value) throw();
/**
* Constructor for Raw parameters.
*/
Param(const std::string &rawParams) throw();
/**
* Constructor for IPAddress.
*/
Param(const std::string &name, const castor::log::IPAddress &value) throw();
/**
* Constructor for TimeStamp.
* Constructor.
*
* @param name The name of the parameter.
* @param value The value of the parameter that will be converted to a string
* using std::ostringstream.
*/
Param(const std::string &name, const castor::log::TimeStamp &value) throw();
/**
* Constructor for objects.
*/
Param(const std::string &name, const castor::IObject *const value) throw();
template <typename T> Param(const std::string &name, const T &value) throw():
m_name(name) {
std::stringstream oss;
oss << value;
m_value = oss.str();
}
/**
* Returns a const reference to the name of the parameter.
......@@ -120,36 +59,9 @@ public:
const std::string &getName() const throw();
/**
* Returns the type of the parameter.
*/
int getType() const throw();
/**
* Returns a const refverence to the string value if there is one, else an
* empty string.
*/
const std::string &getStrValue() const throw();
/**
* Returns the int value if there is one, else 0.
*/
int getIntValue() const throw();
/**
* Returns the unsigned 64-bit int value if there is one, else 0.
*/
u_signed64 getUint64Value() const throw();
/**
* Returns the double value if there is one, else 0.0;
*/
double getDoubleValue() const throw();
/**
* Returns a const reference to the UUID value if there is one, else
* nullCuuid.
* Returns a const reference to the value of the parameter.
*/
const Cuuid_t &getUuidValue() const throw();
const std::string &getValue() const throw();
private:
......@@ -159,40 +71,9 @@ private:
std::string m_name;
/**
* Parameter type, one of LOG_MSG_PARAM_*.
*/
int m_type;
/**
* The string value of the parameter.
*/
std::string m_strValue;
/**
* The int value of the parameter.
*/
int m_intValue;
/**
* The unsigned 64-bit int value of the parameter.
*/
u_signed64 m_uint64Value;
/**
* The double value of the parameter.
*/
double m_doubleValue;
/**
* The UUID value of the parameter.
*/
Cuuid_t m_uuidValue;
/**
* Empty string constant used by the getStrValue() method in the case where
* m_type does not equal LOG_MSG_PARAM_STR.
* The value of the parameter.
*/
const std::string m_emptyStr;
std::string m_value;
}; // class Param
......
......@@ -40,250 +40,26 @@ public:
void tearDown() {
}
void testConstructorForString() {
void testConstructorWithAString() {
std::auto_ptr<Param> param;
CPPUNIT_ASSERT_NO_THROW(
param.reset(new Param("Name", "Value")));
CPPUNIT_ASSERT_EQUAL(
std::string("Name"),
param->getName());
CPPUNIT_ASSERT_EQUAL(
std::string("Value"),
param->getStrValue());
CPPUNIT_ASSERT_EQUAL(
LOG_MSG_PARAM_STR,
param->getType());
CPPUNIT_ASSERT_EQUAL(std::string("Name"), param->getName());
CPPUNIT_ASSERT_EQUAL(std::string("Value"), param->getValue());
}
void testConstructorForUuid() {
const Cuuid_t &value = nullCuuid;
void testConstructorWithAnInt() {