//-----------------------------------------------------------------------------
// wx.NET - Log.cs
//
// The Log wrapper classes.
//
// Written by Alexander Olk (xenomorph2@onlinehome.de)
// (C) 2003 Alexander Olk
// Licensed under the wxWidgets license, see LICENSE.txt for details.
//
// $Id: Log.cs,v 1.9 2007/11/24 17:55:45 harald_meyer Exp $
//-----------------------------------------------------------------------------
using System;
using System.Drawing;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace wx{
/** Wrapper of the \e wxWidgets log class.
* Refer to LogTraceListener for System.Diagnostics.Trace support.
*/
public class Log: Object
{
public enum eLogLevel : int
{
xLOGMESSAGE,
xFATALERROR,
xERROR,
xWARNING,
xINFO,
xVERBOSE,
xSTATUS,
xSYSERROR
}
[DllImport("wx-c")] static extern IntPtr wxLog_ctor();
[DllImport("wx-c")] static extern bool wxLog_IsEnabled();
[DllImport("wx-c")] static extern void wxLog_EnableLogging(bool yesOrNo);
[DllImport("wx-c")] static extern void wxLog_FlushActive();
[DllImport("wx-c")] static extern IntPtr wxLog_SetActiveTargetTextCtrl(IntPtr pLogger);
[DllImport("wx-c")] static internal extern void wxLog_Log_Function(int what, IntPtr szFormat);
[DllImport("wx-c")] static extern void wxLog_AddTraceMask(IntPtr tmask);
[DllImport("wx-c")] static extern void wxLog_RemoveTraceMask(IntPtr tmask);
[DllImport("wx-c")] static extern void wxLog_LogTraceStringMask(IntPtr mask, IntPtr msg);
[DllImport("wx-c")] static extern IntPtr wxLog_GetTraceMasks();
[DllImport("wx-c")] static extern void wxLog_ClearTraceMasks();
[DllImport("wx-c")][return:MarshalAs(UnmanagedType.U1)] static extern bool wxLog_IsAllowedTraceMask(IntPtr mask);
[DllImport("wx-c")] static extern void wxLog_Suspend();
[DllImport("wx-c")] static extern void wxLog_Resume();
[DllImport("wx-c")] static extern void wxLog_SetTimestamp(IntPtr ts);
[DllImport("wx-c")] static extern IntPtr wxLog_GetTimestamp();
public Log(IntPtr wxObject)
: base(wxObject) {}
public Log()
: base(wxLog_ctor()) {}
/** Read whether this is enabled or not and enable or disable assigning \c true or \c false respectively.
*/
public static bool IsEnabled
{
get { return wxLog_IsEnabled(); }
set { wxLog_EnableLogging(value); }
}
/** Gets or sets the timestamp format prepended by the default log targets to all messages.
* The string may contain any normal characters as well as % prefixed
* format specificators, see strftime() manual for details.
* Passing a \c null value (not empty string) to this function disables
* message timestamping.
*/
public static string Timestamp
{
get { return new wxString(wxLog_GetTimestamp()); }
set
{
wxString wxValue = wxString.SafeNew(value);
wxLog_SetTimestamp(Object.SafePtr(wxValue));
}
}
public static void Suspend()
{ wxLog_Suspend(); }
public static void Resume()
{ wxLog_Resume(); }
public static void FlushActive()
{
wxLog_FlushActive();
}
/** at the moment only TextCtrl
*/
public static void SetActiveTarget(TextCtrl pLogger)
{
wxLog_SetActiveTargetTextCtrl(Object.SafePtr(pLogger));
}
/** Add the mask to the list of allowed masks for LogTrace().
*/
public static void AddTraceMask(string tmask)
{
AddTraceMask(new wxString(tmask));
}
public static void AddTraceMask(wxString tmask)
{
wxLog_AddTraceMask(tmask.wxObject);
}
/** Remove the mask from the list of allowed masks for LogTrace().
*See also: AddTraceMask().
*/
public static void RemoveTraceMask(string tmask)
{
Log.RemoveTraceMask(wxString.SafeNew(tmask));
}
public static void RemoveTraceMask(wxString tmask)
{
wxLog_RemoveTraceMask(Object.SafePtr(tmask));
}
public static string[] TraceMasks
{
get
{
ArrayString masks = new ArrayString(wxLog_GetTraceMasks());
return masks.ToArray();
}
}
/** True if mask \c tmask is in TraceMasks().
* This means that output with this mask will be logged.
*/
public static bool IsAllowedTraceMask(string tmask)
{
return IsAllowedTraceMask(wxString.SafeNew(tmask));
}
public static bool IsAllowedTraceMask(wxString tmask)
{
return wxLog_IsAllowedTraceMask(Object.SafePtr(tmask));
}
/** Trace functions only do something in debug build and expand to nothing in the release one.
* The reason for making it a separate function from it is that usually there are a lot of trace
* messages, so it might make sense to separate them from other debug messages.
*
* For the second function (taking a string mask), the message is logged only if the mask has been previously enabled by the call to AddTraceMask or by setting WXTRACE environment variable. The predefined string trace masks used by wxWidgets are:
* \li \c wxTRACE_MemAlloc: trace memory allocation (new/delete)
* \li \c wxTRACE_Messages: trace window messages/X callbacks
* \li \c wxTRACE_ResAlloc: trace GDI resource allocation
* \li \c wxTRACE_RefCount: trace various ref counting operations
* \li \c wxTRACE_OleCalls: trace OLE method calls (Win32 only)
*/
public static void LogTrace(string mask, string format, params object[] param)
{
LogTrace(wxString.SafeNew(mask), wxString.SafeNew(string.Format(format, param)));
}
public static void LogTrace(wxString mask, wxString msg)
{
wxLog_LogTraceStringMask(Object.SafePtr(mask), Object.SafePtr(msg));
}
public static void LogMessage(string message, params object[] param)
{
LogMessage(new wxString(string.Format(message, param)));
}
public static void LogMessage(string message)
{
LogMessage(new wxString(message));
}
public static void LogMessage(wxString message)
{
wxLog_Log_Function((int)eLogLevel.xLOGMESSAGE, message.wxObject);
}
public static void LogFatalError(string message, params object[] param)
{
LogFatalError(new wxString(string.Format(message, param)));
}
public static void LogFatalError(string message)
{
LogFatalError(new wxString(message));
}
public static void LogFatalError(wxString message)
{
wxLog_Log_Function((int)eLogLevel.xFATALERROR, message.wxObject);
}
public static void LogError(string message, params object[] param)
{
LogError(new wxString(string.Format(message, param)));
}
public static void LogError(string message)
{
LogError(new wxString(message));
}
public static void LogError(wxString message)
{
wxLog_Log_Function((int)eLogLevel.xERROR, message.wxObject);
}
public static void LogWarning(string message, params object[] param)
{
LogWarning(new wxString(string.Format(message, param)));
}
public static void LogWarning(string message)
{
LogWarning(new wxString(message));
}
public static void LogWarning(wxString message)
{
wxLog_Log_Function((int)eLogLevel.xWARNING, message.wxObject);
}
public static void LogInfo(string message, params object[] param)
{
LogInfo(new wxString(string.Format(message, param)));
}
public static void LogInfo(string message)
{
LogInfo(new wxString(message));
}
public static void LogInfo(wxString message)
{
wxLog_Log_Function((int)eLogLevel.xINFO, message.wxObject);
}
public static void LogVerbose(string message, params object[] param)
{
LogVerbose(new wxString(string.Format(message, param)));
}
public static void LogVerbose(string message)
{
LogVerbose(new wxString(message));
}
public static void LogVerbose(wxString message)
{
wxLog_Log_Function((int)eLogLevel.xVERBOSE, message.wxObject);
}
public static void LogStatus(string message, params object[] param)
{
LogStatus(new wxString(string.Format(message, param)));
}
public static void LogStatus(string message)
{
LogStatus(new wxString(message));
}
public static void LogStatus(wxString message)
{
wxLog_Log_Function((int)eLogLevel.xSTATUS, message.wxObject);
}
public static void LogSysError(string message, params object[] param)
{
LogSysError(new wxString(string.Format(message, param)));
}
public static void LogSysError(string message)
{
LogSysError(new wxString(message));
}
public static void LogSysError(wxString message)
{
wxLog_Log_Function((int)eLogLevel.xSYSERROR, message.wxObject);
}
}
/** This class integrates \e wxWidget's Log into .NET's trace capability.
* Add an instance of this listener to the Trace and all output to the Trace log
* will also appear in listeners to \e wxWidget's Log.
* */
public class LogTraceListener : TraceListener
{
Log.eLogLevel _eLevel;
public LogTraceListener(Log.eLogLevel level)
{
this._eLevel = level;
}
/** This will post all messages to the Trace as Log.eLogLevel.xLOGMESSAGE().
* */
public LogTraceListener()
: this(Log.eLogLevel.xLOGMESSAGE)
{
}
public override void Write(string message)
{
wxString wxMessage = new wxString(message);
Log.wxLog_Log_Function((int)this._eLevel, wxMessage.wxObject);
}
public override void WriteLine(string message)
{
wxString wxMessage = new wxString(message+"\n");
Log.wxLog_Log_Function((int)this._eLevel, wxMessage.wxObject);
}
/** Returns the log level.
* */
public Log.eLogLevel Level { get { return this._eLevel; } }
}
}
|