|
|
- /**
- * @file Log.cpp
- *
- * Created on: 16.01.2017
- * @author Piotr Dergun
- */
-
- #include "Log.h"
-
- Log::Log()
- {
- this->color = WHITE;
- this->delimiter = "";
- this->lineNumber = 0;
- this->objectName = NULL;
- this->currentLine = 0;
- this->firstLine = 0;
- 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::printProgressBar(int lineNumber, int offset, string msg,
- float percent)
- {
- int cols, rows, bar_width, pools;
- stringstream ss;
- #ifndef DEBUG
- if (this->getObjectName() == NULL)
- return;
-
- if (this->getDelay() == 0)
- return;
-
- getmaxyx(stdscr, rows, cols);
-
- pthread_mutex_lock(this->writeMutex);
- move(lineNumber, 0);
- clrtoeol();
- ss << msg << ":" << (int) (percent * 100) << "%";
- bar_width = cols - ss.str().size() - offset;
- pools = (int) (percent * bar_width);
-
- if (has_colors())
- attron(COLOR_PAIR(this->getColor()));
- printw("%s:", msg.c_str());
- if (has_colors())
- attroff(COLOR_PAIR(this->getColor()));
-
- for (int i = 0; i < offset; ++i)
- printw(" ");
-
- if (has_colors())
- {
- if (percent < 0.66)
- attron(COLOR_PAIR(GREEN));
- else if (percent < 0.90)
- attron(COLOR_PAIR(YELLOW));
- else
- attron(COLOR_PAIR(RED));
- }
- attron(A_BOLD);
-
- for (int i = 0; i < pools; ++i)
- printw("#");
- ss.str("");
- ss << " " << (int) (percent * 100) << "%";
- move(lineNumber, cols - ss.str().size());
- printw(" %d%%", (int) (percent * 100));
-
- attroff(A_BOLD);
-
- if (has_colors())
- {
- if (percent < 0.66)
- attroff(COLOR_PAIR(GREEN));
- else if (percent < 0.90)
- attroff(COLOR_PAIR(YELLOW));
- else
- attroff(COLOR_PAIR(RED));
- }
-
- move(0, 0);
- pthread_mutex_unlock(this->writeMutex);
-
- #else
- cout << this->getObjectName()->c_str() << ": " << this->getDelimiter() << msg << endl;
- #endif
- }
-
- void Log::printLine(string msg, bool force)
- {
- int cols, rows;
- getmaxyx(stdscr, rows, cols);
-
- if (this->currentLine > rows-3)
- this->currentLine = this->firstLine;
-
- this->print(msg, force, this->currentLine);
-
- pthread_mutex_lock(this->writeMutex);
- move(this->currentLine+1, 0);
- clrtoeol();
- move(0,0);
- pthread_mutex_unlock(this->writeMutex);
- ++this->currentLine;
- }
-
- int Log::getFirstLine() const
- {
- return firstLine;
- }
-
- void Log::setFirstLine(int firstLine)
- {
- this->firstLine = firstLine;
- this->currentLine = firstLine;
- }
-
- void Log::setObjectName(string* objectName)
- {
- this->objectName = objectName;
- }
-
- void Log::print(string msg, bool force, int customLine)
- {
- #ifndef DEBUG
- if (this->getObjectName() == NULL)
- return;
-
- if (this->getDelay() == 0 && !force)
- return;
-
- pthread_mutex_lock(this->writeMutex);
- if (customLine > -1)
- move(customLine, 0);
- else
- move(this->getLineNumber(), 0);
- clrtoeol();
-
- if(has_colors())
- attron(COLOR_PAIR(this->getColor()));
-
- printw("%s:%s", this->getObjectName()->c_str(), this->getDelimiter().c_str());
- if(has_colors())
- 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)
- usleep(t);
- else
- sleep(t);
- }
|