N
nin234
I tried searching the newgroups for an answer to this question. I
found
many related topics, but didn't find the exact answer I am looking
for.
I am implementing a class to aid in the logging for our product as
follows. Code snippet is as follows.
//Header file srvLog.h starts here
enum eErrLvl
{
eLOG_DEBUG_5 = 1,
eLOG_DEBUG_4,
eLOG_DEBUG_3,
eLOG_DEBUG_2,
eLOG_DEBUG_1,
eLOG_INFO,
eLOG_WARNING,
eLOG_ERROR,
eLOG_FATAL
};
std:stream& operator << (std:stream&, eErrLvl&);
class srvLog
{
static int nLglvl;
eErrLvl nSeverity;
public:
srvLog () {}
static void setlvl (int lvl) { nLglvl = lvl; }
const srvLog& operator () (eErrLvl nSvrty) { nSeverity =
nSvrty; return *this;}
friend std:stream& operator << (std:stream& , const
srvLog&);
};
//Source file srvLog.C starts here
int srvLog::nLglvl = eLOG_ERROR;
std:stream&
operator << (std:stream& os, eErrLvl& eLvl)
{
switch (eLvl)
{
case eLOG_DEBUG_5:
case eLOG_DEBUG_4:
case eLOG_DEBUG_3:
case eLOG_DEBUG_2:
case eLOG_DEBUG_1:
os << "Debug\t: ";
break;
case eLOG_INFO:
os << "Info\t: ";
break;
case eLOG_WARNING:
os << "Warn\t: ";
break;
case eLOG_ERROR:
os << "Error\t: ";
break;
case eLOG_FATAL:
os << "Fatal\t: ";
break;
}
return os;
}
std:stream& operator << (std:stream& os, const srvLog& oLog)
{
if (oLog.nSeverity >= oLog::nLglvl)
os.clear();
else
{
os.clear (std::ios::failbit);
return os;
}
char msg[100];
//memset (msg, '\0', 100);
struct timeval now;
gettimeofday (&now, NULL);
struct tm *pTm = localtime (&now.tv_sec);
strftime (msg, 100, "%c ", pTm);
os << msg << oLog.nSeverity;
return os;
}
//Sample use case
int main ()
{
srvLog lgstrt;
//Class A
A ainst;
std::cerr << lgstrt(eLOG_FATAL) << "more to log " << ainst <<
std::endl;
pthread_create (, threadA, ... , ...);
pthread_create (, threadB, ... , ...);
}
//ThreadA in a different file and shared library
void threadA ()
{
srvLog lgstrt;
//Class A
A ainst;
std::cerr << lgstrt(eLOG_FATAL) << "more to log " << ainst <<
std::end;
}
//ThreadB in a different file and shared library
void threadB ()
{
srvLog lgstrt;
//Class A
A ainst;
std::cerr << lgstrt(eLOG_FATAL) << "more to log " << ainst <<
std::endl;
}
Logging is controlled by the nLglvl static variable. This is part of
a daemon. So the logging is to a file and the value of nLglvl will be
set by a command line tool.
My questions are the following
1) Is the value of nLglvl shared by threadA, threadB and main()
thread.
2)Where should I call setlvl()?
3) If the answer to 1) is yes. Is it really required to use locking
in
this scenario. Because the change to the value of nLglvl will be
infrequent and I can live with some bad reads.
I am using POSIX threads. Linux and solaris are the current
environments. It must be portable to all unix flavors. I threads are
not part of the C++ standard, but to me it does n't make much sense. I
feel this is as much a C++ question as a Threads one
Ninan
found
many related topics, but didn't find the exact answer I am looking
for.
I am implementing a class to aid in the logging for our product as
follows. Code snippet is as follows.
//Header file srvLog.h starts here
enum eErrLvl
{
eLOG_DEBUG_5 = 1,
eLOG_DEBUG_4,
eLOG_DEBUG_3,
eLOG_DEBUG_2,
eLOG_DEBUG_1,
eLOG_INFO,
eLOG_WARNING,
eLOG_ERROR,
eLOG_FATAL
};
std:stream& operator << (std:stream&, eErrLvl&);
class srvLog
{
static int nLglvl;
eErrLvl nSeverity;
public:
srvLog () {}
static void setlvl (int lvl) { nLglvl = lvl; }
const srvLog& operator () (eErrLvl nSvrty) { nSeverity =
nSvrty; return *this;}
friend std:stream& operator << (std:stream& , const
srvLog&);
};
//Source file srvLog.C starts here
int srvLog::nLglvl = eLOG_ERROR;
std:stream&
operator << (std:stream& os, eErrLvl& eLvl)
{
switch (eLvl)
{
case eLOG_DEBUG_5:
case eLOG_DEBUG_4:
case eLOG_DEBUG_3:
case eLOG_DEBUG_2:
case eLOG_DEBUG_1:
os << "Debug\t: ";
break;
case eLOG_INFO:
os << "Info\t: ";
break;
case eLOG_WARNING:
os << "Warn\t: ";
break;
case eLOG_ERROR:
os << "Error\t: ";
break;
case eLOG_FATAL:
os << "Fatal\t: ";
break;
}
return os;
}
std:stream& operator << (std:stream& os, const srvLog& oLog)
{
if (oLog.nSeverity >= oLog::nLglvl)
os.clear();
else
{
os.clear (std::ios::failbit);
return os;
}
char msg[100];
//memset (msg, '\0', 100);
struct timeval now;
gettimeofday (&now, NULL);
struct tm *pTm = localtime (&now.tv_sec);
strftime (msg, 100, "%c ", pTm);
os << msg << oLog.nSeverity;
return os;
}
//Sample use case
int main ()
{
srvLog lgstrt;
//Class A
A ainst;
std::cerr << lgstrt(eLOG_FATAL) << "more to log " << ainst <<
std::endl;
pthread_create (, threadA, ... , ...);
pthread_create (, threadB, ... , ...);
}
//ThreadA in a different file and shared library
void threadA ()
{
srvLog lgstrt;
//Class A
A ainst;
std::cerr << lgstrt(eLOG_FATAL) << "more to log " << ainst <<
std::end;
}
//ThreadB in a different file and shared library
void threadB ()
{
srvLog lgstrt;
//Class A
A ainst;
std::cerr << lgstrt(eLOG_FATAL) << "more to log " << ainst <<
std::endl;
}
Logging is controlled by the nLglvl static variable. This is part of
a daemon. So the logging is to a file and the value of nLglvl will be
set by a command line tool.
My questions are the following
1) Is the value of nLglvl shared by threadA, threadB and main()
thread.
2)Where should I call setlvl()?
3) If the answer to 1) is yes. Is it really required to use locking
in
this scenario. Because the change to the value of nLglvl will be
infrequent and I can live with some bad reads.
I am using POSIX threads. Linux and solaris are the current
environments. It must be portable to all unix flavors. I threads are
not part of the C++ standard, but to me it does n't make much sense. I
feel this is as much a C++ question as a Threads one
Ninan