ApiLog.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. // FileZilla - a Windows ftp client
  2. // Copyright (C) 2002-2004 - Tim Kosse <[email protected]>
  3. // This program is free software; you can redistribute it and/or
  4. // modify it under the terms of the GNU General Public License
  5. // as published by the Free Software Foundation; either version 2
  6. // of the 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 General Public License for more details.
  11. // You should have received a copy of the GNU General Public License
  12. // along with this program; if not, write to the Free Software
  13. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. // ApiLog.cpp: Implementierung der Klasse CApiLog.
  15. //
  16. //////////////////////////////////////////////////////////////////////
  17. #include "stdafx.h"
  18. #include "ApiLog.h"
  19. #ifdef _DEBUG
  20. #undef THIS_FILE
  21. static char THIS_FILE[]=__FILE__;
  22. #define new DEBUG_NEW
  23. #endif
  24. //////////////////////////////////////////////////////////////////////
  25. // Konstruktion/Destruktion
  26. //////////////////////////////////////////////////////////////////////
  27. CApiLog::CApiLog()
  28. {
  29. m_hTargetWnd=0;
  30. m_pApiLogParent=0;
  31. m_nDebugLevel=0;
  32. m_nLogMessage=0;
  33. }
  34. CApiLog::~CApiLog()
  35. {
  36. }
  37. BOOL CApiLog::InitLog(CApiLog *pParent)
  38. {
  39. if (!pParent)
  40. return FALSE;
  41. while (pParent->m_pApiLogParent)
  42. pParent=pParent->m_pApiLogParent;
  43. m_hTargetWnd=0;
  44. m_pApiLogParent=pParent;
  45. return TRUE;
  46. }
  47. BOOL CApiLog::InitLog(HWND hTargetWnd, int nLogMessage)
  48. {
  49. if (!hTargetWnd)
  50. return FALSE;
  51. m_hTargetWnd=hTargetWnd;
  52. m_nLogMessage=nLogMessage;
  53. m_pApiLogParent=0;
  54. return TRUE;
  55. }
  56. bool CApiLog::LoggingMessageType(int nMessageType) const
  57. {
  58. return
  59. (nMessageType < FZ_LOG_APIERROR) ||
  60. ((nMessageType-FZ_LOG_APIERROR) < m_pApiLogParent->m_nDebugLevel);
  61. }
  62. void CApiLog::LogMessage(int nMessageType, LPCTSTR pMsgFormat, ...) const
  63. {
  64. ASSERT(nMessageType>=0 && nMessageType<=8);
  65. ASSERT(m_hTargetWnd || m_pApiLogParent);
  66. if (!LoggingMessageType(nMessageType))
  67. return;
  68. va_list ap;
  69. va_start(ap, pMsgFormat);
  70. CString text;
  71. text.FormatV(pMsgFormat, ap);
  72. va_end(ap);
  73. #ifdef MPEXT
  74. if (nMessageType>=FZ_LOG_DEBUG)
  75. return;
  76. #endif
  77. SendLogMessage(nMessageType, text);
  78. }
  79. void CApiLog::LogMessageRaw(int nMessageType, LPCTSTR pMsg) const
  80. {
  81. ASSERT(nMessageType>=0 && nMessageType<=8);
  82. ASSERT(m_hTargetWnd || m_pApiLogParent);
  83. if (!LoggingMessageType(nMessageType))
  84. return;
  85. #ifdef MPEXT
  86. if (nMessageType>=FZ_LOG_DEBUG)
  87. return;
  88. #endif
  89. SendLogMessage(nMessageType, pMsg);
  90. }
  91. void CApiLog::LogMessage(int nMessageType, UINT nFormatID, ...) const
  92. {
  93. ASSERT(nMessageType>=0 && nMessageType<=8);
  94. ASSERT(m_hTargetWnd || m_pApiLogParent);
  95. if (!LoggingMessageType(nMessageType))
  96. return;
  97. CString str;
  98. str.LoadString(nFormatID);
  99. va_list ap;
  100. va_start(ap, nFormatID);
  101. CString text;
  102. text.FormatV(str, ap);
  103. va_end(ap);
  104. #ifdef MPEXT
  105. if (nMessageType>=FZ_LOG_DEBUG)
  106. return;
  107. #endif
  108. SendLogMessage(nMessageType, text);
  109. }
  110. void CApiLog::LogMessage(CString SourceFile, int nSourceLine, void *pInstance, int nMessageType, LPCTSTR pMsgFormat, ...) const
  111. {
  112. ASSERT(nMessageType>=4 && nMessageType<=8);
  113. ASSERT(m_hTargetWnd || m_pApiLogParent);
  114. ASSERT(nSourceLine>0);
  115. int pos=SourceFile.ReverseFind(_MPT('\\'));
  116. if (pos!=-1)
  117. SourceFile=SourceFile.Mid(pos+1);
  118. va_list ap;
  119. va_start(ap, pMsgFormat);
  120. CString text;
  121. text.FormatV(pMsgFormat, ap);
  122. va_end(ap);
  123. #ifdef MPEXT
  124. if (nMessageType>=FZ_LOG_DEBUG)
  125. return;
  126. #endif
  127. CString msg;
  128. msg.Format(_T("%s(%d): %s caller=0x%08x"), SourceFile, nSourceLine, text, (int)this);
  129. SendLogMessage(nMessageType, msg);
  130. }
  131. #ifdef MPEXT
  132. BOOL CApiLog::PostMessage(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) const
  133. {
  134. return m_pApiLogParent->PostMessage(hWnd, Msg, wParam, lParam);
  135. }
  136. #endif
  137. void CApiLog::LogMessageRaw(CString SourceFile, int nSourceLine, void *pInstance, int nMessageType, LPCTSTR pMsg) const
  138. {
  139. ASSERT(nMessageType>=4 && nMessageType<=8);
  140. ASSERT(m_hTargetWnd || m_pApiLogParent);
  141. ASSERT(nSourceLine>0);
  142. int pos=SourceFile.ReverseFind(_MPT('\\'));
  143. if (pos!=-1)
  144. SourceFile=SourceFile.Mid(pos+1);
  145. #ifdef MPEXT
  146. if (nMessageType>=FZ_LOG_DEBUG)
  147. return;
  148. #endif
  149. CString msg;
  150. msg.Format(_T("%s(%d): %s caller=0x%08x"), SourceFile, nSourceLine, pMsg, (int)this);
  151. SendLogMessage(nMessageType, msg);
  152. }
  153. void CApiLog::SendLogMessage(int nMessageType, LPCTSTR pMsg) const
  154. {
  155. #ifdef MPEXT
  156. ASSERT(m_pApiLogParent);
  157. ASSERT(m_pApiLogParent->m_hTargetWnd == 0);
  158. ASSERT(m_pApiLogParent->m_nLogMessage == 0);
  159. if (!LoggingMessageType(nMessageType))
  160. return;
  161. //Displays a message in the message log
  162. t_ffam_statusmessage *pStatus = new t_ffam_statusmessage;
  163. pStatus->post = TRUE;
  164. pStatus->status = pMsg;
  165. pStatus->type = nMessageType;
  166. if (!this->PostMessage(m_pApiLogParent->m_hTargetWnd, m_pApiLogParent->m_nLogMessage, FZ_MSG_MAKEMSG(FZ_MSG_STATUS, 0), (LPARAM)pStatus))
  167. delete pStatus;
  168. #else
  169. if (m_hTargetWnd)
  170. {
  171. ASSERT(m_nLogMessage);
  172. if (nMessageType>=FZ_LOG_APIERROR && (nMessageType-FZ_LOG_APIERROR)>=m_nDebugLevel)
  173. return;
  174. }
  175. else
  176. {
  177. ASSERT(m_pApiLogParent);
  178. ASSERT(m_pApiLogParent->m_hTargetWnd);
  179. ASSERT(m_pApiLogParent->m_nLogMessage);
  180. if (!LoggingMessageType(nMessageType))
  181. return;
  182. }
  183. //Displays a message in the message log
  184. t_ffam_statusmessage *pStatus = new t_ffam_statusmessage;
  185. pStatus->post = TRUE;
  186. pStatus->status = pMsg;
  187. pStatus->type = nMessageType;
  188. if (m_hTargetWnd)
  189. {
  190. if (!PostMessage(m_hTargetWnd, m_nLogMessage, FZ_MSG_MAKEMSG(FZ_MSG_STATUS, 0), (LPARAM)pStatus))
  191. delete pStatus;
  192. }
  193. else
  194. if (!PostMessage(m_pApiLogParent->m_hTargetWnd, m_pApiLogParent->m_nLogMessage, FZ_MSG_MAKEMSG(FZ_MSG_STATUS, 0), (LPARAM)pStatus))
  195. delete pStatus;
  196. #endif
  197. }
  198. BOOL CApiLog::SetDebugLevel(int nDebugLevel)
  199. {
  200. if (m_pApiLogParent)
  201. return FALSE;
  202. if (nDebugLevel<0 || nDebugLevel>4)
  203. return FALSE;
  204. m_nDebugLevel=nDebugLevel;
  205. return TRUE;
  206. }
  207. int CApiLog::GetDebugLevel()
  208. {
  209. if (m_pApiLogParent)
  210. return m_pApiLogParent->m_nDebugLevel;
  211. return m_nDebugLevel;
  212. }