Added printThreadId option in ULogger

This commit is contained in:
matlabbe
2016-01-05 13:55:21 -05:00
parent bf2bd3fe50
commit 97dd9bb7d5
8 changed files with 73 additions and 15 deletions
+13
View File
@@ -298,6 +298,13 @@ public:
static void setPrintWhere(bool printWhere) {printWhere_ = printWhere;}
static bool isPrintWhere() {return printWhere_;}
/**
* Print thread ID: default false.
* @param printThreadId true to print where, otherwise set to false.
*/
static void setPrintThreadId(bool printThreadId) {printThreadID_ = printThreadId;}
static bool isPrintThreadId() {return printThreadID_;}
/**
* Print the full path: default true. ULogger::setPrintWhere() must be true to have path printed.
* @param printWhereFullPath true to print the full path, otherwise set to false.
@@ -507,6 +514,12 @@ private:
*/
static bool printWhereFullPath_;
/*
* If the logger prints the thread ID.
* Default is false.
*/
static bool printThreadID_;
/*
* If the logger limit the size of the "where" path to
* characters. If the path is over 8 characters, a "~"
+6 -6
View File
@@ -91,6 +91,10 @@ public:
*/
enum Priority{kPLow, kPBelowNormal, kPNormal, kPAboveNormal, kPRealTime};
public:
//return caller thread id
static unsigned long currentThreadId() {return (unsigned long)UThreadC<void>::Self();}
public:
/**
* The constructor.
@@ -165,7 +169,7 @@ public:
bool isKilled() const;
Handle getThreadHandle() const {return handle_;}
unsigned long getThreadId() const {return threadId_;}
protected:
@@ -248,18 +252,14 @@ private:
private:
void operator=(UThread & t) {}
UThread( const UThread & t ) {}
UThread( const UThread & t ) : state_(kSIdle) {}
private:
enum State{kSIdle, kSCreating, kSRunning, kSKilled}; /* Enum of states. */
State state_; /* The thread state. */
Priority priority_; /* The thread priority. */
Handle handle_; /* The thread handle. */
#ifdef _WIN32
int threadId_; /* The thread id. */
#else
unsigned long threadId_; /* The thread id. */
#endif
int cpuAffinity_; /* The cpu affinity. */
UMutex killSafelyMutex_; /* Mutex used to protect the kill() method. */
UMutex runningMutex_; /* Mutex used to notify the join method when the thread has finished. */
@@ -321,7 +321,7 @@ class UTILITE_EXP UThreadC<void>
}
int Create(
int & ThreadId,
unsigned long & ThreadId,
Handle * const & H = 0,
const bool & CreateDetached = false,
const unsigned int & StackSize = 0,
@@ -330,7 +330,9 @@ class UTILITE_EXP UThreadC<void>
) const
{
*H = InvalidHandle;
*H = CREATE_THREAD2(StackSize,ThreadMainHandler,this, (LPDWORD)&ThreadId);
int id;
*H = CREATE_THREAD2(StackSize,ThreadMainHandler,this, (LPDWORD)&id);
ThreadId = (unsigned long)id;
if ( *H == CREATE_THREAD_FAILED )
{
+12 -2
View File
@@ -50,6 +50,7 @@ bool ULogger::printEndline_ = true;
bool ULogger::printColored_ = true;
bool ULogger::printWhere_ = true;
bool ULogger::printWhereFullPath_ = false;
bool ULogger::printThreadID_ = false;
bool ULogger::limitWhereLength_ = false;
bool ULogger::buffered_ = false;
bool ULogger::exitingState_ = false;
@@ -210,6 +211,7 @@ void ULogger::reset()
printColored_ = true;
printWhere_ = true;
printWhereFullPath_ = false;
printThreadID_ = false;
limitWhereLength_ = false;
level_ = kInfo; // By default, we show all info msgs + upper level (Warning, Error)
logFileName_ = ULogger::kDefaultLogFileName;
@@ -383,6 +385,12 @@ void ULogger::write(ULogger::Level level,
levelStr.append(" ");
}
std::string pidStr;
if(printThreadID_)
{
pidStr = uFormat("{%lu} ", UThread::currentThreadId());
}
std::string whereStr = "";
if(printWhere_ || level == kFatal)
{
@@ -449,6 +457,7 @@ void ULogger::write(ULogger::Level level,
if(buffered_)
{
bufferedMsgs_.append(levelStr.c_str());
bufferedMsgs_.append(pidStr.c_str());
bufferedMsgs_.append(time.c_str());
bufferedMsgs_.append(whereStr.c_str());
bufferedMsgs_.append(uFormatv(msg, args));
@@ -456,6 +465,7 @@ void ULogger::write(ULogger::Level level,
else
{
ULogger::getInstance()->_writeStr(levelStr.c_str());
ULogger::getInstance()->_writeStr(pidStr.c_str());
ULogger::getInstance()->_writeStr(time.c_str());
ULogger::getInstance()->_writeStr(whereStr.c_str());
ULogger::getInstance()->_write(msg, args);
@@ -488,7 +498,7 @@ void ULogger::write(ULogger::Level level,
if(level >= eventLevel_)
{
std::string fullMsg = uFormat("%s%s%s", levelStr.c_str(), time.c_str(), whereStr.c_str());
std::string fullMsg = uFormat("%s%s%s%s", levelStr.c_str(), pidStr.c_str(), time.c_str(), whereStr.c_str());
va_start(args, msg);
fullMsg.append(uFormatv(msg, args));
va_end(args);
@@ -510,7 +520,7 @@ void ULogger::write(ULogger::Level level,
printf("\n*******\n%s message occurred! Application will now exit.\n", levelName_[level]);
if(type_ != kTypeConsole)
{
printf(" %s%s%s", levelStr.c_str(), time.c_str(), whereStr.c_str());
printf(" %s%s%s%s", levelStr.c_str(), pidStr.c_str(), time.c_str(), whereStr.c_str());
va_start(args, msg);
vprintf(msg, args);
va_end(args);