1
0

ApiLog.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. void CApiLog::LogMessage(int nMessageType, LPCTSTR pMsgFormat, ...) const
  57. {
  58. ASSERT(nMessageType>=0 && nMessageType<=8);
  59. ASSERT(m_hTargetWnd || m_pApiLogParent);
  60. if (nMessageType>=FZ_LOG_APIERROR && (nMessageType-FZ_LOG_APIERROR)>=m_pApiLogParent->m_nDebugLevel)
  61. return;
  62. va_list ap;
  63. va_start(ap, pMsgFormat);
  64. CString text;
  65. text.FormatV(pMsgFormat, ap);
  66. va_end(ap);
  67. #ifdef MPEXT
  68. if (nMessageType>=FZ_LOG_DEBUG)
  69. return;
  70. #endif
  71. SendLogMessage(nMessageType, text);
  72. }
  73. void CApiLog::LogMessageRaw(int nMessageType, LPCTSTR pMsg) const
  74. {
  75. ASSERT(nMessageType>=0 && nMessageType<=8);
  76. ASSERT(m_hTargetWnd || m_pApiLogParent);
  77. if (nMessageType>=FZ_LOG_APIERROR && (nMessageType-FZ_LOG_APIERROR)>=m_pApiLogParent->m_nDebugLevel)
  78. return;
  79. #ifdef MPEXT
  80. if (nMessageType>=FZ_LOG_DEBUG)
  81. return;
  82. #endif
  83. SendLogMessage(nMessageType, pMsg);
  84. }
  85. void CApiLog::LogMessage(int nMessageType, UINT nFormatID, ...) const
  86. {
  87. ASSERT(nMessageType>=0 && nMessageType<=8);
  88. ASSERT(m_hTargetWnd || m_pApiLogParent);
  89. if (nMessageType>=FZ_LOG_APIERROR && (nMessageType-FZ_LOG_APIERROR)>=m_pApiLogParent->m_nDebugLevel)
  90. return;
  91. CString str;
  92. str.LoadString(nFormatID);
  93. va_list ap;
  94. va_start(ap, nFormatID);
  95. CString text;
  96. text.FormatV(str, ap);
  97. va_end(ap);
  98. #ifdef MPEXT
  99. if (nMessageType>=FZ_LOG_DEBUG)
  100. return;
  101. #endif
  102. SendLogMessage(nMessageType, text);
  103. }
  104. void CApiLog::LogMessage(CString SourceFile, int nSourceLine, void *pInstance, int nMessageType, LPCTSTR pMsgFormat, ...) const
  105. {
  106. ASSERT(nMessageType>=4 && nMessageType<=8);
  107. ASSERT(m_hTargetWnd || m_pApiLogParent);
  108. ASSERT(nSourceLine>0);
  109. int pos=SourceFile.ReverseFind(_MPT('\\'));
  110. if (pos!=-1)
  111. SourceFile=SourceFile.Mid(pos+1);
  112. va_list ap;
  113. va_start(ap, pMsgFormat);
  114. CString text;
  115. text.FormatV(pMsgFormat, ap);
  116. va_end(ap);
  117. #ifdef MPEXT
  118. if (nMessageType>=FZ_LOG_DEBUG)
  119. return;
  120. #endif
  121. CString msg;
  122. msg.Format(_T("%s(%d): %s caller=0x%08x"), SourceFile, nSourceLine, text, (int)this);
  123. SendLogMessage(nMessageType, msg);
  124. }
  125. #ifdef MPEXT
  126. BOOL CApiLog::PostMessage(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) const
  127. {
  128. return m_pApiLogParent->PostMessage(hWnd, Msg, wParam, lParam);
  129. }
  130. #endif
  131. void CApiLog::LogMessageRaw(CString SourceFile, int nSourceLine, void *pInstance, int nMessageType, LPCTSTR pMsg) const
  132. {
  133. ASSERT(nMessageType>=4 && nMessageType<=8);
  134. ASSERT(m_hTargetWnd || m_pApiLogParent);
  135. ASSERT(nSourceLine>0);
  136. int pos=SourceFile.ReverseFind(_MPT('\\'));
  137. if (pos!=-1)
  138. SourceFile=SourceFile.Mid(pos+1);
  139. #ifdef MPEXT
  140. if (nMessageType>=FZ_LOG_DEBUG)
  141. return;
  142. #endif
  143. CString msg;
  144. msg.Format(_T("%s(%d): %s caller=0x%08x"), SourceFile, nSourceLine, pMsg, (int)this);
  145. SendLogMessage(nMessageType, msg);
  146. }
  147. void CApiLog::SendLogMessage(int nMessageType, LPCTSTR pMsg) const
  148. {
  149. #ifdef MPEXT
  150. ASSERT(m_pApiLogParent);
  151. ASSERT(m_pApiLogParent->m_hTargetWnd == 0);
  152. ASSERT(m_pApiLogParent->m_nLogMessage == 0);
  153. if (nMessageType>=FZ_LOG_APIERROR && (nMessageType-FZ_LOG_APIERROR)>=m_pApiLogParent->m_nDebugLevel)
  154. return;
  155. //Displays a message in the message log
  156. t_ffam_statusmessage *pStatus = new t_ffam_statusmessage;
  157. pStatus->post = TRUE;
  158. pStatus->status = pMsg;
  159. pStatus->type = nMessageType;
  160. if (!this->PostMessage(m_pApiLogParent->m_hTargetWnd, m_pApiLogParent->m_nLogMessage, FZ_MSG_MAKEMSG(FZ_MSG_STATUS, 0), (LPARAM)pStatus))
  161. delete pStatus;
  162. #else
  163. if (m_hTargetWnd)
  164. {
  165. ASSERT(m_nLogMessage);
  166. if (nMessageType>=FZ_LOG_APIERROR && (nMessageType-FZ_LOG_APIERROR)>=m_nDebugLevel)
  167. return;
  168. }
  169. else
  170. {
  171. ASSERT(m_pApiLogParent);
  172. ASSERT(m_pApiLogParent->m_hTargetWnd);
  173. ASSERT(m_pApiLogParent->m_nLogMessage);
  174. if (nMessageType>=FZ_LOG_APIERROR && (nMessageType-FZ_LOG_APIERROR)>=m_pApiLogParent->m_nDebugLevel)
  175. return;
  176. }
  177. //Displays a message in the message log
  178. t_ffam_statusmessage *pStatus = new t_ffam_statusmessage;
  179. pStatus->post = TRUE;
  180. pStatus->status = pMsg;
  181. pStatus->type = nMessageType;
  182. if (m_hTargetWnd)
  183. {
  184. if (!PostMessage(m_hTargetWnd, m_nLogMessage, FZ_MSG_MAKEMSG(FZ_MSG_STATUS, 0), (LPARAM)pStatus))
  185. delete pStatus;
  186. }
  187. else
  188. if (!PostMessage(m_pApiLogParent->m_hTargetWnd, m_pApiLogParent->m_nLogMessage, FZ_MSG_MAKEMSG(FZ_MSG_STATUS, 0), (LPARAM)pStatus))
  189. delete pStatus;
  190. #endif
  191. }
  192. BOOL CApiLog::SetDebugLevel(int nDebugLevel)
  193. {
  194. if (m_pApiLogParent)
  195. return FALSE;
  196. if (nDebugLevel<0 || nDebugLevel>4)
  197. return FALSE;
  198. m_nDebugLevel=nDebugLevel;
  199. return TRUE;
  200. }
  201. int CApiLog::GetDebugLevel()
  202. {
  203. if (m_pApiLogParent)
  204. return m_pApiLogParent->m_nDebugLevel;
  205. return m_nDebugLevel;
  206. }