configengcallback.hpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #if !defined CONFIGENGCALLBACK_HPP
  14. #define CONFIGENGCALLBACK_HPP
  15. #include "deploy.hpp"
  16. class CConfigEngCallback: public CInterface, implements IDeploymentCallback
  17. {
  18. virtual void printStatus(IDeployTask* task)
  19. {
  20. if (!m_verbose || !task || !task->isProcessed())
  21. return;
  22. StringBuffer sb;
  23. const char* name = task->getCompName();
  24. if (!name || !*name)
  25. return;
  26. const char* instance = task->getInstanceName();
  27. const char* caption = task->getCaption();
  28. const char* sourceFile = task->getFileSpec(DT_SOURCE);
  29. const char* targetFile = task->getFileSpec(DT_TARGET);
  30. offset_t size = 0;
  31. try
  32. {
  33. if (!task->getCallback().getAbortStatus())
  34. size = task->getFileSize(DT_TARGET);
  35. }
  36. catch (IException* e)
  37. {
  38. e->Release();
  39. }
  40. StringBuffer s("0");
  41. if (size>0)
  42. {
  43. // format size into: 12,345,456
  44. s.clear().append(task->getFileSize(DT_TARGET));
  45. int i = s.length()-3;
  46. for (;i>0;i-=3)
  47. s.insert(i,",");
  48. }
  49. sb.appendf("\nComponent: %s-%s\nAction: %s\nSource Path:%s\nDestination Path:%s\nTarget Size:%s\n", name, instance, caption, sourceFile, targetFile, s.str());
  50. StringBuffer sbErr(task->getErrorString());
  51. StringBuffer sbWarnings(task->getWarnings());
  52. StringBuffer sbErrCode;
  53. if (task->getErrorCode() > 0)
  54. sbErrCode.appendlong(task->getErrorCode());
  55. if (sbErr.length() == 0 && sbWarnings.length() == 0 && sbErrCode.length() == 0)
  56. sb.append("Result: Success\n");
  57. else
  58. {
  59. if (sbErr.length() > 0) sb.appendf("Errors: %s\n", sbErr.str());
  60. if (sbErrCode.length() > 0) sb.appendf("Error Code: %s\n", sbErrCode.str());
  61. if (sbWarnings.length() > 0) sb.appendf("Warnings: %s\n", sbWarnings.str());
  62. sb.append("Result: Errors or warnings raised.\n");
  63. }
  64. fprintf(stdout, "%s", sb.str());
  65. }
  66. virtual void printStatus(StatusType type, const char* processType, const char* comp,
  67. const char* instance, const char* msg=NULL, ...) __attribute__((format(printf,6,7)))
  68. {
  69. if (!m_verbose) return;
  70. char buf[1024];
  71. if (msg)
  72. {
  73. va_list args;
  74. va_start(args, msg);
  75. if (_vsnprintf(buf, sizeof(buf), msg, args) < 0)
  76. buf[sizeof(buf) - 1] = '\0';
  77. va_end(args);
  78. }
  79. else
  80. *buf = '\0';
  81. StringBuffer sb;
  82. if (processType)
  83. sb.append("Process type: ").append(processType).append("\n");
  84. if (comp)
  85. sb.append("Component: ").append(comp).append("\n");
  86. if (instance)
  87. sb.append("Instance: ").append(instance).append("\n");
  88. if (msg)
  89. sb.append("Message: ").append(buf).append("\n");
  90. if (STATUS_ERROR == type)
  91. fprintf(stderr, "%s", sb.str());
  92. else
  93. fprintf(stdout, "%s", sb.str());
  94. }
  95. virtual bool onDisconnect(const char* target){return true;}
  96. virtual bool getAbortStatus()const {return m_abort;}
  97. virtual void setAbortStatus(bool bAbort){m_abort = bAbort;}
  98. virtual void setEnvironmentUpdated(){}
  99. virtual void getSshAccountInfo(StringBuffer& userid, StringBuffer& password)const{}
  100. //the following throws exception on abort, returns true for ignore
  101. virtual bool processException(const char* processType, const char* process, const char* instance,
  102. IException* e, const char* szMessage=NULL, const char* szCaption=NULL,
  103. IDeployTask* pTask = NULL )
  104. {
  105. StringBuffer sb;
  106. if (m_errIdx > 1)
  107. sb.append("\n");
  108. sb.appendf("Error %d:\n", m_errIdx++);
  109. if (szMessage)
  110. {
  111. if (processType)
  112. sb.append("Component type: ").append(processType).append("\n");
  113. if (process)
  114. sb.append("Component Name: ").append(process).append("\n");
  115. StringBuffer errMsg(szMessage);
  116. String str(errMsg.trim());
  117. #ifdef _USE_XALAN
  118. if (str.lastIndexOf('[') > 0)
  119. {
  120. errMsg.clear();
  121. String* sub1 = str.substring(str.lastIndexOf('[') + 1, str.length() - 1);
  122. if (sub1)
  123. {
  124. String* sub2 = sub1->substring(sub1->indexOf(':') + 1, sub1->length());
  125. if (sub2)
  126. {
  127. StringBuffer sb2(*sub2);
  128. sb2.trim();
  129. sb2.replaceString(" ", " ");
  130. errMsg.append(sb2.str());
  131. delete sub2;
  132. }
  133. delete sub1;
  134. }
  135. }
  136. #endif
  137. sb.append(errMsg).append("\n");
  138. m_sbExMsg.append(sb);
  139. }
  140. if (m_abortOnException)
  141. {
  142. m_abort = true;
  143. throw MakeStringException(0, "%s", m_sbExMsg.str());
  144. }
  145. return true;
  146. }
  147. virtual IEnvDeploymentEngine* getEnvDeploymentEngine()const{return NULL;}
  148. virtual void* getWindowHandle() const{return NULL;}
  149. virtual void installFileListChanged(){}
  150. virtual void fileSizeCopied(offset_t size, bool bWholeFileDone){}
  151. private:
  152. bool m_verbose;
  153. bool m_abortOnException;
  154. bool m_abort;
  155. StringBuffer m_sbExMsg;
  156. unsigned m_errIdx;
  157. public: // IDeploymentCallback
  158. IMPLEMENT_IINTERFACE;
  159. CConfigEngCallback(bool verbose = false, bool abortOnException = false){m_verbose = verbose;m_abortOnException=abortOnException;m_abort=false;m_errIdx=1;}
  160. virtual const char* getErrorMsg() { return m_sbExMsg.str(); }
  161. virtual unsigned getErrorCount() { return m_errIdx;}
  162. };
  163. #endif