using System; using System.Runtime.InteropServices; using System.Text; namespace PlDotNET.Common { /// /// A simple class used to report messages in PostgreSQL. /// public static partial class Elog { /// /// Reports a log message in PostgreSQL. /// /// The message to log. public static void Debug(string message) { pldotnet_Elog(14, ConvertUTF16ToUTF8(message)); } /// /// Reports a log message in PostgreSQL. /// /// The message to log. public static void Log(string message) { pldotnet_Elog(15, ConvertUTF16ToUTF8(message)); } /// /// Reports an information message in PostgreSQL. /// /// The message to log. public static void Info(string message) { pldotnet_Elog(17, ConvertUTF16ToUTF8(message)); } /// /// Reports a notice message in PostgreSQL. /// /// The message to log. public static void Notice(string message) { pldotnet_Elog(18, ConvertUTF16ToUTF8(message)); } /// /// Reports a warning message in PostgreSQL. /// /// The message to log. public static void Warning(string message) { pldotnet_Elog(19, ConvertUTF16ToUTF8(message)); } /// /// Reports an error message in PostgreSQL. /// /// The message to log. public static void Error(string message) { throw new Exception(message); } /// /// Converts a UTF-16 string to a UTF-8 string. /// /// The UTF-16 string to convert. /// The UTF-8 representation of the input string. public static string ConvertUTF16ToUTF8(string utf16String) { // Get the array of bytes that represents the UTF-16 string. byte[] utf16Bytes = Encoding.Unicode.GetBytes(utf16String); // Convert the UTF-16 bytes to UTF-8 bytes. byte[] utf8Bytes = Encoding.Convert(Encoding.Unicode, Encoding.UTF8, utf16Bytes); // Return the UTF-8 bytes as a string. return Encoding.UTF8.GetString(utf8Bytes); } /// /// C function declared in pldotnet_main.h. /// See ::pldotnet_Elog(). /// /// The log level (e.g., DEBUG, LOG, INFO, NOTICE, WARNING). /// The message to log, in UTF-8 format. [LibraryImport("@PKG_LIBDIR/pldotnet.so", StringMarshalling = StringMarshalling.Utf8)] private static partial void pldotnet_Elog(int level, string nessage); } }