/// <summary> /// Load ini config data from ini format string content. /// </summary> /// <param name="content">ini format string content</param> public void LoadFromContent(string content) { _sections.Clear(); unsafe { int sectionCount = 0; IntPtr nativeContent = LibUtil.CreateNativeStr(content); int errDescLen = 0; IntPtr nativeSections = LLBCNative.csllbc_Ini_LoadFromContent(nativeContent, new IntPtr(§ionCount), new IntPtr(&errDescLen)); LibUtil.FreeNativePtr(ref nativeContent, false); if (sectionCount == -1) { byte * nativeErrorDesc = (byte *)nativeSections; string errorDesc = LibUtil.Ptr2Str(nativeErrorDesc, errDescLen); LibUtil.FreeNativePtr(nativeErrorDesc); throw new LLBCException("{0}", errorDesc); } else if (sectionCount == 0) { return; } _LoadAllSections((_NativeIniSection *)nativeSections.ToPointer(), sectionCount); LLBCNative.csllbc_Ini_FreeNativeSections(nativeSections, sectionCount); } }
/// <summary> /// Destroy llbc library. /// </summary> public static void Destroy() { if (LLBCNative.csllbc_Cleanup() != 0) { throw ExceptionUtil.CreateExceptionFromCoreLib(); } }
private void _Log(object tag, LogLevel level, int skipFrames, string fmt, params object[] args) { if (!enabled) { return; } IntPtr fileName = IntPtr.Zero; // Get filename, lineno, if enabled. int lineNo = -1; if (_enabledLogFileInfo) { var st = new System.Diagnostics.StackTrace(true); var frame = st.GetFrame(skipFrames); lineNo = frame.GetFileLineNumber(); fileName = LibUtil.CreateNativeStr(frame.GetFileName()); } IntPtr nativeTag = IntPtr.Zero; IntPtr nativeMsg = IntPtr.Zero; try { // Get log tag. if (tag is Type) { nativeTag = LibUtil.CreateNativeStr((tag as Type).Name); } else if (tag != null) { nativeTag = LibUtil.CreateNativeStr(tag.ToString()); } // Build log message. nativeMsg = LibUtil.CreateNativeStr(string.Format(fmt, args)); LLBCNative.csllbc_Log_LogMsg(_nativeLogger, fileName, lineNo, (int)level, nativeMsg, nativeTag); } catch (Exception e) { // Firstly, free nativeMsg. System.Runtime.InteropServices.Marshal.FreeHGlobal(nativeMsg); // Simply format error message and dump it. string errMsg = string.Format("Log [{0}] failed, exception:{1}, stacktrace:\n{2}", fmt, e.Message, new System.Diagnostics.StackTrace().ToString()); nativeMsg = LibUtil.CreateNativeStr(errMsg); LLBCNative.csllbc_Log_LogMsg(_nativeLogger, fileName, lineNo, (int)LogLevel.Fatal, nativeMsg, nativeTag); } finally { System.Runtime.InteropServices.Marshal.FreeHGlobal(nativeTag); System.Runtime.InteropServices.Marshal.FreeHGlobal(nativeMsg); } System.Runtime.InteropServices.Marshal.FreeHGlobal(fileName); }
private static void _Write(bool toStdout, bool newLine, char value) { unsafe { byte *bytes = stackalloc byte[32]; int wrote = Encoding.UTF8.GetBytes(&value, 1, bytes, 32); LLBCNative.csllbc_Console_SafePrint(toStdout, newLine, new IntPtr(bytes), wrote); } }
/// <summary> /// Schedule timer. /// </summary> /// <param name="dueTime">dueTime, in seconds</param> /// <param name="period">period, in seconds, default is 0.0, means same with dueTime</param> public void Schedule(double dueTime, double period = 0.0) { int ret = LLBCNative.csllbc_Timer_Schedule( _nativeTimer, (ulong)(dueTime * 1000), (ulong)(period * 1000)); if (ret != LLBCNative.LLBC_OK) { throw ExceptionUtil.CreateExceptionFromCoreLib(); } }
/// <summary> /// Destroy log config file. /// </summary> public static void Destroy() { lock (_lock) { _loggers.Clear(); _rootLogger = null; } LLBCNative.csllbc_Log_Destroy(); }
/// <summary> /// Construct new timer. /// </summary> /// <param name="timeoutHandler">timeout handler</param> /// <param name="cancelHandler">timer cancel handler, optional</param> public Timer(TimeoutHandler timeoutHandler, TimerCancelHandler cancelHandler = null) { _timeoutHandler = timeoutHandler; _cancelHandler = cancelHandler; _nativeTimeoutDeleg = _OnTimeout; _nativeCancelDeleg = _OnCancel; _nativeTimer = LLBCNative.csllbc_Timer_Create(_nativeTimeoutDeleg, _nativeCancelDeleg); }
public Logger(string loggerName) { _loggerName = loggerName; IntPtr nativeLoggerName = LibUtil.CreateNativeStr(_loggerName); _nativeLogger = LLBCNative.csllbc_Log_GetLogger(nativeLoggerName); System.Runtime.InteropServices.Marshal.FreeHGlobal(nativeLoggerName); if (_nativeLogger == IntPtr.Zero) { throw ExceptionUtil.CreateExceptionFromCoreLib(); } }
/// <summary> /// Init llbc library. /// </summary> public static void Init(Assembly loaderAssembly) { if (loaderAssembly == null) { throw new LLBCException("Loader assembly could not be null"); } else if (LLBCNative.csllbc_Startup() != 0) { throw ExceptionUtil.CreateExceptionFromCoreLib(); } _loaderAssembly = loaderAssembly; RegHolderCollector.Collect(loaderAssembly, false); }
/// <summary> /// Get string unique Id. /// </summary> /// <param name="str">string</param> /// <returns>the unique Id</returns> public static int UniqueId(string str) { if (string.IsNullOrEmpty(str)) { return(0); } byte[] bytes = Encoding.UTF8.GetBytes(str); unsafe { fixed(byte *ptr = &bytes[0]) { return(LLBCNative.csllbc_Utils_String_HashString(new IntPtr(ptr), bytes.Length)); } } }
/// <summary> /// Trace message, only output when enabled LLBC_DEBUG macro in llbc core library. /// </summary> /// <param name="msg">will trace message</param> public static void Trace(string msg) { if (msg.Length == 0) { return; } byte[] msgBytes = Encoding.UTF8.GetBytes(msg); unsafe { fixed(byte *ptr = &msgBytes[0]) { LLBCNative.csllbc_Console_Trace(new IntPtr(ptr), msgBytes.Length); } } }
private static void _SafeOutput(bool toStdout, bool newLine, string content) { byte[] bytes = Encoding.UTF8.GetBytes(content); if (bytes.Length == 0) { LLBCNative.csllbc_Console_SafePrint(toStdout, newLine, IntPtr.Zero, 0); return; } unsafe { fixed(byte *ptr = &bytes[0]) { LLBCNative.csllbc_Console_SafePrint(toStdout, newLine, new IntPtr(ptr), bytes.Length); } } }
/// <summary> /// Initialize Log. /// </summary> /// <param name="logCfgFile">log config file</param> public static void Init(string logCfgFile) { if (string.IsNullOrEmpty(logCfgFile)) { throw new LLBCException("please specific log config file"); } lock (_lock) { IntPtr nativeStr = LibUtil.CreateNativeStr(logCfgFile); int ret = LLBCNative.csllbc_Log_Init(nativeStr); System.Runtime.InteropServices.Marshal.FreeHGlobal(nativeStr); if (ret != LLBCNative.LLBC_OK) { throw ExceptionUtil.CreateExceptionFromCoreLib(); } _rootLogger = Get("root"); } }
/// <summary> /// Create exception from llbc core library. /// </summary> /// <returns></returns> public static LLBCException CreateExceptionFromCoreLib(uint errNo = 0) { unsafe { int errStrLen = 0; IntPtr errStr; if (errNo == 0) { errStr = LLBCNative.csllbc_FormatLastError(new IntPtr(&errStrLen)); } else { errStr = LLBCNative.csllbc_StrError(errNo, new IntPtr(&errStrLen)); } if (errStrLen > 0) { return(new LLBCException(LibUtil.Ptr2Str(errStr, errStrLen))); } return(new LLBCException("unknown error")); } }
/// <summary> /// Reset console color. /// </summary> public static void ResetColor() { LLBCNative.csllbc_Console_ResetColor(); }
/// <summary> /// Flush all standard error buffer to console. /// </summary> public static void FlushError() { LLBCNative.csllbc_Console_SafeFlush(false); }
/// <summary> /// Flush all standard output buffer to console. /// </summary> public static void Flush() { LLBCNative.csllbc_Console_SafeFlush(true); }
/// <summary> /// Cancel timer. /// </summary> public void Cancel() { LLBCNative.csllbc_Timer_Cancel(_nativeTimer); }