|
|
- /*
- * Log.cpp
- *
- * Created on: 16.01.2017
- * Author: piotrek
- */
-
- #include "Log.h"
-
- Log::Log()
- {
- this->color = WHITE;
- this->delimiter = "";
- this->lineNumber = 0;
- this->objectName = NULL;
-
- this->writeMutex = Log::getMutex();
- this->setDelay(1);
- }
-
- LOG_COLOR Log::getColor() const
- {
- return color;
- }
-
- void Log::setColor(LOG_COLOR color)
- {
- this->color = color;
- }
-
- const string& Log::getDelimiter() const
- {
- return delimiter;
- }
-
- void Log::setDelimiter(const string& delimiter)
- {
- this->delimiter = delimiter;
- }
-
- int Log::getLineNumber() const
- {
- return lineNumber;
- }
-
- void Log::setLineNumber(int lineNumber)
- {
- this->lineNumber = lineNumber;
- }
-
- string* Log::getObjectName() const
- {
- return objectName;
- }
-
- void Log::setObjectName(string* objectName)
- {
- this->objectName = objectName;
- }
-
- void Log::print(string msg)
- {
- #ifndef DEBUG
- if (this->getObjectName() == NULL)
- return;
-
- pthread_mutex_lock(this->writeMutex);
- move(this->getLineNumber(), 0);
- clrtoeol();
-
- if(has_colors())
- attron(COLOR_PAIR(this->getColor()));
-
- //printw("%s:%s%s", this->getObjectName()->c_str(), this->getDelimiter().c_str(), msg.c_str());
- printw("%s:%s", this->getObjectName()->c_str(), this->getDelimiter().c_str());
- attron(A_BOLD);
- printw("%s", msg.c_str());
- attroff(A_BOLD);
- if(has_colors())
- attroff(COLOR_PAIR(this->getColor()));
-
- move(0, 0);
- pthread_mutex_unlock(this->writeMutex);
-
- #else
- cout << this->getObjectName()->c_str() << ": " << this->getDelimiter() << msg << endl;
- #endif
- }
-
- void Log::setLogParams(int lineNumber, LOG_COLOR color, string delimiter)
- {
- this->setLineNumber(lineNumber);
- this->setColor(color);
- this->setDelimiter(delimiter);
- }
-
- int Log::getDelay() const
- {
- return delayVal;
- }
-
- void Log::setDelay(int delay)
- {
- this->delayVal = delay;
- }
-
- pthread_mutex_t* Log::getMutex()
- {
- static pthread_mutex_t m;
- static int mutex_initalized;
- if (mutex_initalized != 0)
- {
- pthread_mutex_init(&m, NULL);
- }
-
- return &m;
- }
-
- void Log::delay(int optional)
- {
- int t = (optional > -1) ? optional : this->delayVal;
-
- if (t == 0)
- return;
-
- if (t >= 10) // ns
- usleep(t);
- else
- sleep(t); // s
- }
|