ApiLog.cpp 6.1 KB

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