configengcallback.hpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*##############################################################################
  2. Copyright (C) 2011 HPCC Systems.
  3. All rights reserved. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ############################################################################## */
  14. #if !defined CONFIGENGCALLBACK_HPP
  15. #define CONFIGENGCALLBACK_HPP
  16. #include "deploy.hpp"
  17. class CConfigEngCallback: public CInterface, implements IDeploymentCallback
  18. {
  19. virtual void printStatus(IDeployTask* task)
  20. {
  21. if (!m_verbose || !task || !task->isProcessed())
  22. return;
  23. StringBuffer sb;
  24. const char* name = task->getCompName();
  25. if (!name || !*name)
  26. return;
  27. const char* instance = task->getInstanceName();
  28. const char* caption = task->getCaption();
  29. const char* sourceFile = task->getFileSpec(DT_SOURCE);
  30. const char* targetFile = task->getFileSpec(DT_TARGET);
  31. offset_t size = 0;
  32. try
  33. {
  34. if (!task->getCallback().getAbortStatus())
  35. size = task->getFileSize(DT_TARGET);
  36. }
  37. catch (IException* e)
  38. {
  39. e->Release();
  40. }
  41. StringBuffer s("0");
  42. if (size>0)
  43. {
  44. // format size into: 12,345,456
  45. s.clear().append(task->getFileSize(DT_TARGET));
  46. int i = s.length()-3;
  47. for (;i>0;i-=3)
  48. s.insert(i,",");
  49. }
  50. sb.appendf("\nComponent: %s-%s\nAction: %s\nSource Path:%s\nDestination Path:%s\nTarget Size:%s\n", name, instance, caption, sourceFile, targetFile, s.str());
  51. StringBuffer sbErr(task->getErrorString());
  52. StringBuffer sbWarnings(task->getWarnings());
  53. StringBuffer sbErrCode;
  54. if (task->getErrorCode() > 0)
  55. sbErrCode.appendlong(task->getErrorCode());
  56. if (sbErr.length() == 0 && sbWarnings.length() == 0 && sbErrCode.length() == 0)
  57. sb.append("Result: Success\n");
  58. else
  59. {
  60. if (sbErr.length() > 0) sb.appendf("Errors: %s\n", sbErr.str());
  61. if (sbErrCode.length() > 0) sb.appendf("Error Code: %s\n", sbErrCode.str());
  62. if (sbWarnings.length() > 0) sb.appendf("Warnings: %s\n", sbWarnings.str());
  63. sb.append("Result: Errors or warnings raised.\n");
  64. }
  65. fprintf(stdout, "%s", sb.str());
  66. }
  67. virtual void printStatus(StatusType type, const char* processType, const char* comp,
  68. const char* instance, const char* msg=NULL, ...)
  69. {
  70. if (!m_verbose) return;
  71. char buf[1024];
  72. if (msg)
  73. {
  74. va_list args;
  75. va_start(args, msg);
  76. if (_vsnprintf(buf, sizeof(buf), msg, args) < 0)
  77. buf[sizeof(buf) - 1] = '\0';
  78. va_end(args);
  79. }
  80. else
  81. *buf = '\0';
  82. StringBuffer sb;
  83. if (processType)
  84. sb.append("Process type: ").append(processType).append("\n");
  85. if (comp)
  86. sb.append("Component: ").append(comp).append("\n");
  87. if (instance)
  88. sb.append("Instance: ").append(instance).append("\n");
  89. if (msg)
  90. sb.append("Message: ").append(buf).append("\n");
  91. if (STATUS_ERROR == type)
  92. fprintf(stderr, "%s", sb.str());
  93. else
  94. fprintf(stdout, "%s", sb.str());
  95. }
  96. virtual bool onDisconnect(const char* target){return true;}
  97. virtual bool getAbortStatus()const {return m_abort;}
  98. virtual void setAbortStatus(bool bAbort){m_abort = bAbort;}
  99. virtual void setEnvironmentUpdated(){}
  100. virtual void getSshAccountInfo(StringBuffer& userid, StringBuffer& password)const{}
  101. //the following throws exception on abort, returns true for ignore
  102. virtual bool processException(const char* processType, const char* process, const char* instance,
  103. IException* e, const char* szMessage=NULL, const char* szCaption=NULL,
  104. IDeployTask* pTask = NULL )
  105. {
  106. if (m_abortOnException)
  107. {
  108. StringBuffer sb;
  109. if (processType)
  110. sb.append("Process type: ").append(processType).append("\n");
  111. if (process)
  112. sb.append("Component: ").append(process).append("\n");
  113. if (instance)
  114. sb.append("Instance: ").append(instance).append("\n");
  115. if (szMessage)
  116. sb.append("Message: ").append(szMessage).append("\n");
  117. if (!m_abort)
  118. m_abort = true;
  119. if (szMessage)
  120. m_sbExMsg.append(sb);
  121. throw MakeStringException(0, "%s", m_sbExMsg.str());
  122. }
  123. return true;
  124. }
  125. virtual IEnvDeploymentEngine* getEnvDeploymentEngine()const{return NULL;}
  126. virtual void* getWindowHandle() const{return NULL;}
  127. virtual void installFileListChanged(){}
  128. virtual void fileSizeCopied(offset_t size, bool bWholeFileDone){}
  129. private:
  130. bool m_verbose;
  131. bool m_abortOnException;
  132. bool m_abort;
  133. StringBuffer m_sbExMsg;
  134. public: // IDeploymentCallback
  135. IMPLEMENT_IINTERFACE;
  136. CConfigEngCallback(bool verbose = false, bool abortOnException = false){m_verbose = verbose;m_abortOnException=abortOnException;m_abort=false;}
  137. };
  138. #endif