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.

234 lines
4.1 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. /**
  2. * @file Log.cpp
  3. *
  4. * Created on: 16.01.2017
  5. * @author Piotr Dergun
  6. */
  7. #include "Log.h"
  8. Log::Log()
  9. {
  10. this->color = WHITE;
  11. this->delimiter = "";
  12. this->lineNumber = 0;
  13. this->objectName = NULL;
  14. this->currentLine = 0;
  15. this->firstLine = 0;
  16. this->writeMutex = Log::getMutex();
  17. this->setDelay(1);
  18. }
  19. LOG_COLOR Log::getColor() const
  20. {
  21. return color;
  22. }
  23. void Log::setColor(LOG_COLOR color)
  24. {
  25. this->color = color;
  26. }
  27. const string& Log::getDelimiter() const
  28. {
  29. return delimiter;
  30. }
  31. void Log::setDelimiter(const string& delimiter)
  32. {
  33. this->delimiter = delimiter;
  34. }
  35. int Log::getLineNumber() const
  36. {
  37. return lineNumber;
  38. }
  39. void Log::setLineNumber(int lineNumber)
  40. {
  41. this->lineNumber = lineNumber;
  42. }
  43. string* Log::getObjectName() const
  44. {
  45. return objectName;
  46. }
  47. void Log::printProgressBar(int lineNumber, int offset, string msg,
  48. float percent)
  49. {
  50. int cols, rows, bar_width, pools;
  51. stringstream ss;
  52. #ifndef DEBUG
  53. if (this->getObjectName() == NULL)
  54. return;
  55. if (this->getDelay() == 0)
  56. return;
  57. getmaxyx(stdscr, rows, cols);
  58. pthread_mutex_lock(this->writeMutex);
  59. move(lineNumber, 0);
  60. clrtoeol();
  61. ss << msg << ":" << (int) (percent * 100) << "%";
  62. bar_width = cols - ss.str().size() - offset;
  63. pools = (int) (percent * bar_width);
  64. if (has_colors())
  65. attron(COLOR_PAIR(this->getColor()));
  66. printw("%s:", msg.c_str());
  67. if (has_colors())
  68. attroff(COLOR_PAIR(this->getColor()));
  69. for (int i = 0; i < offset; ++i)
  70. printw(" ");
  71. if (has_colors())
  72. {
  73. if (percent < 0.66)
  74. attron(COLOR_PAIR(GREEN));
  75. else if (percent < 0.90)
  76. attron(COLOR_PAIR(YELLOW));
  77. else
  78. attron(COLOR_PAIR(RED));
  79. }
  80. attron(A_BOLD);
  81. for (int i = 0; i < pools; ++i)
  82. printw("#");
  83. ss.str("");
  84. ss << " " << (int) (percent * 100) << "%";
  85. move(lineNumber, cols - ss.str().size());
  86. printw(" %d%%", (int) (percent * 100));
  87. attroff(A_BOLD);
  88. if (has_colors())
  89. {
  90. if (percent < 0.66)
  91. attroff(COLOR_PAIR(GREEN));
  92. else if (percent < 0.90)
  93. attroff(COLOR_PAIR(YELLOW));
  94. else
  95. attroff(COLOR_PAIR(RED));
  96. }
  97. move(0, 0);
  98. pthread_mutex_unlock(this->writeMutex);
  99. #else
  100. cout << this->getObjectName()->c_str() << ": " << this->getDelimiter() << msg << endl;
  101. #endif
  102. }
  103. void Log::printLine(string msg, bool force)
  104. {
  105. int cols, rows;
  106. getmaxyx(stdscr, rows, cols);
  107. if (this->currentLine > rows-3)
  108. this->currentLine = this->firstLine;
  109. this->print(msg, force, this->currentLine);
  110. pthread_mutex_lock(this->writeMutex);
  111. move(this->currentLine+1, 0);
  112. clrtoeol();
  113. move(0,0);
  114. pthread_mutex_unlock(this->writeMutex);
  115. ++this->currentLine;
  116. }
  117. int Log::getFirstLine() const
  118. {
  119. return firstLine;
  120. }
  121. void Log::setFirstLine(int firstLine)
  122. {
  123. this->firstLine = firstLine;
  124. this->currentLine = firstLine;
  125. }
  126. void Log::setObjectName(string* objectName)
  127. {
  128. this->objectName = objectName;
  129. }
  130. void Log::print(string msg, bool force, int customLine)
  131. {
  132. #ifndef DEBUG
  133. if (this->getObjectName() == NULL)
  134. return;
  135. if (this->getDelay() == 0 && !force)
  136. return;
  137. pthread_mutex_lock(this->writeMutex);
  138. if (customLine > -1)
  139. move(customLine, 0);
  140. else
  141. move(this->getLineNumber(), 0);
  142. clrtoeol();
  143. if(has_colors())
  144. attron(COLOR_PAIR(this->getColor()));
  145. printw("%s:%s", this->getObjectName()->c_str(), this->getDelimiter().c_str());
  146. if(has_colors())
  147. attron(A_BOLD);
  148. printw("%s", msg.c_str());
  149. attroff(A_BOLD);
  150. if(has_colors())
  151. attroff(COLOR_PAIR(this->getColor()));
  152. move(0, 0);
  153. pthread_mutex_unlock(this->writeMutex);
  154. #else
  155. cout << this->getObjectName()->c_str() << ": " << this->getDelimiter() << msg << endl;
  156. #endif
  157. }
  158. void Log::setLogParams(int lineNumber, LOG_COLOR color, string delimiter)
  159. {
  160. this->setLineNumber(lineNumber);
  161. this->setColor(color);
  162. this->setDelimiter(delimiter);
  163. }
  164. int Log::getDelay() const
  165. {
  166. return delayVal;
  167. }
  168. void Log::setDelay(int delay)
  169. {
  170. this->delayVal = delay;
  171. }
  172. pthread_mutex_t* Log::getMutex()
  173. {
  174. static pthread_mutex_t m;
  175. static int mutex_initalized;
  176. if (mutex_initalized != 0)
  177. {
  178. pthread_mutex_init(&m, NULL);
  179. }
  180. return &m;
  181. }
  182. void Log::delay(int optional)
  183. {
  184. int t = (optional > -1) ? optional : this->delayVal;
  185. if (t == 0)
  186. return;
  187. if (t >= 10)
  188. usleep(t);
  189. else
  190. sleep(t);
  191. }