/* * Copyright 2019 by its authors. See AUTHORS. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #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 /// EFL Logging facilities. /// Since EFL 1.23. /// public static 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(); /// The levels of logging. /// Since EFL 1.23. /// public enum Level { /// Critical events. /// Since EFL 1.23. /// Critical, /// Error events. /// Since EFL 1.23. /// Error, /// Warning events. /// Since EFL 1.23. /// Warning, /// Informative events. /// Since EFL 1.23. /// Info, /// Debugging messages. /// Since EFL 1.23. /// Debug, /// Unknown events. /// Since EFL 1.23. /// Unkown = (-2147483647 - 1) } /// The colors to be used by the logging system. /// Since EFL 1.23. /// public static class Color { /// Light red /// Since EFL 1.23. /// public const string LIGHTRED = "\033[31;1m"; /// Red /// Since EFL 1.23. /// public const string RED = "\033[31m"; /// Light blue /// Since EFL 1.23. /// public const string LIGHTBLUE = "\033[34;1m"; /// Blue /// Since EFL 1.23. /// public const string BLUE = "\033[34m"; /// Green /// Since EFL 1.23. /// public const string GREEN = "\033[32;1m"; /// Yellow /// Since EFL 1.23. /// public const string YELLOW = "\033[33;1m"; /// Orange /// Since EFL 1.23. /// public const string ORANGE = "\033[0;33m"; /// White /// Since EFL 1.23. /// public const string WHITE = "\033[37;1m"; /// Light cyan /// Since EFL 1.23. /// public const string LIGHTCYAN = "\033[36;1m"; /// Cyan /// Since EFL 1.23. /// public const string CYAN = "\033[36m"; /// Reset /// Since EFL 1.23. /// public const string RESET = "\033[0m"; /// Bold /// Since EFL 1.23. /// public const string HIGH = "\033[1m"; } private static int domain = -1; /// Static class initializer. /// Since EFL 1.23. /// 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."); } } /// Prints a critical message with context info. This context is /// filled automatically by the C# compiler. /// Since EFL 1.23. /// /// The message to be printed. /// The line number this method was called from. /// The file this method was called from. /// The enlosing method this method was called from. 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); } /// Prints an error message with context info. This context is /// filled automatically by the C# compiler. /// Since EFL 1.23. /// /// The message to be printed. /// The line number this method was called from. /// The file this method was called from. /// The enlosing method this method was called from. 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); } /// Prints a warning message with context info. This context is /// filled automatically by the C# compiler. /// Since EFL 1.23. /// /// The message to be printed. /// The line number this method was called from. /// The file this method was called from. /// The enlosing method this method was called from. 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); } /// Prints an informative message with context info. This context /// is filled automatically by the C# compiler. /// Since EFL 1.23. /// /// The message to be printed. /// The line number this method was called from. /// The file this method was called from. /// The enlosing method this method was called from. 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); } /// Prints a debug message with context info. This context is /// filled automatically by the C# compiler. /// Since EFL 1.23. /// /// The message to be printed. /// The line number this method was called from. /// The file this method was called from. /// The enlosing method this method was called from. 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); } /// Sets the highest level log messages should be printed. Values /// larger than this one are ignored. /// Since EFL 1.23. /// /// The global message level. public static void GlobalLevelSet(Level level) { eina_log_level_set(level); } /// Gets the lowest level of messages that are not ignored. /// Since EFL 1.23. /// /// The current message level. public static Level GlobalLevelGet() { return eina_log_level_get(); } } }