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.

106 lines
1.8 KiB

  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. }
  16. LOG_COLOR Log::getColor() const
  17. {
  18. return color;
  19. }
  20. void Log::setColor(LOG_COLOR color)
  21. {
  22. this->color = color;
  23. }
  24. const string& Log::getDelimiter() const
  25. {
  26. return delimiter;
  27. }
  28. void Log::setDelimiter(const string& delimiter)
  29. {
  30. this->delimiter = delimiter;
  31. }
  32. int Log::getLineNumber() const
  33. {
  34. return lineNumber;
  35. }
  36. void Log::setLineNumber(int lineNumber)
  37. {
  38. this->lineNumber = lineNumber;
  39. }
  40. string* Log::getObjectName() const
  41. {
  42. return objectName;
  43. }
  44. void Log::setObjectName(string* objectName)
  45. {
  46. this->objectName = objectName;
  47. }
  48. void Log::print(string msg)
  49. {
  50. #ifndef DEBUG
  51. if (this->getObjectName() == NULL)
  52. return;
  53. pthread_mutex_lock(this->writeMutex);
  54. move(this->getLineNumber(), 0);
  55. clrtoeol();
  56. if(has_colors())
  57. attron(COLOR_PAIR(this->getColor()));
  58. //printw("%s:%s%s", this->getObjectName()->c_str(), this->getDelimiter().c_str(), msg.c_str());
  59. printw("%s:%s", this->getObjectName()->c_str(), this->getDelimiter().c_str());
  60. attron(A_BOLD);
  61. printw("%s", msg.c_str());
  62. attroff(A_BOLD);
  63. if(has_colors())
  64. attroff(COLOR_PAIR(this->getColor()));
  65. move(0, 0);
  66. pthread_mutex_unlock(this->writeMutex);
  67. #else
  68. cout << this->getObjectName()->c_str() << ": " << this->getDelimiter() << msg << endl;
  69. #endif
  70. }
  71. void Log::setLogParams(int lineNumber, LOG_COLOR color, string delimiter)
  72. {
  73. this->setLineNumber(lineNumber);
  74. this->setColor(color);
  75. this->setDelimiter(delimiter);
  76. }
  77. pthread_mutex_t* Log::getMutex()
  78. {
  79. static pthread_mutex_t m;
  80. static int mutex_initalized;
  81. if (mutex_initalized != 0)
  82. {
  83. pthread_mutex_init(&m, NULL);
  84. }
  85. return &m;
  86. }