mirror of
https://github.com/opensim/opensim.git
synced 2026-08-11 11:57:03 +08:00
* example soft logger by delegate
This commit is contained in:
85
Common/OpenSim.Framework/Logger.cs
Normal file
85
Common/OpenSim.Framework/Logger.cs
Normal file
@@ -0,0 +1,85 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace OpenSim.Framework
|
||||
{
|
||||
public class Logger
|
||||
{
|
||||
public static Logger Instance = new Logger( false );
|
||||
|
||||
public delegate void LoggerMethodDelegate();
|
||||
private delegate bool LoggerDelegate( LoggerMethodDelegate whatToDo );
|
||||
|
||||
|
||||
private LoggerDelegate m_delegate;
|
||||
|
||||
public Logger( bool log )
|
||||
{
|
||||
if( log )
|
||||
{
|
||||
m_delegate = CatchAndLog;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_delegate = DontCatch;
|
||||
}
|
||||
}
|
||||
|
||||
public bool Wrap( LoggerMethodDelegate whatToDo )
|
||||
{
|
||||
return m_delegate( whatToDo );
|
||||
}
|
||||
|
||||
|
||||
private bool CatchAndLog(LoggerMethodDelegate whatToDo)
|
||||
{
|
||||
try
|
||||
{
|
||||
whatToDo();
|
||||
return true;
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
System.Console.WriteLine( "Exception logged!!! Woah!!!!" );
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private bool DontCatch(LoggerMethodDelegate whatToDo)
|
||||
{
|
||||
whatToDo();
|
||||
return true;
|
||||
}
|
||||
|
||||
public class LoggerExample
|
||||
{
|
||||
public void TryWrap()
|
||||
{
|
||||
// This will log and ignore
|
||||
Logger log = new Logger(true);
|
||||
|
||||
log.Wrap(delegate()
|
||||
{
|
||||
Int16.Parse("waa!");
|
||||
});
|
||||
|
||||
// This will throw;
|
||||
try
|
||||
{
|
||||
|
||||
log = new Logger(false);
|
||||
|
||||
log.Wrap(delegate()
|
||||
{
|
||||
Int16.Parse("waa!");
|
||||
});
|
||||
}
|
||||
catch
|
||||
{
|
||||
System.Console.WriteLine("Example barfed!");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -102,6 +102,9 @@
|
||||
<Compile Include="IRegionCommsListener.cs">
|
||||
<SubType>Code</SubType>
|
||||
</Compile>
|
||||
<Compile Include="Logger.cs">
|
||||
<SubType>Code</SubType>
|
||||
</Compile>
|
||||
<Compile Include="LoginService.cs">
|
||||
<SubType>Code</SubType>
|
||||
</Compile>
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
<include name="AuthenticateSessionBase.cs" />
|
||||
<include name="BlockingQueue.cs" />
|
||||
<include name="IRegionCommsListener.cs" />
|
||||
<include name="Logger.cs" />
|
||||
<include name="LoginService.cs" />
|
||||
<include name="RegionCommsListener.cs" />
|
||||
<include name="Remoting.cs" />
|
||||
|
||||
Reference in New Issue
Block a user