Symulacja NAT na przedmiot Symulacje Komputerowe
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

201 lines
3.4 KiB

/*
* 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::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::setObjectName(string* objectName)
{
this->objectName = objectName;
}
void Log::print(string msg, bool force)
{
#ifndef DEBUG
if (this->getObjectName() == NULL)
return;
if (this->getDelay() == 0 && !force)
return;
pthread_mutex_lock(this->writeMutex);
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) // ns
usleep(t);
else
sleep(t); // s
}