msg.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #include "msg.h"
  2. #include "core-activation.h"
  3. #include "inter.h"
  4. #include "env-var.h"
  5. namespace aFuncore {
  6. NormalMessage::~NormalMessage(){
  7. this->obj = nullptr;
  8. }
  9. void NormalMessage::topProgress(){
  10. aFuntool::printf_stdout(0, "NORMAL: %p\n", obj);
  11. }
  12. ErrorMessage::ErrorMessage(const std::string &error_type_, const std::string &error_info_, Activation *activation)
  13. : TopMessage("ERROR"), error_type{error_type_}, error_info{error_info_}, inter{activation->inter}{
  14. for (NULL; activation != nullptr; activation = activation->toPrev()) {
  15. if (activation->getFileLine() != 0)
  16. trackback.push_front({activation->getFilePath(), activation->getFileLine()});
  17. }
  18. }
  19. void ErrorMessage::topProgress(){
  20. int32_t error_std = 0;
  21. inter.getEnvVarSpace().findNumber("sys:error_std", error_std);
  22. if (error_std == 0) {
  23. aFuntool::printf_stderr(0, "Error TrackBack\n");
  24. for (auto &begin: trackback)
  25. aFuntool::printf_stderr(0, " File \"%s\", line %d\n", begin.path.c_str(), begin.line);
  26. aFuntool::printf_stderr(0, "%s: %s\n", error_type.c_str(), error_info.c_str());
  27. } else {
  28. aFuntool::printf_stdout(0, "Error TrackBack\n");
  29. for (auto &begin: trackback)
  30. aFuntool::printf_stdout(0, " File \"%s\", line %d\n", begin.path.c_str(), begin.line);
  31. aFuntool::printf_stdout(0, "%s: %s\n", error_type.c_str(), error_info.c_str());
  32. }
  33. }
  34. MessageStream::MessageStream(){
  35. stream = nullptr;
  36. }
  37. MessageStream::~MessageStream(){
  38. for (Message *msg = stream, *tmp; msg != nullptr; msg = tmp) {
  39. tmp = msg->next;
  40. delete msg;
  41. }
  42. }
  43. /**
  44. * 压入 Message
  45. * @param msg Message
  46. */
  47. void MessageStream::pushMessage(Message *msg){
  48. msg->next = stream;
  49. stream = msg;
  50. }
  51. /**
  52. * 获取 Message
  53. * @param type 类型
  54. * @return Message
  55. */
  56. Message *MessageStream::_getMessage(const std::string &type) const{
  57. for (Message *msg = stream; msg != nullptr; msg = msg->next) {
  58. if (msg->type == type)
  59. return msg;
  60. }
  61. return nullptr;
  62. }
  63. /**
  64. * 弹出Message (使Message脱离数据流)
  65. * @param type 类型
  66. * @return Message
  67. */
  68. Message *MessageStream::popMessage(const std::string &type){
  69. for (Message **msg = &stream; *msg != nullptr; msg = &((*msg)->next)) {
  70. if ((*msg)->type == type) {
  71. Message *ret = *msg;
  72. *msg = ret->next;
  73. return ret;
  74. }
  75. }
  76. return nullptr;
  77. }
  78. UpMessage::UpMessage(const UpMessage *old) : MessageStream(){
  79. if (old != nullptr)
  80. this->old = old->stream;
  81. else
  82. this->old = nullptr;
  83. this->stream = this->old;
  84. }
  85. UpMessage::~UpMessage(){
  86. if (old != nullptr) {
  87. for (Message **msg = &stream; *msg != nullptr; msg = &((*msg)->next)) {
  88. if (*msg == old) {
  89. *msg = nullptr;
  90. break;
  91. }
  92. }
  93. }
  94. }
  95. /**
  96. * 弹出Message (使Message脱离数据流)
  97. * 注意: 不会弹出继承的Message
  98. * @param type 类型
  99. * @return Message
  100. */
  101. Message *UpMessage::popMessage(const std::string &type){
  102. for (Message **msg = &stream; *msg != nullptr; msg = &((*msg)->next)) {
  103. if ((*msg) == old)
  104. break;
  105. if ((*msg)->type == type) {
  106. Message *ret = *msg;
  107. *msg = ret->next;
  108. return ret;
  109. }
  110. }
  111. return nullptr;
  112. }
  113. /**
  114. * 拼接数据流
  115. * @param msg
  116. */
  117. void DownMessage::joinMsg(DownMessage &msg){
  118. Message *m = stream;
  119. if (m == nullptr)
  120. return;
  121. while (m->next != nullptr)
  122. m = m->next;
  123. m->next = msg.stream;
  124. msg.stream = m;
  125. stream = nullptr;
  126. }
  127. }