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.

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