summaryrefslogtreecommitdiff
path: root/src/bindings/mono/eina_mono/eina_log.cs
blob: 267197155006ccdd7eb462180bb43b9e96f7c25a (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#pragma warning disable 1591

using System;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
using System.Diagnostics.Contracts;

namespace Eina
{
// Manual wrappers around eina functions

/// <summary>EFL Logging facilities.
/// <para>Since EFL 1.23.</para>
/// </summary>
public class Log
{
    [DllImport(efl.Libs.Eina)] private static extern void eina_log_print(
            int domain,
            Level level,
            [MarshalAs(UnmanagedType.LPStr)] String file,
            [MarshalAs(UnmanagedType.LPStr)] String function,
            int line,
            [MarshalAs(UnmanagedType.LPStr)] String message);

    [DllImport(efl.Libs.Eina)] private static extern int eina_log_domain_register(
            [MarshalAs(UnmanagedType.LPStr)] String name,
            [MarshalAs(UnmanagedType.LPStr)] String color);

    [DllImport(efl.Libs.Eina)] private static extern void eina_log_level_set(Level level);

    [DllImport(efl.Libs.Eina)] private static extern Level eina_log_level_get();

    /// <summary>The levels of logging.
    /// <para>Since EFL 1.23.</para>
    /// </summary>
    public enum Level
    {
        /// <summary>Critical events.
        /// <para>Since EFL 1.23.</para>
        /// </summary>
        Critical,
        /// <summary>Error events.
        /// <para>Since EFL 1.23.</para>
        /// </summary>
        Error,
        /// <summary>Warning events.
        /// <para>Since EFL 1.23.</para>
        /// </summary>
        Warning,
        /// <summary>Informative events.
        /// <para>Since EFL 1.23.</para>
        /// </summary>
        Info,
        /// <summary>Debugging messages.
        /// <para>Since EFL 1.23.</para>
        /// </summary>
        Debug,
        /// <summary>Unknown events.
        /// <para>Since EFL 1.23.</para>
        /// </summary>
        Unkown = (-2147483647 - 1)
    }

    /// <summary>The colors to be used by the logging system.
    /// <para>Since EFL 1.23.</para>
    /// </summary>
    public class Color
    {
        /// <summary>Light red
        /// <para>Since EFL 1.23.</para>
        /// </summary>
        public static string LIGHTRED  = "\033[31;1m";
        /// <summary>Red
        /// <para>Since EFL 1.23.</para>
        /// </summary>
        public static string RED       = "\033[31m";
        /// <summary>Light blue
        /// <para>Since EFL 1.23.</para>
        /// </summary>
        public static string LIGHTBLUE = "\033[34;1m";
        /// <summary>Blue
        /// <para>Since EFL 1.23.</para>
        /// </summary>
        public static string BLUE      = "\033[34m";
        /// <summary>Green
        /// <para>Since EFL 1.23.</para>
        /// </summary>
        public static string GREEN     = "\033[32;1m";
        /// <summary>Yellow
        /// <para>Since EFL 1.23.</para>
        /// </summary>
        public static string YELLOW    = "\033[33;1m";
        /// <summary>Orange
        /// <para>Since EFL 1.23.</para>
        /// </summary>
        public static string ORANGE    = "\033[0;33m";
        /// <summary>White
        /// <para>Since EFL 1.23.</para>
        /// </summary>
        public static string WHITE     = "\033[37;1m";
        /// <summary>Light cyan
        /// <para>Since EFL 1.23.</para>
        /// </summary>
        public static string LIGHTCYAN = "\033[36;1m";
        /// <summary>Cyan
        /// <para>Since EFL 1.23.</para>
        /// </summary>
        public static string CYAN      = "\033[36m";
        /// <summary>Reset
        /// <para>Since EFL 1.23.</para>
        /// </summary>
        public static string RESET     = "\033[0m";
        /// <summary>Bold
        /// <para>Since EFL 1.23.</para>
        /// </summary>
        public static string HIGH      = "\033[1m";
    }

    private static int domain = -1;

    /// <summary>Static class initializer.
    /// <para>Since EFL 1.23.</para>
    /// </summary>
    static Log()
    {
        const String name = "mono";
        const String color = "\033[32;1m";

        // Maybe move this check outside when other eina stuff get support?
        domain = eina_log_domain_register(name, color);
        if (domain < 0)
        {
            Console.WriteLine("Error: Couldn't register Eina log domain for name {0}.", name);
        }
        else
        {
            Info($"Registered mono domain with number {domain}");
        }
    }

    private static void EnsureDomainRegistered()
    {
        if (domain < 0)
        {
            throw new InvalidOperationException("Log domain is not registered.");
        }
    }

    /// <summary>Prints a critical message with context info. This context is
    /// filled automatically by the C# compiler.
    /// <para>Since EFL 1.23.</para>
    /// </summary>
    /// <param name="message">The message to be printed.</param>
    /// <param name="line">The line number this method was called from.</param>
    /// <param name="file">The file this method was called from.</param>
    /// <param name="member">The enlosing method this method was called from.</param>
    public static void Critical(String message, [CallerLineNumber] int line = 0, [CallerFilePath] string file = null, [CallerMemberName] string member = null)
    {
        EnsureDomainRegistered();
        eina_log_print(domain, Level.Critical, file, member, line, message);
    }

    /// <summary>Prints an error message with context info. This context is
    /// filled automatically by the C# compiler.
    /// <para>Since EFL 1.23.</para>
    /// </summary>
    /// <param name="message">The message to be printed.</param>
    /// <param name="line">The line number this method was called from.</param>
    /// <param name="file">The file this method was called from.</param>
    /// <param name="member">The enlosing method this method was called from.</param>
    public static void Error(String message, [CallerLineNumber] int line = 0, [CallerFilePath] string file = null, [CallerMemberName] string member = null)
    {
        EnsureDomainRegistered();
        eina_log_print(domain, Level.Error, file, member, line, message);
    }

    /// <summary>Prints a warning message with context info. This context is
    /// filled automatically by the C# compiler.
    /// <para>Since EFL 1.23.</para>
    /// </summary>
    /// <param name="message">The message to be printed.</param>
    /// <param name="line">The line number this method was called from.</param>
    /// <param name="file">The file this method was called from.</param>
    /// <param name="member">The enlosing method this method was called from.</param>
    public static void Warning(String message, [CallerLineNumber] int line = 0, [CallerFilePath] string file = null, [CallerMemberName] string member = null)
    {
        EnsureDomainRegistered();
        eina_log_print(domain, Level.Warning, file, member, line, message);
    }

    /// <summary>Prints an informative message with context info. This context
    /// is filled automatically by the C# compiler.
    /// <para>Since EFL 1.23.</para>
    /// </summary>
    /// <param name="message">The message to be printed.</param>
    /// <param name="line">The line number this method was called from.</param>
    /// <param name="file">The file this method was called from.</param>
    /// <param name="member">The enlosing method this method was called from.</param>
    public static void Info(String message, [CallerLineNumber] int line = 0, [CallerFilePath] string file = null, [CallerMemberName] string member = null)
    {
        EnsureDomainRegistered();
        eina_log_print(domain, Level.Info, file, member, line, message);
    }

    /// <summary>Prints a debug message with context info. This context is
    /// filled automatically by the C# compiler.
    /// <para>Since EFL 1.23.</para>
    /// </summary>
    /// <param name="message">The message to be printed.</param>
    /// <param name="line">The line number this method was called from.</param>
    /// <param name="file">The file this method was called from.</param>
    /// <param name="member">The enlosing method this method was called from.</param>
    public static void Debug(String message, [CallerLineNumber] int line = 0, [CallerFilePath] string file = null, [CallerMemberName] string member = null)
    {
        EnsureDomainRegistered();
        eina_log_print(domain, Level.Debug, file, member, line, message);
    }

    /// <summary>Sets the highest level log messages should be printed. Values
    /// larger than this one are ignored.
    /// <para>Since EFL 1.23.</para>
    /// </summary>
    /// <param name="level">The global message level.</param>
    public static void GlobalLevelSet(Level level)
    {
        eina_log_level_set(level);
    }

    /// <summary>Gets the lowest level of messages that are not ignored.
    /// <para>Since EFL 1.23.</para>
    /// </summary>
    /// <returns>The current message level.</returns>
    public static Level GlobalLevelGet()
    {
        return eina_log_level_get();
    }
}

}