Commit 1324448f authored by Steven Murray's avatar Steven Murray
Browse files

Replaced the use of the "C style" static keyword in Log.ccp with an unnamed

namespace.
parent cc5d7d29
......@@ -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
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment