/dataaccess/oledb_conformance/modulecore/src/csuperlog.cpp

https://github.com/pauldotknopf/WindowsSDK7-Samples · C++ · 292 lines · 181 code · 68 blank · 43 comment · 17 complexity · ac5f059bb1ee85a9d61a1ada8dad0abc MD5 · raw file

  1. //-----------------------------------------------------------------------------
  2. // Microsoft Local Test Manager (LTM)
  3. // Copyright (C) 1997 - 1999 By Microsoft Corporation.
  4. //
  5. // @doc
  6. //
  7. // @module CSUPERLOG.CPP
  8. //
  9. //-----------------------------------------------------------------------------------
  10. ///////////////////////////////////////////////////////////////////////////
  11. // Includes
  12. //
  13. //////////////////////////////////////////////////////////////////////////
  14. #include "CSuperLog.hpp"
  15. #include "DTMGuids.hpp"
  16. #include <wchar.h>
  17. /////////////////////////////////////////////////////////////////////////
  18. // Defines
  19. //
  20. /////////////////////////////////////////////////////////////////////////
  21. #define CHECK_MEMORY(pv) if(!pv) goto CLEANUP
  22. #define SAFE_ALLOC(pv, type, cb) { pv = CoTaskMemAlloc((cb)*sizeof(type)); CHECK_MEMORY(pv); }
  23. #define SAFE_REALLOC(pv, type, cb) { pv = (type*)CoTaskMemRealloc(pv, (cb)*sizeof(type)); CHECK_MEMORY(pv); }
  24. #define SAFE_SYSALLOC(pv, bstr) { pv = SysAllocString(bstr); CHECK_MEMORY(pv); }
  25. #define SAFE_FREE(pv) { CoTaskMemFree(pv); pv = NULL; }
  26. #define SAFE_SYSFREE(bstr) { SysFreeString(bstr); bstr = NULL; }
  27. //IUnknown->Release Wrapper
  28. #define SAFE_ADDREF(pv) if(pv) { (pv)->AddRef(); }
  29. #define SAFE_RELEASE(pv) if(pv) { (pv)->Release(); (pv) = NULL; }
  30. #define SAFE_DELETE(pv) if(pv) { delete pv; pv = NULL; }
  31. ///////////////////////////////////////////////////////////////////////////
  32. // CSuperLog
  33. //
  34. //////////////////////////////////////////////////////////////////////////
  35. CSuperLog::CSuperLog(void)
  36. {
  37. m_nTabLevel = 0;
  38. //Error Object
  39. //Our way of transmitting the text to LTM
  40. m_fTriedErrorAlready = FALSE;
  41. m_pIError = NULL;
  42. }
  43. CSuperLog::~CSuperLog()
  44. {
  45. SAFE_RELEASE(m_pIError);
  46. }
  47. void CSuperLog::SetTabLevel(int iLevel)
  48. {
  49. m_nTabLevel = iLevel;
  50. }
  51. CSuperLog & CSuperLog::operator << (const BYTE bVal)
  52. {
  53. WCHAR wszBuffer[50];
  54. //Convert to String...
  55. _itow(bVal, wszBuffer, 10);
  56. OutputText(wszBuffer);
  57. return *this;
  58. }
  59. CSuperLog & CSuperLog::operator << (const ULONGLONG ulVal)
  60. {
  61. WCHAR wszBuffer[50];
  62. //Convert to String...
  63. _ui64tow(ulVal, wszBuffer, 10);
  64. OutputText(wszBuffer);
  65. return *this;
  66. }
  67. CSuperLog & CSuperLog::operator << (const LONGLONG slVal)
  68. {
  69. WCHAR wszBuffer[50];
  70. //Convert to String...
  71. _i64tow(slVal, wszBuffer, 10);
  72. OutputText(wszBuffer);
  73. return *this;
  74. }
  75. CSuperLog & CSuperLog::operator << (const unsigned long ulVal)
  76. {
  77. WCHAR wszBuffer[50];
  78. //Convert to String...
  79. _ultow(ulVal, wszBuffer, 10);
  80. OutputText(wszBuffer);
  81. return *this;
  82. }
  83. CSuperLog & CSuperLog::operator << (const signed long slVal)
  84. {
  85. WCHAR wszBuffer[50];
  86. //Convert to String...
  87. _ltow(slVal, wszBuffer, 10);
  88. OutputText(wszBuffer);
  89. return *this;
  90. }
  91. CSuperLog & CSuperLog::operator << (const unsigned short usVal)
  92. {
  93. WCHAR wszBuffer[50];
  94. //Convert to String...
  95. _ultow(usVal, wszBuffer, 10);
  96. OutputText(wszBuffer);
  97. return *this;
  98. }
  99. CSuperLog & CSuperLog::operator << (const signed short ssVal)
  100. {
  101. WCHAR wszBuffer[50];
  102. //Convert to String...
  103. _itow(ssVal, wszBuffer, 10);
  104. OutputText(wszBuffer);
  105. return *this;
  106. }
  107. CSuperLog & CSuperLog::operator << (const signed sVal)
  108. {
  109. WCHAR wszBuffer[50];
  110. //Convert to String...
  111. _itow(sVal, wszBuffer, 10);
  112. OutputText(wszBuffer);
  113. return *this;
  114. }
  115. CSuperLog & CSuperLog::operator << (const unsigned uVal)
  116. {
  117. WCHAR wszBuffer[50];
  118. //Convert to String...
  119. _ultow(uVal, wszBuffer, 10);
  120. OutputText(wszBuffer);
  121. return *this;
  122. }
  123. CSuperLog & CSuperLog::operator << (char const *szVal)
  124. {
  125. WCHAR wszBuffer[256];
  126. size_t len = szVal ? strlen(szVal) : 0;
  127. if(len < 256)
  128. {
  129. //static buffer
  130. mbstowcs(wszBuffer, szVal, 256);
  131. OutputText(wszBuffer);
  132. }
  133. else
  134. {
  135. //dynamically alloc
  136. WCHAR* pwsz = (WCHAR*)CoTaskMemAlloc((len+1) * sizeof(WCHAR));
  137. mbstowcs(pwsz, szVal, len+1);
  138. OutputText(pwsz);
  139. SAFE_FREE(pwsz);
  140. }
  141. return *this;
  142. }
  143. CSuperLog & CSuperLog::operator << (wchar_t const * wszVal)
  144. {
  145. OutputText((WCHAR*)wszVal);
  146. return *this;
  147. }
  148. CSuperLog & CSuperLog::operator << (const double rVal)
  149. {
  150. WCHAR wszBuffer[50];
  151. //Convert to String...
  152. swprintf(wszBuffer, 50, L"%e", rVal);
  153. OutputText(wszBuffer);
  154. return *this;
  155. }
  156. CSuperLog & CSuperLog::operator << (const VARIANT vVariant)
  157. {
  158. //no-op - bstr
  159. switch(V_VT(&vVariant))
  160. {
  161. //no-op - no need to copy data.
  162. case VT_BSTR:
  163. OutputText(V_BSTR(&vVariant));
  164. break;
  165. default:
  166. {
  167. VARIANT vVarTemp;
  168. VariantInit(&vVarTemp);
  169. //Delgate
  170. HRESULT hr = VariantChangeType(
  171. &vVarTemp, // Destination (convert not in place)
  172. (VARIANT*)&vVariant, // Source
  173. 0, // dwFlags
  174. VT_BSTR);
  175. if(SUCCEEDED(hr))
  176. OutputText(V_BSTR(&vVarTemp));
  177. VariantClear(&vVarTemp);
  178. break;
  179. }
  180. };
  181. return *this;
  182. }
  183. int CSuperLog::ScreenLogging(BOOL fScreen)
  184. {
  185. return fScreen;
  186. }
  187. HRESULT CSuperLog::SetErrorInterface(IError* pIError)
  188. {
  189. if(pIError == NULL)
  190. return E_INVALIDARG;
  191. SAFE_RELEASE(m_pIError)
  192. return pIError->QueryInterface(IID_IError, (void**)&m_pIError);
  193. }
  194. HRESULT CSuperLog::GetErrorInterface(IError** ppIError)
  195. {
  196. if(m_pIError)
  197. return m_pIError->QueryInterface(IID_IError, (void**)ppIError);
  198. return E_FAIL;
  199. }
  200. HRESULT CSuperLog::OutputText(WCHAR* pwszText)
  201. {
  202. if(pwszText)
  203. {
  204. if(/*!m_fTriedErrorAlready &&*/ m_pIError == NULL)
  205. {
  206. //Try to find LTM
  207. HRESULT hr = CoCreateInstance(CLSID_LTMLITE, NULL, CLSCTX_INPROC_SERVER, IID_IError, (void **)&m_pIError);
  208. if(SUCCEEDED(hr) && m_pIError)
  209. m_pIError->Initialize();
  210. m_fTriedErrorAlready = TRUE;
  211. }
  212. if(m_pIError == NULL)
  213. return E_FAIL;
  214. //Unfortunately for marshalling the string must be a "real"
  215. //BSTR, and not just a WCHAR*, since it will be marsalled accross boundaries...
  216. BSTR bstrString = NULL;
  217. SAFE_SYSALLOC(bstrString, pwszText);
  218. //Transmit the string using the IError::Transmit method...
  219. m_pIError->Transmit(bstrString);
  220. SAFE_SYSFREE(bstrString);
  221. }
  222. CLEANUP:
  223. return S_OK;
  224. }