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.

103 lines
1.6 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. if(has_colors())
  60. attroff(COLOR_PAIR(this->getColor()));
  61. move(0, 0);
  62. pthread_mutex_unlock(this->writeMutex);
  63. #else
  64. cout << this->getObjectName()->c_str() << ": " << this->getDelimiter() << msg << endl;
  65. #endif
  66. }
  67. void Log::setLogParams(int lineNumber, LOG_COLOR color, string delimiter)
  68. {
  69. this->setLineNumber(lineNumber);
  70. this->setColor(color);
  71. this->setDelimiter(delimiter);
  72. }
  73. pthread_mutex_t* Log::getMutex()
  74. {
  75. static pthread_mutex_t m;
  76. static int mutex_initalized;
  77. if (mutex_initalized != 0)
  78. {
  79. pthread_mutex_init(&m, NULL);
  80. }
  81. return &m;
  82. }