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

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