aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Common/OpenSim.Framework/Logger.cs
blob: e7eaa038403e9920b843399aa60ae34b18374099 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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!");
                }
            }
        }
    }              
}