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.

236 lines
4.1 KiB

7 years ago
7 years ago
7 years ago
  1. /*
  2. * Log.cpp
  3. *
  4. * Created on: 16.01.2017
  5. * Author: piotrek
  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. /*
  104. * metoda wypisuje log w wielu liniach od firstLineNumber do rows-2
  105. */
  106. void Log::printLine(string msg, bool force)
  107. {
  108. int cols, rows;
  109. getmaxyx(stdscr, rows, cols);
  110. if (this->currentLine > rows-3)
  111. this->currentLine = this->firstLine;
  112. this->print(msg, force, this->currentLine);
  113. pthread_mutex_lock(this->writeMutex);
  114. move(this->currentLine+1, 0);
  115. clrtoeol();
  116. move(0,0);
  117. pthread_mutex_unlock(this->writeMutex);
  118. ++this->currentLine;
  119. }
  120. int Log::getFirstLine() const
  121. {
  122. return firstLine;
  123. }
  124. void Log::setFirstLine(int firstLine)
  125. {
  126. this->firstLine = firstLine;
  127. this->currentLine = firstLine;
  128. }
  129. void Log::setObjectName(string* objectName)
  130. {
  131. this->objectName = objectName;
  132. }
  133. void Log::print(string msg, bool force, int customLine)
  134. {
  135. #ifndef DEBUG
  136. if (this->getObjectName() == NULL)
  137. return;
  138. if (this->getDelay() == 0 && !force)
  139. return;
  140. pthread_mutex_lock(this->writeMutex);
  141. if (customLine > -1)
  142. move(customLine, 0);
  143. else
  144. move(this->getLineNumber(), 0);
  145. clrtoeol();
  146. if(has_colors())
  147. attron(COLOR_PAIR(this->getColor()));
  148. printw("%s:%s", this->getObjectName()->c_str(), this->getDelimiter().c_str());
  149. if(has_colors())
  150. attron(A_BOLD);
  151. printw("%s", msg.c_str());
  152. attroff(A_BOLD);
  153. if(has_colors())
  154. attroff(COLOR_PAIR(this->getColor()));
  155. move(0, 0);
  156. pthread_mutex_unlock(this->writeMutex);
  157. #else
  158. cout << this->getObjectName()->c_str() << ": " << this->getDelimiter() << msg << endl;
  159. #endif
  160. }
  161. void Log::setLogParams(int lineNumber, LOG_COLOR color, string delimiter)
  162. {
  163. this->setLineNumber(lineNumber);
  164. this->setColor(color);
  165. this->setDelimiter(delimiter);
  166. }
  167. int Log::getDelay() const
  168. {
  169. return delayVal;
  170. }
  171. void Log::setDelay(int delay)
  172. {
  173. this->delayVal = delay;
  174. }
  175. pthread_mutex_t* Log::getMutex()
  176. {
  177. static pthread_mutex_t m;
  178. static int mutex_initalized;
  179. if (mutex_initalized != 0)
  180. {
  181. pthread_mutex_init(&m, NULL);
  182. }
  183. return &m;
  184. }
  185. void Log::delay(int optional)
  186. {
  187. int t = (optional > -1) ? optional : this->delayVal;
  188. if (t == 0)
  189. return;
  190. if (t >= 10) // ns
  191. usleep(t);
  192. else
  193. sleep(t); // s
  194. }