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.

130 lines
2.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->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::setObjectName(string* objectName)
  46. {
  47. this->objectName = objectName;
  48. }
  49. void Log::print(string msg)
  50. {
  51. #ifndef DEBUG
  52. if (this->getObjectName() == NULL)
  53. return;
  54. pthread_mutex_lock(this->writeMutex);
  55. move(this->getLineNumber(), 0);
  56. clrtoeol();
  57. if(has_colors())
  58. attron(COLOR_PAIR(this->getColor()));
  59. //printw("%s:%s%s", this->getObjectName()->c_str(), this->getDelimiter().c_str(), msg.c_str());
  60. printw("%s:%s", this->getObjectName()->c_str(), this->getDelimiter().c_str());
  61. attron(A_BOLD);
  62. printw("%s", msg.c_str());
  63. attroff(A_BOLD);
  64. if(has_colors())
  65. attroff(COLOR_PAIR(this->getColor()));
  66. move(0, 0);
  67. pthread_mutex_unlock(this->writeMutex);
  68. #else
  69. cout << this->getObjectName()->c_str() << ": " << this->getDelimiter() << msg << endl;
  70. #endif
  71. }
  72. void Log::setLogParams(int lineNumber, LOG_COLOR color, string delimiter)
  73. {
  74. this->setLineNumber(lineNumber);
  75. this->setColor(color);
  76. this->setDelimiter(delimiter);
  77. }
  78. int Log::getDelay() const
  79. {
  80. return delayVal;
  81. }
  82. void Log::setDelay(int delay)
  83. {
  84. this->delayVal = delay;
  85. }
  86. pthread_mutex_t* Log::getMutex()
  87. {
  88. static pthread_mutex_t m;
  89. static int mutex_initalized;
  90. if (mutex_initalized != 0)
  91. {
  92. pthread_mutex_init(&m, NULL);
  93. }
  94. return &m;
  95. }
  96. void Log::delay(int optional)
  97. {
  98. int t = (optional > -1) ? optional : this->delayVal;
  99. if (t == 0)
  100. return;
  101. if (t >= 10) // ns
  102. usleep(t);
  103. else
  104. sleep(t); // s
  105. }