PIRClientLog.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* Copyright (C) 2014 Carlos Aguilar Melchor, Joris Barrier, Marc-Olivier Killijian
  2. * This file is part of XPIR.
  3. *
  4. * XPIR is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * XPIR is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with XPIR. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "PIRClientLog.hpp"
  18. PIRClientLog::PIRClientLog(PIRController* controller_) :
  19. PIRView(controller_),
  20. file("clientError.log", std::ios::app)
  21. {}
  22. PIRClientLog::~PIRClientLog()
  23. {
  24. file.close();
  25. }
  26. const std::string PIRClientLog::getCurrentTime()
  27. {
  28. time_t now = time(0);
  29. struct tm tstruct;
  30. char buf[80];
  31. tstruct = *localtime(&now);
  32. strftime(buf, sizeof(buf), "%Y-%m-%d.%X", &tstruct);
  33. return buf;
  34. }
  35. /**
  36. * Called by the model when a information as to be displayed.
  37. * Param :
  38. * - MessageEvent& event : the message to write.
  39. **/
  40. void PIRClientLog::messageUpdate(MessageEvent& event)
  41. {
  42. std::string m;
  43. switch(event.getMessageType())
  44. {
  45. case WARNING :
  46. {
  47. m = "Warning";
  48. break;
  49. }
  50. case ERROR :
  51. {
  52. m = "Message";
  53. break;
  54. }
  55. case CHOICE:
  56. {
  57. m = "Choice ";
  58. break;
  59. }
  60. case DEFAULT:
  61. {
  62. m = "Default";
  63. break;
  64. }
  65. case RETRY:
  66. {
  67. m = "Retry";
  68. break;
  69. }
  70. }
  71. writeMessage(m, event);
  72. }
  73. /**
  74. * Called by the model when catalog is entierely received.
  75. * Param :
  76. * - CatalogEvent& event : new catalog event to write.
  77. **/
  78. void PIRClientLog::catalogUpdate(CatalogEvent& event)
  79. {
  80. file << "Proposed files : " << getCurrentTime() << std::endl;
  81. file << "--------------- " << std::endl;
  82. const std::vector<std::string>& fileList = event.getCatalog();
  83. for (unsigned int i = 0 ; i < fileList.size() ; i++)
  84. {
  85. file << fileList.at(i) << " ";
  86. if (i+1 % 10 == 0)
  87. file << std::endl;
  88. }
  89. file << std::endl;
  90. file << "--------------- " << std::endl;
  91. std::flush(file);
  92. }
  93. void PIRClientLog::writeUpdate(WriteEvent& event)
  94. {
  95. event.getSizeToWrite();
  96. }
  97. void PIRClientLog::writeMessage(std::string type, MessageEvent& event)
  98. {
  99. file << getCurrentTime() << " " << type << " : " << event.getMessage() << " : " << event.getInfo() << std::endl;
  100. std::flush(file);
  101. }